src/share/jaxws_classes/com/sun/xml/internal/ws/wsdl/parser/W3CAddressingWSDLParserExtension.java

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 368
0989ad8c0860
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

ohair@286 1 /*
ohair@286 2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
ohair@286 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286 4 *
ohair@286 5 * This code is free software; you can redistribute it and/or modify it
ohair@286 6 * under the terms of the GNU General Public License version 2 only, as
ohair@286 7 * published by the Free Software Foundation. Oracle designates this
ohair@286 8 * particular file as subject to the "Classpath" exception as provided
ohair@286 9 * by Oracle in the LICENSE file that accompanied this code.
ohair@286 10 *
ohair@286 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286 14 * version 2 for more details (a copy is included in the LICENSE file that
ohair@286 15 * accompanied this code).
ohair@286 16 *
ohair@286 17 * You should have received a copy of the GNU General Public License version
ohair@286 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286 20 *
ohair@286 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286 22 * or visit www.oracle.com if you need additional information or have any
ohair@286 23 * questions.
ohair@286 24 */
ohair@286 25
ohair@286 26 package com.sun.xml.internal.ws.wsdl.parser;
ohair@286 27
ohair@286 28 import com.sun.xml.internal.ws.api.addressing.AddressingVersion;
ohair@286 29 import com.sun.xml.internal.ws.api.model.wsdl.*;
ohair@286 30 import com.sun.xml.internal.ws.api.wsdl.parser.WSDLParserExtension;
ohair@286 31 import com.sun.xml.internal.ws.api.wsdl.parser.WSDLParserExtensionContext;
ohair@286 32 import com.sun.xml.internal.ws.model.wsdl.*;
ohair@286 33 import com.sun.xml.internal.ws.streaming.XMLStreamReaderUtil;
ohair@286 34 import com.sun.xml.internal.ws.resources.AddressingMessages;
ohair@286 35
ohair@286 36 import javax.xml.namespace.QName;
ohair@286 37 import javax.xml.stream.XMLStreamException;
ohair@286 38 import javax.xml.stream.XMLStreamReader;
ohair@286 39 import javax.xml.ws.WebServiceException;
ohair@286 40 import javax.xml.ws.soap.AddressingFeature;
ohair@286 41 import java.util.Map;
ohair@286 42
ohair@286 43 /**
ohair@286 44 * W3C WS-Addressing Runtime WSDL parser extension
ohair@286 45 *
ohair@286 46 * @author Arun Gupta
ohair@286 47 */
ohair@286 48 public class W3CAddressingWSDLParserExtension extends WSDLParserExtension {
ohair@286 49 @Override
ohair@286 50 public boolean bindingElements(WSDLBoundPortType binding, XMLStreamReader reader) {
ohair@286 51 return addressibleElement(reader, binding);
ohair@286 52 }
ohair@286 53
ohair@286 54 @Override
ohair@286 55 public boolean portElements(WSDLPort port, XMLStreamReader reader) {
ohair@286 56 return addressibleElement(reader, port);
ohair@286 57 }
ohair@286 58
ohair@286 59 private boolean addressibleElement(XMLStreamReader reader, WSDLFeaturedObject binding) {
ohair@286 60 QName ua = reader.getName();
ohair@286 61 if (ua.equals(AddressingVersion.W3C.wsdlExtensionTag)) {
ohair@286 62 String required = reader.getAttributeValue(WSDLConstants.NS_WSDL, "required");
ohair@286 63 binding.addFeature(new AddressingFeature(true, Boolean.parseBoolean(required)));
ohair@286 64 XMLStreamReaderUtil.skipElement(reader);
ohair@286 65 return true; // UsingAddressing is consumed
ohair@286 66 }
ohair@286 67
ohair@286 68 return false;
ohair@286 69 }
ohair@286 70
ohair@286 71 @Override
ohair@286 72 public boolean bindingOperationElements(WSDLBoundOperation operation, XMLStreamReader reader) {
ohair@286 73 WSDLBoundOperationImpl impl = (WSDLBoundOperationImpl)operation;
ohair@286 74
ohair@286 75 QName anon = reader.getName();
ohair@286 76 if (anon.equals(AddressingVersion.W3C.wsdlAnonymousTag)) {
ohair@286 77 try {
ohair@286 78 String value = reader.getElementText();
ohair@286 79 if (value == null || value.trim().equals("")) {
ohair@286 80 throw new WebServiceException("Null values not permitted in wsaw:Anonymous.");
ohair@286 81 // TODO: throw exception only if wsdl:required=true
ohair@286 82 // TODO: is this the right exception ?
ohair@286 83 } else if (value.equals("optional")) {
ohair@286 84 impl.setAnonymous(WSDLBoundOperation.ANONYMOUS.optional);
ohair@286 85 } else if (value.equals("required")) {
ohair@286 86 impl.setAnonymous(WSDLBoundOperation.ANONYMOUS.required);
ohair@286 87 } else if (value.equals("prohibited")) {
ohair@286 88 impl.setAnonymous(WSDLBoundOperation.ANONYMOUS.prohibited);
ohair@286 89 } else {
ohair@286 90 throw new WebServiceException("wsaw:Anonymous value \"" + value + "\" not understood.");
ohair@286 91 // TODO: throw exception only if wsdl:required=true
ohair@286 92 // TODO: is this the right exception ?
ohair@286 93 }
ohair@286 94 } catch (XMLStreamException e) {
ohair@286 95 throw new WebServiceException(e); // TODO: is this the correct behavior ?
ohair@286 96 }
ohair@286 97
ohair@286 98 return true; // consumed the element
ohair@286 99 }
ohair@286 100
ohair@286 101 return false;
ohair@286 102 }
ohair@286 103
ohair@286 104 public void portTypeOperationInputAttributes(WSDLInput input, XMLStreamReader reader) {
ohair@286 105 String action = ParserUtil.getAttribute(reader, getWsdlActionTag());
ohair@286 106 if (action != null) {
ohair@286 107 ((WSDLInputImpl)input).setAction(action);
ohair@286 108 ((WSDLInputImpl)input).setDefaultAction(false);
ohair@286 109 }
ohair@286 110 }
ohair@286 111
ohair@286 112
ohair@286 113 public void portTypeOperationOutputAttributes(WSDLOutput output, XMLStreamReader reader) {
ohair@286 114 String action = ParserUtil.getAttribute(reader, getWsdlActionTag());
ohair@286 115 if (action != null) {
ohair@286 116 ((WSDLOutputImpl)output).setAction(action);
ohair@286 117 ((WSDLOutputImpl)output).setDefaultAction(false);
ohair@286 118 }
ohair@286 119 }
ohair@286 120
ohair@286 121
ohair@286 122 public void portTypeOperationFaultAttributes(WSDLFault fault, XMLStreamReader reader) {
ohair@286 123 String action = ParserUtil.getAttribute(reader, getWsdlActionTag());
ohair@286 124 if (action != null) {
ohair@286 125 ((WSDLFaultImpl) fault).setAction(action);
ohair@286 126 ((WSDLFaultImpl) fault).setDefaultAction(false);
ohair@286 127 }
ohair@286 128 }
ohair@286 129
ohair@286 130 /**
ohair@286 131 * Process wsdl:portType operation after the entire WSDL model has been populated.
ohair@286 132 * The task list includes: <p>
ohair@286 133 * <ul>
ohair@286 134 * <li>Patch the value of UsingAddressing in wsdl:port and wsdl:binding</li>
ohair@286 135 * <li>Populate actions for the messages that do not have an explicit wsaw:Action</li>
ohair@286 136 * <li>Patch the default value of wsaw:Anonymous=optional if none is specified</li>
ohair@286 137 * </ul>
ohair@286 138 * @param context
ohair@286 139 */
ohair@286 140 @Override
ohair@286 141 public void finished(WSDLParserExtensionContext context) {
ohair@286 142 WSDLModel model = context.getWSDLModel();
ohair@286 143 for (WSDLService service : model.getServices().values()) {
ohair@286 144 for (WSDLPort wp : service.getPorts()) {
ohair@286 145 WSDLPortImpl port = (WSDLPortImpl)wp;
ohair@286 146 WSDLBoundPortTypeImpl binding = port.getBinding();
ohair@286 147
ohair@286 148 // populate actions for the messages that do not have an explicit wsaw:Action
ohair@286 149 populateActions(binding);
ohair@286 150
ohair@286 151 // patch the default value of wsaw:Anonymous=optional if none is specified
ohair@286 152 patchAnonymousDefault(binding);
ohair@286 153 }
ohair@286 154 }
ohair@286 155 }
ohair@286 156
ohair@286 157 protected String getNamespaceURI() {
ohair@286 158 return AddressingVersion.W3C.wsdlNsUri;
ohair@286 159 }
ohair@286 160
ohair@286 161 protected QName getWsdlActionTag() {
ohair@286 162 return AddressingVersion.W3C.wsdlActionTag;
ohair@286 163 }
ohair@286 164 /**
ohair@286 165 * Populate all the Actions
ohair@286 166 *
ohair@286 167 * @param binding soapbinding:operation
ohair@286 168 */
ohair@286 169 private void populateActions(WSDLBoundPortTypeImpl binding) {
ohair@286 170 WSDLPortTypeImpl porttype = binding.getPortType();
ohair@286 171 for (WSDLOperationImpl o : porttype.getOperations()) {
ohair@286 172 // TODO: this may be performance intensive. Alternatively default action
ohair@286 173 // TODO: can be calculated when the operation is actually invoked.
ohair@286 174 WSDLBoundOperationImpl wboi = binding.get(o.getName());
ohair@286 175
ohair@286 176 if (wboi == null) {
ohair@286 177 //If this operation is unbound set the action to default
ohair@286 178 o.getInput().setAction(defaultInputAction(o));
ohair@286 179 continue;
ohair@286 180 }
ohair@286 181 String soapAction = wboi.getSOAPAction();
ohair@286 182 if (o.getInput().getAction() == null || o.getInput().getAction().equals("")) {
ohair@286 183 // explicit wsaw:Action is not specified
ohair@286 184
ohair@286 185 if (soapAction != null && !soapAction.equals("")) {
ohair@286 186 // if soapAction is non-empty, use that
ohair@286 187 o.getInput().setAction(soapAction);
ohair@286 188 } else {
ohair@286 189 // otherwise generate default Action
ohair@286 190 o.getInput().setAction(defaultInputAction(o));
ohair@286 191 }
ohair@286 192 }
ohair@286 193
ohair@286 194 // skip output and fault processing for one-way methods
ohair@286 195 if (o.getOutput() == null)
ohair@286 196 continue;
ohair@286 197
ohair@286 198 if (o.getOutput().getAction() == null || o.getOutput().getAction().equals("")) {
ohair@286 199 o.getOutput().setAction(defaultOutputAction(o));
ohair@286 200 }
ohair@286 201
ohair@286 202 if (o.getFaults() == null || !o.getFaults().iterator().hasNext())
ohair@286 203 continue;
ohair@286 204
ohair@286 205 for (WSDLFault f : o.getFaults()) {
ohair@286 206 if (f.getAction() == null || f.getAction().equals("")) {
ohair@286 207 ((WSDLFaultImpl)f).setAction(defaultFaultAction(f.getName(), o));
ohair@286 208 }
ohair@286 209
ohair@286 210 }
ohair@286 211 }
ohair@286 212 }
ohair@286 213
ohair@286 214 /**
ohair@286 215 * Patch the default value of wsaw:Anonymous=optional if none is specified
ohair@286 216 *
ohair@286 217 * @param binding WSDLBoundPortTypeImpl
ohair@286 218 */
ohair@286 219 protected void patchAnonymousDefault(WSDLBoundPortTypeImpl binding) {
ohair@286 220 for (WSDLBoundOperationImpl wbo : binding.getBindingOperations()) {
ohair@286 221 if (wbo.getAnonymous() == null)
ohair@286 222 wbo.setAnonymous(WSDLBoundOperation.ANONYMOUS.optional);
ohair@286 223 }
ohair@286 224 }
ohair@286 225
ohair@286 226 private String defaultInputAction(WSDLOperation o) {
ohair@286 227 return buildAction(o.getInput().getName(), o, false);
ohair@286 228 }
ohair@286 229
ohair@286 230 private String defaultOutputAction(WSDLOperation o) {
ohair@286 231 return buildAction(o.getOutput().getName(), o, false);
ohair@286 232 }
ohair@286 233
ohair@286 234 private String defaultFaultAction(String name, WSDLOperation o) {
ohair@286 235 return buildAction(name, o, true);
ohair@286 236 }
ohair@286 237
ohair@286 238 protected static final String buildAction(String name, WSDLOperation o, boolean isFault) {
ohair@286 239 String tns = o.getName().getNamespaceURI();
ohair@286 240
ohair@286 241 String delim = SLASH_DELIMITER;
ohair@286 242
ohair@286 243 // TODO: is this the correct way to find the separator ?
ohair@286 244 if (!tns.startsWith("http"))
ohair@286 245 delim = COLON_DELIMITER;
ohair@286 246
ohair@286 247 if (tns.endsWith(delim))
ohair@286 248 tns = tns.substring(0, tns.length()-1);
ohair@286 249
ohair@286 250 if (o.getPortTypeName() == null)
ohair@286 251 throw new WebServiceException("\"" + o.getName() + "\" operation's owning portType name is null.");
ohair@286 252
ohair@286 253 return tns +
ohair@286 254 delim +
ohair@286 255 o.getPortTypeName().getLocalPart() +
ohair@286 256 delim +
ohair@286 257 (isFault ? o.getName().getLocalPart() + delim + "Fault" + delim : "") +
ohair@286 258 name;
ohair@286 259 }
ohair@286 260
ohair@286 261 protected static final String COLON_DELIMITER = ":";
ohair@286 262 protected static final String SLASH_DELIMITER = "/";
ohair@286 263 }

mercurial