View Javadoc

1   /*
2    * Copyright 2002-2009 the original author or authors.
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.springframework.ws.soap.axiom;
18  
19  import java.io.File;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.util.Iterator;
23  import java.util.Locale;
24  import javax.xml.stream.XMLInputFactory;
25  import javax.xml.stream.XMLStreamException;
26  import javax.xml.stream.XMLStreamReader;
27  
28  import org.apache.axiom.attachments.Attachments;
29  import org.apache.axiom.om.OMException;
30  import org.apache.axiom.om.impl.MTOMConstants;
31  import org.apache.axiom.soap.SOAP11Constants;
32  import org.apache.axiom.soap.SOAP12Constants;
33  import org.apache.axiom.soap.SOAPFactory;
34  import org.apache.axiom.soap.SOAPMessage;
35  import org.apache.axiom.soap.impl.builder.MTOMStAXSOAPModelBuilder;
36  import org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder;
37  import org.apache.axiom.soap.impl.llom.soap11.SOAP11Factory;
38  import org.apache.axiom.soap.impl.llom.soap12.SOAP12Factory;
39  import org.apache.commons.logging.Log;
40  import org.apache.commons.logging.LogFactory;
41  
42  import org.springframework.beans.factory.InitializingBean;
43  import org.springframework.util.Assert;
44  import org.springframework.util.StringUtils;
45  import org.springframework.ws.WebServiceMessage;
46  import org.springframework.ws.server.endpoint.interceptor.PayloadLoggingInterceptor;
47  import org.springframework.ws.server.endpoint.mapping.PayloadRootQNameEndpointMapping;
48  import org.springframework.ws.soap.SoapMessageFactory;
49  import org.springframework.ws.soap.SoapVersion;
50  import org.springframework.ws.soap.server.endpoint.interceptor.PayloadValidatingInterceptor;
51  import org.springframework.ws.soap.server.endpoint.mapping.SoapActionEndpointMapping;
52  import org.springframework.ws.soap.support.SoapUtils;
53  import org.springframework.ws.transport.TransportConstants;
54  import org.springframework.ws.transport.TransportInputStream;
55  
56  /**
57   * Axiom-specific implementation of the {@link org.springframework.ws.WebServiceMessageFactory WebServiceMessageFactory}
58   * interface. Creates {@link org.springframework.ws.soap.axiom.AxiomSoapMessage AxiomSoapMessages}.
59   * <p/>
60   * To increase reading performance on the the SOAP request created by this message factory, you can set the {@link
61   * #setPayloadCaching(boolean) payloadCaching} property to <code>false</code> (default is <code>true</code>). This this
62   * will read the contents of the body directly from the stream. However, <strong>when this setting is enabled, the
63   * payload can only be read once</strong>. This means that any endpoint mappings or interceptors which are based on the
64   * message payload (such as the {@link PayloadRootQNameEndpointMapping}, the {@link PayloadValidatingInterceptor}, or
65   * the {@link PayloadLoggingInterceptor}) cannot be used. Instead, use an endpoint mapping that does not consume the
66   * payload (i.e. the {@link SoapActionEndpointMapping}).
67   * <p/>
68   * Additionally, this message factory can cache large attachments to disk by setting the {@link
69   * #setAttachmentCaching(boolean) attachmentCaching} property to <code>true</code> (default is <code>false</code>).
70   * Optionally, the location where attachments are stored can be defined via the {@link #setAttachmentCacheDir(File)
71   * attachmentCacheDir} property (defaults to the system temp file path).
72   * <p/>
73   * Mostly derived from <code>org.apache.axis2.transport.http.HTTPTransportUtils</code> and
74   * <code>org.apache.axis2.transport.TransportUtils</code>, which we cannot use since they are not part of the Axiom
75   * distribution.
76   *
77   * @author Arjen Poutsma
78   * @see AxiomSoapMessage
79   * @see #setPayloadCaching(boolean)
80   * @since 1.0.0
81   */
82  public class AxiomSoapMessageFactory implements SoapMessageFactory, InitializingBean {
83  
84      private static final String CHARSET_PARAMETER = "charset";
85  
86      private static final String DEFAULT_CHARSET_ENCODING = "UTF-8";
87  
88      private static final String MULTI_PART_RELATED_CONTENT_TYPE = "multipart/related";
89  
90      private static final Log logger = LogFactory.getLog(AxiomSoapMessageFactory.class);
91  
92      private XMLInputFactory inputFactory;
93  
94      private boolean payloadCaching = true;
95  
96      private boolean attachmentCaching = false;
97  
98      private File attachmentCacheDir;
99  
100     private int attachmentCacheThreshold = 4096;
101 
102     // use SOAP 1.1 by default
103     private SOAPFactory soapFactory = new SOAP11Factory();
104 
105     private boolean langAttributeOnSoap11FaultString = true;
106 
107     /** Default constructor. */
108     public AxiomSoapMessageFactory() {
109         inputFactory = XMLInputFactory.newInstance();
110     }
111 
112     /**
113      * Indicates whether the SOAP Body payload should be cached or not. Default is <code>true</code>.
114      * <p/>
115      * Setting this to <code>false</code> will increase performance, but also result in the fact that the message
116      * payload can only be read once.
117      */
118     public void setPayloadCaching(boolean payloadCaching) {
119         this.payloadCaching = payloadCaching;
120     }
121 
122     /**
123      * Indicates whether SOAP attachments should be cached or not. Default is <code>false</code>.
124      * <p/>
125      * Setting this to <code>true</code> will cause Axiom to store larger attachments on disk, rather than in memory.
126      * This decreases memory consumption, but decreases performance.
127      */
128     public void setAttachmentCaching(boolean attachmentCaching) {
129         this.attachmentCaching = attachmentCaching;
130     }
131 
132     /**
133      * Sets the directory where SOAP attachments will be stored. Only used when {@link #setAttachmentCaching(boolean)
134      * attachmentCaching} is set to <code>true</code>.
135      * <p/>
136      * The parameter should be an existing, writable directory. This property defaults to the temporary directory of the
137      * operating system (i.e. the value of the <code>java.io.tmpdir</code> system property).
138      */
139     public void setAttachmentCacheDir(File attachmentCacheDir) {
140         Assert.notNull(attachmentCacheDir, "'attachmentCacheDir' must not be null");
141         Assert.isTrue(attachmentCacheDir.isDirectory(), "'attachmentCacheDir' must be a directory");
142         Assert.isTrue(attachmentCacheDir.canWrite(), "'attachmentCacheDir' must be writable");
143         this.attachmentCacheDir = attachmentCacheDir;
144     }
145 
146     /**
147      * Sets the threshold for attachments caching, in bytes. Attachments larger than this threshold will be cached in
148      * the {@link #setAttachmentCacheDir(File) attachment cache directory}. Only used when {@link
149      * #setAttachmentCaching(boolean) attachmentCaching} is set to <code>true</code>.
150      * <p/>
151      * Defaults to 4096 bytes (i.e. 4 kilobytes).
152      */
153     public void setAttachmentCacheThreshold(int attachmentCacheThreshold) {
154         Assert.isTrue(attachmentCacheThreshold > 0, "'attachmentCacheThreshold' must be larger than 0");
155         this.attachmentCacheThreshold = attachmentCacheThreshold;
156     }
157 
158     public void setSoapVersion(SoapVersion version) {
159         if (SoapVersion.SOAP_11 == version) {
160             soapFactory = new SOAP11Factory();
161         }
162         else if (SoapVersion.SOAP_12 == version) {
163             soapFactory = new SOAP12Factory();
164         }
165         else {
166             throw new IllegalArgumentException(
167                     "Invalid version [" + version + "]. " + "Expected the SOAP_11 or SOAP_12 constant");
168         }
169     }
170 
171     /**
172      * Defines whether a {@code xml:lang} attribute should be set on SOAP 1.1 {@code <faultstring>} elements.
173      * <p/>
174      * The default is {@code true}, to comply with WS-I, but this flag can be set to {@code false} to the older W3C SOAP
175      * 1.1 specification.
176      *
177      * @see <a href="http://www.ws-i.org/Profiles/BasicProfile-1.1.html#SOAP_Fault_Language">WS-I Basic Profile 1.1</a>
178      */
179     public void setlangAttributeOnSoap11FaultString(boolean langAttributeOnSoap11FaultString) {
180         this.langAttributeOnSoap11FaultString = langAttributeOnSoap11FaultString;
181     }
182 
183     public void afterPropertiesSet() throws Exception {
184         if (logger.isInfoEnabled()) {
185             logger.info(payloadCaching ? "Enabled payload caching" : "Disabled payload caching");
186         }
187         if (attachmentCacheDir == null) {
188             String tempDir = System.getProperty("java.io.tmpdir");
189             setAttachmentCacheDir(new File(tempDir));
190         }
191     }
192 
193     public WebServiceMessage createWebServiceMessage() {
194         return new AxiomSoapMessage(soapFactory, payloadCaching, langAttributeOnSoap11FaultString);
195     }
196 
197     public WebServiceMessage createWebServiceMessage(InputStream inputStream) throws IOException {
198         Assert.isInstanceOf(TransportInputStream.class, inputStream,
199                 "AxiomSoapMessageFactory requires a TransportInputStream");
200         TransportInputStream transportInputStream = (TransportInputStream) inputStream;
201         String contentType = getHeaderValue(transportInputStream, TransportConstants.HEADER_CONTENT_TYPE);
202         if (!StringUtils.hasLength(contentType)) {
203             if (logger.isDebugEnabled()) {
204                 logger.debug("TransportInputStream has no Content-Type header; defaulting to \"" +
205                         SoapVersion.SOAP_11.getContentType() + "\"");
206             }
207             contentType = SoapVersion.SOAP_11.getContentType();
208         }
209         String soapAction = getHeaderValue(transportInputStream, TransportConstants.HEADER_SOAP_ACTION);
210         if (!StringUtils.hasLength(soapAction)) {
211             soapAction = SoapUtils.extractActionFromContentType(contentType);
212         }
213         try {
214             if (isMultiPartRelated(contentType)) {
215                 return createMultiPartAxiomSoapMessage(inputStream, contentType, soapAction);
216             }
217             else {
218                 return createAxiomSoapMessage(inputStream, contentType, soapAction);
219             }
220         }
221         catch (XMLStreamException ex) {
222             throw new AxiomSoapMessageCreationException("Could not parse request: " + ex.getMessage(), ex);
223         }
224         catch (OMException ex) {
225             throw new AxiomSoapMessageCreationException("Could not create message: " + ex.getMessage(), ex);
226         }
227     }
228 
229     private String getHeaderValue(TransportInputStream transportInputStream, String header) throws IOException {
230         String contentType = null;
231         Iterator iterator = transportInputStream.getHeaders(header);
232         if (iterator.hasNext()) {
233             contentType = (String) iterator.next();
234         }
235         return contentType;
236     }
237 
238     private boolean isMultiPartRelated(String contentType) {
239         contentType = contentType.toLowerCase(Locale.ENGLISH);
240         return contentType.indexOf(MULTI_PART_RELATED_CONTENT_TYPE) != -1;
241     }
242 
243     /** Creates an AxiomSoapMessage without attachments. */
244     private WebServiceMessage createAxiomSoapMessage(InputStream inputStream, String contentType, String soapAction)
245             throws XMLStreamException {
246         XMLStreamReader reader = inputFactory.createXMLStreamReader(inputStream, getCharSetEncoding(contentType));
247         String envelopeNamespace = getSoapEnvelopeNamespace(contentType);
248         StAXSOAPModelBuilder builder = new StAXSOAPModelBuilder(reader, soapFactory, envelopeNamespace);
249         SOAPMessage soapMessage = builder.getSoapMessage();
250         return new AxiomSoapMessage(soapMessage, soapAction, payloadCaching, langAttributeOnSoap11FaultString);
251     }
252 
253     /** Creates an AxiomSoapMessage with attachments. */
254     private AxiomSoapMessage createMultiPartAxiomSoapMessage(InputStream inputStream,
255                                                              String contentType,
256                                                              String soapAction) throws XMLStreamException {
257         Attachments attachments =
258                 new Attachments(inputStream, contentType, attachmentCaching, attachmentCacheDir.getAbsolutePath(),
259                         Integer.toString(attachmentCacheThreshold));
260         XMLStreamReader reader = inputFactory.createXMLStreamReader(attachments.getSOAPPartInputStream(),
261                 getCharSetEncoding(attachments.getSOAPPartContentType()));
262         StAXSOAPModelBuilder builder;
263         String envelopeNamespace = getSoapEnvelopeNamespace(contentType);
264         if (MTOMConstants.SWA_TYPE.equals(attachments.getAttachmentSpecType()) ||
265                 MTOMConstants.SWA_TYPE_12.equals(attachments.getAttachmentSpecType())) {
266             builder = new StAXSOAPModelBuilder(reader, soapFactory, envelopeNamespace);
267         }
268         else if (MTOMConstants.MTOM_TYPE.equals(attachments.getAttachmentSpecType())) {
269             builder = new MTOMStAXSOAPModelBuilder(reader, attachments, envelopeNamespace);
270         }
271         else {
272             throw new AxiomSoapMessageCreationException(
273                     "Unknown attachment type: [" + attachments.getAttachmentSpecType() + "]");
274         }
275         return new AxiomSoapMessage(builder.getSoapMessage(), attachments, soapAction, payloadCaching,
276                 langAttributeOnSoap11FaultString);
277     }
278 
279     private String getSoapEnvelopeNamespace(String contentType) {
280         if (contentType.indexOf(SOAP11Constants.SOAP_11_CONTENT_TYPE) != -1) {
281             return SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI;
282         }
283         else if (contentType.indexOf(SOAP12Constants.SOAP_12_CONTENT_TYPE) != -1) {
284             return SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI;
285         }
286         else {
287             throw new AxiomSoapMessageCreationException("Unknown content type '" + contentType + "'");
288         }
289 
290     }
291 
292     /**
293      * Returns the character set from the given content type. Mostly copied
294      *
295      * @return the character set encoding
296      */
297     protected String getCharSetEncoding(String contentType) {
298         int charSetIdx = contentType.indexOf(CHARSET_PARAMETER);
299         if (charSetIdx == -1) {
300             return DEFAULT_CHARSET_ENCODING;
301         }
302         int eqIdx = contentType.indexOf("=", charSetIdx);
303 
304         int indexOfSemiColon = contentType.indexOf(";", eqIdx);
305         String value;
306 
307         if (indexOfSemiColon > 0) {
308             value = contentType.substring(eqIdx + 1, indexOfSemiColon);
309         }
310         else {
311             value = contentType.substring(eqIdx + 1, contentType.length()).trim();
312         }
313         if (value.startsWith("\"")) {
314             value = value.substring(1);
315         }
316         if (value.endsWith("\"")) {
317             return value.substring(0, value.length() - 1);
318         }
319         if ("null".equalsIgnoreCase(value)) {
320             return DEFAULT_CHARSET_ENCODING;
321         }
322         else {
323             return value.trim();
324         }
325     }
326 
327     public String toString() {
328         StringBuffer buffer = new StringBuffer("AxiomSoapMessageFactory[");
329         if (soapFactory instanceof SOAP11Factory) {
330             buffer.append("SOAP 1.1");
331         }
332         else if (soapFactory instanceof SOAP12Factory) {
333             buffer.append("SOAP 1.2");
334         }
335         buffer.append(']');
336         return buffer.toString();
337     }
338 }