View Javadoc

1   /*
2    * Copyright 2005-2010 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.springframework.util.Assert;
27  import org.springframework.ws.WebServiceMessageFactory;
28  import org.springframework.xml.transform.TransformerObjectSupport;
29  
30  import org.w3c.dom.Document;
31  import org.xml.sax.SAXException;
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      }
53  
54      /** Sets the content-type for the {@link DomPoxMessage}. */
55      public void setContentType(String contentType) {
56          Assert.hasLength(contentType, "'contentType' must not be empty");
57          this.contentType = contentType;
58      }
59  
60      /** Set whether or not the XML parser should be XML namespace aware. Default is <code>true</code>. */
61      public void setNamespaceAware(boolean namespaceAware) {
62          documentBuilderFactory.setNamespaceAware(namespaceAware);
63      }
64  
65      /** Set if the XML parser should validate the document. Default is <code>false</code>. */
66      public void setValidating(boolean validating) {
67          documentBuilderFactory.setValidating(validating);
68      }
69  
70      public DomPoxMessage createWebServiceMessage() {
71          try {
72              DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
73              Document request = documentBuilder.newDocument();
74              return new DomPoxMessage(request, createTransformer(), contentType);
75          }
76          catch (ParserConfigurationException ex) {
77              throw new DomPoxMessageException("Could not create message context", ex);
78          }
79          catch (TransformerConfigurationException ex) {
80              throw new DomPoxMessageException("Could not create transformer", ex);
81          }
82      }
83  
84      public DomPoxMessage createWebServiceMessage(InputStream inputStream) throws IOException {
85          try {
86              DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
87              Document request = documentBuilder.parse(inputStream);
88              return new DomPoxMessage(request, createTransformer(), contentType);
89          }
90          catch (ParserConfigurationException ex) {
91              throw new DomPoxMessageException("Could not create message context", ex);
92          }
93          catch (SAXException ex) {
94              throw new DomPoxMessageException("Could not parse request message", ex);
95          }
96          catch (TransformerConfigurationException ex) {
97              throw new DomPoxMessageException("Could not create transformer", ex);
98          }
99      }
100 }