View Javadoc

1   /*
2    * Copyright 2002-2013 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.pox.dom;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  import javax.xml.parsers.DocumentBuilder;
22  import javax.xml.parsers.DocumentBuilderFactory;
23  import javax.xml.parsers.ParserConfigurationException;
24  import javax.xml.transform.TransformerConfigurationException;
25  
26  import org.w3c.dom.Document;
27  import org.xml.sax.SAXException;
28  
29  import org.springframework.util.Assert;
30  import org.springframework.ws.WebServiceMessageFactory;
31  import org.springframework.xml.transform.TransformerObjectSupport;
32  
33  /**
34   * Implementation of the {@link WebServiceMessageFactory} interface that creates a {@link DomPoxMessage}.
35   *
36   * @author Arjen Poutsma
37   * @see org.springframework.ws.pox.dom.DomPoxMessage
38   * @since 1.0.0
39   */
40  public class DomPoxMessageFactory extends TransformerObjectSupport implements WebServiceMessageFactory {
41  
42      /** The default content type for the POX messages. */
43      public static final String DEFAULT_CONTENT_TYPE = "application/xml";
44  
45      private DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
46  
47      private String contentType = DEFAULT_CONTENT_TYPE;
48  
49      public DomPoxMessageFactory() {
50          documentBuilderFactory.setNamespaceAware(true);
51          documentBuilderFactory.setValidating(false);
52  	    documentBuilderFactory.setExpandEntityReferences(false);
53      }
54  
55      /** Sets the content-type for the {@link DomPoxMessage}. */
56      public void setContentType(String contentType) {
57          Assert.hasLength(contentType, "'contentType' must not be empty");
58          this.contentType = contentType;
59      }
60  
61      /** Set whether or not the XML parser should be XML namespace aware. Default is <code>true</code>. */
62      public void setNamespaceAware(boolean namespaceAware) {
63          documentBuilderFactory.setNamespaceAware(namespaceAware);
64      }
65  
66      /** Set if the XML parser should validate the document. Default is <code>false</code>. */
67      public void setValidating(boolean validating) {
68          documentBuilderFactory.setValidating(validating);
69      }
70  
71  	/**
72  	 * Set if the XML parser should expand entity reference nodes. Default is
73  	 * {@code false}.
74  	 */
75  	public void setExpandEntityReferences(boolean expandEntityRef) {
76  		documentBuilderFactory.setExpandEntityReferences(expandEntityRef);
77  	}
78  
79      public DomPoxMessage createWebServiceMessage() {
80          try {
81              DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
82              Document request = documentBuilder.newDocument();
83              return new DomPoxMessage(request, createTransformer(), contentType);
84          }
85          catch (ParserConfigurationException ex) {
86              throw new DomPoxMessageException("Could not create message context", ex);
87          }
88          catch (TransformerConfigurationException ex) {
89              throw new DomPoxMessageException("Could not create transformer", ex);
90          }
91      }
92  
93      public DomPoxMessage createWebServiceMessage(InputStream inputStream) throws IOException {
94          try {
95              DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
96              Document request = documentBuilder.parse(inputStream);
97              return new DomPoxMessage(request, createTransformer(), contentType);
98          }
99          catch (ParserConfigurationException ex) {
100             throw new DomPoxMessageException("Could not create message context", ex);
101         }
102         catch (SAXException ex) {
103             throw new DomPoxMessageException("Could not parse request message", ex);
104         }
105         catch (TransformerConfigurationException ex) {
106             throw new DomPoxMessageException("Could not create transformer", ex);
107         }
108     }
109 }