001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.ws.addressing;
038:
039: import com.sun.xml.ws.addressing.model.InvalidMapException;
040: import com.sun.xml.ws.addressing.model.MapRequiredException;
041: import com.sun.xml.ws.api.SOAPVersion;
042: import com.sun.xml.ws.api.WSBinding;
043: import com.sun.xml.ws.api.addressing.AddressingVersion;
044: import com.sun.xml.ws.api.message.Packet;
045: import com.sun.xml.ws.api.model.wsdl.WSDLBoundOperation;
046: import com.sun.xml.ws.api.model.wsdl.WSDLFault;
047: import com.sun.xml.ws.api.model.wsdl.WSDLOperation;
048: import com.sun.xml.ws.api.model.wsdl.WSDLOutput;
049: import com.sun.xml.ws.api.model.wsdl.WSDLPort;
050: import com.sun.xml.ws.api.model.SEIModel;
051: import com.sun.xml.ws.model.wsdl.WSDLOperationImpl;
052: import com.sun.xml.ws.model.JavaMethodImpl;
053: import com.sun.xml.ws.model.CheckedExceptionImpl;
054: import com.sun.istack.Nullable;
055: import com.sun.istack.NotNull;
056: import org.w3c.dom.Element;
057:
058: import javax.xml.bind.Marshaller;
059: import javax.xml.bind.Unmarshaller;
060: import javax.xml.namespace.QName;
061: import javax.xml.soap.Detail;
062: import javax.xml.soap.SOAPConstants;
063: import javax.xml.soap.SOAPException;
064: import javax.xml.soap.SOAPFactory;
065: import javax.xml.soap.SOAPFault;
066: import javax.xml.soap.SOAPMessage;
067: import javax.xml.ws.WebServiceException;
068: import java.util.Map;
069:
070: /**
071: * @author Arun Gupta
072: */
073: public abstract class WsaTubeHelper {
074:
075: public WsaTubeHelper(WSBinding binding, SEIModel seiModel,
076: WSDLPort wsdlPort) {
077: this .binding = binding;
078: this .wsdlPort = wsdlPort;
079: this .seiModel = seiModel;
080: this .soapVer = binding.getSOAPVersion();
081: this .addVer = binding.getAddressingVersion();
082:
083: }
084:
085: public String getFaultAction(Packet requestPacket,
086: Packet responsePacket) {
087: String action = null;
088: if (seiModel != null) {
089: action = getFaultActionFromSEIModel(requestPacket,
090: responsePacket);
091: }
092: if (action != null)
093: return action;
094: else
095: action = addVer.getDefaultFaultAction();
096: if (wsdlPort == null)
097: return action;
098: WSDLBoundOperation wbo = requestPacket.getMessage()
099: .getOperation(wsdlPort);
100: return getFaultAction(wbo, responsePacket);
101: }
102:
103: String getFaultActionFromSEIModel(Packet requestPacket,
104: Packet responsePacket) {
105: String action = null;
106: if (seiModel == null || wsdlPort == null)
107: return action;
108:
109: try {
110: SOAPMessage sm = responsePacket.getMessage().copy()
111: .readAsSOAPMessage();
112: if (sm == null)
113: return action;
114:
115: if (sm.getSOAPBody() == null)
116: return action;
117:
118: if (sm.getSOAPBody().getFault() == null)
119: return action;
120:
121: Detail detail = sm.getSOAPBody().getFault().getDetail();
122: if (detail == null)
123: return action;
124:
125: String ns = detail.getFirstChild().getNamespaceURI();
126: String name = detail.getFirstChild().getLocalName();
127: JavaMethodImpl jm = (JavaMethodImpl) requestPacket
128: .getMessage().getMethod(seiModel);
129: for (CheckedExceptionImpl ce : jm.getCheckedExceptions()) {
130: if (ce.getDetailType().tagName.getLocalPart().equals(
131: name)
132: && ce.getDetailType().tagName.getNamespaceURI()
133: .equals(ns)) {
134: return ce.getFaultAction();
135: }
136: }
137: return action;
138: } catch (SOAPException e) {
139: throw new WebServiceException(e);
140: }
141: }
142:
143: String getFaultAction(@Nullable
144: WSDLBoundOperation wbo, Packet responsePacket) {
145: String action = addVer.getDefaultFaultAction();
146: if (wbo == null)
147: return action;
148:
149: try {
150: SOAPMessage sm = responsePacket.getMessage().copy()
151: .readAsSOAPMessage();
152: if (sm == null)
153: return action;
154:
155: if (sm.getSOAPBody() == null)
156: return action;
157:
158: if (sm.getSOAPBody().getFault() == null)
159: return action;
160:
161: Detail detail = sm.getSOAPBody().getFault().getDetail();
162: if (detail == null)
163: return action;
164:
165: String ns = detail.getFirstChild().getNamespaceURI();
166: String name = detail.getFirstChild().getLocalName();
167:
168: WSDLOperation o = wbo.getOperation();
169:
170: WSDLFault fault = o.getFault(new QName(ns, name));
171: if (fault == null)
172: return action;
173:
174: WSDLOperationImpl impl = (WSDLOperationImpl) o;
175: Map<String, String> map = impl.getFaultActionMap();
176: if (map == null)
177: return action;
178:
179: action = map.get(fault.getName());
180:
181: return action;
182: } catch (SOAPException e) {
183: throw new WebServiceException(e);
184: }
185: }
186:
187: public String getInputAction(Packet packet) {
188: String action = null;
189:
190: if (wsdlPort != null) {
191: WSDLBoundOperation wbo = wsdlPort.getBinding()
192: .getOperation(
193: packet.getMessage()
194: .getPayloadNamespaceURI(),
195: packet.getMessage().getPayloadLocalPart());
196: if (wbo != null) {
197: WSDLOperation op = wbo.getOperation();
198: action = op.getInput().getAction();
199: }
200: }
201:
202: return action;
203: }
204:
205: /**
206: * This method gives the Input addressing Action for a message.
207: * It gives the Action set in the wsdl operation for the corresponding payload.
208: * If it is not explicitly set, it gives the soapAction
209: * @param packet
210: * @return input Action
211: */
212: public String getEffectiveInputAction(Packet packet) {
213: //non-default SOAPAction beomes wsa:action
214: if (packet.soapAction != null && !packet.soapAction.equals(""))
215: return packet.soapAction;
216: String action = null;
217:
218: if (wsdlPort != null) {
219: WSDLBoundOperation wbo = wsdlPort.getBinding()
220: .getOperation(
221: packet.getMessage()
222: .getPayloadNamespaceURI(),
223: packet.getMessage().getPayloadLocalPart());
224: if (wbo != null) {
225: WSDLOperation op = wbo.getOperation();
226: action = op.getInput().getAction();
227: } else
228: action = packet.soapAction;
229: } else {
230: action = packet.soapAction;
231: }
232: return action;
233: }
234:
235: public boolean isInputActionDefault(Packet packet) {
236: if (wsdlPort == null)
237: return false;
238:
239: WSDLBoundOperation wbo = wsdlPort.getBinding().getOperation(
240: packet.getMessage().getPayloadNamespaceURI(),
241: packet.getMessage().getPayloadLocalPart());
242: if (wbo == null)
243: return false;
244:
245: WSDLOperation op = wbo.getOperation();
246: return ((WSDLOperationImpl) op).getInput().isDefaultAction();
247:
248: }
249:
250: public String getSOAPAction(Packet packet) {
251: String action = "";
252:
253: if (packet == null)
254: return action;
255:
256: if (packet.getMessage() == null)
257: return action;
258:
259: WSDLBoundOperation op = packet.getMessage().getOperation(
260: wsdlPort);
261: if (op == null)
262: return action;
263:
264: action = op.getSOAPAction();
265:
266: return action;
267: }
268:
269: public String getOutputAction(Packet packet) {
270: String action = AddressingVersion.UNSET_OUTPUT_ACTION;
271: if (seiModel != null) {
272: JavaMethodImpl jm = (JavaMethodImpl) packet.getMessage()
273: .getMethod(seiModel);
274: if (jm.getOutputAction() != null
275: && !jm.getOutputAction().equals("")) {
276: return jm.getOutputAction();
277: }
278: }
279: if (wsdlPort != null) {
280: WSDLBoundOperation wbo = wsdlPort.getBinding()
281: .getOperation(
282: packet.getMessage()
283: .getPayloadNamespaceURI(),
284: packet.getMessage().getPayloadLocalPart());
285: return getOutputAction(wbo);
286: }
287: return action;
288: }
289:
290: String getOutputAction(@Nullable
291: WSDLBoundOperation wbo) {
292: String action = AddressingVersion.UNSET_OUTPUT_ACTION;
293: if (wbo != null) {
294: WSDLOutput op = wbo.getOperation().getOutput();
295: if (op != null)
296: action = op.getAction();
297: }
298: return action;
299: }
300:
301: public SOAPFault newInvalidMapFault(InvalidMapException e,
302: AddressingVersion av) {
303: QName name = e.getMapQName();
304: QName subsubcode = e.getSubsubcode();
305: QName subcode = av.invalidMapTag;
306: String faultstring = String.format(av.getInvalidMapText(),
307: name, subsubcode);
308:
309: try {
310: SOAPFactory factory;
311: SOAPFault fault;
312: if (soapVer == SOAPVersion.SOAP_12) {
313: factory = SOAPVersion.SOAP_12.saajSoapFactory;
314: fault = factory.createFault();
315: fault.setFaultCode(SOAPConstants.SOAP_SENDER_FAULT);
316: fault.appendFaultSubcode(subcode);
317: fault.appendFaultSubcode(subsubcode);
318: getInvalidMapDetail(name, fault.addDetail());
319: } else {
320: factory = SOAPVersion.SOAP_11.saajSoapFactory;
321: fault = factory.createFault();
322: fault.setFaultCode(subsubcode);
323: }
324:
325: fault.setFaultString(faultstring);
326:
327: return fault;
328: } catch (SOAPException se) {
329: throw new WebServiceException(se);
330: }
331: }
332:
333: public SOAPFault newMapRequiredFault(MapRequiredException e,
334: AddressingVersion av) {
335: QName subcode = av.mapRequiredTag;
336: QName subsubcode = av.mapRequiredTag;
337: String faultstring = av.getMapRequiredText();
338:
339: try {
340: SOAPFactory factory;
341: SOAPFault fault;
342: if (soapVer == SOAPVersion.SOAP_12) {
343: factory = SOAPVersion.SOAP_12.saajSoapFactory;
344: fault = factory.createFault();
345: fault.setFaultCode(SOAPConstants.SOAP_SENDER_FAULT);
346: fault.appendFaultSubcode(subcode);
347: fault.appendFaultSubcode(subsubcode);
348: getMapRequiredDetail(e.getMapQName(), fault.addDetail());
349: } else {
350: factory = SOAPVersion.SOAP_11.saajSoapFactory;
351: fault = factory.createFault();
352: fault.setFaultCode(subsubcode);
353: }
354:
355: fault.setFaultString(faultstring);
356:
357: return fault;
358: } catch (SOAPException se) {
359: throw new WebServiceException(se);
360: }
361: }
362:
363: public abstract void getProblemActionDetail(String action,
364: Element element);
365:
366: public abstract void getInvalidMapDetail(QName name, Element element);
367:
368: public abstract void getMapRequiredDetail(QName name,
369: Element element);
370:
371: protected Unmarshaller unmarshaller;
372: protected Marshaller marshaller;
373: protected SEIModel seiModel;
374: protected WSDLPort wsdlPort;
375: protected WSBinding binding;
376: protected final SOAPVersion soapVer;
377: protected final AddressingVersion addVer;
378: }
|