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.soap.saaj;
18  
19  import java.io.IOException;
20  import java.io.OutputStream;
21  import java.util.Iterator;
22  import java.util.Locale;
23  import javax.activation.DataHandler;
24  import javax.xml.namespace.QName;
25  import javax.xml.soap.AttachmentPart;
26  import javax.xml.soap.Detail;
27  import javax.xml.soap.DetailEntry;
28  import javax.xml.soap.MimeHeader;
29  import javax.xml.soap.MimeHeaders;
30  import javax.xml.soap.SOAPBody;
31  import javax.xml.soap.SOAPConstants;
32  import javax.xml.soap.SOAPElement;
33  import javax.xml.soap.SOAPEnvelope;
34  import javax.xml.soap.SOAPException;
35  import javax.xml.soap.SOAPFault;
36  import javax.xml.soap.SOAPHeader;
37  import javax.xml.soap.SOAPHeaderElement;
38  import javax.xml.soap.SOAPMessage;
39  import javax.xml.transform.Result;
40  import javax.xml.transform.Source;
41  import javax.xml.transform.dom.DOMResult;
42  import javax.xml.transform.dom.DOMSource;
43  
44  import org.springframework.util.ObjectUtils;
45  import org.springframework.ws.soap.SoapVersion;
46  import org.springframework.ws.transport.TransportConstants;
47  import org.springframework.ws.transport.TransportOutputStream;
48  
49  /**
50   * SAAJ 1.3 specific implementation of the <code>SaajImplementation</code> interface.
51   *
52   * @author Arjen Poutsma
53   * @since 1.0.0
54   */
55  class Saaj13Implementation extends SaajImplementation {
56  
57      private static final Saaj13Implementation INSTANCE = new Saaj13Implementation();
58  
59      private Saaj13Implementation() {
60      }
61  
62      public static Saaj13Implementation getInstance() {
63          return INSTANCE;
64      }
65  
66      @Override
67      public QName getName(SOAPElement element) {
68          return element.getElementQName();
69      }
70  
71      @Override
72      public QName getFaultCode(SOAPFault fault) {
73          return fault.getFaultCodeAsQName();
74      }
75  
76      public boolean isSoap11(SOAPElement element) {
77          return SOAPConstants.URI_NS_SOAP_1_1_ENVELOPE.equals(element.getNamespaceURI());
78      }
79  
80      @Override
81      public DetailEntry addDetailEntry(Detail detail, QName name) throws SOAPException {
82          return detail.addDetailEntry(name);
83      }
84  
85      @Override
86      public SOAPHeaderElement addHeaderElement(SOAPHeader header, QName name) throws SOAPException {
87          return header.addHeaderElement(name);
88      }
89  
90      @Override
91      public String getFaultRole(SOAPFault fault) {
92          return fault.getFaultRole();
93      }
94  
95      @Override
96      public void setFaultRole(SOAPFault fault, String role) throws SOAPException {
97          fault.setFaultRole(role);
98      }
99  
100     @Override
101     public SOAPHeaderElement addNotUnderstoodHeaderElement(SOAPHeader header, QName name) throws SOAPException {
102         return header.addNotUnderstoodHeaderElement(name);
103     }
104 
105     @Override
106     public SOAPHeaderElement addUpgradeHeaderElement(SOAPHeader header, String[] supportedSoapUris)
107             throws SOAPException {
108         return header.addUpgradeHeaderElement(supportedSoapUris);
109     }
110 
111     @Override
112     @SuppressWarnings("unchecked")
113     public Iterator<QName> getFaultSubcodes(SOAPFault fault) {
114         return fault.getFaultSubcodes();
115     }
116 
117     @Override
118     public void appendFaultSubcode(SOAPFault fault, QName subcode) throws SOAPException {
119         fault.appendFaultSubcode(subcode);
120     }
121 
122     @Override
123     public String getFaultNode(SOAPFault fault) {
124         return fault.getFaultNode();
125     }
126 
127     @Override
128     public void setFaultNode(SOAPFault fault, String uri) throws SOAPException {
129         fault.setFaultNode(uri);
130     }
131 
132     @Override
133     public String getFaultReasonText(SOAPFault fault, Locale locale) throws SOAPException {
134         return fault.getFaultReasonText(locale);
135     }
136 
137     @Override
138     public void setFaultReasonText(SOAPFault fault, Locale locale, String text) throws SOAPException {
139         fault.addFaultReasonText(text, locale);
140     }
141 
142     @Override
143     public SOAPFault addFault(SOAPBody body, QName faultCode, String faultString, Locale locale) throws SOAPException {
144         if (locale == null) {
145             return body.addFault(faultCode, faultString);
146         }
147         else {
148             return body.addFault(faultCode, faultString, locale);
149         }
150     }
151 
152     @Override
153     public Source getSource(SOAPElement element) {
154         return new DOMSource(element);
155     }
156 
157     @Override
158     public Result getResult(SOAPElement element) {
159         return new DOMResult(element);
160     }
161 
162     @Override
163     public String getText(SOAPElement element) {
164         return element.getValue();
165     }
166 
167     @Override
168     public void setText(SOAPElement element, String content) {
169         element.setValue(content);
170     }
171 
172     @Override
173     public void addAttribute(SOAPElement element, QName name, String value) throws SOAPException {
174         element.addAttribute(name, value);
175     }
176 
177     @Override
178     public void removeAttribute(SOAPElement element, QName name) throws SOAPException {
179         element.removeAttribute(name);
180     }
181 
182     @Override
183     public String getAttributeValue(SOAPElement element, QName name) throws SOAPException {
184         return element.getAttributeValue(name);
185     }
186 
187     @Override
188     @SuppressWarnings("unchecked")
189     public Iterator<QName> getAllAttributes(SOAPElement element) {
190         return element.getAllAttributesAsQNames();
191     }
192 
193     @Override
194     public SOAPEnvelope getEnvelope(SOAPMessage message) throws SOAPException {
195         return message.getSOAPPart().getEnvelope();
196     }
197 
198     @Override
199     public SOAPHeader getHeader(SOAPEnvelope envelope) throws SOAPException {
200         return envelope.getHeader();
201     }
202 
203     @Override
204     public SOAPBody getBody(SOAPEnvelope envelope) throws SOAPException {
205         return envelope.getBody();
206     }
207 
208     @Override
209     @SuppressWarnings("unchecked")
210     public Iterator<SOAPHeaderElement> examineAllHeaderElements(SOAPHeader header) {
211         return header.examineAllHeaderElements();
212     }
213 
214     @Override
215     @SuppressWarnings("unchecked")
216     public Iterator<SOAPHeaderElement> examineMustUnderstandHeaderElements(SOAPHeader header, String actorOrRole) {
217         return header.examineMustUnderstandHeaderElements(actorOrRole);
218     }
219 
220     @Override
221     public String getActorOrRole(SOAPHeaderElement headerElement) {
222         return headerElement.getActor();
223     }
224 
225     @Override
226     public void setActorOrRole(SOAPHeaderElement headerElement, String actorOrRole) {
227         headerElement.setActor(actorOrRole);
228     }
229 
230     @Override
231     public boolean getMustUnderstand(SOAPHeaderElement headerElement) {
232         return headerElement.getMustUnderstand();
233     }
234 
235     @Override
236     public void setMustUnderstand(SOAPHeaderElement headerElement, boolean mustUnderstand) {
237         headerElement.setMustUnderstand(mustUnderstand);
238     }
239 
240     @Override
241     public boolean hasFault(SOAPBody body) {
242         return body.hasFault();
243     }
244 
245     @Override
246     public SOAPFault getFault(SOAPBody body) {
247         return body.getFault();
248     }
249 
250     @Override
251     public String getFaultActor(SOAPFault fault) {
252         return fault.getFaultActor();
253     }
254 
255     @Override
256     public void setFaultActor(SOAPFault fault, String actorOrRole) throws SOAPException {
257         fault.setFaultActor(actorOrRole);
258     }
259 
260     @Override
261     public String getFaultString(SOAPFault fault) {
262         return fault.getFaultString();
263     }
264 
265     @Override
266     public Locale getFaultStringLocale(SOAPFault fault) {
267         return fault.getFaultStringLocale();
268     }
269 
270     @Override
271     public Detail getFaultDetail(SOAPFault fault) {
272         return fault.getDetail();
273     }
274 
275     @Override
276     public Detail addFaultDetail(SOAPFault fault) throws SOAPException {
277         return fault.addDetail();
278     }
279 
280     @Override
281     public void addTextNode(DetailEntry detailEntry, String text) throws SOAPException {
282         detailEntry.addTextNode(text);
283     }
284 
285     @Override
286     @SuppressWarnings("unchecked")
287     public Iterator<DetailEntry> getDetailEntries(Detail detail) {
288         return detail.getDetailEntries();
289     }
290 
291     @Override
292     public SOAPElement getFirstBodyElement(SOAPBody body) {
293         for (Iterator<?> iterator = body.getChildElements(); iterator.hasNext();) {
294             Object child = iterator.next();
295             if (child instanceof SOAPElement) {
296                 return (SOAPElement) child;
297             }
298         }
299         return null;
300     }
301 
302     @Override
303     public void removeContents(SOAPElement element) {
304         element.removeContents();
305     }
306 
307     @Override
308     @SuppressWarnings("unchecked")
309     Iterator<SOAPElement> getChildElements(SOAPElement element, QName name) throws SOAPException {
310         return element.getChildElements(name);
311     }
312 
313     @Override
314     void addNamespaceDeclaration(SOAPElement element, String prefix, String namespaceUri) throws SOAPException {
315         element.addNamespaceDeclaration(prefix, namespaceUri);
316     }
317 
318     @Override
319     public void writeTo(SOAPMessage message, OutputStream outputStream) throws SOAPException, IOException {
320         if (message.saveRequired()) {
321             message.saveChanges();
322         }
323         if (outputStream instanceof TransportOutputStream) {
324             TransportOutputStream transportOutputStream = (TransportOutputStream) outputStream;
325             // some SAAJ implementations (Axis 1) do not have a Content-Type header by default
326             MimeHeaders headers = message.getMimeHeaders();
327             if (ObjectUtils.isEmpty(headers.getHeader(TransportConstants.HEADER_CONTENT_TYPE))) {
328                 SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
329                 if (envelope.getElementQName().getNamespaceURI()
330                         .equals(SoapVersion.SOAP_11.getEnvelopeNamespaceUri())) {
331                     headers.addHeader(TransportConstants.HEADER_CONTENT_TYPE, SoapVersion.SOAP_11.getContentType());
332                 }
333                 else {
334                     headers.addHeader(TransportConstants.HEADER_CONTENT_TYPE, SoapVersion.SOAP_12.getContentType());
335                 }
336                 if (message.saveRequired()) {
337                     message.saveChanges();
338                 }
339             }
340             for (Iterator<?> iterator = headers.getAllHeaders(); iterator.hasNext();) {
341                 MimeHeader mimeHeader = (MimeHeader) iterator.next();
342                 transportOutputStream.addHeader(mimeHeader.getName(), mimeHeader.getValue());
343             }
344         }
345         message.writeTo(outputStream);
346 
347     }
348 
349     @Override
350     public MimeHeaders getMimeHeaders(SOAPMessage message) {
351         return message.getMimeHeaders();
352     }
353 
354     @Override
355     @SuppressWarnings("unchecked")
356     public Iterator<AttachmentPart> getAttachments(SOAPMessage message) {
357         return message.getAttachments();
358     }
359 
360     @Override
361     @SuppressWarnings("unchecked")
362     public Iterator<AttachmentPart> getAttachment(SOAPMessage message, MimeHeaders mimeHeaders) {
363         return message.getAttachments(mimeHeaders);
364     }
365 
366     @Override
367     public AttachmentPart addAttachmentPart(SOAPMessage message, DataHandler dataHandler) {
368         AttachmentPart attachmentPart = message.createAttachmentPart(dataHandler);
369         message.addAttachmentPart(attachmentPart);
370         return attachmentPart;
371     }
372 }