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.message.jaxb;
  27 
  28 import com.sun.istack.internal.NotNull;
  29 import com.sun.istack.internal.XMLStreamException2;
  30 import com.sun.xml.internal.bind.api.Bridge;
  31 import com.sun.xml.internal.stream.buffer.XMLStreamBuffer;
  32 import com.sun.xml.internal.stream.buffer.XMLStreamBufferResult;
  33 import com.sun.xml.internal.ws.api.message.Header;
  34 import com.sun.xml.internal.ws.encoding.SOAPBindingCodec;
  35 import com.sun.xml.internal.ws.message.AbstractHeaderImpl;
  36 import com.sun.xml.internal.ws.message.RootElementSniffer;
  37 import com.sun.xml.internal.ws.spi.db.BindingContext;
  38 import com.sun.xml.internal.ws.spi.db.XMLBridge;
  39 import com.sun.xml.internal.ws.streaming.XMLStreamWriterUtil;
  40 import org.xml.sax.Attributes;
  41 import org.xml.sax.ContentHandler;
  42 import org.xml.sax.ErrorHandler;
  43 import org.xml.sax.SAXException;
  44 import org.xml.sax.SAXParseException;
  45 
  46 import javax.xml.bind.JAXBElement;
  47 import javax.xml.bind.JAXBException;
  48 import javax.xml.bind.Unmarshaller;
  49 import javax.xml.bind.util.JAXBResult;
  50 import javax.xml.namespace.QName;
  51 import javax.xml.soap.SOAPException;
  52 import javax.xml.soap.SOAPMessage;
  53 import javax.xml.soap.SOAPHeader;
  54 import javax.xml.stream.XMLStreamException;
  55 import javax.xml.stream.XMLStreamReader;
  56 import javax.xml.stream.XMLStreamWriter;
  57 import java.io.OutputStream;
  58 
  59 /**
  60  * {@link Header} whose physical data representation is a JAXB bean.
  61  *
  62  * @author Kohsuke Kawaguchi
  63  */
  64 public final class JAXBHeader extends AbstractHeaderImpl {
  65 
  66     /**
  67      * The JAXB object that represents the header.
  68      */
  69     private final Object jaxbObject;
  70 
  71     private final XMLBridge bridge;
  72 
  73     // information about this header. lazily obtained.
  74     private String nsUri;
  75     private String localName;
  76     private Attributes atts;
  77 
  78     /**
  79      * Once the header is turned into infoset,
  80      * this buffer keeps it.
  81      */
  82     private XMLStreamBuffer infoset;
  83 
  84     public JAXBHeader(BindingContext context, Object jaxbObject) {
  85         this.jaxbObject = jaxbObject;
  86 //        this.bridge = new MarshallerBridge(context);
  87         this.bridge = context.createFragmentBridge();
  88 
  89         if (jaxbObject instanceof JAXBElement) {
  90             JAXBElement e = (JAXBElement) jaxbObject;
  91             this.nsUri = e.getName().getNamespaceURI();
  92             this.localName = e.getName().getLocalPart();
  93         }
  94     }
  95 
  96     public JAXBHeader(XMLBridge bridge, Object jaxbObject) {
  97         this.jaxbObject = jaxbObject;
  98         this.bridge = bridge;
  99 
 100         QName tagName = bridge.getTypeInfo().tagName;
 101         this.nsUri = tagName.getNamespaceURI();
 102         this.localName = tagName.getLocalPart();
 103     }
 104 
 105     /**
 106      * Lazily parse the first element to obtain attribute values on it.
 107      */
 108     private void parse() {
 109         RootElementSniffer sniffer = new RootElementSniffer();
 110         try {
 111             bridge.marshal(jaxbObject,sniffer,null);
 112         } catch (JAXBException e) {
 113             // if it's due to us aborting the processing after the first element,
 114             // we can safely ignore this exception.
 115             //
 116             // if it's due to error in the object, the same error will be reported
 117             // when the readHeader() method is used, so we don't have to report
 118             // an error right now.
 119             nsUri = sniffer.getNsUri();
 120             localName = sniffer.getLocalName();
 121             atts = sniffer.getAttributes();
 122         }
 123     }
 124 
 125 
 126     public @NotNull String getNamespaceURI() {
 127         if(nsUri==null)
 128             parse();
 129         return nsUri;
 130     }
 131 
 132     public @NotNull String getLocalPart() {
 133         if(localName==null)
 134             parse();
 135         return localName;
 136     }
 137 
 138     public String getAttribute(String nsUri, String localName) {
 139         if(atts==null)
 140             parse();
 141         return atts.getValue(nsUri,localName);
 142     }
 143 
 144     public XMLStreamReader readHeader() throws XMLStreamException {
 145         try {
 146             if(infoset==null) {
 147                 XMLStreamBufferResult sbr = new XMLStreamBufferResult();
 148                 bridge.marshal(jaxbObject,sbr);
 149                 infoset = sbr.getXMLStreamBuffer();
 150             }
 151             return infoset.readAsXMLStreamReader();
 152         } catch (JAXBException e) {
 153             throw new XMLStreamException2(e);
 154         }
 155     }
 156 
 157     public <T> T readAsJAXB(Unmarshaller unmarshaller) throws JAXBException {
 158         try {
 159             JAXBResult r = new JAXBResult(unmarshaller);
 160             // bridge marshals a fragment, so we need to add start/endDocument by ourselves
 161             r.getHandler().startDocument();
 162             bridge.marshal(jaxbObject,r);
 163             r.getHandler().endDocument();
 164             return (T)r.getResult();
 165         } catch (SAXException e) {
 166             throw new JAXBException(e);
 167         }
 168     }
 169     /** @deprecated */
 170     public <T> T readAsJAXB(Bridge<T> bridge) throws JAXBException {
 171         return bridge.unmarshal(new JAXBBridgeSource(this.bridge,jaxbObject));
 172     }
 173 
 174         public <T> T readAsJAXB(XMLBridge<T> bond) throws JAXBException {
 175         return bond.unmarshal(new JAXBBridgeSource(this.bridge,jaxbObject),null);
 176         }
 177 
 178     public void writeTo(XMLStreamWriter sw) throws XMLStreamException {
 179         try {
 180             // Get the encoding of the writer
 181             String encoding = XMLStreamWriterUtil.getEncoding(sw);
 182 
 183             // Get output stream and use JAXB UTF-8 writer
 184             OutputStream os = bridge.supportOutputStream() ? XMLStreamWriterUtil.getOutputStream(sw) : null;
 185             if (os != null && encoding != null && encoding.equalsIgnoreCase(SOAPBindingCodec.UTF8_ENCODING)) {
 186                 bridge.marshal(jaxbObject, os, sw.getNamespaceContext(), null);
 187             } else {
 188                 bridge.marshal(jaxbObject,sw, null);
 189             }
 190         } catch (JAXBException e) {
 191             throw new XMLStreamException2(e);
 192         }
 193     }
 194 
 195     public void writeTo(SOAPMessage saaj) throws SOAPException {
 196         try {
 197             SOAPHeader header = saaj.getSOAPHeader();
 198             if (header == null)
 199                 header = saaj.getSOAPPart().getEnvelope().addHeader();
 200             bridge.marshal(jaxbObject,header);
 201         } catch (JAXBException e) {
 202             throw new SOAPException(e);
 203         }
 204     }
 205 
 206     public void writeTo(ContentHandler contentHandler, ErrorHandler errorHandler) throws SAXException {
 207         try {
 208             bridge.marshal(jaxbObject,contentHandler,null);
 209         } catch (JAXBException e) {
 210             SAXParseException x = new SAXParseException(e.getMessage(),null,null,-1,-1,e);
 211             errorHandler.fatalError(x);
 212             throw x;
 213         }
 214     }
 215 }