325N/A/*
325N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.xml.internal.ws.message.jaxb;
325N/A
325N/Aimport com.sun.istack.internal.NotNull;
325N/Aimport com.sun.istack.internal.XMLStreamException2;
325N/Aimport com.sun.xml.internal.bind.api.Bridge;
325N/Aimport com.sun.xml.internal.bind.api.JAXBRIContext;
325N/Aimport com.sun.xml.internal.stream.buffer.XMLStreamBuffer;
325N/Aimport com.sun.xml.internal.stream.buffer.XMLStreamBufferResult;
325N/Aimport com.sun.xml.internal.ws.api.message.Header;
325N/Aimport com.sun.xml.internal.ws.message.AbstractHeaderImpl;
325N/Aimport com.sun.xml.internal.ws.message.RootElementSniffer;
325N/Aimport com.sun.xml.internal.ws.streaming.XMLStreamWriterUtil;
325N/Aimport org.xml.sax.Attributes;
325N/Aimport org.xml.sax.ContentHandler;
325N/Aimport org.xml.sax.ErrorHandler;
325N/Aimport org.xml.sax.SAXException;
325N/Aimport org.xml.sax.SAXParseException;
325N/A
325N/Aimport javax.xml.bind.JAXBElement;
325N/Aimport javax.xml.bind.JAXBException;
325N/Aimport javax.xml.bind.Unmarshaller;
325N/Aimport javax.xml.bind.util.JAXBResult;
325N/Aimport javax.xml.namespace.QName;
325N/Aimport javax.xml.soap.SOAPException;
325N/Aimport javax.xml.soap.SOAPMessage;
325N/Aimport javax.xml.soap.SOAPHeader;
325N/Aimport javax.xml.stream.XMLStreamException;
325N/Aimport javax.xml.stream.XMLStreamReader;
325N/Aimport javax.xml.stream.XMLStreamWriter;
325N/Aimport java.io.OutputStream;
325N/A
325N/A/**
325N/A * {@link Header} whose physical data representation is a JAXB bean.
325N/A *
325N/A * @author Kohsuke Kawaguchi
325N/A */
325N/Apublic final class JAXBHeader extends AbstractHeaderImpl {
325N/A
325N/A /**
325N/A * The JAXB object that represents the header.
325N/A */
325N/A private final Object jaxbObject;
325N/A
325N/A private final Bridge bridge;
325N/A
325N/A // information about this header. lazily obtained.
325N/A private String nsUri;
325N/A private String localName;
325N/A private Attributes atts;
325N/A
325N/A /**
325N/A * Once the header is turned into infoset,
325N/A * this buffer keeps it.
325N/A */
325N/A private XMLStreamBuffer infoset;
325N/A
325N/A public JAXBHeader(JAXBRIContext context, Object jaxbObject) {
325N/A this.jaxbObject = jaxbObject;
325N/A this.bridge = new MarshallerBridge(context);
325N/A
325N/A if (jaxbObject instanceof JAXBElement) {
325N/A JAXBElement e = (JAXBElement) jaxbObject;
325N/A this.nsUri = e.getName().getNamespaceURI();
325N/A this.localName = e.getName().getLocalPart();
325N/A }
325N/A }
325N/A
325N/A public JAXBHeader(Bridge bridge, Object jaxbObject) {
325N/A this.jaxbObject = jaxbObject;
325N/A this.bridge = bridge;
325N/A
325N/A QName tagName = bridge.getTypeReference().tagName;
325N/A this.nsUri = tagName.getNamespaceURI();
325N/A this.localName = tagName.getLocalPart();
325N/A }
325N/A
325N/A /**
325N/A * Lazily parse the first element to obtain attribute values on it.
325N/A */
325N/A private void parse() {
325N/A RootElementSniffer sniffer = new RootElementSniffer();
325N/A try {
325N/A bridge.marshal(jaxbObject,sniffer);
325N/A } catch (JAXBException e) {
325N/A // if it's due to us aborting the processing after the first element,
325N/A // we can safely ignore this exception.
325N/A //
325N/A // if it's due to error in the object, the same error will be reported
325N/A // when the readHeader() method is used, so we don't have to report
325N/A // an error right now.
325N/A nsUri = sniffer.getNsUri();
325N/A localName = sniffer.getLocalName();
325N/A atts = sniffer.getAttributes();
325N/A }
325N/A }
325N/A
325N/A
325N/A public @NotNull String getNamespaceURI() {
325N/A if(nsUri==null)
325N/A parse();
325N/A return nsUri;
325N/A }
325N/A
325N/A public @NotNull String getLocalPart() {
325N/A if(localName==null)
325N/A parse();
325N/A return localName;
325N/A }
325N/A
325N/A public String getAttribute(String nsUri, String localName) {
325N/A if(atts==null)
325N/A parse();
325N/A return atts.getValue(nsUri,localName);
325N/A }
325N/A
325N/A public XMLStreamReader readHeader() throws XMLStreamException {
325N/A try {
325N/A if(infoset==null) {
325N/A XMLStreamBufferResult sbr = new XMLStreamBufferResult();
325N/A bridge.marshal(jaxbObject,sbr);
325N/A infoset = sbr.getXMLStreamBuffer();
325N/A }
325N/A return infoset.readAsXMLStreamReader();
325N/A } catch (JAXBException e) {
325N/A throw new XMLStreamException2(e);
325N/A }
325N/A }
325N/A
325N/A public <T> T readAsJAXB(Unmarshaller unmarshaller) throws JAXBException {
325N/A try {
325N/A JAXBResult r = new JAXBResult(unmarshaller);
325N/A // bridge marshals a fragment, so we need to add start/endDocument by ourselves
325N/A r.getHandler().startDocument();
325N/A bridge.marshal(jaxbObject,r);
325N/A r.getHandler().endDocument();
325N/A return (T)r.getResult();
325N/A } catch (SAXException e) {
325N/A throw new JAXBException(e);
325N/A }
325N/A }
325N/A
325N/A public <T> T readAsJAXB(Bridge<T> bridge) throws JAXBException {
325N/A return bridge.unmarshal(new JAXBBridgeSource(this.bridge,jaxbObject));
325N/A }
325N/A
325N/A public void writeTo(XMLStreamWriter sw) throws XMLStreamException {
325N/A try {
325N/A // Get output stream and use JAXB UTF-8 writer
325N/A OutputStream os = XMLStreamWriterUtil.getOutputStream(sw);
325N/A if (os != null) {
325N/A bridge.marshal(jaxbObject, os, sw.getNamespaceContext());
325N/A } else {
325N/A bridge.marshal(jaxbObject,sw);
325N/A }
325N/A } catch (JAXBException e) {
325N/A throw new XMLStreamException2(e);
325N/A }
325N/A }
325N/A
325N/A public void writeTo(SOAPMessage saaj) throws SOAPException {
325N/A try {
325N/A SOAPHeader header = saaj.getSOAPHeader();
325N/A if (header == null)
325N/A header = saaj.getSOAPPart().getEnvelope().addHeader();
325N/A bridge.marshal(jaxbObject,header);
325N/A } catch (JAXBException e) {
325N/A throw new SOAPException(e);
325N/A }
325N/A }
325N/A
325N/A public void writeTo(ContentHandler contentHandler, ErrorHandler errorHandler) throws SAXException {
325N/A try {
325N/A bridge.marshal(jaxbObject,contentHandler);
325N/A } catch (JAXBException e) {
325N/A SAXParseException x = new SAXParseException(e.getMessage(),null,null,-1,-1,e);
325N/A errorHandler.fatalError(x);
325N/A throw x;
325N/A }
325N/A }
325N/A}