View Javadoc

1   /*
2    * Copyright [2006] [University Corporation for Advanced Internet Development, Inc.]
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.opensaml.ws.soap.soap11.encoder;
18  
19  import java.io.IOException;
20  import java.io.OutputStreamWriter;
21  import java.io.UnsupportedEncodingException;
22  import java.io.Writer;
23  
24  import org.opensaml.ws.message.MessageContext;
25  import org.opensaml.ws.message.encoder.MessageEncodingException;
26  import org.opensaml.ws.message.handler.BaseHandlerChainAwareMessageEncoder;
27  import org.opensaml.ws.soap.common.SOAPObjectBuilder;
28  import org.opensaml.ws.soap.soap11.Body;
29  import org.opensaml.ws.soap.soap11.Envelope;
30  import org.opensaml.ws.transport.OutTransport;
31  import org.opensaml.xml.Configuration;
32  import org.opensaml.xml.XMLObjectBuilderFactory;
33  import org.opensaml.xml.util.XMLHelper;
34  import org.slf4j.Logger;
35  import org.slf4j.LoggerFactory;
36  import org.w3c.dom.Element;
37  
38  /**
39   * Basic SOAP 1.1 encoder.
40   */
41  public class SOAP11Encoder extends BaseHandlerChainAwareMessageEncoder {
42  
43      /** Class logger. */
44      private final Logger log = LoggerFactory.getLogger(SOAP11Encoder.class);
45      
46      /** SOAP Envelope builder. */
47      private SOAPObjectBuilder<Envelope> envBuilder;
48      
49      /** SOAP Body builder. */
50      private SOAPObjectBuilder<Body> bodyBuilder;
51      
52  
53      /** Constructor. */
54      @SuppressWarnings("unchecked")
55      public SOAP11Encoder() {
56          super();
57          XMLObjectBuilderFactory builderFactory = Configuration.getBuilderFactory();
58          envBuilder = (SOAPObjectBuilder<Envelope>) builderFactory.getBuilder(Envelope.DEFAULT_ELEMENT_NAME);
59          bodyBuilder = (SOAPObjectBuilder<Body>) builderFactory.getBuilder(Body.DEFAULT_ELEMENT_NAME);
60      }
61      
62      /** {@inheritDoc} */
63      public boolean providesMessageConfidentiality(MessageContext messageContext) throws MessageEncodingException {
64          return messageContext.getOutboundMessageTransport().isConfidential();
65      }
66  
67      /** {@inheritDoc} */
68      public boolean providesMessageIntegrity(MessageContext messageContext) throws MessageEncodingException {
69          return messageContext.getOutboundMessageTransport().isIntegrityProtected();
70      }
71      
72      /** {@inheritDoc} */
73      protected void prepareMessageContext(MessageContext messageContext) throws MessageEncodingException {
74          messageContext.setOutboundMessage(buildSOAPEnvelope(messageContext));
75      }
76      
77      /** {@inheritDoc} */
78      protected void encodeToTransport(MessageContext messageContext) throws MessageEncodingException {
79          Element envelopeElem = marshallMessage(messageContext.getOutboundMessage());
80          
81          preprocessTransport(messageContext);
82          
83          try {
84              OutTransport outTransport = messageContext.getOutboundMessageTransport();
85              Writer out = new OutputStreamWriter(outTransport.getOutgoingStream(), "UTF-8");
86              XMLHelper.writeNode(envelopeElem, out);
87              out.flush();
88          } catch (UnsupportedEncodingException e) {
89              log.error("JVM does not support required UTF-8 encoding");
90              throw new MessageEncodingException("JVM does not support required UTF-8 encoding");
91          } catch (IOException e) {
92              log.error("Unable to write message content to outbound stream", e);
93              throw new MessageEncodingException("Unable to write message content to outbound stream", e);
94          }
95      }
96      
97      /**
98       * Perform any processing or fixup on the message context's outbound transport, prior to encoding the actual
99       * message.
100      * 
101      * <p>
102      * The default implementation does nothing. Subclasses should override to implement transport-specific 
103      * behavior.
104      * </p>
105      * 
106      * @param messageContext the current message context being processed
107      * 
108      * @throws MessageEncodingException thrown if there is a problem preprocessing the transport
109      */
110     protected void preprocessTransport(MessageContext messageContext) throws MessageEncodingException {
111     }
112 
113     /**
114      * Builds the SOAP envelope and body skeleton to be encoded.
115      * 
116      * @param messageContext the message context being processed
117      * 
118      * @return the minimal SOAP message envelope skeleton
119      */
120     protected Envelope buildSOAPEnvelope(MessageContext messageContext) {
121         log.debug("Building SOAP envelope");
122 
123         Envelope envelope = envBuilder.buildObject();
124 
125         Body body = bodyBuilder.buildObject();
126         envelope.setBody(body);
127 
128         return envelope;
129     }
130  
131 }