001: /*
002: * Copyright 2007 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.ArrayList;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Locale;
025: import javax.activation.DataHandler;
026: import javax.xml.namespace.QName;
027: import javax.xml.soap.AttachmentPart;
028: import javax.xml.soap.Detail;
029: import javax.xml.soap.DetailEntry;
030: import javax.xml.soap.MimeHeader;
031: import javax.xml.soap.MimeHeaders;
032: import javax.xml.soap.Name;
033: import javax.xml.soap.SOAPBody;
034: import javax.xml.soap.SOAPElement;
035: import javax.xml.soap.SOAPEnvelope;
036: import javax.xml.soap.SOAPException;
037: import javax.xml.soap.SOAPFault;
038: import javax.xml.soap.SOAPHeader;
039: import javax.xml.soap.SOAPHeaderElement;
040: import javax.xml.soap.SOAPMessage;
041: import javax.xml.transform.Result;
042: import javax.xml.transform.Source;
043: import javax.xml.transform.dom.DOMResult;
044: import javax.xml.transform.dom.DOMSource;
045:
046: import org.springframework.util.ObjectUtils;
047: import org.springframework.ws.soap.SoapVersion;
048: import org.springframework.ws.soap.saaj.support.SaajUtils;
049: import org.springframework.ws.transport.TransportOutputStream;
050:
051: /**
052: * SAAJ 1.2 specific implementation of the <code>SaajImplementation</code> interface.
053: *
054: * @author Arjen Poutsma
055: * @since 1.0.0
056: */
057: public class Saaj12Implementation implements SaajImplementation {
058:
059: private static final Saaj12Implementation INSTANCE = new Saaj12Implementation();
060:
061: private Saaj12Implementation() {
062: }
063:
064: public static Saaj12Implementation getInstance() {
065: return INSTANCE;
066: }
067:
068: public QName getName(SOAPElement element) {
069: return SaajUtils.toQName(element.getElementName());
070: }
071:
072: public QName getFaultCode(SOAPFault fault) {
073: return SaajUtils.toQName(fault.getFaultCodeAsName());
074: }
075:
076: public boolean isSoap11(SOAPElement element) {
077: return true;
078: }
079:
080: public DetailEntry addDetailEntry(Detail detail, QName name)
081: throws SOAPException {
082: Name detailEntryName = SaajUtils.toName(name, detail);
083: return detail.addDetailEntry(detailEntryName);
084: }
085:
086: public SOAPHeaderElement addHeaderElement(SOAPHeader header,
087: QName name) throws SOAPException {
088: Name saajName = SaajUtils.toName(name, header);
089: return header.addHeaderElement(saajName);
090: }
091:
092: public SOAPFault addFault(SOAPBody body, QName faultCode,
093: String faultString, Locale locale) throws SOAPException {
094: Name name = SaajUtils.toName(faultCode, body);
095: if (locale == null) {
096: return body.addFault(name, faultString);
097: } else {
098: return body.addFault(name, faultString, locale);
099: }
100: }
101:
102: public Source getSource(SOAPElement element) {
103: return new DOMSource(element);
104: }
105:
106: public Result getResult(SOAPElement element) {
107: return new DOMResult(element);
108: }
109:
110: public void addAttribute(SOAPElement element, QName name,
111: String value) throws SOAPException {
112: Name attributeName = SaajUtils.toName(name, element);
113: element.addAttribute(attributeName, value);
114: }
115:
116: public void removeAttribute(SOAPElement element, QName name)
117: throws SOAPException {
118: Name attributeName = SaajUtils.toName(name, element);
119: element.removeAttribute(attributeName);
120: }
121:
122: public String getAttributeValue(SOAPElement element, QName name)
123: throws SOAPException {
124: Name attributeName = SaajUtils.toName(name, element);
125: return element.getAttributeValue(attributeName);
126: }
127:
128: public Iterator getAllAttibutes(SOAPElement element) {
129: List results = new ArrayList();
130: for (Iterator iterator = element.getAllAttributes(); iterator
131: .hasNext();) {
132: Name attributeName = (Name) iterator.next();
133: results.add(SaajUtils.toQName(attributeName));
134: }
135: return results.iterator();
136: }
137:
138: public String getText(SOAPElement element) {
139: return element.getValue();
140: }
141:
142: public void setText(SOAPElement element, String content) {
143: element.setValue(content);
144: }
145:
146: public SOAPEnvelope getEnvelope(SOAPMessage message)
147: throws SOAPException {
148: return message.getSOAPPart().getEnvelope();
149: }
150:
151: public SOAPHeader getHeader(SOAPEnvelope envelope)
152: throws SOAPException {
153: return envelope.getHeader();
154: }
155:
156: public SOAPBody getBody(SOAPEnvelope envelope) throws SOAPException {
157: return envelope.getBody();
158: }
159:
160: public Iterator examineAllHeaderElements(SOAPHeader header) {
161: return header.examineAllHeaderElements();
162: }
163:
164: public Iterator examineMustUnderstandHeaderElements(
165: SOAPHeader header, String actorOrRole) {
166: return header.examineMustUnderstandHeaderElements(actorOrRole);
167: }
168:
169: public String getActorOrRole(SOAPHeaderElement headerElement) {
170: return headerElement.getActor();
171: }
172:
173: public void setActorOrRole(SOAPHeaderElement headerElement,
174: String actorOrRole) {
175: headerElement.setActor(actorOrRole);
176: }
177:
178: public boolean getMustUnderstand(SOAPHeaderElement headerElement) {
179: return headerElement.getMustUnderstand();
180: }
181:
182: public void setMustUnderstand(SOAPHeaderElement headerElement,
183: boolean mustUnderstand) {
184: headerElement.setMustUnderstand(mustUnderstand);
185: }
186:
187: public boolean hasFault(SOAPBody body) {
188: return body.hasFault();
189: }
190:
191: public SOAPFault getFault(SOAPBody body) {
192: return body.getFault();
193: }
194:
195: public String getFaultActor(SOAPFault fault) {
196: return fault.getFaultActor();
197: }
198:
199: public void setFaultActor(SOAPFault fault, String actorOrRole)
200: throws SOAPException {
201: fault.setFaultActor(actorOrRole);
202: }
203:
204: public String getFaultString(SOAPFault fault) {
205: return fault.getFaultString();
206: }
207:
208: public Locale getFaultStringLocale(SOAPFault fault) {
209: return fault.getFaultStringLocale();
210: }
211:
212: public Detail getFaultDetail(SOAPFault fault) {
213: return fault.getDetail();
214: }
215:
216: public Detail addFaultDetail(SOAPFault fault) throws SOAPException {
217: return fault.addDetail();
218: }
219:
220: public void addTextNode(DetailEntry detailEntry, String text)
221: throws SOAPException {
222: detailEntry.addTextNode(text);
223: }
224:
225: public Iterator getDetailEntries(Detail detail) {
226: return detail.getDetailEntries();
227: }
228:
229: public SOAPElement getFirstBodyElement(SOAPBody body) {
230: for (Iterator iterator = body.getChildElements(); iterator
231: .hasNext();) {
232: Object child = iterator.next();
233: if (child instanceof SOAPElement) {
234: return (SOAPElement) child;
235: }
236: }
237: return null;
238: }
239:
240: public void removeContents(SOAPElement element) {
241: element.removeContents();
242: }
243:
244: public void writeTo(SOAPMessage message, OutputStream outputStream)
245: throws SOAPException, IOException {
246: if (message.saveRequired()) {
247: message.saveChanges();
248: }
249: if (outputStream instanceof TransportOutputStream) {
250: TransportOutputStream transportOutputStream = (TransportOutputStream) outputStream;
251: // some SAAJ implementations (Axis 1) do not have a Content-Type header by default
252: MimeHeaders headers = message.getMimeHeaders();
253: if (ObjectUtils.isEmpty(headers.getHeader("Content-Type"))) {
254: headers.addHeader("Content-Type", SoapVersion.SOAP_11
255: .getContentType());
256: if (message.saveRequired()) {
257: message.saveChanges();
258: }
259: }
260: for (Iterator iterator = headers.getAllHeaders(); iterator
261: .hasNext();) {
262: MimeHeader mimeHeader = (MimeHeader) iterator.next();
263: transportOutputStream.addHeader(mimeHeader.getName(),
264: mimeHeader.getValue());
265: }
266: }
267: message.writeTo(outputStream);
268:
269: }
270:
271: public MimeHeaders getMimeHeaders(SOAPMessage message) {
272: return message.getMimeHeaders();
273: }
274:
275: public Iterator getAttachments(SOAPMessage message) {
276: return message.getAttachments();
277: }
278:
279: public Iterator getAttachment(SOAPMessage message,
280: MimeHeaders mimeHeaders) {
281: return message.getAttachments(mimeHeaders);
282: }
283:
284: public AttachmentPart addAttachmentPart(SOAPMessage message,
285: DataHandler dataHandler) {
286: AttachmentPart attachmentPart = message
287: .createAttachmentPart(dataHandler);
288: message.addAttachmentPart(attachmentPart);
289: return attachmentPart;
290: }
291:
292: //
293: // Unsupported
294: //
295:
296: public String getFaultRole(SOAPFault fault) {
297: throw new UnsupportedOperationException(
298: "SAAJ 1.2 does not support SOAP 1.2");
299: }
300:
301: public void setFaultRole(SOAPFault fault, String role) {
302: throw new UnsupportedOperationException(
303: "SAAJ 1.2 does not support SOAP 1.2");
304: }
305:
306: public SOAPHeaderElement addNotUnderstoodHeaderElement(
307: SOAPHeader header, QName name) {
308: throw new UnsupportedOperationException(
309: "SAAJ 1.2 does not support SOAP 1.2");
310: }
311:
312: public SOAPHeaderElement addUpgradeHeaderElement(SOAPHeader header,
313: String[] supportedSoapUris) {
314: throw new UnsupportedOperationException(
315: "SAAJ 1.2 does not support SOAP 1.2");
316: }
317:
318: public Iterator getFaultSubcodes(SOAPFault fault) {
319: throw new UnsupportedOperationException(
320: "SAAJ 1.2 does not support SOAP 1.2");
321: }
322:
323: public void appendFaultSubcode(SOAPFault fault, QName subcode) {
324: throw new UnsupportedOperationException(
325: "SAAJ 1.2 does not support SOAP 1.2");
326: }
327:
328: public String getFaultNode(SOAPFault fault) {
329: throw new UnsupportedOperationException(
330: "SAAJ 1.2 does not support SOAP 1.2");
331: }
332:
333: public void setFaultNode(SOAPFault fault, String uri) {
334: throw new UnsupportedOperationException(
335: "SAAJ 1.2 does not support SOAP 1.2");
336: }
337:
338: public String getFaultReasonText(SOAPFault fault, Locale locale) {
339: throw new UnsupportedOperationException(
340: "SAAJ 1.2 does not support SOAP 1.2");
341: }
342:
343: public void setFaultReasonText(SOAPFault fault, Locale locale,
344: String text) {
345: throw new UnsupportedOperationException(
346: "SAAJ 1.2 does not support SOAP 1.2");
347: }
348:
349: }
|