View Javadoc
1   /*
2    * Copyright (c) 1997, 2012, 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.api.message;
27  
28  import com.sun.istack.internal.NotNull;
29  import com.sun.istack.internal.Nullable;
30  import com.sun.xml.internal.bind.api.Bridge;
31  import com.sun.xml.internal.ws.api.SOAPVersion;
32  import com.sun.xml.internal.ws.api.addressing.AddressingVersion;
33  import com.sun.xml.internal.ws.api.addressing.WSEndpointReference;
34  import com.sun.xml.internal.ws.spi.db.XMLBridge;
35  
36  import org.xml.sax.ContentHandler;
37  import org.xml.sax.ErrorHandler;
38  import org.xml.sax.SAXException;
39  import org.xml.sax.SAXParseException;
40  
41  import javax.xml.bind.JAXBException;
42  import javax.xml.bind.Unmarshaller;
43  import javax.xml.namespace.QName;
44  import javax.xml.soap.SOAPException;
45  import javax.xml.soap.SOAPMessage;
46  import javax.xml.stream.XMLStreamException;
47  import javax.xml.stream.XMLStreamReader;
48  import javax.xml.stream.XMLStreamWriter;
49  import javax.xml.ws.WebServiceException;
50  import java.util.Set;
51  
52  
53  /**
54   * A SOAP header.
55   *
56   * <p>
57   * A header is immutable, but unlike body it can be read
58   * multiple times.
59   * The {@link Header} abstraction hides how the header
60   * data is represented in memory; instead, it commits to
61   * the ability to write itself to XML infoset.
62   *
63   * <p>
64   * When a message is received from the transport and
65   * being processed, the processor needs to "peek"
66   * some information of a header, such as the tag name,
67   * the mustUnderstand attribute, and so on. Therefore,
68   * the {@link Header} interface exposes those information
69   * as properties, so that they can be checked without
70   * replaying the infoset, which is efficiently but still
71   * costly.
72   *
73   * <p>
74   * A {@link Header} may belong to more than one {@link HeaderList}
75   * due to wrapping of {@link Message}.
76   *
77   * @see HeaderList
78   * @see Headers
79   */
80  public interface Header {
81      // TODO: Vivek pointed out that the only time we are looking at
82      // mustUnderstand and role are when we do the mustUnderstand error check
83      // (that is, to find out if there's any header with @mustUnderstand that
84      // has appropriate role for us.)
85      // if that's the case, it might be better if we define this whole operation
86      // as one method, instead of exposing two properties.
87  
88      /**
89       * Checks if this header is ignorable for us (IOW, make sure
90       * that this header has a problematic "mustUnderstand" header value
91       * that we have to reject.)
92       *
93       * <p>
94       * This method is used as a part of the
95       * <a href="HeaderList.html#MU">mustUnderstanx processing</a>.
96       * At the end of the processing, the JAX-WS identifies a list of {@link Header}s
97       * that were not understood. This method is invoked on those {@link Header}s,
98       * to verify that we don't need to report an error for it.
99       *
100      * <p>
101      * specifically, this method has to perform the following tasks:
102      *
103      * <ul>
104      *  <li>If this header does not have <tt>mustUnderstand</tt> as "1" nor "true",
105      *      then this method must return true.
106      *  <li>Otherwise, check the role attribute (for SOAP 1.2) or the actor attribute (for SOAP 1.1).
107      *      When those attributes are absent, the default values have to be assumed.
108      *      See {@link #getRole(SOAPVersion)} for how the values are defaulted.
109      *      Now, see if the {@code roles} set contains the value.
110      *      If so, this method must return false (indicating that an error is in order.)
111      *  <li>Otherwise return true (since we don't play the role this header is intended for.)
112      * </ul>
113      *
114      * @param soapVersion
115      *      The caller specifies the SOAP version that the pipeline is working against.
116      *      Often each {@link Header} implementation already knows the SOAP version
117      *      anyway, but this allows some {@link Header}s to avoid keeping it.
118      *      That's why this redundant parameter is passed in.
119      * @param roles
120      *      The set of role values that the current JAX-WS pipeline is assuming.
121      *      Note that SOAP 1.1 and SOAP 1.2 use different strings for the same role,
122      *      and the caller is responsible for supplying a proper value depending on the
123      *      active SOAP version in use.
124      *
125      * @return
126      *      true if no error needs to be reported. False if an error needs to be raised.
127      *      See the method javadoc for more discussion.
128      */
129     public boolean isIgnorable(@NotNull SOAPVersion soapVersion, @NotNull Set<String> roles);
130 
131     /**
132      * Gets the value of the soap:role attribute (or soap:actor for SOAP 1.1).
133      *
134      * <p>
135      * If the attribute is omitted, the value defaults to {@link SOAPVersion#implicitRole}.
136      *
137      * @param soapVersion
138      *      The caller specifies the SOAP version that the pipeline is working against.
139      *      Often each {@link Header} implementation already knows the SOAP version
140      *      anyway, but this allows some {@link Header}s to avoid keeping it.
141      *      That's why this redundant parameter is passed in.
142      * @return
143      *      never null. This string need not be interned.
144      */
145     public @NotNull String getRole(@NotNull SOAPVersion soapVersion);
146 
147     /**
148      * True if this header is to be relayed if not processed.
149      * For SOAP 1.1 messages, this method always return false.
150      *
151      * <p>
152      * IOW, this method returns true if there's @soap:relay='true'
153      * is present.
154      *
155      * <h3>Implementation Note</h3>
156      * <p>
157      * The implementation needs to check for both "true" and "1",
158      * but because attribute values are normalized, it doesn't have
159      * to consider " true", " 1 ", and so on.
160      *
161      * @return
162      *      false.
163      */
164     public boolean isRelay();
165 
166     /**
167      * Gets the namespace URI of this header element.
168      *
169      * @return
170      *      this string must be interned.
171      */
172     public @NotNull String getNamespaceURI();
173 
174     /**
175      * Gets the local name of this header element.
176      *
177      * @return
178      *      this string must be interned.
179      */
180     public @NotNull String getLocalPart();
181 
182     /**
183      * Gets the attribute value on the header element.
184      *
185      * @param nsUri
186      *      The namespace URI of the attribute. Can be empty.
187      * @param localName
188      *      The local name of the attribute.
189      *
190      * @return
191      *      if the attribute is found, return the whitespace normalized value.
192      *      (meaning no leading/trailing space, no consequtive whitespaces in-between.)
193      *      Otherwise null. Note that the XML parsers are responsible for
194      *      whitespace-normalizing attributes, so {@link Header} implementation
195      *      doesn't have to do anything.
196      */
197     @Nullable String getAttribute(@NotNull String nsUri, @NotNull String localName);
198 
199     /**
200      * Gets the attribute value on the header element.
201      *
202      * <p>
203      * This is a convenience method that calls into {@link #getAttribute(String, String)}
204      *
205      * @param name
206      *      Never null.
207      *
208      * @see #getAttribute(String, String)
209      */
210     @Nullable String getAttribute(@NotNull QName name);
211 
212     /**
213      * Reads the header as a {@link XMLStreamReader}.
214      *
215      * <p>
216      * The returned parser points at the start element of this header.
217      * (IOW, {@link XMLStreamReader#getEventType()} would return
218      * {@link XMLStreamReader#START_ELEMENT}.
219      *
220      * <h3>Performance Expectation</h3>
221      * <p>
222      * For some {@link Header} implementations, this operation
223      * is a non-trivial operation. Therefore, use of this method
224      * is discouraged unless the caller is interested in reading
225      * the whole header.
226      *
227      * <p>
228      * Similarly, if the caller wants to use this method only to do
229      * the API conversion (such as simply firing SAX events from
230      * {@link XMLStreamReader}), then the JAX-WS team requests
231      * that you talk to us.
232      *
233      * <p>
234      * {@link Message}s that come from tranport usually provides
235      * a reasonably efficient implementation of this method.
236      *
237      * @return
238      *      must not null.
239      */
240     public XMLStreamReader readHeader() throws XMLStreamException;
241 
242     /**
243      * Reads the header as a JAXB object by using the given unmarshaller.
244      */
245     public <T> T readAsJAXB(Unmarshaller unmarshaller) throws JAXBException;
246 
247     /**
248      * Reads the header as a JAXB object by using the given unmarshaller.
249      * @deprecated
250      */
251     public <T> T readAsJAXB(Bridge<T> bridge) throws JAXBException;
252 
253     /**
254      * Reads the header as a data-bond object
255      */
256     public <T> T readAsJAXB(XMLBridge<T> bridge) throws JAXBException;
257 
258     /**
259      * Reads this header as an {@link WSEndpointReference}.
260      *
261      * @param expected
262      *      The version of the addressing used to parse the EPR.
263      *      If the actual infoset and this doesn't agree, then
264      *      you'll get an {@link WebServiceException} stating that fact.
265      *
266      * @return
267      *      On a successful return, this method never returns null.
268      */
269     public @NotNull WSEndpointReference readAsEPR(AddressingVersion expected) throws XMLStreamException;
270 
271     /**
272      * Writes out the header as a fragment.
273      *
274      * @throws XMLStreamException
275      *      if the operation fails for some reason. This leaves the
276      *      writer to an undefined state.
277      */
278     public void writeTo(XMLStreamWriter w) throws XMLStreamException;
279 
280     /**
281      * Writes out the header to the given SOAPMessage.
282      *
283      * <p>
284      * Sometimes a {@link Message} needs to produce itself
285      * as {@link SOAPMessage}, in which case each header needs
286      * to turn itself into a header.
287      *
288      * @throws SOAPException
289      *      if the operation fails for some reason. This leaves the
290      *      writer to an undefined state.
291      */
292     public void writeTo(SOAPMessage saaj) throws SOAPException;
293 
294     /**
295      * Writes out the header as SAX events.
296      *
297      * <p>
298      * Sometimes a {@link Message} needs to produce SAX events,
299      * and this method is necessary for headers to participate to it.
300      *
301      * <p>
302      * A header is responsible for producing the SAX events for its part,
303      * including <tt>startPrefixMapping</tt> and <tt>endPrefixMapping</tt>,
304      * but not startDocument/endDocument.
305      *
306      * <p>
307      * Note that SAX contract requires that any error that does NOT originate
308      * from {@link ContentHandler} (meaning any parsing error and etc) must
309      * be first reported to {@link ErrorHandler}. If the SAX event production
310      * cannot be continued and the processing needs to abort, the code may
311      * then throw the same {@link SAXParseException} reported to {@link ErrorHandler}.
312      *
313      * @param contentHandler
314      *      The {@link ContentHandler} that receives SAX events.
315      *
316      * @param errorHandler
317      *      The {@link ErrorHandler} that receives parsing errors.
318      */
319     public void writeTo(ContentHandler contentHandler, ErrorHandler errorHandler) throws SAXException;
320 
321     /**
322      * Used to obtain value XYZ from a header that looks like
323      * "&lt;header&gt;XYZ&lt;/header&gt;". The primary use of this header
324      * for now is to access certain Addressing headers quickly.
325      *
326      * @throws WebServiceException
327      *      If the structure of the header is more complicated than
328      *      a simple string header.
329      *
330      * @return
331      *      Can be empty but always non-null.
332      */
333     public @NotNull String getStringContent();
334 }