001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036: /*
037: * $Id: W3CAddressingWSDLGeneratorExtension.java,v 1.7 2007/05/30 01:21:36 ofung Exp $
038: */
039:
040: package com.sun.xml.ws.wsdl.writer;
041:
042: import com.sun.xml.txw2.TypedXmlWriter;
043: import com.sun.xml.ws.api.WSBinding;
044: import com.sun.xml.ws.api.addressing.AddressingVersion;
045: import com.sun.xml.ws.api.model.CheckedException;
046: import com.sun.xml.ws.api.model.JavaMethod;
047: import com.sun.xml.ws.api.wsdl.writer.WSDLGenExtnContext;
048: import com.sun.xml.ws.api.wsdl.writer.WSDLGeneratorExtension;
049:
050: import javax.xml.ws.Action;
051: import javax.xml.ws.FaultAction;
052: import javax.xml.ws.soap.AddressingFeature;
053: import java.net.URI;
054: import java.net.URISyntaxException;
055: import java.util.logging.Logger;
056:
057: /**
058: * @author Arun Gupta
059: * @author Rama Pulavarthi
060: */
061: public class W3CAddressingWSDLGeneratorExtension extends
062: WSDLGeneratorExtension {
063: private boolean enabled;
064: private boolean required = false;
065:
066: @Override
067: public void start(WSDLGenExtnContext ctxt) {
068: WSBinding binding = ctxt.getBinding();
069: TypedXmlWriter root = ctxt.getRoot();
070: enabled = binding.isFeatureEnabled(AddressingFeature.class);
071: if (!enabled)
072: return;
073: AddressingFeature ftr = binding
074: .getFeature(AddressingFeature.class);
075: required = ftr.isRequired();
076: root._namespace(AddressingVersion.W3C.wsdlNsUri,
077: AddressingVersion.W3C.getWsdlPrefix());
078: }
079:
080: @Override
081: public void addOperationInputExtension(TypedXmlWriter input,
082: JavaMethod method) {
083: if (!enabled)
084: return;
085:
086: Action a = method.getSEIMethod().getAnnotation(Action.class);
087: if (a != null && !a.input().equals("")) {
088: addAttribute(input, a.input());
089: } else {
090: if (method.getBinding().getSOAPAction().equals("")) {
091: //hack: generate default action for interop with .Net3.0 when soapAction is non-empty
092: String defaultAction = getDefaultAction(method);
093: addAttribute(input, defaultAction);
094: }
095: }
096: }
097:
098: protected static final String getDefaultAction(JavaMethod method) {
099: String tns = method.getOwner().getTargetNamespace();
100: String delim = "/";
101: // TODO: is this the correct way to find the separator ?
102: try {
103: URI uri = new URI(tns);
104: if (uri.getScheme().equalsIgnoreCase("urn"))
105: delim = ":";
106: } catch (URISyntaxException e) {
107: LOGGER
108: .warning("TargetNamespace of WebService is not a valid URI");
109: }
110: if (tns.endsWith(delim))
111: tns = tns.substring(0, tns.length() - 1);
112: //this assumes that fromjava case there won't be input name.
113: // if there is input name in future, then here name=inputName
114: //else use operation name as follows.
115: String name = (method.getMEP().isOneWay()) ? method
116: .getOperationName() : method.getOperationName()
117: + "Request";
118:
119: return new StringBuilder(tns).append(delim).append(
120: method.getOwner().getPortTypeName().getLocalPart())
121: .append(delim).append(name).toString();
122: }
123:
124: @Override
125: public void addOperationOutputExtension(TypedXmlWriter output,
126: JavaMethod method) {
127: if (!enabled)
128: return;
129:
130: Action a = method.getSEIMethod().getAnnotation(Action.class);
131: if (a != null && !a.output().equals("")) {
132: addAttribute(output, a.output());
133: }
134: }
135:
136: @Override
137: public void addOperationFaultExtension(TypedXmlWriter fault,
138: JavaMethod method, CheckedException ce) {
139: if (!enabled)
140: return;
141:
142: Action a = method.getSEIMethod().getAnnotation(Action.class);
143: Class[] exs = method.getSEIMethod().getExceptionTypes();
144:
145: if (exs == null)
146: return;
147:
148: if (a != null && a.fault() != null) {
149: for (FaultAction fa : a.fault()) {
150: if (fa.className().getName().equals(
151: ce.getExceptionClass().getName())) {
152: if (fa.value().equals(""))
153: return;
154:
155: addAttribute(fault, fa.value());
156: return;
157: }
158: }
159: }
160: }
161:
162: private void addAttribute(TypedXmlWriter writer, String attrValue) {
163: writer._attribute(AddressingVersion.W3C.wsdlActionTag,
164: attrValue);
165: }
166:
167: @Override
168: public void addBindingExtension(TypedXmlWriter binding) {
169: if (!enabled)
170: return;
171: UsingAddressing ua = binding._element(
172: AddressingVersion.W3C.wsdlExtensionTag,
173: UsingAddressing.class);
174: /*
175: Do not generate wsdl:required=true
176: if(required) {
177: ua.required(true);
178: }
179: */
180: }
181:
182: private static final Logger LOGGER = Logger
183: .getLogger(W3CAddressingWSDLGeneratorExtension.class
184: .getName());
185: }
|