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.soap.saaj;
018:
019: import java.io.IOException;
020: import java.io.OutputStream;
021: import java.util.Iterator;
022: import javax.activation.DataHandler;
023: import javax.xml.soap.AttachmentPart;
024: import javax.xml.soap.MimeHeaders;
025: import javax.xml.soap.SOAPBody;
026: import javax.xml.soap.SOAPElement;
027: import javax.xml.soap.SOAPEnvelope;
028: import javax.xml.soap.SOAPException;
029: import javax.xml.soap.SOAPMessage;
030: import javax.xml.soap.SOAPPart;
031:
032: import org.springframework.util.Assert;
033: import org.springframework.util.ObjectUtils;
034: import org.springframework.ws.mime.Attachment;
035: import org.springframework.ws.mime.AttachmentException;
036: import org.springframework.ws.soap.AbstractSoapMessage;
037: import org.springframework.ws.soap.SoapEnvelope;
038: import org.springframework.ws.soap.SoapMessage;
039: import org.springframework.ws.soap.saaj.support.SaajUtils;
040: import org.springframework.ws.transport.TransportConstants;
041:
042: /**
043: * SAAJ-specific implementation of the {@link SoapMessage} interface. Created via the {@link SaajSoapMessageFactory},
044: * wraps a {@link SOAPMessage}.
045: *
046: * @author Arjen Poutsma
047: * @see SOAPMessage
048: * @since 1.0.0
049: */
050: public class SaajSoapMessage extends AbstractSoapMessage {
051:
052: private SOAPMessage saajMessage;
053:
054: private SoapEnvelope envelope;
055:
056: private static final String CONTENT_TYPE_XOP = "application/xop+xml";
057:
058: /**
059: * Create a new <code>SaajSoapMessage</code> based on the given SAAJ <code>SOAPMessage</code>.
060: *
061: * @param soapMessage the SAAJ SOAPMessage
062: */
063: public SaajSoapMessage(SOAPMessage soapMessage) {
064: Assert.notNull(soapMessage, "soapMessage must not be null");
065: saajMessage = soapMessage;
066: }
067:
068: /** Return the SAAJ <code>SOAPMessage</code> that this <code>SaajSoapMessage</code> is based on. */
069: public SOAPMessage getSaajMessage() {
070: return saajMessage;
071: }
072:
073: /** Sets the SAAJ <code>SOAPMessage</code> that this <code>SaajSoapMessage</code> is based on. */
074: public void setSaajMessage(SOAPMessage soapMessage) {
075: Assert.notNull(soapMessage, "soapMessage must not be null");
076: saajMessage = soapMessage;
077: }
078:
079: public SoapEnvelope getEnvelope() {
080: if (envelope == null) {
081: try {
082: SOAPEnvelope saajEnvelope = getImplementation()
083: .getEnvelope(getSaajMessage());
084: envelope = new SaajSoapEnvelope(saajEnvelope);
085: } catch (SOAPException ex) {
086: throw new SaajSoapEnvelopeException(ex);
087: }
088: }
089: return envelope;
090: }
091:
092: public String getSoapAction() {
093: MimeHeaders mimeHeaders = getImplementation().getMimeHeaders(
094: getSaajMessage());
095: String[] values = mimeHeaders
096: .getHeader(TransportConstants.HEADER_SOAP_ACTION);
097: return ObjectUtils.isEmpty(values) ? null : values[0];
098: }
099:
100: public void setSoapAction(String soapAction) {
101: MimeHeaders mimeHeaders = getImplementation().getMimeHeaders(
102: getSaajMessage());
103: mimeHeaders.setHeader(TransportConstants.HEADER_SOAP_ACTION,
104: soapAction);
105: }
106:
107: public void writeTo(OutputStream outputStream) throws IOException {
108: try {
109: getImplementation().writeTo(getSaajMessage(), outputStream);
110: outputStream.flush();
111: } catch (SOAPException ex) {
112: throw new SaajSoapMessageException(
113: "Could not write message to OutputStream: "
114: + ex.getMessage(), ex);
115: }
116: }
117:
118: public boolean isXopPackage() {
119: if (SaajUtils.getSaajVersion() >= SaajUtils.SAAJ_13) {
120: SOAPPart saajPart = saajMessage.getSOAPPart();
121: String[] contentTypes = saajPart
122: .getMimeHeader(TransportConstants.HEADER_CONTENT_TYPE);
123: for (int i = 0; i < contentTypes.length; i++) {
124: if (contentTypes[i].indexOf(CONTENT_TYPE_XOP) != -1) {
125: return true;
126: }
127: }
128: }
129: return false;
130: }
131:
132: public boolean convertToXopPackage() {
133: if (SaajUtils.getSaajVersion() >= SaajUtils.SAAJ_13) {
134: convertMessageToXop();
135: convertPartToXop();
136: return true;
137: } else {
138: return false;
139: }
140: }
141:
142: private void convertMessageToXop() {
143: MimeHeaders mimeHeaders = saajMessage.getMimeHeaders();
144: String[] oldContentTypes = mimeHeaders
145: .getHeader(TransportConstants.HEADER_CONTENT_TYPE);
146: String oldContentType = !ObjectUtils.isEmpty(oldContentTypes) ? oldContentTypes[0]
147: : getVersion().getContentType();
148: StringBuffer buffer = new StringBuffer(CONTENT_TYPE_XOP);
149: buffer.append(";type=");
150: buffer.append('"');
151: buffer.append(oldContentType);
152: buffer.append('"');
153: mimeHeaders.setHeader(TransportConstants.HEADER_CONTENT_TYPE,
154: buffer.toString());
155: }
156:
157: private void convertPartToXop() {
158: SOAPPart saajPart = saajMessage.getSOAPPart();
159: String[] oldContentTypes = saajPart
160: .getMimeHeader(TransportConstants.HEADER_CONTENT_TYPE);
161: String oldContentType = !ObjectUtils.isEmpty(oldContentTypes) ? oldContentTypes[0]
162: : getVersion().getContentType();
163: StringBuffer buffer = new StringBuffer(CONTENT_TYPE_XOP);
164: buffer.append(";type=");
165: buffer.append('"');
166: buffer.append(oldContentType);
167: buffer.append('"');
168: saajPart.setMimeHeader(TransportConstants.HEADER_CONTENT_TYPE,
169: buffer.toString());
170: }
171:
172: public Iterator getAttachments() throws AttachmentException {
173: Iterator iterator = getImplementation().getAttachments(
174: getSaajMessage());
175: return new SaajAttachmentIterator(iterator);
176: }
177:
178: public Attachment getAttachment(String contentId) {
179: Assert.hasLength(contentId, "contentId must not be empty");
180: MimeHeaders mimeHeaders = new MimeHeaders();
181: mimeHeaders.setHeader(TransportConstants.HEADER_CONTENT_ID,
182: contentId);
183: Iterator iterator = getImplementation().getAttachment(
184: getSaajMessage(), mimeHeaders);
185: if (!iterator.hasNext()) {
186: return null;
187: }
188: AttachmentPart saajAttachment = (AttachmentPart) iterator
189: .next();
190: return new SaajAttachment(saajAttachment);
191: }
192:
193: public Attachment addAttachment(String contentId,
194: DataHandler dataHandler) {
195: Assert.hasLength(contentId, "contentId must not be empty");
196: Assert.notNull(dataHandler, "dataHandler must not be null");
197: AttachmentPart saajAttachment = getImplementation()
198: .addAttachmentPart(getSaajMessage(), dataHandler);
199: saajAttachment.setContentId(contentId);
200: return new SaajAttachment(saajAttachment);
201: }
202:
203: protected SaajImplementation getImplementation() {
204: if (SaajUtils.getSaajVersion() == SaajUtils.SAAJ_13) {
205: return Saaj13Implementation.getInstance();
206: } else if (SaajUtils.getSaajVersion() == SaajUtils.SAAJ_12) {
207: return Saaj12Implementation.getInstance();
208: } else if (SaajUtils.getSaajVersion() == SaajUtils.SAAJ_11) {
209: return Saaj11Implementation.getInstance();
210: } else {
211: throw new IllegalStateException(
212: "Could not find SAAJ on the classpath");
213: }
214: }
215:
216: public String toString() {
217: StringBuffer buffer = new StringBuffer("SaajSoapMessage");
218: try {
219: SOAPEnvelope envelope = getImplementation().getEnvelope(
220: saajMessage);
221: if (envelope != null) {
222: SOAPBody body = getImplementation().getBody(envelope);
223: if (body != null) {
224: SOAPElement bodyElement = getImplementation()
225: .getFirstBodyElement(body);
226: if (bodyElement != null) {
227: buffer.append(' ');
228: buffer.append(getImplementation().getName(
229: bodyElement));
230: }
231: }
232: }
233: } catch (SOAPException ex) {
234: // ignore
235: }
236: return buffer.toString();
237: }
238:
239: private static class SaajAttachmentIterator implements Iterator {
240:
241: private final Iterator saajIterator;
242:
243: public SaajAttachmentIterator(Iterator saajIterator) {
244: this .saajIterator = saajIterator;
245: }
246:
247: public boolean hasNext() {
248: return saajIterator.hasNext();
249: }
250:
251: public Object next() {
252: AttachmentPart saajAttachment = (AttachmentPart) saajIterator
253: .next();
254: return new SaajAttachment(saajAttachment);
255: }
256:
257: public void remove() {
258: saajIterator.remove();
259: }
260: }
261:
262: }
|