1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
35
36
37
38
39
40 public class DomPoxMessageFactory extends TransformerObjectSupport implements WebServiceMessageFactory {
41
42
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
56 public void setContentType(String contentType) {
57 Assert.hasLength(contentType, "'contentType' must not be empty");
58 this.contentType = contentType;
59 }
60
61
62 public void setNamespaceAware(boolean namespaceAware) {
63 documentBuilderFactory.setNamespaceAware(namespaceAware);
64 }
65
66
67 public void setValidating(boolean validating) {
68 documentBuilderFactory.setValidating(validating);
69 }
70
71
72
73
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 }