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.http;
18  
19  import java.util.List;
20  
21  import org.opensaml.ws.message.MessageContext;
22  import org.opensaml.ws.message.encoder.MessageEncodingException;
23  import org.opensaml.ws.soap.soap11.Envelope;
24  import org.opensaml.ws.soap.soap11.Header;
25  import org.opensaml.ws.soap.soap11.encoder.SOAP11Encoder;
26  import org.opensaml.ws.transport.http.HTTPOutTransport;
27  import org.opensaml.ws.transport.http.HTTPTransportUtils;
28  import org.opensaml.ws.wsaddressing.Action;
29  import org.opensaml.xml.XMLObject;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  /**
34   * Basic SOAP 1.1 encoder for HTTP transport.
35   */
36  public class HTTPSOAP11Encoder extends SOAP11Encoder {
37  
38      /** Class logger. */
39      private final Logger log = LoggerFactory.getLogger(HTTPSOAP11Encoder.class);
40      
41  
42      /** Constructor. */
43      public HTTPSOAP11Encoder() {
44          super();
45      }
46  
47      /** {@inheritDoc} */
48      protected void doEncode(MessageContext messageContext) throws MessageEncodingException {
49          if (!(messageContext.getOutboundMessageTransport() instanceof HTTPOutTransport)) {
50              log.error("Invalid outbound message transport type, this encoder only support HTTPOutTransport");
51              throw new MessageEncodingException(
52                      "Invalid outbound message transport type, this encoder only support HTTPOutTransport");
53          }
54          
55          super.doEncode(messageContext);
56      }
57      
58      /**
59       * <p>
60       * This implementation performs the following actions on the context's {@link HTTPOutTransport}:
61       * <ol>
62       *   <li>Adds the HTTP header: "Cache-control: no-cache, no-store"</li>
63       *   <li>Adds the HTTP header: "Pragma: no-cache"</li>
64       *   <li>Sets the character encoding to: "UTF-8"</li>
65       *   <li>Sets the content type to: "text/xml"</li>
66       *   <li>Sets the SOAPAction HTTP header the value returned by {@link #getSOAPAction(MessageContext)}, if
67       *   that returns non-null.</li>
68       * </ol>
69       * </p>
70       * 
71       * <p>
72       * Subclasses should NOT set the SOAPAction HTTP header in this method. Instead, they should override 
73       * the method {@link #getSOAPAction(MessageContext)}.
74       * </p>
75       * 
76       * @param messageContext the current message context being processed
77       * 
78       * @throws MessageEncodingException thrown if there is a problem preprocessing the transport
79       */
80      protected void preprocessTransport(MessageContext messageContext) throws MessageEncodingException {
81          HTTPOutTransport outTransport = (HTTPOutTransport) messageContext.getOutboundMessageTransport();
82          HTTPTransportUtils.addNoCacheHeaders(outTransport);
83          HTTPTransportUtils.setUTF8Encoding(outTransport);
84          HTTPTransportUtils.setContentType(outTransport, "text/xml");
85          
86          String soapAction = getSOAPAction(messageContext);
87          if (soapAction != null) {
88              outTransport.setHeader("SOAPAction", soapAction);
89          } else {
90              outTransport.setHeader("SOAPAction", "");
91          }
92      }
93   
94      /**
95       * Determine the value of the SOAPAction HTTP header to send.
96       * 
97       * <p>
98       * The default behavior is to return the value of the SOAP Envelope's WS-Addressing Action header,
99       * if present.
100      * </p>
101      * 
102      * @param messageContext the current message context being processed
103      * @return a SOAPAction HTTP header URI value
104      */
105     protected String getSOAPAction(MessageContext messageContext) {
106         Envelope env = (Envelope) messageContext.getOutboundMessage();
107         Header header = env.getHeader();
108         if (header == null) {
109             return null;
110         }
111         List<XMLObject> objList = header.getUnknownXMLObjects(Action.ELEMENT_NAME);
112         if (objList == null || objList.isEmpty()) {
113             return null;
114         } else {
115             return ((Action)objList.get(0)).getValue();
116         }
117     }
118     
119 }