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  
29  import com.sun.xml.internal.ws.api.model.wsdl.editable.*;
30  import com.sun.xml.internal.ws.api.wsdl.parser.WSDLParserExtension;
31  
32  import javax.xml.namespace.QName;
33  import javax.xml.stream.XMLStreamConstants;
34  import javax.xml.stream.XMLStreamReader;
35  
36  /**
37   * {@link WSDLParserExtension} filter that checks if
38   * another {@link WSDLParserExtension} is following the contract.
39   *
40   * <p>
41   * This code protects the JAX-WS RI from broken extensions.
42   *
43   * <p>
44   * For now it just checks if {@link XMLStreamReader} is placed
45   * at the expected start/end element.
46   *
47   * @author Kohsuke Kawaguchi
48   */
49  final class FoolProofParserExtension extends DelegatingParserExtension {
50  
51      public FoolProofParserExtension(WSDLParserExtension core) {
52          super(core);
53      }
54  
55      private QName pre(XMLStreamReader xsr) {
56          return xsr.getName();
57      }
58  
59      private boolean post(QName tagName, XMLStreamReader xsr, boolean result) {
60          if(!tagName.equals(xsr.getName()))
61              return foundFool();
62          if(result) {
63              if(xsr.getEventType()!=XMLStreamConstants.END_ELEMENT)
64                  foundFool();
65          } else {
66              if(xsr.getEventType()!=XMLStreamConstants.START_ELEMENT)
67                  foundFool();
68          }
69          return result;
70      }
71  
72      private boolean foundFool() {
73          throw new AssertionError("XMLStreamReader is placed at the wrong place after invoking "+core);
74      }
75  
76      public boolean serviceElements(EditableWSDLService service, XMLStreamReader reader) {
77          return post(pre(reader),reader,super.serviceElements(service, reader));
78      }
79  
80      public boolean portElements(EditableWSDLPort port, XMLStreamReader reader) {
81          return post(pre(reader),reader,super.portElements(port, reader));
82      }
83  
84      public boolean definitionsElements(XMLStreamReader reader) {
85          return post(pre(reader),reader,super.definitionsElements(reader));
86      }
87  
88      public boolean bindingElements(EditableWSDLBoundPortType binding, XMLStreamReader reader) {
89          return post(pre(reader),reader,super.bindingElements(binding, reader));
90      }
91  
92      public boolean portTypeElements(EditableWSDLPortType portType, XMLStreamReader reader) {
93          return post(pre(reader),reader,super.portTypeElements(portType, reader));
94      }
95  
96      public boolean portTypeOperationElements(EditableWSDLOperation operation, XMLStreamReader reader) {
97          return post(pre(reader),reader,super.portTypeOperationElements(operation, reader));
98      }
99  
100     public boolean bindingOperationElements(EditableWSDLBoundOperation operation, XMLStreamReader reader) {
101         return post(pre(reader),reader,super.bindingOperationElements(operation, reader));
102     }
103 
104     public boolean messageElements(EditableWSDLMessage msg, XMLStreamReader reader) {
105         return post(pre(reader),reader,super.messageElements(msg, reader));
106     }
107 
108     public boolean portTypeOperationInputElements(EditableWSDLInput input, XMLStreamReader reader) {
109         return post(pre(reader),reader,super.portTypeOperationInputElements(input, reader));
110     }
111 
112     public boolean portTypeOperationOutputElements(EditableWSDLOutput output, XMLStreamReader reader) {
113         return post(pre(reader),reader,super.portTypeOperationOutputElements(output, reader));
114     }
115 
116     public boolean portTypeOperationFaultElements(EditableWSDLFault fault, XMLStreamReader reader) {
117         return post(pre(reader),reader,super.portTypeOperationFaultElements(fault, reader));
118     }
119 
120     public boolean bindingOperationInputElements(EditableWSDLBoundOperation operation, XMLStreamReader reader) {
121         return super.bindingOperationInputElements(operation, reader);
122     }
123 
124     public boolean bindingOperationOutputElements(EditableWSDLBoundOperation operation, XMLStreamReader reader) {
125         return post(pre(reader),reader,super.bindingOperationOutputElements(operation, reader));
126     }
127 
128     public boolean bindingOperationFaultElements(EditableWSDLBoundFault fault, XMLStreamReader reader) {
129         return post(pre(reader),reader,super.bindingOperationFaultElements(fault, reader));
130     }
131 }