src/share/jaxws_classes/com/sun/xml/internal/ws/wsdl/writer/W3CAddressingWSDLGeneratorExtension.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/wsdl/writer/W3CAddressingWSDLGeneratorExtension.java	Tue Mar 06 16:09:35 2012 -0800
     1.3 @@ -0,0 +1,155 @@
     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.wsdl.writer;
    1.30 +
    1.31 +import com.sun.xml.internal.txw2.TypedXmlWriter;
    1.32 +import com.sun.xml.internal.ws.api.WSBinding;
    1.33 +import com.sun.xml.internal.ws.api.addressing.AddressingVersion;
    1.34 +import com.sun.xml.internal.ws.api.model.CheckedException;
    1.35 +import com.sun.xml.internal.ws.api.model.JavaMethod;
    1.36 +import com.sun.xml.internal.ws.api.wsdl.writer.WSDLGenExtnContext;
    1.37 +import com.sun.xml.internal.ws.api.wsdl.writer.WSDLGeneratorExtension;
    1.38 +
    1.39 +import javax.xml.ws.Action;
    1.40 +import javax.xml.ws.FaultAction;
    1.41 +import javax.xml.ws.soap.AddressingFeature;
    1.42 +import java.net.URI;
    1.43 +import java.net.URISyntaxException;
    1.44 +import java.util.logging.Logger;
    1.45 +
    1.46 +/**
    1.47 + * @author Arun Gupta
    1.48 + * @author Rama Pulavarthi
    1.49 + */
    1.50 +public class W3CAddressingWSDLGeneratorExtension extends WSDLGeneratorExtension {
    1.51 +    private boolean enabled;
    1.52 +    private boolean required = false;
    1.53 +
    1.54 +    @Override
    1.55 +    public void start(WSDLGenExtnContext ctxt) {
    1.56 +        WSBinding binding = ctxt.getBinding();
    1.57 +        TypedXmlWriter root = ctxt.getRoot();
    1.58 +        enabled = binding.isFeatureEnabled(AddressingFeature.class);
    1.59 +        if (!enabled)
    1.60 +            return;
    1.61 +        AddressingFeature ftr = binding.getFeature(AddressingFeature.class);
    1.62 +        required = ftr.isRequired();
    1.63 +        root._namespace(AddressingVersion.W3C.wsdlNsUri, AddressingVersion.W3C.getWsdlPrefix());
    1.64 +    }
    1.65 +
    1.66 +    @Override
    1.67 +    public void addOperationInputExtension(TypedXmlWriter input, JavaMethod method) {
    1.68 +        if (!enabled)
    1.69 +            return;
    1.70 +
    1.71 +        Action a = method.getSEIMethod().getAnnotation(Action.class);
    1.72 +        if (a != null && !a.input().equals("")) {
    1.73 +            addAttribute(input, a.input());
    1.74 +        } else {
    1.75 +            if (method.getBinding().getSOAPAction().equals("")) {
    1.76 +                //hack: generate default action for interop with .Net3.0 when soapAction is non-empty
    1.77 +                String defaultAction = getDefaultAction(method);
    1.78 +                addAttribute(input, defaultAction);
    1.79 +            }
    1.80 +        }
    1.81 +    }
    1.82 +
    1.83 +    protected static final String getDefaultAction(JavaMethod method) {
    1.84 +        String tns = method.getOwner().getTargetNamespace();
    1.85 +        String delim = "/";
    1.86 +        // TODO: is this the correct way to find the separator ?
    1.87 +        try {
    1.88 +            URI uri = new URI(tns);
    1.89 +            if(uri.getScheme().equalsIgnoreCase("urn"))
    1.90 +                delim = ":";
    1.91 +        } catch (URISyntaxException e) {
    1.92 +            LOGGER.warning("TargetNamespace of WebService is not a valid URI");
    1.93 +        }
    1.94 +        if (tns.endsWith(delim))
    1.95 +            tns = tns.substring(0, tns.length() - 1);
    1.96 +        //this assumes that fromjava case there won't be input name.
    1.97 +        // if there is input name in future, then here name=inputName
    1.98 +        //else use operation name as follows.
    1.99 +        String name = (method.getMEP().isOneWay())?method.getOperationName():method.getOperationName()+"Request";
   1.100 +
   1.101 +        return new StringBuilder(tns).append(delim).append(
   1.102 +                method.getOwner().getPortTypeName().getLocalPart()).append(
   1.103 +                delim).append(name).toString();
   1.104 +    }
   1.105 +
   1.106 +    @Override
   1.107 +    public void addOperationOutputExtension(TypedXmlWriter output, JavaMethod method) {
   1.108 +        if (!enabled)
   1.109 +            return;
   1.110 +
   1.111 +        Action a = method.getSEIMethod().getAnnotation(Action.class);
   1.112 +        if (a != null && !a.output().equals("")) {
   1.113 +            addAttribute(output, a.output());
   1.114 +        }
   1.115 +    }
   1.116 +
   1.117 +    @Override
   1.118 +    public void addOperationFaultExtension(TypedXmlWriter fault, JavaMethod method, CheckedException ce) {
   1.119 +        if (!enabled)
   1.120 +            return;
   1.121 +
   1.122 +        Action a = method.getSEIMethod().getAnnotation(Action.class);
   1.123 +        Class[] exs = method.getSEIMethod().getExceptionTypes();
   1.124 +
   1.125 +        if (exs == null)
   1.126 +            return;
   1.127 +
   1.128 +        if (a != null && a.fault() != null) {
   1.129 +            for (FaultAction fa : a.fault()) {
   1.130 +                if (fa.className().getName().equals(ce.getExceptionClass().getName())) {
   1.131 +                    if (fa.value().equals(""))
   1.132 +                        return;
   1.133 +
   1.134 +                    addAttribute(fault, fa.value());
   1.135 +                    return;
   1.136 +                }
   1.137 +            }
   1.138 +        }
   1.139 +    }
   1.140 +
   1.141 +    private void addAttribute(TypedXmlWriter writer, String attrValue) {
   1.142 +        writer._attribute(AddressingVersion.W3C.wsdlActionTag, attrValue);
   1.143 +    }
   1.144 +
   1.145 +    @Override
   1.146 +    public void addBindingExtension(TypedXmlWriter binding) {
   1.147 +        if (!enabled)
   1.148 +            return;
   1.149 +        UsingAddressing ua = binding._element(AddressingVersion.W3C.wsdlExtensionTag, UsingAddressing.class);
   1.150 +        /*
   1.151 +        Do not generate wsdl:required=true
   1.152 +        if(required) {
   1.153 +            ua.required(true);
   1.154 +        }
   1.155 +        */
   1.156 +    }
   1.157 +     private static final Logger LOGGER = Logger.getLogger(W3CAddressingWSDLGeneratorExtension.class.getName());
   1.158 +}

mercurial