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

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 368
0989ad8c0860
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

     1 /*
     2  * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    26 package com.sun.xml.internal.ws.wsdl.parser;
    28 import com.sun.xml.internal.ws.api.addressing.AddressingVersion;
    29 import com.sun.xml.internal.ws.api.model.wsdl.*;
    30 import com.sun.xml.internal.ws.api.wsdl.parser.WSDLParserExtension;
    31 import com.sun.xml.internal.ws.api.wsdl.parser.WSDLParserExtensionContext;
    32 import com.sun.xml.internal.ws.model.wsdl.*;
    33 import com.sun.xml.internal.ws.streaming.XMLStreamReaderUtil;
    34 import com.sun.xml.internal.ws.resources.AddressingMessages;
    36 import javax.xml.namespace.QName;
    37 import javax.xml.stream.XMLStreamException;
    38 import javax.xml.stream.XMLStreamReader;
    39 import javax.xml.ws.WebServiceException;
    40 import javax.xml.ws.soap.AddressingFeature;
    41 import java.util.Map;
    43 /**
    44  * W3C WS-Addressing Runtime WSDL parser extension
    45  *
    46  * @author Arun Gupta
    47  */
    48 public class W3CAddressingWSDLParserExtension extends WSDLParserExtension {
    49     @Override
    50     public boolean bindingElements(WSDLBoundPortType binding, XMLStreamReader reader) {
    51         return addressibleElement(reader, binding);
    52     }
    54     @Override
    55     public boolean portElements(WSDLPort port, XMLStreamReader reader) {
    56         return addressibleElement(reader, port);
    57     }
    59     private boolean addressibleElement(XMLStreamReader reader, WSDLFeaturedObject binding) {
    60         QName ua = reader.getName();
    61         if (ua.equals(AddressingVersion.W3C.wsdlExtensionTag)) {
    62             String required = reader.getAttributeValue(WSDLConstants.NS_WSDL, "required");
    63             binding.addFeature(new AddressingFeature(true, Boolean.parseBoolean(required)));
    64             XMLStreamReaderUtil.skipElement(reader);
    65             return true;        // UsingAddressing is consumed
    66         }
    68         return false;
    69     }
    71     @Override
    72     public boolean bindingOperationElements(WSDLBoundOperation operation, XMLStreamReader reader) {
    73         WSDLBoundOperationImpl impl = (WSDLBoundOperationImpl)operation;
    75         QName anon = reader.getName();
    76         if (anon.equals(AddressingVersion.W3C.wsdlAnonymousTag)) {
    77             try {
    78                 String value = reader.getElementText();
    79                 if (value == null || value.trim().equals("")) {
    80                     throw new WebServiceException("Null values not permitted in wsaw:Anonymous.");
    81                     // TODO: throw exception only if wsdl:required=true
    82                     // TODO: is this the right exception ?
    83                 } else if (value.equals("optional")) {
    84                     impl.setAnonymous(WSDLBoundOperation.ANONYMOUS.optional);
    85                 } else if (value.equals("required")) {
    86                     impl.setAnonymous(WSDLBoundOperation.ANONYMOUS.required);
    87                 } else if (value.equals("prohibited")) {
    88                     impl.setAnonymous(WSDLBoundOperation.ANONYMOUS.prohibited);
    89                 } else {
    90                     throw new WebServiceException("wsaw:Anonymous value \"" + value + "\" not understood.");
    91                     // TODO: throw exception only if wsdl:required=true
    92                     // TODO: is this the right exception ?
    93                 }
    94             } catch (XMLStreamException e) {
    95                 throw new WebServiceException(e);       // TODO: is this the correct behavior ?
    96             }
    98             return true;        // consumed the element
    99         }
   101         return false;
   102     }
   104     public void portTypeOperationInputAttributes(WSDLInput input, XMLStreamReader reader) {
   105        String action = ParserUtil.getAttribute(reader, getWsdlActionTag());
   106        if (action != null) {
   107             ((WSDLInputImpl)input).setAction(action);
   108             ((WSDLInputImpl)input).setDefaultAction(false);
   109         }
   110     }
   113     public void portTypeOperationOutputAttributes(WSDLOutput output, XMLStreamReader reader) {
   114        String action = ParserUtil.getAttribute(reader, getWsdlActionTag());
   115        if (action != null) {
   116             ((WSDLOutputImpl)output).setAction(action);
   117             ((WSDLOutputImpl)output).setDefaultAction(false);
   118         }
   119     }
   122     public void portTypeOperationFaultAttributes(WSDLFault fault, XMLStreamReader reader) {
   123         String action = ParserUtil.getAttribute(reader, getWsdlActionTag());
   124         if (action != null) {
   125             ((WSDLFaultImpl) fault).setAction(action);
   126             ((WSDLFaultImpl) fault).setDefaultAction(false);
   127         }
   128     }
   130     /**
   131      * Process wsdl:portType operation after the entire WSDL model has been populated.
   132      * The task list includes: <p>
   133      * <ul>
   134      * <li>Patch the value of UsingAddressing in wsdl:port and wsdl:binding</li>
   135      * <li>Populate actions for the messages that do not have an explicit wsaw:Action</li>
   136      * <li>Patch the default value of wsaw:Anonymous=optional if none is specified</li>
   137      * </ul>
   138      * @param context
   139      */
   140     @Override
   141     public void finished(WSDLParserExtensionContext context) {
   142         WSDLModel model = context.getWSDLModel();
   143         for (WSDLService service : model.getServices().values()) {
   144             for (WSDLPort wp : service.getPorts()) {
   145                 WSDLPortImpl port = (WSDLPortImpl)wp;
   146                 WSDLBoundPortTypeImpl binding = port.getBinding();
   148                 // populate actions for the messages that do not have an explicit wsaw:Action
   149                 populateActions(binding);
   151                 // patch the default value of wsaw:Anonymous=optional if none is specified
   152                 patchAnonymousDefault(binding);
   153             }
   154         }
   155     }
   157     protected String getNamespaceURI() {
   158         return AddressingVersion.W3C.wsdlNsUri;
   159     }
   161     protected QName getWsdlActionTag() {
   162        return AddressingVersion.W3C.wsdlActionTag;
   163     }
   164     /**
   165      * Populate all the Actions
   166      *
   167      * @param binding soapbinding:operation
   168      */
   169     private void populateActions(WSDLBoundPortTypeImpl binding) {
   170         WSDLPortTypeImpl porttype = binding.getPortType();
   171         for (WSDLOperationImpl o : porttype.getOperations()) {
   172             // TODO: this may be performance intensive. Alternatively default action
   173             // TODO: can be calculated when the operation is actually invoked.
   174             WSDLBoundOperationImpl wboi = binding.get(o.getName());
   176             if (wboi == null) {
   177                 //If this operation is unbound set the action to default
   178                 o.getInput().setAction(defaultInputAction(o));
   179                 continue;
   180             }
   181                 String soapAction = wboi.getSOAPAction();
   182             if (o.getInput().getAction() == null || o.getInput().getAction().equals("")) {
   183                 // explicit wsaw:Action is not specified
   185                 if (soapAction != null && !soapAction.equals("")) {
   186                     // if soapAction is non-empty, use that
   187                     o.getInput().setAction(soapAction);
   188                 } else {
   189                     // otherwise generate default Action
   190                     o.getInput().setAction(defaultInputAction(o));
   191                 }
   192             }
   194             // skip output and fault processing for one-way methods
   195             if (o.getOutput() == null)
   196                 continue;
   198             if (o.getOutput().getAction() == null || o.getOutput().getAction().equals("")) {
   199                 o.getOutput().setAction(defaultOutputAction(o));
   200             }
   202             if (o.getFaults() == null || !o.getFaults().iterator().hasNext())
   203                 continue;
   205             for (WSDLFault f : o.getFaults()) {
   206                 if (f.getAction() == null || f.getAction().equals("")) {
   207                     ((WSDLFaultImpl)f).setAction(defaultFaultAction(f.getName(), o));
   208                 }
   210             }
   211         }
   212     }
   214     /**
   215      * Patch the default value of wsaw:Anonymous=optional if none is specified
   216      *
   217      * @param binding WSDLBoundPortTypeImpl
   218      */
   219     protected void patchAnonymousDefault(WSDLBoundPortTypeImpl binding) {
   220         for (WSDLBoundOperationImpl wbo : binding.getBindingOperations()) {
   221             if (wbo.getAnonymous() == null)
   222                 wbo.setAnonymous(WSDLBoundOperation.ANONYMOUS.optional);
   223         }
   224     }
   226     private String defaultInputAction(WSDLOperation o) {
   227         return buildAction(o.getInput().getName(), o, false);
   228     }
   230     private String defaultOutputAction(WSDLOperation o) {
   231         return buildAction(o.getOutput().getName(), o, false);
   232     }
   234     private String defaultFaultAction(String name, WSDLOperation o) {
   235         return buildAction(name, o, true);
   236     }
   238     protected static final String buildAction(String name, WSDLOperation o, boolean isFault) {
   239         String tns = o.getName().getNamespaceURI();
   241         String delim = SLASH_DELIMITER;
   243         // TODO: is this the correct way to find the separator ?
   244         if (!tns.startsWith("http"))
   245             delim = COLON_DELIMITER;
   247         if (tns.endsWith(delim))
   248             tns = tns.substring(0, tns.length()-1);
   250         if (o.getPortTypeName() == null)
   251             throw new WebServiceException("\"" + o.getName() + "\" operation's owning portType name is null.");
   253         return tns +
   254             delim +
   255             o.getPortTypeName().getLocalPart() +
   256             delim +
   257             (isFault ? o.getName().getLocalPart() + delim + "Fault" + delim : "") +
   258             name;
   259     }
   261     protected static final String COLON_DELIMITER = ":";
   262     protected static final String SLASH_DELIMITER = "/";
   263 }

mercurial