Thu, 31 Aug 2017 15:18:52 +0800
merge
1 /*
2 * Copyright (c) 1997, 2012, 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.writer;
28 import com.sun.xml.internal.txw2.TypedXmlWriter;
29 import com.sun.xml.internal.ws.api.WSBinding;
30 import com.sun.xml.internal.ws.api.addressing.AddressingVersion;
31 import com.sun.xml.internal.ws.api.model.CheckedException;
32 import com.sun.xml.internal.ws.api.model.JavaMethod;
33 import com.sun.xml.internal.ws.api.wsdl.writer.WSDLGenExtnContext;
34 import com.sun.xml.internal.ws.api.wsdl.writer.WSDLGeneratorExtension;
36 import javax.xml.ws.Action;
37 import javax.xml.ws.FaultAction;
38 import javax.xml.ws.soap.AddressingFeature;
39 import java.net.URI;
40 import java.net.URISyntaxException;
41 import java.util.logging.Logger;
43 /**
44 * @author Arun Gupta
45 * @author Rama Pulavarthi
46 */
47 public class W3CAddressingWSDLGeneratorExtension extends WSDLGeneratorExtension {
48 private boolean enabled;
49 private boolean required = false;
51 @Override
52 public void start(WSDLGenExtnContext ctxt) {
53 WSBinding binding = ctxt.getBinding();
54 TypedXmlWriter root = ctxt.getRoot();
55 enabled = binding.isFeatureEnabled(AddressingFeature.class);
56 if (!enabled)
57 return;
58 AddressingFeature ftr = binding.getFeature(AddressingFeature.class);
59 required = ftr.isRequired();
60 root._namespace(AddressingVersion.W3C.wsdlNsUri, AddressingVersion.W3C.getWsdlPrefix());
61 }
63 @Override
64 public void addOperationInputExtension(TypedXmlWriter input, JavaMethod method) {
65 if (!enabled)
66 return;
68 Action a = method.getSEIMethod().getAnnotation(Action.class);
69 if (a != null && !a.input().equals("")) {
70 addAttribute(input, a.input());
71 } else {
73 String soapAction = method.getBinding().getSOAPAction();
74 // in SOAP 1.2 soapAction is optional ...
75 if (soapAction == null || soapAction.equals("")) {
76 //hack: generate default action for interop with .Net3.0 when soapAction is non-empty
77 String defaultAction = getDefaultAction(method);
78 addAttribute(input, defaultAction);
79 }
80 }
81 }
83 protected static final String getDefaultAction(JavaMethod method) {
84 String tns = method.getOwner().getTargetNamespace();
85 String delim = "/";
86 // TODO: is this the correct way to find the separator ?
87 try {
88 URI uri = new URI(tns);
89 if(uri.getScheme().equalsIgnoreCase("urn"))
90 delim = ":";
91 } catch (URISyntaxException e) {
92 LOGGER.warning("TargetNamespace of WebService is not a valid URI");
93 }
94 if (tns.endsWith(delim))
95 tns = tns.substring(0, tns.length() - 1);
96 //this assumes that fromjava case there won't be input name.
97 // if there is input name in future, then here name=inputName
98 //else use operation name as follows.
99 String name = (method.getMEP().isOneWay())?method.getOperationName():method.getOperationName()+"Request";
101 return new StringBuilder(tns).append(delim).append(
102 method.getOwner().getPortTypeName().getLocalPart()).append(
103 delim).append(name).toString();
104 }
106 @Override
107 public void addOperationOutputExtension(TypedXmlWriter output, JavaMethod method) {
108 if (!enabled)
109 return;
111 Action a = method.getSEIMethod().getAnnotation(Action.class);
112 if (a != null && !a.output().equals("")) {
113 addAttribute(output, a.output());
114 }
115 }
117 @Override
118 public void addOperationFaultExtension(TypedXmlWriter fault, JavaMethod method, CheckedException ce) {
119 if (!enabled)
120 return;
122 Action a = method.getSEIMethod().getAnnotation(Action.class);
123 Class[] exs = method.getSEIMethod().getExceptionTypes();
125 if (exs == null)
126 return;
128 if (a != null && a.fault() != null) {
129 for (FaultAction fa : a.fault()) {
130 if (fa.className().getName().equals(ce.getExceptionClass().getName())) {
131 if (fa.value().equals(""))
132 return;
134 addAttribute(fault, fa.value());
135 return;
136 }
137 }
138 }
139 }
141 private void addAttribute(TypedXmlWriter writer, String attrValue) {
142 writer._attribute(AddressingVersion.W3C.wsdlActionTag, attrValue);
143 }
145 @Override
146 public void addBindingExtension(TypedXmlWriter binding) {
147 if (!enabled)
148 return;
149 binding._element(AddressingVersion.W3C.wsdlExtensionTag, UsingAddressing.class);
150 /*
151 Do not generate wsdl:required=true
152 if(required) {
153 ua.required(true);
154 }
155 */
156 }
157 private static final Logger LOGGER = Logger.getLogger(W3CAddressingWSDLGeneratorExtension.class.getName());
158 }