src/share/jaxws_classes/com/sun/xml/internal/ws/wsdl/parser/W3CAddressingWSDLParserExtension.java

changeset 0
373ffda63c9a
child 637
9c07ef4934dd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/wsdl/parser/W3CAddressingWSDLParserExtension.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,261 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2013, 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.wsdl.parser;
    1.30 +
    1.31 +import com.sun.xml.internal.ws.api.addressing.AddressingVersion;
    1.32 +import com.sun.xml.internal.ws.api.model.wsdl.WSDLFeaturedObject;
    1.33 +import static com.sun.xml.internal.ws.api.model.wsdl.WSDLBoundOperation.ANONYMOUS;
    1.34 +import com.sun.xml.internal.ws.api.model.wsdl.editable.*;
    1.35 +import com.sun.xml.internal.ws.api.wsdl.parser.WSDLParserExtension;
    1.36 +import com.sun.xml.internal.ws.api.wsdl.parser.WSDLParserExtensionContext;
    1.37 +import com.sun.xml.internal.ws.streaming.XMLStreamReaderUtil;
    1.38 +
    1.39 +import javax.xml.namespace.QName;
    1.40 +import javax.xml.stream.XMLStreamException;
    1.41 +import javax.xml.stream.XMLStreamReader;
    1.42 +import javax.xml.ws.WebServiceException;
    1.43 +import javax.xml.ws.soap.AddressingFeature;
    1.44 +
    1.45 +/**
    1.46 + * W3C WS-Addressing Runtime WSDL parser extension
    1.47 + *
    1.48 + * @author Arun Gupta
    1.49 + */
    1.50 +public class W3CAddressingWSDLParserExtension extends WSDLParserExtension {
    1.51 +    @Override
    1.52 +    public boolean bindingElements(EditableWSDLBoundPortType binding, XMLStreamReader reader) {
    1.53 +        return addressibleElement(reader, binding);
    1.54 +    }
    1.55 +
    1.56 +    @Override
    1.57 +    public boolean portElements(EditableWSDLPort port, XMLStreamReader reader) {
    1.58 +        return addressibleElement(reader, port);
    1.59 +    }
    1.60 +
    1.61 +    private boolean addressibleElement(XMLStreamReader reader, WSDLFeaturedObject binding) {
    1.62 +        QName ua = reader.getName();
    1.63 +        if (ua.equals(AddressingVersion.W3C.wsdlExtensionTag)) {
    1.64 +            String required = reader.getAttributeValue(WSDLConstants.NS_WSDL, "required");
    1.65 +            binding.addFeature(new AddressingFeature(true, Boolean.parseBoolean(required)));
    1.66 +            XMLStreamReaderUtil.skipElement(reader);
    1.67 +            return true;        // UsingAddressing is consumed
    1.68 +        }
    1.69 +
    1.70 +        return false;
    1.71 +    }
    1.72 +
    1.73 +    @Override
    1.74 +    public boolean bindingOperationElements(EditableWSDLBoundOperation operation, XMLStreamReader reader) {
    1.75 +        EditableWSDLBoundOperation edit = (EditableWSDLBoundOperation) operation;
    1.76 +
    1.77 +        QName anon = reader.getName();
    1.78 +        if (anon.equals(AddressingVersion.W3C.wsdlAnonymousTag)) {
    1.79 +            try {
    1.80 +                String value = reader.getElementText();
    1.81 +                if (value == null || value.trim().equals("")) {
    1.82 +                    throw new WebServiceException("Null values not permitted in wsaw:Anonymous.");
    1.83 +                    // TODO: throw exception only if wsdl:required=true
    1.84 +                    // TODO: is this the right exception ?
    1.85 +                } else if (value.equals("optional")) {
    1.86 +                    edit.setAnonymous(ANONYMOUS.optional);
    1.87 +                } else if (value.equals("required")) {
    1.88 +                    edit.setAnonymous(ANONYMOUS.required);
    1.89 +                } else if (value.equals("prohibited")) {
    1.90 +                    edit.setAnonymous(ANONYMOUS.prohibited);
    1.91 +                } else {
    1.92 +                    throw new WebServiceException("wsaw:Anonymous value \"" + value + "\" not understood.");
    1.93 +                    // TODO: throw exception only if wsdl:required=true
    1.94 +                    // TODO: is this the right exception ?
    1.95 +                }
    1.96 +            } catch (XMLStreamException e) {
    1.97 +                throw new WebServiceException(e);       // TODO: is this the correct behavior ?
    1.98 +            }
    1.99 +
   1.100 +            return true;        // consumed the element
   1.101 +        }
   1.102 +
   1.103 +        return false;
   1.104 +    }
   1.105 +
   1.106 +    public void portTypeOperationInputAttributes(EditableWSDLInput input, XMLStreamReader reader) {
   1.107 +       String action = ParserUtil.getAttribute(reader, getWsdlActionTag());
   1.108 +       if (action != null) {
   1.109 +            input.setAction(action);
   1.110 +            input.setDefaultAction(false);
   1.111 +        }
   1.112 +    }
   1.113 +
   1.114 +
   1.115 +    public void portTypeOperationOutputAttributes(EditableWSDLOutput output, XMLStreamReader reader) {
   1.116 +       String action = ParserUtil.getAttribute(reader, getWsdlActionTag());
   1.117 +       if (action != null) {
   1.118 +            output.setAction(action);
   1.119 +            output.setDefaultAction(false);
   1.120 +        }
   1.121 +    }
   1.122 +
   1.123 +
   1.124 +    public void portTypeOperationFaultAttributes(EditableWSDLFault fault, XMLStreamReader reader) {
   1.125 +        String action = ParserUtil.getAttribute(reader, getWsdlActionTag());
   1.126 +        if (action != null) {
   1.127 +            fault.setAction(action);
   1.128 +            fault.setDefaultAction(false);
   1.129 +        }
   1.130 +    }
   1.131 +
   1.132 +    /**
   1.133 +     * Process wsdl:portType operation after the entire WSDL model has been populated.
   1.134 +     * The task list includes: <p>
   1.135 +     * <ul>
   1.136 +     * <li>Patch the value of UsingAddressing in wsdl:port and wsdl:binding</li>
   1.137 +     * <li>Populate actions for the messages that do not have an explicit wsaw:Action</li>
   1.138 +     * <li>Patch the default value of wsaw:Anonymous=optional if none is specified</li>
   1.139 +     * </ul>
   1.140 +     * @param context
   1.141 +     */
   1.142 +    @Override
   1.143 +    public void finished(WSDLParserExtensionContext context) {
   1.144 +        EditableWSDLModel model = context.getWSDLModel();
   1.145 +        for (EditableWSDLService service : model.getServices().values()) {
   1.146 +            for (EditableWSDLPort port : service.getPorts()) {
   1.147 +                EditableWSDLBoundPortType binding = port.getBinding();
   1.148 +
   1.149 +                // populate actions for the messages that do not have an explicit wsaw:Action
   1.150 +                populateActions(binding);
   1.151 +
   1.152 +                // patch the default value of wsaw:Anonymous=optional if none is specified
   1.153 +                patchAnonymousDefault(binding);
   1.154 +            }
   1.155 +        }
   1.156 +    }
   1.157 +
   1.158 +    protected String getNamespaceURI() {
   1.159 +        return AddressingVersion.W3C.wsdlNsUri;
   1.160 +    }
   1.161 +
   1.162 +    protected QName getWsdlActionTag() {
   1.163 +       return AddressingVersion.W3C.wsdlActionTag;
   1.164 +    }
   1.165 +    /**
   1.166 +     * Populate all the Actions
   1.167 +     *
   1.168 +     * @param binding soapbinding:operation
   1.169 +     */
   1.170 +    private void populateActions(EditableWSDLBoundPortType binding) {
   1.171 +        EditableWSDLPortType porttype = binding.getPortType();
   1.172 +        for (EditableWSDLOperation o : porttype.getOperations()) {
   1.173 +            // TODO: this may be performance intensive. Alternatively default action
   1.174 +            // TODO: can be calculated when the operation is actually invoked.
   1.175 +                EditableWSDLBoundOperation wboi = binding.get(o.getName());
   1.176 +
   1.177 +            if (wboi == null) {
   1.178 +                //If this operation is unbound set the action to default
   1.179 +                o.getInput().setAction(defaultInputAction(o));
   1.180 +                continue;
   1.181 +            }
   1.182 +                String soapAction = wboi.getSOAPAction();
   1.183 +            if (o.getInput().getAction() == null || o.getInput().getAction().equals("")) {
   1.184 +                // explicit wsaw:Action is not specified
   1.185 +
   1.186 +                if (soapAction != null && !soapAction.equals("")) {
   1.187 +                    // if soapAction is non-empty, use that
   1.188 +                    o.getInput().setAction(soapAction);
   1.189 +                } else {
   1.190 +                    // otherwise generate default Action
   1.191 +                    o.getInput().setAction(defaultInputAction(o));
   1.192 +                }
   1.193 +            }
   1.194 +
   1.195 +            // skip output and fault processing for one-way methods
   1.196 +            if (o.getOutput() == null)
   1.197 +                continue;
   1.198 +
   1.199 +            if (o.getOutput().getAction() == null || o.getOutput().getAction().equals("")) {
   1.200 +                o.getOutput().setAction(defaultOutputAction(o));
   1.201 +            }
   1.202 +
   1.203 +            if (o.getFaults() == null || !o.getFaults().iterator().hasNext())
   1.204 +                continue;
   1.205 +
   1.206 +            for (EditableWSDLFault f : o.getFaults()) {
   1.207 +                if (f.getAction() == null || f.getAction().equals("")) {
   1.208 +                    f.setAction(defaultFaultAction(f.getName(), o));
   1.209 +                }
   1.210 +
   1.211 +            }
   1.212 +        }
   1.213 +    }
   1.214 +
   1.215 +    /**
   1.216 +     * Patch the default value of wsaw:Anonymous=optional if none is specified
   1.217 +     *
   1.218 +     * @param binding WSDLBoundPortTypeImpl
   1.219 +     */
   1.220 +    protected void patchAnonymousDefault(EditableWSDLBoundPortType binding) {
   1.221 +        for (EditableWSDLBoundOperation wbo : binding.getBindingOperations()) {
   1.222 +            if (wbo.getAnonymous() == null)
   1.223 +                wbo.setAnonymous(ANONYMOUS.optional);
   1.224 +        }
   1.225 +    }
   1.226 +
   1.227 +    private String defaultInputAction(EditableWSDLOperation o) {
   1.228 +        return buildAction(o.getInput().getName(), o, false);
   1.229 +    }
   1.230 +
   1.231 +    private String defaultOutputAction(EditableWSDLOperation o) {
   1.232 +        return buildAction(o.getOutput().getName(), o, false);
   1.233 +    }
   1.234 +
   1.235 +    private String defaultFaultAction(String name, EditableWSDLOperation o) {
   1.236 +        return buildAction(name, o, true);
   1.237 +    }
   1.238 +
   1.239 +    protected static final String buildAction(String name, EditableWSDLOperation o, boolean isFault) {
   1.240 +        String tns = o.getName().getNamespaceURI();
   1.241 +
   1.242 +        String delim = SLASH_DELIMITER;
   1.243 +
   1.244 +        // TODO: is this the correct way to find the separator ?
   1.245 +        if (!tns.startsWith("http"))
   1.246 +            delim = COLON_DELIMITER;
   1.247 +
   1.248 +        if (tns.endsWith(delim))
   1.249 +            tns = tns.substring(0, tns.length()-1);
   1.250 +
   1.251 +        if (o.getPortTypeName() == null)
   1.252 +            throw new WebServiceException("\"" + o.getName() + "\" operation's owning portType name is null.");
   1.253 +
   1.254 +        return tns +
   1.255 +            delim +
   1.256 +            o.getPortTypeName().getLocalPart() +
   1.257 +            delim +
   1.258 +            (isFault ? o.getName().getLocalPart() + delim + "Fault" + delim : "") +
   1.259 +            name;
   1.260 +    }
   1.261 +
   1.262 +    protected static final String COLON_DELIMITER = ":";
   1.263 +    protected static final String SLASH_DELIMITER = "/";
   1.264 +}

mercurial