001: /*
002: * Copyright 2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.ws.pox.dom;
018:
019: import java.io.IOException;
020: import java.io.OutputStream;
021: import javax.xml.transform.Result;
022: import javax.xml.transform.Source;
023: import javax.xml.transform.Transformer;
024: import javax.xml.transform.TransformerException;
025: import javax.xml.transform.dom.DOMResult;
026: import javax.xml.transform.dom.DOMSource;
027: import javax.xml.transform.stream.StreamResult;
028:
029: import org.springframework.util.Assert;
030: import org.springframework.ws.pox.PoxMessage;
031: import org.springframework.ws.transport.TransportConstants;
032: import org.springframework.ws.transport.TransportOutputStream;
033: import org.springframework.xml.namespace.QNameUtils;
034: import org.w3c.dom.Document;
035: import org.w3c.dom.Element;
036:
037: /**
038: * Implementation of the <code>PoxMessage</code> interface that is based on a DOM Document.
039: *
040: * @author Arjen Poutsma
041: * @see Document
042: * @since 1.0.0
043: */
044: public class DomPoxMessage implements PoxMessage {
045:
046: private final String contentType;
047:
048: private final Document document;
049:
050: private final Transformer transformer;
051:
052: /**
053: * Constructs a new instance of the <code>DomPoxMessage</code> with the given document.
054: *
055: * @param document the document to base the message on
056: */
057: public DomPoxMessage(Document document, Transformer transformer,
058: String contentType) {
059: Assert.notNull(document, "'document' must not be null");
060: Assert.notNull(transformer, "'transformer' must not be null");
061: Assert
062: .hasLength(contentType,
063: "'contentType' must not be empty");
064: this .document = document;
065: this .transformer = transformer;
066: this .contentType = contentType;
067: }
068:
069: /** Returns the document underlying this message. */
070: public Document getDocument() {
071: return document;
072: }
073:
074: public Result getPayloadResult() {
075: return new DOMResult(document);
076: }
077:
078: public Source getPayloadSource() {
079: return new DOMSource(document);
080: }
081:
082: public boolean hasFault() {
083: return false;
084: }
085:
086: public String getFaultReason() {
087: return null;
088: }
089:
090: public String toString() {
091: StringBuffer buffer = new StringBuffer("DomPoxMessage ");
092: Element root = document.getDocumentElement();
093: if (root != null) {
094: buffer.append(' ');
095: buffer.append(QNameUtils.getQNameForNode(root));
096: }
097: return buffer.toString();
098: }
099:
100: public void writeTo(OutputStream outputStream) throws IOException {
101: try {
102: if (outputStream instanceof TransportOutputStream) {
103: TransportOutputStream transportOutputStream = (TransportOutputStream) outputStream;
104: transportOutputStream.addHeader(
105: TransportConstants.HEADER_CONTENT_TYPE,
106: contentType);
107: }
108: transformer.transform(getPayloadSource(), new StreamResult(
109: outputStream));
110: } catch (TransformerException ex) {
111: throw new DomPoxMessageException("Could write document: "
112: + ex.getMessage(), ex);
113: }
114: }
115: }
|