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: package com.sun.xml.ws.message.jaxb;
037:
038: import com.sun.istack.NotNull;
039: import com.sun.istack.XMLStreamException2;
040: import com.sun.xml.bind.api.Bridge;
041: import com.sun.xml.bind.api.JAXBRIContext;
042: import com.sun.xml.stream.buffer.XMLStreamBuffer;
043: import com.sun.xml.stream.buffer.XMLStreamBufferResult;
044: import com.sun.xml.ws.api.message.Header;
045: import com.sun.xml.ws.message.AbstractHeaderImpl;
046: import com.sun.xml.ws.message.RootElementSniffer;
047: import com.sun.xml.ws.streaming.XMLStreamWriterUtil;
048: import org.xml.sax.Attributes;
049: import org.xml.sax.ContentHandler;
050: import org.xml.sax.ErrorHandler;
051: import org.xml.sax.SAXException;
052: import org.xml.sax.SAXParseException;
053:
054: import javax.xml.bind.JAXBElement;
055: import javax.xml.bind.JAXBException;
056: import javax.xml.bind.Unmarshaller;
057: import javax.xml.bind.util.JAXBResult;
058: import javax.xml.namespace.QName;
059: import javax.xml.soap.SOAPException;
060: import javax.xml.soap.SOAPMessage;
061: import javax.xml.stream.XMLStreamException;
062: import javax.xml.stream.XMLStreamReader;
063: import javax.xml.stream.XMLStreamWriter;
064: import java.io.OutputStream;
065: import java.util.Map;
066:
067: /**
068: * {@link Header} whose physical data representation is a JAXB bean.
069: *
070: * @author Kohsuke Kawaguchi
071: */
072: public final class JAXBHeader extends AbstractHeaderImpl {
073:
074: /**
075: * The JAXB object that represents the header.
076: */
077: private final Object jaxbObject;
078:
079: private final Bridge bridge;
080:
081: // information about this header. lazily obtained.
082: private String nsUri;
083: private String localName;
084: private Attributes atts;
085:
086: /**
087: * Once the header is turned into infoset,
088: * this buffer keeps it.
089: */
090: private XMLStreamBuffer infoset;
091:
092: public JAXBHeader(JAXBRIContext context, Object jaxbObject) {
093: this .jaxbObject = jaxbObject;
094: this .bridge = new MarshallerBridge(context);
095:
096: if (jaxbObject instanceof JAXBElement) {
097: JAXBElement e = (JAXBElement) jaxbObject;
098: this .nsUri = e.getName().getNamespaceURI();
099: this .localName = e.getName().getLocalPart();
100: }
101: }
102:
103: public JAXBHeader(Bridge bridge, Object jaxbObject) {
104: this .jaxbObject = jaxbObject;
105: this .bridge = bridge;
106:
107: QName tagName = bridge.getTypeReference().tagName;
108: this .nsUri = tagName.getNamespaceURI();
109: this .localName = tagName.getLocalPart();
110: }
111:
112: /**
113: * Lazily parse the first element to obtain attribute values on it.
114: */
115: private void parse() {
116: RootElementSniffer sniffer = new RootElementSniffer();
117: try {
118: bridge.marshal(jaxbObject, sniffer);
119: } catch (JAXBException e) {
120: // if it's due to us aborting the processing after the first element,
121: // we can safely ignore this exception.
122: //
123: // if it's due to error in the object, the same error will be reported
124: // when the readHeader() method is used, so we don't have to report
125: // an error right now.
126: nsUri = sniffer.getNsUri();
127: localName = sniffer.getLocalName();
128: atts = sniffer.getAttributes();
129: }
130: }
131:
132: public @NotNull
133: String getNamespaceURI() {
134: if (nsUri == null)
135: parse();
136: return nsUri;
137: }
138:
139: public @NotNull
140: String getLocalPart() {
141: if (localName == null)
142: parse();
143: return localName;
144: }
145:
146: public String getAttribute(String nsUri, String localName) {
147: if (atts == null)
148: parse();
149: return atts.getValue(nsUri, localName);
150: }
151:
152: public XMLStreamReader readHeader() throws XMLStreamException {
153: try {
154: if (infoset == null) {
155: XMLStreamBufferResult sbr = new XMLStreamBufferResult();
156: bridge.marshal(jaxbObject, sbr);
157: infoset = sbr.getXMLStreamBuffer();
158: }
159: return infoset.readAsXMLStreamReader();
160: } catch (JAXBException e) {
161: throw new XMLStreamException2(e);
162: }
163: }
164:
165: public <T> T readAsJAXB(Unmarshaller unmarshaller)
166: throws JAXBException {
167: JAXBResult r = new JAXBResult(unmarshaller);
168: bridge.marshal(jaxbObject, r);
169: return (T) r.getResult();
170: }
171:
172: public <T> T readAsJAXB(Bridge<T> bridge) throws JAXBException {
173: return bridge.unmarshal(new JAXBBridgeSource(this .bridge,
174: jaxbObject));
175: }
176:
177: public void writeTo(XMLStreamWriter sw) throws XMLStreamException {
178: try {
179: // Get output stream and use JAXB UTF-8 writer
180: OutputStream os = XMLStreamWriterUtil.getOutputStream(sw);
181: if (os != null) {
182: bridge
183: .marshal(jaxbObject, os, sw
184: .getNamespaceContext());
185: } else {
186: bridge.marshal(jaxbObject, sw);
187: }
188: } catch (JAXBException e) {
189: throw new XMLStreamException2(e);
190: }
191: }
192:
193: public void writeTo(SOAPMessage saaj) throws SOAPException {
194: try {
195: bridge.marshal(jaxbObject, saaj.getSOAPHeader());
196: } catch (JAXBException e) {
197: throw new SOAPException(e);
198: }
199: }
200:
201: public void writeTo(ContentHandler contentHandler,
202: ErrorHandler errorHandler) throws SAXException {
203: try {
204: bridge.marshal(jaxbObject, contentHandler);
205: } catch (JAXBException e) {
206: SAXParseException x = new SAXParseException(e.getMessage(),
207: null, null, -1, -1, e);
208: errorHandler.fatalError(x);
209: throw x;
210: }
211: }
212: }
|