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
26package com.sun.xml.internal.ws.api.message;
27
28import com.sun.istack.internal.NotNull;
29import com.sun.xml.internal.bind.api.Bridge;
30import com.sun.xml.internal.bind.api.JAXBRIContext;
31import com.sun.xml.internal.bind.v2.runtime.MarshallerImpl;
32import com.sun.xml.internal.ws.api.SOAPVersion;
33import com.sun.xml.internal.ws.api.pipe.Pipe;
34import com.sun.xml.internal.ws.message.DOMHeader;
35import com.sun.xml.internal.ws.message.StringHeader;
36import com.sun.xml.internal.ws.message.jaxb.JAXBHeader;
37import com.sun.xml.internal.ws.message.saaj.SAAJHeader;
38import com.sun.xml.internal.ws.message.stream.StreamHeader11;
39import com.sun.xml.internal.ws.message.stream.StreamHeader12;
40import com.sun.xml.internal.ws.spi.db.BindingContext;
41import com.sun.xml.internal.ws.spi.db.BindingContextFactory;
42import com.sun.xml.internal.ws.spi.db.XMLBridge;
43
44import org.w3c.dom.Element;
45
46import javax.xml.bind.JAXBContext;
47import javax.xml.bind.JAXBElement;
48import javax.xml.bind.Marshaller;
49import javax.xml.namespace.QName;
50import javax.xml.soap.SOAPHeaderElement;
51import javax.xml.stream.XMLStreamException;
52import javax.xml.stream.XMLStreamReader;
53
54/**
55 * Factory methods for various {@link Header} implementations.
56 *
57 * <p>
58 * This class provides various methods to create different
59 * flavors of {@link Header} classes that store data
60 * in different formats.
61 *
62 * <p>
63 * This is a part of the JAX-WS RI internal API so that
64 * {@link Pipe} implementations can reuse the implementations
65 * done inside the JAX-WS without having a strong dependency
66 * to the actual class.
67 *
68 * <p>
69 * If you find some of the useful convenience methods missing
70 * from this class, please talk to us.
71 *
72 *
73 * @author Kohsuke Kawaguchi
74 */
75public abstract class Headers {
76    private Headers() {}
77
78    /**
79     * @deprecated
80     *      Use {@link #create(BindingContext, Object)} instead.
81     */
82    public static Header create(SOAPVersion soapVersion, Marshaller m, Object o) {
83        return new JAXBHeader(BindingContextFactory.getBindingContext(m),o);
84    }
85
86    /**
87     * Creates a {@link Header} backed a by a JAXB bean.
88     */
89    public static Header create(JAXBContext context, Object o) {
90        return new JAXBHeader(BindingContextFactory.create(context),o);
91    }
92
93    public static Header create(BindingContext context, Object o) {
94        return new JAXBHeader(context,o);
95    }
96
97    /**
98     * Creates a {@link Header} backed a by a JAXB bean, with the given tag name.
99     *
100     * See {@link #create(SOAPVersion, Marshaller, Object)} for the meaning
101     * of other parameters.
102     *
103     * @param tagName
104     *      The name of the newly created header. Must not be null.
105     * @param o
106     *      The JAXB bean that represents the contents of the header. Must not be null.
107     */
108    public static Header create(SOAPVersion soapVersion, Marshaller m, QName tagName, Object o) {
109        return create(soapVersion,m,new JAXBElement(tagName,o.getClass(),o));
110    }
111
112    /**
113     * Creates a {@link Header} backed a by a JAXB bean.
114     * @deprecated
115     */
116    public static Header create(Bridge bridge, Object jaxbObject) {
117        return new JAXBHeader(new com.sun.xml.internal.ws.db.glassfish.BridgeWrapper(null,bridge), jaxbObject);
118    }
119
120    public static Header create(XMLBridge bridge, Object jaxbObject) {
121        return new JAXBHeader(bridge, jaxbObject);
122    }
123
124    /**
125     * Creates a new {@link Header} backed by a SAAJ object.
126     */
127    public static Header create(SOAPHeaderElement header) {
128        return new SAAJHeader(header);
129    }
130
131    /**
132     * Creates a new {@link Header} backed by an {@link Element}.
133     */
134    public static Header create( Element node ) {
135        return new DOMHeader<Element>(node);
136    }
137
138    /**
139     * @deprecated
140     *      Use {@link #create(Element)}
141     */
142    public static Header create( SOAPVersion soapVersion, Element node ) {
143        return create(node);
144    }
145
146    /**
147     * Creates a new {@link Header} that reads from {@link XMLStreamReader}.
148     *
149     * <p>
150     * Note that the header implementation will read the entire data
151     * into memory anyway, so this might not be as efficient as you might hope.
152     */
153    public static Header create( SOAPVersion soapVersion, XMLStreamReader reader ) throws XMLStreamException {
154        switch(soapVersion) {
155        case SOAP_11:
156            return new StreamHeader11(reader);
157        case SOAP_12:
158            return new StreamHeader12(reader);
159        default:
160            throw new AssertionError();
161        }
162    }
163
164    /**
165     * Creates a new {@link Header} that that has a single text value in it
166     * (IOW, of the form &lt;foo>text&lt;/foo>.)
167     *
168     * @param name QName of the header element
169     * @param value text value of the header
170     */
171    public static Header create(QName name, String value) {
172        return new StringHeader(name, value);
173    }
174
175    /**
176     * Creates a new {@link Header} that that has a single text value in it
177     * (IOW, of the form &lt;foo>text&lt;/foo>.)
178     *
179     * @param name QName of the header element
180     * @param value text value of the header
181     */
182    public static Header createMustUnderstand(@NotNull SOAPVersion soapVersion, @NotNull QName name,@NotNull String value) {
183        return new StringHeader(name, value,soapVersion,true);
184    }
185}
186