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