325N/A/*
325N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.xml.internal.ws.wsdl.parser;
325N/A
325N/Aimport com.sun.xml.internal.ws.api.addressing.AddressingVersion;
325N/Aimport com.sun.xml.internal.ws.api.model.wsdl.*;
325N/Aimport com.sun.xml.internal.ws.api.wsdl.parser.WSDLParserExtension;
325N/Aimport com.sun.xml.internal.ws.api.wsdl.parser.WSDLParserExtensionContext;
325N/Aimport com.sun.xml.internal.ws.model.wsdl.*;
325N/Aimport com.sun.xml.internal.ws.streaming.XMLStreamReaderUtil;
325N/Aimport com.sun.xml.internal.ws.resources.AddressingMessages;
325N/A
325N/Aimport javax.xml.namespace.QName;
325N/Aimport javax.xml.stream.XMLStreamException;
325N/Aimport javax.xml.stream.XMLStreamReader;
325N/Aimport javax.xml.ws.WebServiceException;
325N/Aimport javax.xml.ws.soap.AddressingFeature;
325N/Aimport java.util.Map;
325N/A
325N/A/**
325N/A * W3C WS-Addressing Runtime WSDL parser extension
325N/A *
325N/A * @author Arun Gupta
325N/A */
325N/Apublic class W3CAddressingWSDLParserExtension extends WSDLParserExtension {
325N/A @Override
325N/A public boolean bindingElements(WSDLBoundPortType binding, XMLStreamReader reader) {
325N/A return addressibleElement(reader, binding);
325N/A }
325N/A
325N/A @Override
325N/A public boolean portElements(WSDLPort port, XMLStreamReader reader) {
325N/A return addressibleElement(reader, port);
325N/A }
325N/A
325N/A private boolean addressibleElement(XMLStreamReader reader, WSDLFeaturedObject binding) {
325N/A QName ua = reader.getName();
325N/A if (ua.equals(AddressingVersion.W3C.wsdlExtensionTag)) {
325N/A String required = reader.getAttributeValue(WSDLConstants.NS_WSDL, "required");
325N/A binding.addFeature(new AddressingFeature(true, Boolean.parseBoolean(required)));
325N/A XMLStreamReaderUtil.skipElement(reader);
325N/A return true; // UsingAddressing is consumed
325N/A }
325N/A
325N/A return false;
325N/A }
325N/A
325N/A @Override
325N/A public boolean bindingOperationElements(WSDLBoundOperation operation, XMLStreamReader reader) {
325N/A WSDLBoundOperationImpl impl = (WSDLBoundOperationImpl)operation;
325N/A
325N/A QName anon = reader.getName();
325N/A if (anon.equals(AddressingVersion.W3C.wsdlAnonymousTag)) {
325N/A try {
325N/A String value = reader.getElementText();
325N/A if (value == null || value.trim().equals("")) {
325N/A throw new WebServiceException("Null values not permitted in wsaw:Anonymous.");
325N/A // TODO: throw exception only if wsdl:required=true
325N/A // TODO: is this the right exception ?
325N/A } else if (value.equals("optional")) {
325N/A impl.setAnonymous(WSDLBoundOperation.ANONYMOUS.optional);
325N/A } else if (value.equals("required")) {
325N/A impl.setAnonymous(WSDLBoundOperation.ANONYMOUS.required);
325N/A } else if (value.equals("prohibited")) {
325N/A impl.setAnonymous(WSDLBoundOperation.ANONYMOUS.prohibited);
325N/A } else {
325N/A throw new WebServiceException("wsaw:Anonymous value \"" + value + "\" not understood.");
325N/A // TODO: throw exception only if wsdl:required=true
325N/A // TODO: is this the right exception ?
325N/A }
325N/A } catch (XMLStreamException e) {
325N/A throw new WebServiceException(e); // TODO: is this the correct behavior ?
325N/A }
325N/A
325N/A return true; // consumed the element
325N/A }
325N/A
325N/A return false;
325N/A }
325N/A
325N/A public void portTypeOperationInputAttributes(WSDLInput input, XMLStreamReader reader) {
325N/A String action = ParserUtil.getAttribute(reader, getWsdlActionTag());
325N/A if (action != null) {
325N/A ((WSDLInputImpl)input).setAction(action);
325N/A ((WSDLInputImpl)input).setDefaultAction(false);
325N/A }
325N/A }
325N/A
325N/A
325N/A public void portTypeOperationOutputAttributes(WSDLOutput output, XMLStreamReader reader) {
325N/A String action = ParserUtil.getAttribute(reader, getWsdlActionTag());
325N/A if (action != null) {
325N/A ((WSDLOutputImpl)output).setAction(action);
325N/A ((WSDLOutputImpl)output).setDefaultAction(false);
325N/A }
325N/A }
325N/A
325N/A
325N/A public void portTypeOperationFaultAttributes(WSDLFault fault, XMLStreamReader reader) {
325N/A String action = ParserUtil.getAttribute(reader, getWsdlActionTag());
325N/A if (action != null) {
325N/A ((WSDLFaultImpl) fault).setAction(action);
325N/A ((WSDLFaultImpl) fault).setDefaultAction(false);
325N/A }
325N/A }
325N/A
325N/A /**
325N/A * Process wsdl:portType operation after the entire WSDL model has been populated.
325N/A * The task list includes: <p>
325N/A * <ul>
325N/A * <li>Patch the value of UsingAddressing in wsdl:port and wsdl:binding</li>
325N/A * <li>Populate actions for the messages that do not have an explicit wsaw:Action</li>
325N/A * <li>Patch the default value of wsaw:Anonymous=optional if none is specified</li>
325N/A * </ul>
325N/A * @param context
325N/A */
325N/A @Override
325N/A public void finished(WSDLParserExtensionContext context) {
325N/A WSDLModel model = context.getWSDLModel();
325N/A for (WSDLService service : model.getServices().values()) {
325N/A for (WSDLPort wp : service.getPorts()) {
325N/A WSDLPortImpl port = (WSDLPortImpl)wp;
325N/A WSDLBoundPortTypeImpl binding = port.getBinding();
325N/A
325N/A // populate actions for the messages that do not have an explicit wsaw:Action
325N/A populateActions(binding);
325N/A
325N/A // patch the default value of wsaw:Anonymous=optional if none is specified
325N/A patchAnonymousDefault(binding);
325N/A }
325N/A }
325N/A }
325N/A
325N/A protected String getNamespaceURI() {
325N/A return AddressingVersion.W3C.wsdlNsUri;
325N/A }
325N/A
325N/A protected QName getWsdlActionTag() {
325N/A return AddressingVersion.W3C.wsdlActionTag;
325N/A }
325N/A /**
325N/A * Populate all the Actions
325N/A *
325N/A * @param binding soapbinding:operation
325N/A */
325N/A private void populateActions(WSDLBoundPortTypeImpl binding) {
325N/A WSDLPortTypeImpl porttype = binding.getPortType();
325N/A for (WSDLOperationImpl o : porttype.getOperations()) {
325N/A // TODO: this may be performance intensive. Alternatively default action
325N/A // TODO: can be calculated when the operation is actually invoked.
325N/A WSDLBoundOperationImpl wboi = binding.get(o.getName());
325N/A
325N/A if (wboi == null) {
325N/A //If this operation is unbound set the action to default
325N/A o.getInput().setAction(defaultInputAction(o));
325N/A continue;
325N/A }
325N/A String soapAction = wboi.getSOAPAction();
325N/A if (o.getInput().getAction() == null || o.getInput().getAction().equals("")) {
325N/A // explicit wsaw:Action is not specified
325N/A
325N/A if (soapAction != null && !soapAction.equals("")) {
325N/A // if soapAction is non-empty, use that
325N/A o.getInput().setAction(soapAction);
325N/A } else {
325N/A // otherwise generate default Action
325N/A o.getInput().setAction(defaultInputAction(o));
325N/A }
325N/A }
325N/A
325N/A // skip output and fault processing for one-way methods
325N/A if (o.getOutput() == null)
325N/A continue;
325N/A
325N/A if (o.getOutput().getAction() == null || o.getOutput().getAction().equals("")) {
325N/A o.getOutput().setAction(defaultOutputAction(o));
325N/A }
325N/A
325N/A if (o.getFaults() == null || !o.getFaults().iterator().hasNext())
325N/A continue;
325N/A
325N/A for (WSDLFault f : o.getFaults()) {
325N/A if (f.getAction() == null || f.getAction().equals("")) {
325N/A ((WSDLFaultImpl)f).setAction(defaultFaultAction(f.getName(), o));
325N/A }
325N/A
325N/A }
325N/A }
325N/A }
325N/A
325N/A /**
325N/A * Patch the default value of wsaw:Anonymous=optional if none is specified
325N/A *
325N/A * @param binding WSDLBoundPortTypeImpl
325N/A */
325N/A protected void patchAnonymousDefault(WSDLBoundPortTypeImpl binding) {
325N/A for (WSDLBoundOperationImpl wbo : binding.getBindingOperations()) {
325N/A if (wbo.getAnonymous() == null)
325N/A wbo.setAnonymous(WSDLBoundOperation.ANONYMOUS.optional);
325N/A }
325N/A }
325N/A
325N/A private String defaultInputAction(WSDLOperation o) {
325N/A return buildAction(o.getInput().getName(), o, false);
325N/A }
325N/A
325N/A private String defaultOutputAction(WSDLOperation o) {
325N/A return buildAction(o.getOutput().getName(), o, false);
325N/A }
325N/A
325N/A private String defaultFaultAction(String name, WSDLOperation o) {
325N/A return buildAction(name, o, true);
325N/A }
325N/A
325N/A protected static final String buildAction(String name, WSDLOperation o, boolean isFault) {
325N/A String tns = o.getName().getNamespaceURI();
325N/A
325N/A String delim = SLASH_DELIMITER;
325N/A
325N/A // TODO: is this the correct way to find the separator ?
325N/A if (!tns.startsWith("http"))
325N/A delim = COLON_DELIMITER;
325N/A
325N/A if (tns.endsWith(delim))
325N/A tns = tns.substring(0, tns.length()-1);
325N/A
325N/A if (o.getPortTypeName() == null)
325N/A throw new WebServiceException("\"" + o.getName() + "\" operation's owning portType name is null.");
325N/A
325N/A return tns +
325N/A delim +
325N/A o.getPortTypeName().getLocalPart() +
325N/A delim +
325N/A (isFault ? o.getName().getLocalPart() + delim + "Fault" + delim : "") +
325N/A name;
325N/A }
325N/A
325N/A protected static final String COLON_DELIMITER = ":";
325N/A protected static final String SLASH_DELIMITER = "/";
325N/A}