View Javadoc
1   /*
2    * Copyright (c) 1997, 2013, 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   */
25  
26  package com.sun.xml.internal.ws.wsdl.parser;
27  
28  import com.sun.xml.internal.ws.api.addressing.AddressingVersion;
29  import com.sun.xml.internal.ws.api.model.wsdl.WSDLFeaturedObject;
30  import static com.sun.xml.internal.ws.api.model.wsdl.WSDLBoundOperation.ANONYMOUS;
31  import com.sun.xml.internal.ws.api.model.wsdl.editable.*;
32  import com.sun.xml.internal.ws.api.wsdl.parser.WSDLParserExtension;
33  import com.sun.xml.internal.ws.api.wsdl.parser.WSDLParserExtensionContext;
34  import com.sun.xml.internal.ws.streaming.XMLStreamReaderUtil;
35  
36  import javax.xml.namespace.QName;
37  import javax.xml.stream.XMLStreamException;
38  import javax.xml.stream.XMLStreamReader;
39  import javax.xml.ws.WebServiceException;
40  import javax.xml.ws.soap.AddressingFeature;
41  
42  /**
43   * W3C WS-Addressing Runtime WSDL parser extension
44   *
45   * @author Arun Gupta
46   */
47  public class W3CAddressingWSDLParserExtension extends WSDLParserExtension {
48      @Override
49      public boolean bindingElements(EditableWSDLBoundPortType binding, XMLStreamReader reader) {
50          return addressibleElement(reader, binding);
51      }
52  
53      @Override
54      public boolean portElements(EditableWSDLPort port, XMLStreamReader reader) {
55          return addressibleElement(reader, port);
56      }
57  
58      private boolean addressibleElement(XMLStreamReader reader, WSDLFeaturedObject binding) {
59          QName ua = reader.getName();
60          if (ua.equals(AddressingVersion.W3C.wsdlExtensionTag)) {
61              String required = reader.getAttributeValue(WSDLConstants.NS_WSDL, "required");
62              binding.addFeature(new AddressingFeature(true, Boolean.parseBoolean(required)));
63              XMLStreamReaderUtil.skipElement(reader);
64              return true;        // UsingAddressing is consumed
65          }
66  
67          return false;
68      }
69  
70      @Override
71      public boolean bindingOperationElements(EditableWSDLBoundOperation operation, XMLStreamReader reader) {
72          EditableWSDLBoundOperation edit = (EditableWSDLBoundOperation) operation;
73  
74          QName anon = reader.getName();
75          if (anon.equals(AddressingVersion.W3C.wsdlAnonymousTag)) {
76              try {
77                  String value = reader.getElementText();
78                  if (value == null || value.trim().equals("")) {
79                      throw new WebServiceException("Null values not permitted in wsaw:Anonymous.");
80                      // TODO: throw exception only if wsdl:required=true
81                      // TODO: is this the right exception ?
82                  } else if (value.equals("optional")) {
83                      edit.setAnonymous(ANONYMOUS.optional);
84                  } else if (value.equals("required")) {
85                      edit.setAnonymous(ANONYMOUS.required);
86                  } else if (value.equals("prohibited")) {
87                      edit.setAnonymous(ANONYMOUS.prohibited);
88                  } else {
89                      throw new WebServiceException("wsaw:Anonymous value \"" + value + "\" not understood.");
90                      // TODO: throw exception only if wsdl:required=true
91                      // TODO: is this the right exception ?
92                  }
93              } catch (XMLStreamException e) {
94                  throw new WebServiceException(e);       // TODO: is this the correct behavior ?
95              }
96  
97              return true;        // consumed the element
98          }
99  
100         return false;
101     }
102 
103     public void portTypeOperationInputAttributes(EditableWSDLInput input, XMLStreamReader reader) {
104        String action = ParserUtil.getAttribute(reader, getWsdlActionTag());
105        if (action != null) {
106             input.setAction(action);
107             input.setDefaultAction(false);
108         }
109     }
110 
111 
112     public void portTypeOperationOutputAttributes(EditableWSDLOutput output, XMLStreamReader reader) {
113        String action = ParserUtil.getAttribute(reader, getWsdlActionTag());
114        if (action != null) {
115             output.setAction(action);
116             output.setDefaultAction(false);
117         }
118     }
119 
120 
121     public void portTypeOperationFaultAttributes(EditableWSDLFault fault, XMLStreamReader reader) {
122         String action = ParserUtil.getAttribute(reader, getWsdlActionTag());
123         if (action != null) {
124             fault.setAction(action);
125             fault.setDefaultAction(false);
126         }
127     }
128 
129     /**
130      * Process wsdl:portType operation after the entire WSDL model has been populated.
131      * The task list includes: <p>
132      * <ul>
133      * <li>Patch the value of UsingAddressing in wsdl:port and wsdl:binding</li>
134      * <li>Populate actions for the messages that do not have an explicit wsaw:Action</li>
135      * <li>Patch the default value of wsaw:Anonymous=optional if none is specified</li>
136      * </ul>
137      * @param context
138      */
139     @Override
140     public void finished(WSDLParserExtensionContext context) {
141         EditableWSDLModel model = context.getWSDLModel();
142         for (EditableWSDLService service : model.getServices().values()) {
143             for (EditableWSDLPort port : service.getPorts()) {
144                 EditableWSDLBoundPortType binding = port.getBinding();
145 
146                 // populate actions for the messages that do not have an explicit wsaw:Action
147                 populateActions(binding);
148 
149                 // patch the default value of wsaw:Anonymous=optional if none is specified
150                 patchAnonymousDefault(binding);
151             }
152         }
153     }
154 
155     protected String getNamespaceURI() {
156         return AddressingVersion.W3C.wsdlNsUri;
157     }
158 
159     protected QName getWsdlActionTag() {
160        return AddressingVersion.W3C.wsdlActionTag;
161     }
162     /**
163      * Populate all the Actions
164      *
165      * @param binding soapbinding:operation
166      */
167     private void populateActions(EditableWSDLBoundPortType binding) {
168         EditableWSDLPortType porttype = binding.getPortType();
169         for (EditableWSDLOperation o : porttype.getOperations()) {
170             // TODO: this may be performance intensive. Alternatively default action
171             // TODO: can be calculated when the operation is actually invoked.
172                 EditableWSDLBoundOperation wboi = binding.get(o.getName());
173 
174             if (wboi == null) {
175                 //If this operation is unbound set the action to default
176                 o.getInput().setAction(defaultInputAction(o));
177                 continue;
178             }
179                 String soapAction = wboi.getSOAPAction();
180             if (o.getInput().getAction() == null || o.getInput().getAction().equals("")) {
181                 // explicit wsaw:Action is not specified
182 
183                 if (soapAction != null && !soapAction.equals("")) {
184                     // if soapAction is non-empty, use that
185                     o.getInput().setAction(soapAction);
186                 } else {
187                     // otherwise generate default Action
188                     o.getInput().setAction(defaultInputAction(o));
189                 }
190             }
191 
192             // skip output and fault processing for one-way methods
193             if (o.getOutput() == null)
194                 continue;
195 
196             if (o.getOutput().getAction() == null || o.getOutput().getAction().equals("")) {
197                 o.getOutput().setAction(defaultOutputAction(o));
198             }
199 
200             if (o.getFaults() == null || !o.getFaults().iterator().hasNext())
201                 continue;
202 
203             for (EditableWSDLFault f : o.getFaults()) {
204                 if (f.getAction() == null || f.getAction().equals("")) {
205                     f.setAction(defaultFaultAction(f.getName(), o));
206                 }
207 
208             }
209         }
210     }
211 
212     /**
213      * Patch the default value of wsaw:Anonymous=optional if none is specified
214      *
215      * @param binding WSDLBoundPortTypeImpl
216      */
217     protected void patchAnonymousDefault(EditableWSDLBoundPortType binding) {
218         for (EditableWSDLBoundOperation wbo : binding.getBindingOperations()) {
219             if (wbo.getAnonymous() == null)
220                 wbo.setAnonymous(ANONYMOUS.optional);
221         }
222     }
223 
224     private String defaultInputAction(EditableWSDLOperation o) {
225         return buildAction(o.getInput().getName(), o, false);
226     }
227 
228     private String defaultOutputAction(EditableWSDLOperation o) {
229         return buildAction(o.getOutput().getName(), o, false);
230     }
231 
232     private String defaultFaultAction(String name, EditableWSDLOperation o) {
233         return buildAction(name, o, true);
234     }
235 
236     protected static final String buildAction(String name, EditableWSDLOperation o, boolean isFault) {
237         String tns = o.getName().getNamespaceURI();
238 
239         String delim = SLASH_DELIMITER;
240 
241         // TODO: is this the correct way to find the separator ?
242         if (!tns.startsWith("http"))
243             delim = COLON_DELIMITER;
244 
245         if (tns.endsWith(delim))
246             tns = tns.substring(0, tns.length()-1);
247 
248         if (o.getPortTypeName() == null)
249             throw new WebServiceException("\"" + o.getName() + "\" operation's owning portType name is null.");
250 
251         return tns +
252             delim +
253             o.getPortTypeName().getLocalPart() +
254             delim +
255             (isFault ? o.getName().getLocalPart() + delim + "Fault" + delim : "") +
256             name;
257     }
258 
259     protected static final String COLON_DELIMITER = ":";
260     protected static final String SLASH_DELIMITER = "/";
261 }