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.OutputStream;
21  import javax.xml.transform.Result;
22  import javax.xml.transform.Source;
23  import javax.xml.transform.Transformer;
24  import javax.xml.transform.TransformerException;
25  import javax.xml.transform.dom.DOMResult;
26  import javax.xml.transform.dom.DOMSource;
27  import javax.xml.transform.stream.StreamResult;
28  
29  import org.springframework.util.Assert;
30  import org.springframework.ws.pox.PoxMessage;
31  import org.springframework.ws.transport.TransportConstants;
32  import org.springframework.ws.transport.TransportOutputStream;
33  import org.springframework.xml.namespace.QNameUtils;
34  
35  import org.w3c.dom.Document;
36  import org.w3c.dom.Element;
37  import org.w3c.dom.NodeList;
38  
39  /**
40   * Implementation of the <code>PoxMessage</code> interface that is based on a DOM Document.
41   *
42   * @author Arjen Poutsma
43   * @see Document
44   * @since 1.0.0
45   */
46  public class DomPoxMessage implements PoxMessage {
47  
48      private final String contentType;
49  
50      private final Document document;
51  
52      private final Transformer transformer;
53  
54      /**
55       * Constructs a new instance of the <code>DomPoxMessage</code> with the given document.
56       *
57       * @param document the document to base the message on
58       */
59      public DomPoxMessage(Document document, Transformer transformer, String contentType) {
60          Assert.notNull(document, "'document' must not be null");
61          Assert.notNull(transformer, "'transformer' must not be null");
62          Assert.hasLength(contentType, "'contentType' must not be empty");
63          this.document = document;
64          this.transformer = transformer;
65          this.contentType = contentType;
66      }
67  
68      /** Returns the document underlying this message. */
69      public Document getDocument() {
70          return document;
71      }
72  
73      public Result getPayloadResult() {
74          NodeList children = document.getChildNodes();
75          for (int i = 0; i < children.getLength(); i++) {
76              document.removeChild(children.item(i));
77          }
78          return new DOMResult(document);
79      }
80  
81      public Source getPayloadSource() {
82          return new DOMSource(document);
83      }
84  
85      public boolean hasFault() {
86          return false;
87      }
88  
89      public String getFaultReason() {
90          return null;
91      }
92  
93      public String toString() {
94          StringBuilder builder = new StringBuilder("DomPoxMessage ");
95          Element root = document.getDocumentElement();
96          if (root != null) {
97              builder.append(' ');
98              builder.append(QNameUtils.getQNameForNode(root));
99          }
100         return builder.toString();
101     }
102 
103     public void writeTo(OutputStream outputStream) throws IOException {
104         try {
105             if (outputStream instanceof TransportOutputStream) {
106                 TransportOutputStream transportOutputStream = (TransportOutputStream) outputStream;
107                 transportOutputStream.addHeader(TransportConstants.HEADER_CONTENT_TYPE, contentType);
108             }
109             transformer.transform(getPayloadSource(), new StreamResult(outputStream));
110         }
111         catch (TransformerException ex) {
112             throw new DomPoxMessageException("Could write document: " + ex.getMessage(), ex);
113         }
114     }
115 }