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