ohair@286: /* alanb@368: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. ohair@286: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ohair@286: * ohair@286: * This code is free software; you can redistribute it and/or modify it ohair@286: * under the terms of the GNU General Public License version 2 only, as ohair@286: * published by the Free Software Foundation. Oracle designates this ohair@286: * particular file as subject to the "Classpath" exception as provided ohair@286: * by Oracle in the LICENSE file that accompanied this code. ohair@286: * ohair@286: * This code is distributed in the hope that it will be useful, but WITHOUT ohair@286: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ohair@286: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ohair@286: * version 2 for more details (a copy is included in the LICENSE file that ohair@286: * accompanied this code). ohair@286: * ohair@286: * You should have received a copy of the GNU General Public License version ohair@286: * 2 along with this work; if not, write to the Free Software Foundation, ohair@286: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ohair@286: * ohair@286: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@286: * or visit www.oracle.com if you need additional information or have any ohair@286: * questions. ohair@286: */ ohair@286: ohair@286: package com.sun.xml.internal.ws.addressing; ohair@286: ohair@286: import com.sun.xml.internal.ws.addressing.model.InvalidAddressingHeaderException; ohair@286: import com.sun.xml.internal.ws.addressing.model.MissingAddressingHeaderException; ohair@286: import com.sun.xml.internal.ws.api.SOAPVersion; ohair@286: import com.sun.xml.internal.ws.api.WSBinding; ohair@286: import com.sun.xml.internal.ws.api.addressing.AddressingVersion; alanb@368: import com.sun.xml.internal.ws.api.message.AddressingUtils; ohair@286: import com.sun.xml.internal.ws.api.message.Packet; ohair@286: import com.sun.xml.internal.ws.api.model.wsdl.WSDLBoundOperation; ohair@286: import com.sun.xml.internal.ws.api.model.wsdl.WSDLFault; ohair@286: import com.sun.xml.internal.ws.api.model.wsdl.WSDLOperation; ohair@286: import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput; ohair@286: import com.sun.xml.internal.ws.api.model.wsdl.WSDLPort; ohair@286: import com.sun.xml.internal.ws.api.model.SEIModel; ohair@286: import com.sun.xml.internal.ws.api.model.JavaMethod; alanb@368: import com.sun.xml.internal.ws.api.model.WSDLOperationMapping; ohair@286: import com.sun.xml.internal.ws.model.JavaMethodImpl; ohair@286: import com.sun.xml.internal.ws.model.CheckedExceptionImpl; ohair@286: import com.sun.istack.internal.Nullable; ohair@286: import org.w3c.dom.Element; ohair@286: ohair@286: import javax.xml.namespace.QName; ohair@286: import javax.xml.soap.Detail; ohair@286: import javax.xml.soap.SOAPConstants; ohair@286: import javax.xml.soap.SOAPException; ohair@286: import javax.xml.soap.SOAPFactory; ohair@286: import javax.xml.soap.SOAPFault; ohair@286: import javax.xml.soap.SOAPMessage; ohair@286: import javax.xml.ws.WebServiceException; ohair@286: ohair@286: /** ohair@286: * @author Rama Pulavarthi ohair@286: * @author Arun Gupta ohair@286: */ ohair@286: public abstract class WsaTubeHelper { ohair@286: ohair@286: public WsaTubeHelper(WSBinding binding, SEIModel seiModel, WSDLPort wsdlPort) { ohair@286: this.binding = binding; ohair@286: this.wsdlPort = wsdlPort; ohair@286: this.seiModel = seiModel; ohair@286: this.soapVer = binding.getSOAPVersion(); ohair@286: this.addVer = binding.getAddressingVersion(); ohair@286: ohair@286: } ohair@286: ohair@286: public String getFaultAction(Packet requestPacket, Packet responsePacket) { ohair@286: String action = null; ohair@286: if(seiModel != null) { ohair@286: action = getFaultActionFromSEIModel(requestPacket,responsePacket); ohair@286: } alanb@368: if (action != null) { ohair@286: return action; alanb@368: } else { ohair@286: action = addVer.getDefaultFaultAction(); alanb@368: } ohair@286: if (wsdlPort != null) { alanb@368: WSDLOperationMapping wsdlOp = requestPacket.getWSDLOperationMapping(); ohair@286: if (wsdlOp != null) { alanb@368: WSDLBoundOperation wbo = wsdlOp.getWSDLBoundOperation(); ohair@286: return getFaultAction(wbo, responsePacket); ohair@286: } ohair@286: } ohair@286: return action; ohair@286: } ohair@286: ohair@286: String getFaultActionFromSEIModel(Packet requestPacket, Packet responsePacket) { ohair@286: String action = null; alanb@368: if (seiModel == null || wsdlPort == null) { ohair@286: return action; alanb@368: } ohair@286: ohair@286: try { ohair@286: SOAPMessage sm = responsePacket.getMessage().copy().readAsSOAPMessage(); alanb@368: if (sm == null) { ohair@286: return action; alanb@368: } ohair@286: alanb@368: if (sm.getSOAPBody() == null) { ohair@286: return action; alanb@368: } ohair@286: alanb@368: if (sm.getSOAPBody().getFault() == null) { ohair@286: return action; alanb@368: } ohair@286: ohair@286: Detail detail = sm.getSOAPBody().getFault().getDetail(); alanb@368: if (detail == null) { ohair@286: return action; alanb@368: } ohair@286: ohair@286: String ns = detail.getFirstChild().getNamespaceURI(); ohair@286: String name = detail.getFirstChild().getLocalName(); ohair@286: alanb@368: WSDLOperationMapping wsdlOp = requestPacket.getWSDLOperationMapping(); alanb@368: JavaMethodImpl jm = (wsdlOp != null) ? (JavaMethodImpl)wsdlOp.getJavaMethod() : null; ohair@286: if (jm != null) { ohair@286: for (CheckedExceptionImpl ce : jm.getCheckedExceptions()) { ohair@286: if (ce.getDetailType().tagName.getLocalPart().equals(name) && ohair@286: ce.getDetailType().tagName.getNamespaceURI().equals(ns)) { ohair@286: return ce.getFaultAction(); ohair@286: } ohair@286: } ohair@286: } ohair@286: return action; ohair@286: } catch (SOAPException e) { ohair@286: throw new WebServiceException(e); ohair@286: } ohair@286: } ohair@286: ohair@286: String getFaultAction(@Nullable WSDLBoundOperation wbo, Packet responsePacket) { alanb@368: String action = AddressingUtils.getAction(responsePacket.getMessage().getHeaders(), addVer, soapVer); alanb@368: if (action != null) { alanb@368: return action; alanb@368: } ohair@286: ohair@286: action = addVer.getDefaultFaultAction(); alanb@368: if (wbo == null) { ohair@286: return action; alanb@368: } ohair@286: ohair@286: try { ohair@286: SOAPMessage sm = responsePacket.getMessage().copy().readAsSOAPMessage(); alanb@368: if (sm == null) { ohair@286: return action; alanb@368: } ohair@286: alanb@368: if (sm.getSOAPBody() == null) { ohair@286: return action; alanb@368: } ohair@286: alanb@368: if (sm.getSOAPBody().getFault() == null) { ohair@286: return action; alanb@368: } ohair@286: ohair@286: Detail detail = sm.getSOAPBody().getFault().getDetail(); alanb@368: if (detail == null) { ohair@286: return action; alanb@368: } ohair@286: ohair@286: String ns = detail.getFirstChild().getNamespaceURI(); ohair@286: String name = detail.getFirstChild().getLocalName(); ohair@286: ohair@286: WSDLOperation o = wbo.getOperation(); ohair@286: ohair@286: WSDLFault fault = o.getFault(new QName(ns, name)); alanb@368: if (fault == null) { ohair@286: return action; alanb@368: } ohair@286: ohair@286: action = fault.getAction(); ohair@286: ohair@286: return action; ohair@286: } catch (SOAPException e) { ohair@286: throw new WebServiceException(e); ohair@286: } ohair@286: } ohair@286: ohair@286: public String getInputAction(Packet packet) { ohair@286: String action = null; ohair@286: ohair@286: if (wsdlPort != null) { alanb@368: WSDLOperationMapping wsdlOp = packet.getWSDLOperationMapping(); ohair@286: if (wsdlOp != null) { alanb@368: WSDLBoundOperation wbo = wsdlOp.getWSDLBoundOperation(); ohair@286: WSDLOperation op = wbo.getOperation(); ohair@286: action = op.getInput().getAction(); ohair@286: } ohair@286: } ohair@286: ohair@286: return action; ohair@286: } ohair@286: ohair@286: /** ohair@286: * This method gives the Input addressing Action for a message. ohair@286: * It gives the Action set in the wsdl operation for the corresponding payload. ohair@286: * If it is not explicitly set, it gives the soapAction ohair@286: * @param packet ohair@286: * @return input Action ohair@286: */ ohair@286: public String getEffectiveInputAction(Packet packet) { ohair@286: //non-default SOAPAction beomes wsa:action alanb@368: if(packet.soapAction != null && !packet.soapAction.equals("")) { alanb@368: return packet.soapAction; alanb@368: } alanb@368: String action; ohair@286: ohair@286: if (wsdlPort != null) { alanb@368: WSDLOperationMapping wsdlOp = packet.getWSDLOperationMapping(); ohair@286: if (wsdlOp != null) { alanb@368: WSDLBoundOperation wbo = wsdlOp.getWSDLBoundOperation(); ohair@286: WSDLOperation op = wbo.getOperation(); ohair@286: action = op.getInput().getAction(); alanb@368: } else { ohair@286: action = packet.soapAction; alanb@368: } ohair@286: } else { ohair@286: action = packet.soapAction; ohair@286: } ohair@286: return action; ohair@286: } ohair@286: ohair@286: public boolean isInputActionDefault(Packet packet) { alanb@368: if (wsdlPort == null) { ohair@286: return false; alanb@368: } alanb@368: WSDLOperationMapping wsdlOp = packet.getWSDLOperationMapping(); alanb@368: if(wsdlOp == null) { ohair@286: return false; alanb@368: } alanb@368: WSDLBoundOperation wbo = wsdlOp.getWSDLBoundOperation(); ohair@286: WSDLOperation op = wbo.getOperation(); mkos@408: return op.getInput().isDefaultAction(); ohair@286: ohair@286: } ohair@286: ohair@286: public String getSOAPAction(Packet packet) { ohair@286: String action = ""; ohair@286: alanb@368: if (packet == null || packet.getMessage() == null) { ohair@286: return action; alanb@368: } ohair@286: alanb@368: if (wsdlPort == null) { ohair@286: return action; alanb@368: } ohair@286: alanb@368: WSDLOperationMapping wsdlOp = packet.getWSDLOperationMapping(); alanb@368: if (wsdlOp == null) { ohair@286: return action; alanb@368: } ohair@286: alanb@368: WSDLBoundOperation op = wsdlOp.getWSDLBoundOperation(); ohair@286: action = op.getSOAPAction(); ohair@286: return action; ohair@286: } ohair@286: ohair@286: public String getOutputAction(Packet packet) { ohair@286: //String action = AddressingVersion.UNSET_OUTPUT_ACTION; ohair@286: String action = null; alanb@368: WSDLOperationMapping wsdlOp = packet.getWSDLOperationMapping(); ohair@286: if (wsdlOp != null) { alanb@368: JavaMethod javaMethod = wsdlOp.getJavaMethod(); alanb@368: if (javaMethod != null) { alanb@368: JavaMethodImpl jm = (JavaMethodImpl) javaMethod; ohair@286: if (jm != null && jm.getOutputAction() != null && !jm.getOutputAction().equals("")) { ohair@286: return jm.getOutputAction(); ohair@286: } ohair@286: } alanb@368: WSDLBoundOperation wbo = wsdlOp.getWSDLBoundOperation(); alanb@368: if (wbo != null) return getOutputAction(wbo); ohair@286: } ohair@286: return action; ohair@286: } ohair@286: ohair@286: String getOutputAction(@Nullable WSDLBoundOperation wbo) { ohair@286: String action = AddressingVersion.UNSET_OUTPUT_ACTION; ohair@286: if (wbo != null) { ohair@286: WSDLOutput op = wbo.getOperation().getOutput(); alanb@368: if (op != null) { ohair@286: action = op.getAction(); alanb@368: } ohair@286: } ohair@286: return action; ohair@286: } ohair@286: ohair@286: public SOAPFault createInvalidAddressingHeaderFault(InvalidAddressingHeaderException e, AddressingVersion av) { ohair@286: QName name = e.getProblemHeader(); ohair@286: QName subsubcode = e.getSubsubcode(); ohair@286: QName subcode = av.invalidMapTag; ohair@286: String faultstring = String.format(av.getInvalidMapText(), name, subsubcode); ohair@286: ohair@286: try { ohair@286: SOAPFactory factory; ohair@286: SOAPFault fault; ohair@286: if (soapVer == SOAPVersion.SOAP_12) { ohair@286: factory = SOAPVersion.SOAP_12.getSOAPFactory(); ohair@286: fault = factory.createFault(); ohair@286: fault.setFaultCode(SOAPConstants.SOAP_SENDER_FAULT); ohair@286: fault.appendFaultSubcode(subcode); ohair@286: fault.appendFaultSubcode(subsubcode); ohair@286: getInvalidMapDetail(name, fault.addDetail()); ohair@286: } else { ohair@286: factory = SOAPVersion.SOAP_11.getSOAPFactory(); ohair@286: fault = factory.createFault(); ohair@286: fault.setFaultCode(subsubcode); ohair@286: } ohair@286: ohair@286: fault.setFaultString(faultstring); ohair@286: ohair@286: return fault; ohair@286: } catch (SOAPException se) { ohair@286: throw new WebServiceException(se); ohair@286: } ohair@286: } ohair@286: ohair@286: public SOAPFault newMapRequiredFault(MissingAddressingHeaderException e) { ohair@286: QName subcode = addVer.mapRequiredTag; ohair@286: QName subsubcode = addVer.mapRequiredTag; ohair@286: String faultstring = addVer.getMapRequiredText(); ohair@286: ohair@286: try { ohair@286: SOAPFactory factory; ohair@286: SOAPFault fault; ohair@286: if (soapVer == SOAPVersion.SOAP_12) { ohair@286: factory = SOAPVersion.SOAP_12.getSOAPFactory(); ohair@286: fault = factory.createFault(); ohair@286: fault.setFaultCode(SOAPConstants.SOAP_SENDER_FAULT); ohair@286: fault.appendFaultSubcode(subcode); ohair@286: fault.appendFaultSubcode(subsubcode); ohair@286: getMapRequiredDetail(e.getMissingHeaderQName(), fault.addDetail()); ohair@286: } else { ohair@286: factory = SOAPVersion.SOAP_11.getSOAPFactory(); ohair@286: fault = factory.createFault(); ohair@286: fault.setFaultCode(subsubcode); ohair@286: } ohair@286: ohair@286: fault.setFaultString(faultstring); ohair@286: ohair@286: return fault; ohair@286: } catch (SOAPException se) { ohair@286: throw new WebServiceException(se); ohair@286: } ohair@286: } ohair@286: ohair@286: public abstract void getProblemActionDetail(String action, Element element); ohair@286: public abstract void getInvalidMapDetail(QName name, Element element); ohair@286: public abstract void getMapRequiredDetail(QName name, Element element); ohair@286: ohair@286: protected SEIModel seiModel; ohair@286: protected WSDLPort wsdlPort; ohair@286: protected WSBinding binding; ohair@286: protected final SOAPVersion soapVer; ohair@286: protected final AddressingVersion addVer; ohair@286: }