001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.ws.api.message;
038:
039: import com.sun.xml.bind.api.Bridge;
040: import com.sun.xml.bind.api.JAXBRIContext;
041: import com.sun.xml.bind.v2.runtime.MarshallerImpl;
042: import com.sun.xml.ws.api.SOAPVersion;
043: import com.sun.xml.ws.api.pipe.Pipe;
044: import com.sun.xml.ws.message.DOMHeader;
045: import com.sun.xml.ws.message.StringHeader;
046: import com.sun.xml.ws.message.jaxb.JAXBHeader;
047: import com.sun.xml.ws.message.saaj.SAAJHeader;
048: import com.sun.xml.ws.message.stream.StreamHeader11;
049: import com.sun.xml.ws.message.stream.StreamHeader12;
050: import org.w3c.dom.Element;
051:
052: import javax.xml.bind.JAXBElement;
053: import javax.xml.bind.Marshaller;
054: import javax.xml.namespace.QName;
055: import javax.xml.soap.SOAPHeaderElement;
056: import javax.xml.stream.XMLStreamException;
057: import javax.xml.stream.XMLStreamReader;
058:
059: /**
060: * Factory methods for various {@link Header} implementations.
061: *
062: * <p>
063: * This class provides various methods to create different
064: * flavors of {@link Header} classes that store data
065: * in different formats.
066: *
067: * <p>
068: * This is a part of the JAX-WS RI internal API so that
069: * {@link Pipe} implementations can reuse the implementations
070: * done inside the JAX-WS without having a strong dependency
071: * to the actual class.
072: *
073: * <p>
074: * If you find some of the useful convenience methods missing
075: * from this class, please talk to us.
076: *
077: *
078: * @author Kohsuke Kawaguchi
079: */
080: public abstract class Headers {
081: private Headers() {
082: }
083:
084: /**
085: * @deprecated
086: * Use {@link #create(JAXBRIContext, Object)} instead.
087: */
088: public static Header create(SOAPVersion soapVersion, Marshaller m,
089: Object o) {
090: return new JAXBHeader(((MarshallerImpl) m).getContext(), o);
091: }
092:
093: /**
094: * Creates a {@link Header} backed a by a JAXB bean.
095: */
096: public static Header create(JAXBRIContext context, Object o) {
097: return new JAXBHeader(context, o);
098: }
099:
100: /**
101: * Creates a {@link Header} backed a by a JAXB bean, with the given tag name.
102: *
103: * See {@link #create(SOAPVersion, Marshaller, Object)} for the meaning
104: * of other parameters.
105: *
106: * @param tagName
107: * The name of the newly created header. Must not be null.
108: * @param o
109: * The JAXB bean that represents the contents of the header. Must not be null.
110: */
111: public static Header create(SOAPVersion soapVersion, Marshaller m,
112: QName tagName, Object o) {
113: return create(soapVersion, m, new JAXBElement(tagName, o
114: .getClass(), o));
115: }
116:
117: /**
118: * Creates a {@link Header} backed a by a JAXB bean.
119: */
120: public static Header create(Bridge 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,
154: XMLStreamReader reader) throws XMLStreamException {
155: switch (soapVersion) {
156: case SOAP_11:
157: return new StreamHeader11(reader);
158: case SOAP_12:
159: return new StreamHeader12(reader);
160: default:
161: throw new AssertionError();
162: }
163: }
164:
165: /**
166: * Creates a new {@link Header} that that has a single text value in it
167: * (IOW, of the form <foo>text</foo>.)
168: *
169: * @param name QName of the header element
170: * @param value text value of the header
171: */
172: public static Header create(QName name, String value) {
173: return new StringHeader(name, value);
174: }
175: }
|