src/share/jaxws_classes/com/sun/xml/internal/ws/addressing/WsaTubeHelper.java

changeset 286
f50545b5e2f1
child 368
0989ad8c0860
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/addressing/WsaTubeHelper.java	Tue Mar 06 16:09:35 2012 -0800
     1.3 @@ -0,0 +1,342 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.xml.internal.ws.addressing;
    1.30 +
    1.31 +import com.sun.xml.internal.ws.addressing.model.InvalidAddressingHeaderException;
    1.32 +import com.sun.xml.internal.ws.addressing.model.MissingAddressingHeaderException;
    1.33 +import com.sun.xml.internal.ws.api.SOAPVersion;
    1.34 +import com.sun.xml.internal.ws.api.WSBinding;
    1.35 +import com.sun.xml.internal.ws.api.addressing.AddressingVersion;
    1.36 +import com.sun.xml.internal.ws.api.message.Message;
    1.37 +import com.sun.xml.internal.ws.api.message.Packet;
    1.38 +import com.sun.xml.internal.ws.api.model.wsdl.WSDLBoundOperation;
    1.39 +import com.sun.xml.internal.ws.api.model.wsdl.WSDLFault;
    1.40 +import com.sun.xml.internal.ws.api.model.wsdl.WSDLOperation;
    1.41 +import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
    1.42 +import com.sun.xml.internal.ws.api.model.wsdl.WSDLPort;
    1.43 +import com.sun.xml.internal.ws.api.model.SEIModel;
    1.44 +import com.sun.xml.internal.ws.api.model.JavaMethod;
    1.45 +import com.sun.xml.internal.ws.model.wsdl.WSDLOperationImpl;
    1.46 +import com.sun.xml.internal.ws.model.JavaMethodImpl;
    1.47 +import com.sun.xml.internal.ws.model.CheckedExceptionImpl;
    1.48 +import com.sun.istack.internal.Nullable;
    1.49 +import org.w3c.dom.Element;
    1.50 +
    1.51 +import javax.xml.namespace.QName;
    1.52 +import javax.xml.soap.Detail;
    1.53 +import javax.xml.soap.SOAPConstants;
    1.54 +import javax.xml.soap.SOAPException;
    1.55 +import javax.xml.soap.SOAPFactory;
    1.56 +import javax.xml.soap.SOAPFault;
    1.57 +import javax.xml.soap.SOAPMessage;
    1.58 +import javax.xml.ws.WebServiceException;
    1.59 +import java.util.Map;
    1.60 +
    1.61 +/**
    1.62 + * @author Rama Pulavarthi
    1.63 + * @author Arun Gupta
    1.64 + */
    1.65 +public abstract class WsaTubeHelper {
    1.66 +
    1.67 +    public WsaTubeHelper(WSBinding binding, SEIModel seiModel, WSDLPort wsdlPort) {
    1.68 +        this.binding = binding;
    1.69 +        this.wsdlPort = wsdlPort;
    1.70 +        this.seiModel = seiModel;
    1.71 +        this.soapVer = binding.getSOAPVersion();
    1.72 +        this.addVer = binding.getAddressingVersion();
    1.73 +
    1.74 +    }
    1.75 +
    1.76 +    public String getFaultAction(Packet requestPacket, Packet responsePacket) {
    1.77 +        String action = null;
    1.78 +        if(seiModel != null) {
    1.79 +            action = getFaultActionFromSEIModel(requestPacket,responsePacket);
    1.80 +        }
    1.81 +        if(action != null)
    1.82 +            return action;
    1.83 +        else
    1.84 +            action = addVer.getDefaultFaultAction();
    1.85 +        if (wsdlPort != null) {
    1.86 +            QName wsdlOp = requestPacket.getWSDLOperation();
    1.87 +            if (wsdlOp != null) {
    1.88 +                WSDLBoundOperation wbo = wsdlPort.getBinding().get(wsdlOp);
    1.89 +                return getFaultAction(wbo, responsePacket);
    1.90 +            }
    1.91 +        }
    1.92 +        return action;
    1.93 +    }
    1.94 +
    1.95 +    String getFaultActionFromSEIModel(Packet requestPacket, Packet responsePacket) {
    1.96 +        String action = null;
    1.97 +        if (seiModel == null || wsdlPort == null)
    1.98 +            return action;
    1.99 +
   1.100 +        try {
   1.101 +            SOAPMessage sm = responsePacket.getMessage().copy().readAsSOAPMessage();
   1.102 +            if (sm == null)
   1.103 +                return action;
   1.104 +
   1.105 +            if (sm.getSOAPBody() == null)
   1.106 +                return action;
   1.107 +
   1.108 +            if (sm.getSOAPBody().getFault() == null)
   1.109 +                return action;
   1.110 +
   1.111 +            Detail detail = sm.getSOAPBody().getFault().getDetail();
   1.112 +            if (detail == null)
   1.113 +                return action;
   1.114 +
   1.115 +            String ns = detail.getFirstChild().getNamespaceURI();
   1.116 +            String name = detail.getFirstChild().getLocalName();
   1.117 +
   1.118 +            QName wsdlOp = requestPacket.getWSDLOperation();
   1.119 +            JavaMethodImpl jm = (JavaMethodImpl) seiModel.getJavaMethodForWsdlOperation(wsdlOp);
   1.120 +            if (jm != null) {
   1.121 +              for (CheckedExceptionImpl ce : jm.getCheckedExceptions()) {
   1.122 +                  if (ce.getDetailType().tagName.getLocalPart().equals(name) &&
   1.123 +                          ce.getDetailType().tagName.getNamespaceURI().equals(ns)) {
   1.124 +                      return ce.getFaultAction();
   1.125 +                  }
   1.126 +              }
   1.127 +            }
   1.128 +            return action;
   1.129 +        } catch (SOAPException e) {
   1.130 +            throw new WebServiceException(e);
   1.131 +        }
   1.132 +    }
   1.133 +
   1.134 +    String getFaultAction(@Nullable WSDLBoundOperation wbo, Packet responsePacket) {
   1.135 +        String action = responsePacket.getMessage().getHeaders().getAction(addVer, soapVer);
   1.136 +        if (action != null)
   1.137 +                return action;
   1.138 +
   1.139 +        action = addVer.getDefaultFaultAction();
   1.140 +        if (wbo == null)
   1.141 +            return action;
   1.142 +
   1.143 +        try {
   1.144 +            SOAPMessage sm = responsePacket.getMessage().copy().readAsSOAPMessage();
   1.145 +            if (sm == null)
   1.146 +                return action;
   1.147 +
   1.148 +            if (sm.getSOAPBody() == null)
   1.149 +                return action;
   1.150 +
   1.151 +            if (sm.getSOAPBody().getFault() == null)
   1.152 +                return action;
   1.153 +
   1.154 +            Detail detail = sm.getSOAPBody().getFault().getDetail();
   1.155 +            if (detail == null)
   1.156 +                return action;
   1.157 +
   1.158 +            String ns = detail.getFirstChild().getNamespaceURI();
   1.159 +            String name = detail.getFirstChild().getLocalName();
   1.160 +
   1.161 +            WSDLOperation o = wbo.getOperation();
   1.162 +
   1.163 +            WSDLFault fault = o.getFault(new QName(ns, name));
   1.164 +            if (fault == null)
   1.165 +                return action;
   1.166 +
   1.167 +            WSDLOperationImpl impl = (WSDLOperationImpl)o;
   1.168 +            action = fault.getAction();
   1.169 +
   1.170 +            return action;
   1.171 +        } catch (SOAPException e) {
   1.172 +            throw new WebServiceException(e);
   1.173 +        }
   1.174 +    }
   1.175 +
   1.176 +    public String getInputAction(Packet packet) {
   1.177 +        String action = null;
   1.178 +
   1.179 +        if (wsdlPort != null) {
   1.180 +            QName wsdlOp = packet.getWSDLOperation();
   1.181 +            if (wsdlOp != null) {
   1.182 +                WSDLBoundOperation wbo = wsdlPort.getBinding().get(wsdlOp);
   1.183 +                WSDLOperation op = wbo.getOperation();
   1.184 +                action = op.getInput().getAction();
   1.185 +            }
   1.186 +        }
   1.187 +
   1.188 +        return action;
   1.189 +    }
   1.190 +
   1.191 +    /**
   1.192 +     * This method gives the Input addressing Action for a message.
   1.193 +     * It gives the Action set in the wsdl operation for the corresponding payload.
   1.194 +     * If it is not explicitly set, it gives the soapAction
   1.195 +     * @param packet
   1.196 +     * @return input Action
   1.197 +     */
   1.198 +    public String getEffectiveInputAction(Packet packet) {
   1.199 +        //non-default SOAPAction beomes wsa:action
   1.200 +        if(packet.soapAction != null && !packet.soapAction.equals(""))
   1.201 +                return packet.soapAction;
   1.202 +        String action = null;
   1.203 +
   1.204 +        if (wsdlPort != null) {
   1.205 +            QName wsdlOp = packet.getWSDLOperation();
   1.206 +            if (wsdlOp != null) {
   1.207 +                WSDLBoundOperation wbo = wsdlPort.getBinding().get(wsdlOp);
   1.208 +                WSDLOperation op = wbo.getOperation();
   1.209 +                action = op.getInput().getAction();
   1.210 +            } else
   1.211 +                action = packet.soapAction;
   1.212 +        } else {
   1.213 +            action = packet.soapAction;
   1.214 +        }
   1.215 +        return action;
   1.216 +    }
   1.217 +
   1.218 +    public boolean isInputActionDefault(Packet packet) {
   1.219 +        if (wsdlPort == null)
   1.220 +            return false;
   1.221 +        QName wsdlOp = packet.getWSDLOperation();
   1.222 +        if(wsdlOp == null)
   1.223 +            return false;
   1.224 +        WSDLBoundOperation wbo = wsdlPort.getBinding().get(wsdlOp);
   1.225 +        WSDLOperation op = wbo.getOperation();
   1.226 +        return ((WSDLOperationImpl) op).getInput().isDefaultAction();
   1.227 +
   1.228 +    }
   1.229 +
   1.230 +    public String getSOAPAction(Packet packet) {
   1.231 +        String action = "";
   1.232 +
   1.233 +        if (packet == null || packet.getMessage() == null)
   1.234 +            return action;
   1.235 +
   1.236 +        if (wsdlPort == null)
   1.237 +            return action;
   1.238 +
   1.239 +        QName opName = packet.getWSDLOperation();
   1.240 +        if(opName == null)
   1.241 +            return action;
   1.242 +
   1.243 +        WSDLBoundOperation op = wsdlPort.getBinding().get(opName);
   1.244 +        action = op.getSOAPAction();
   1.245 +        return action;
   1.246 +    }
   1.247 +
   1.248 +    public String getOutputAction(Packet packet) {
   1.249 +        //String action = AddressingVersion.UNSET_OUTPUT_ACTION;
   1.250 +        String action = null;
   1.251 +        QName wsdlOp = packet.getWSDLOperation();
   1.252 +        if (wsdlOp != null) {
   1.253 +            if (seiModel != null) {
   1.254 +                JavaMethodImpl jm = (JavaMethodImpl) seiModel.getJavaMethodForWsdlOperation(wsdlOp);
   1.255 +                if (jm != null && jm.getOutputAction() != null && !jm.getOutputAction().equals("")) {
   1.256 +                    return jm.getOutputAction();
   1.257 +                }
   1.258 +            }
   1.259 +            if (wsdlPort != null) {
   1.260 +                WSDLBoundOperation wbo = wsdlPort.getBinding().get(wsdlOp);
   1.261 +                return getOutputAction(wbo);
   1.262 +            }
   1.263 +        }
   1.264 +        return action;
   1.265 +    }
   1.266 +
   1.267 +    String getOutputAction(@Nullable WSDLBoundOperation wbo) {
   1.268 +        String action = AddressingVersion.UNSET_OUTPUT_ACTION;
   1.269 +        if (wbo != null) {
   1.270 +            WSDLOutput op = wbo.getOperation().getOutput();
   1.271 +            if (op != null)
   1.272 +                action = op.getAction();
   1.273 +        }
   1.274 +        return action;
   1.275 +    }
   1.276 +
   1.277 +    public SOAPFault createInvalidAddressingHeaderFault(InvalidAddressingHeaderException e, AddressingVersion av) {
   1.278 +        QName name = e.getProblemHeader();
   1.279 +        QName subsubcode = e.getSubsubcode();
   1.280 +        QName subcode = av.invalidMapTag;
   1.281 +        String faultstring = String.format(av.getInvalidMapText(), name, subsubcode);
   1.282 +
   1.283 +        try {
   1.284 +            SOAPFactory factory;
   1.285 +            SOAPFault fault;
   1.286 +            if (soapVer == SOAPVersion.SOAP_12) {
   1.287 +                factory = SOAPVersion.SOAP_12.getSOAPFactory();
   1.288 +                fault = factory.createFault();
   1.289 +                fault.setFaultCode(SOAPConstants.SOAP_SENDER_FAULT);
   1.290 +                fault.appendFaultSubcode(subcode);
   1.291 +                fault.appendFaultSubcode(subsubcode);
   1.292 +                getInvalidMapDetail(name, fault.addDetail());
   1.293 +            } else {
   1.294 +                factory = SOAPVersion.SOAP_11.getSOAPFactory();
   1.295 +                fault = factory.createFault();
   1.296 +                fault.setFaultCode(subsubcode);
   1.297 +            }
   1.298 +
   1.299 +            fault.setFaultString(faultstring);
   1.300 +
   1.301 +            return fault;
   1.302 +        } catch (SOAPException se) {
   1.303 +            throw new WebServiceException(se);
   1.304 +        }
   1.305 +    }
   1.306 +
   1.307 +    public SOAPFault newMapRequiredFault(MissingAddressingHeaderException e) {
   1.308 +        QName subcode = addVer.mapRequiredTag;
   1.309 +        QName subsubcode = addVer.mapRequiredTag;
   1.310 +        String faultstring = addVer.getMapRequiredText();
   1.311 +
   1.312 +        try {
   1.313 +            SOAPFactory factory;
   1.314 +            SOAPFault fault;
   1.315 +            if (soapVer == SOAPVersion.SOAP_12) {
   1.316 +                factory = SOAPVersion.SOAP_12.getSOAPFactory();
   1.317 +                fault = factory.createFault();
   1.318 +                fault.setFaultCode(SOAPConstants.SOAP_SENDER_FAULT);
   1.319 +                fault.appendFaultSubcode(subcode);
   1.320 +                fault.appendFaultSubcode(subsubcode);
   1.321 +                getMapRequiredDetail(e.getMissingHeaderQName(), fault.addDetail());
   1.322 +            } else {
   1.323 +                factory = SOAPVersion.SOAP_11.getSOAPFactory();
   1.324 +                fault = factory.createFault();
   1.325 +                fault.setFaultCode(subsubcode);
   1.326 +            }
   1.327 +
   1.328 +            fault.setFaultString(faultstring);
   1.329 +
   1.330 +            return fault;
   1.331 +        } catch (SOAPException se) {
   1.332 +            throw new WebServiceException(se);
   1.333 +        }
   1.334 +    }
   1.335 +
   1.336 +    public abstract void getProblemActionDetail(String action, Element element);
   1.337 +    public abstract void getInvalidMapDetail(QName name, Element element);
   1.338 +    public abstract void getMapRequiredDetail(QName name, Element element);
   1.339 +
   1.340 +    protected SEIModel seiModel;
   1.341 +    protected WSDLPort wsdlPort;
   1.342 +    protected WSBinding binding;
   1.343 +    protected final SOAPVersion soapVer;
   1.344 +    protected final AddressingVersion addVer;
   1.345 +}

mercurial