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: package com.sun.xml.ws.wsdl.parser;
038:
039: import com.sun.xml.ws.api.addressing.AddressingVersion;
040: import com.sun.xml.ws.api.model.wsdl.WSDLBoundOperation;
041: import com.sun.xml.ws.api.model.wsdl.WSDLBoundPortType;
042: import com.sun.xml.ws.api.model.wsdl.WSDLFault;
043: import com.sun.xml.ws.api.model.wsdl.WSDLFeaturedObject;
044: import com.sun.xml.ws.api.model.wsdl.WSDLModel;
045: import com.sun.xml.ws.api.model.wsdl.WSDLOperation;
046: import com.sun.xml.ws.api.model.wsdl.WSDLPort;
047: import com.sun.xml.ws.api.model.wsdl.WSDLService;
048: import com.sun.xml.ws.api.wsdl.parser.WSDLParserExtension;
049: import com.sun.xml.ws.api.wsdl.parser.WSDLParserExtensionContext;
050: import com.sun.xml.ws.model.wsdl.WSDLBoundOperationImpl;
051: import com.sun.xml.ws.model.wsdl.WSDLBoundPortTypeImpl;
052: import com.sun.xml.ws.model.wsdl.WSDLOperationImpl;
053: import com.sun.xml.ws.model.wsdl.WSDLPortImpl;
054: import com.sun.xml.ws.model.wsdl.WSDLPortTypeImpl;
055: import com.sun.xml.ws.streaming.XMLStreamReaderUtil;
056: import com.sun.xml.ws.resources.AddressingMessages;
057:
058: import javax.xml.namespace.QName;
059: import javax.xml.stream.XMLStreamException;
060: import javax.xml.stream.XMLStreamReader;
061: import javax.xml.ws.WebServiceException;
062: import javax.xml.ws.soap.AddressingFeature;
063: import java.util.Map;
064:
065: /**
066: * W3C WS-Addressing Runtime WSDL parser extension
067: *
068: * @author Arun Gupta
069: */
070: public class W3CAddressingWSDLParserExtension extends
071: WSDLParserExtension {
072: @Override
073: public boolean bindingElements(WSDLBoundPortType binding,
074: XMLStreamReader reader) {
075: return addressibleElement(reader, binding);
076: }
077:
078: @Override
079: public boolean portElements(WSDLPort port, XMLStreamReader reader) {
080: return addressibleElement(reader, port);
081: }
082:
083: private boolean addressibleElement(XMLStreamReader reader,
084: WSDLFeaturedObject binding) {
085: QName ua = reader.getName();
086: if (ua.equals(AddressingVersion.W3C.wsdlExtensionTag)) {
087: String required = reader.getAttributeValue(
088: WSDLConstants.NS_WSDL, "required");
089: binding.addFeature(new AddressingFeature(true, Boolean
090: .parseBoolean(required)));
091: XMLStreamReaderUtil.skipElement(reader);
092: return true; // UsingAddressing is consumed
093: }
094:
095: return false;
096: }
097:
098: @Override
099: public boolean bindingOperationElements(
100: WSDLBoundOperation operation, XMLStreamReader reader) {
101: WSDLBoundOperationImpl impl = (WSDLBoundOperationImpl) operation;
102:
103: QName anon = reader.getName();
104: if (anon.equals(AddressingVersion.W3C.wsdlAnonymousTag)) {
105: try {
106: String value = reader.getElementText();
107: if (value == null || value.trim().equals("")) {
108: throw new WebServiceException(
109: "Null values not permitted in wsaw:Anonymous.");
110: // TODO: throw exception only if wsdl:required=true
111: // TODO: is this the right exception ?
112: } else if (value.equals("optional")) {
113: impl
114: .setAnonymous(WSDLBoundOperation.ANONYMOUS.optional);
115: } else if (value.equals("required")) {
116: impl
117: .setAnonymous(WSDLBoundOperation.ANONYMOUS.required);
118: } else if (value.equals("prohibited")) {
119: impl
120: .setAnonymous(WSDLBoundOperation.ANONYMOUS.prohibited);
121: } else {
122: throw new WebServiceException(
123: "wsaw:Anonymous value \"" + value
124: + "\" not understood.");
125: // TODO: throw exception only if wsdl:required=true
126: // TODO: is this the right exception ?
127: }
128: } catch (XMLStreamException e) {
129: throw new WebServiceException(e); // TODO: is this the correct behavior ?
130: }
131:
132: return true; // consumed the element
133: }
134:
135: return false;
136: }
137:
138: @Override
139: public boolean portTypeOperationInput(WSDLOperation o,
140: XMLStreamReader reader) {
141: WSDLOperationImpl impl = (WSDLOperationImpl) o;
142:
143: String action = ParserUtil.getAttribute(reader,
144: AddressingVersion.W3C.wsdlActionTag);
145: if (action != null) {
146: impl.getInput().setAction(action);
147: impl.getInput().setDefaultAction(false);
148: }
149:
150: return false;
151: }
152:
153: @Override
154: public boolean portTypeOperationOutput(WSDLOperation o,
155: XMLStreamReader reader) {
156: WSDLOperationImpl impl = (WSDLOperationImpl) o;
157:
158: String action = ParserUtil.getAttribute(reader,
159: AddressingVersion.W3C.wsdlActionTag);
160: if (action != null) {
161: impl.getOutput().setAction(action);
162: }
163:
164: return false;
165: }
166:
167: @Override
168: public boolean portTypeOperationFault(WSDLOperation o,
169: XMLStreamReader reader) {
170: WSDLOperationImpl impl = (WSDLOperationImpl) o;
171:
172: String action = ParserUtil.getAttribute(reader,
173: AddressingVersion.W3C.wsdlActionTag);
174: if (action != null) {
175: String name = ParserUtil.getMandatoryNonEmptyAttribute(
176: reader, "name");
177: impl.getFaultActionMap().put(name, action);
178: }
179:
180: return false;
181: }
182:
183: /**
184: * Process wsdl:portType operation after the entire WSDL model has been populated.
185: * The task list includes: <p>
186: * <ul>
187: * <li>Patch the value of UsingAddressing in wsdl:port and wsdl:binding</li>
188: * <li>Populate actions for the messages that do not have an explicit wsaw:Action</li>
189: * <li>Patch the default value of wsaw:Anonymous=optional if none is specified</li>
190: * </ul>
191: * @param context
192: */
193: @Override
194: public void finished(WSDLParserExtensionContext context) {
195: WSDLModel model = context.getWSDLModel();
196: for (WSDLService service : model.getServices().values()) {
197: for (WSDLPort wp : service.getPorts()) {
198: WSDLPortImpl port = (WSDLPortImpl) wp;
199: WSDLBoundPortTypeImpl binding = port.getBinding();
200:
201: // populate actions for the messages that do not have an explicit wsaw:Action
202: populateActions(binding);
203:
204: // patch the default value of wsaw:Anonymous=optional if none is specified
205: patchAnonymousDefault(binding);
206: }
207: }
208: }
209:
210: protected String getNamespaceURI() {
211: return AddressingVersion.W3C.wsdlNsUri;
212: }
213:
214: /**
215: * Populate all the Actions
216: *
217: * @param binding soapbinding:operation
218: */
219: private void populateActions(WSDLBoundPortTypeImpl binding) {
220: WSDLPortTypeImpl porttype = binding.getPortType();
221: for (WSDLOperationImpl o : porttype.getOperations()) {
222: // TODO: this may be performance intensive. Alternatively default action
223: // TODO: can be calculated when the operation is actually invoked.
224: WSDLBoundOperationImpl wboi = binding.get(o.getName());
225:
226: if (wboi == null) {
227: //If this operation is unbound set the action to default
228: o.getInput().setAction(defaultInputAction(o));
229: continue;
230: }
231: String soapAction = wboi.getSOAPAction();
232: if (o.getInput().getAction() == null
233: || o.getInput().getAction().equals("")) {
234: // explicit wsaw:Action is not specified
235:
236: if (soapAction != null && !soapAction.equals("")) {
237: // if soapAction is non-empty, use that
238: o.getInput().setAction(soapAction);
239: } else {
240: // otherwise generate default Action
241: o.getInput().setAction(defaultInputAction(o));
242: }
243: }
244:
245: // skip output and fault processing for one-way methods
246: if (o.getOutput() == null)
247: continue;
248:
249: if (o.getOutput().getAction() == null
250: || o.getOutput().getAction().equals("")) {
251: o.getOutput().setAction(defaultOutputAction(o));
252: }
253:
254: if (o.getFaults() == null
255: || !o.getFaults().iterator().hasNext())
256: continue;
257:
258: Map<String, String> map = o.getFaultActionMap();
259: for (WSDLFault f : o.getFaults()) {
260: if (map.get(f.getName()) == null
261: || map.get(f.getName()).equals(""))
262: map.put(f.getName(), defaultFaultAction(
263: f.getName(), o));
264: }
265: }
266: }
267:
268: /**
269: * Patch the default value of wsaw:Anonymous=optional if none is specified
270: *
271: * @param binding WSDLBoundPortTypeImpl
272: */
273: protected void patchAnonymousDefault(WSDLBoundPortTypeImpl binding) {
274: for (WSDLBoundOperationImpl wbo : binding
275: .getBindingOperations()) {
276: if (wbo.getAnonymous() == null)
277: wbo.setAnonymous(WSDLBoundOperation.ANONYMOUS.optional);
278: }
279: }
280:
281: private String defaultInputAction(WSDLOperation o) {
282: return buildAction(o.getInput().getName(), o, false);
283: }
284:
285: private String defaultOutputAction(WSDLOperation o) {
286: return buildAction(o.getOutput().getName(), o, false);
287: }
288:
289: private String defaultFaultAction(String name, WSDLOperation o) {
290: return buildAction(name, o, true);
291: }
292:
293: protected static final String buildAction(String name,
294: WSDLOperation o, boolean isFault) {
295: String tns = o.getName().getNamespaceURI();
296:
297: String delim = SLASH_DELIMITER;
298:
299: // TODO: is this the correct way to find the separator ?
300: if (!tns.startsWith("http"))
301: delim = COLON_DELIMITER;
302:
303: if (tns.endsWith(delim))
304: tns = tns.substring(0, tns.length() - 1);
305:
306: if (o.getPortTypeName() == null)
307: throw new WebServiceException("\"" + o.getName()
308: + "\" operation's owning portType name is null.");
309:
310: return tns
311: + delim
312: + o.getPortTypeName().getLocalPart()
313: + delim
314: + (isFault ? o.getName().getLocalPart() + delim
315: + "Fault" + delim : "") + name;
316: }
317:
318: protected static final String COLON_DELIMITER = ":";
319: protected static final String SLASH_DELIMITER = "/";
320: }
|