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.handler;
038:
039: import com.sun.xml.ws.api.message.Packet;
040: import com.sun.xml.ws.api.message.Message;
041: import com.sun.xml.ws.util.xml.XmlUtil;
042:
043: import javax.xml.bind.JAXBContext;
044: import javax.xml.bind.JAXBException;
045: import javax.xml.bind.Marshaller;
046: import javax.xml.bind.Unmarshaller;
047: import javax.xml.transform.Source;
048: import javax.xml.transform.Transformer;
049: import javax.xml.transform.TransformerException;
050: import javax.xml.transform.dom.DOMResult;
051: import javax.xml.transform.dom.DOMSource;
052: import javax.xml.ws.LogicalMessage;
053: import javax.xml.ws.WebServiceException;
054:
055: /**
056: * Implementation of {@link LogicalMessage}. This class implements the methods
057: * used by LogicalHandlers to get/set the request or response either
058: * as a JAXB object or as javax.xml.transform.Source.
059: *
060: * <p>The {@link Message} that is passed into the constructor
061: * is used to retrieve the payload of the request or response.
062: *
063: * @see Message
064: * @see LogicalMessageContextImpl
065: *
066: * @author WS Development Team
067: */
068: /**
069: * TODO: Take care of variations in behavior wrt to vaious sources.
070: * DOMSource : changes made should be reflected, StreamSource or SAXSource, Give copy
071: */
072: class LogicalMessageImpl implements LogicalMessage {
073: private Packet packet;
074: // This holds the (modified)payload set by User
075: private Source payloadSrc = null;
076: // Flag to check if the PayloadSrc is accessed/modified
077: private boolean payloadModifed = false;
078:
079: /** Creates a new instance of LogicalMessageImplRearch */
080: public LogicalMessageImpl(Packet packet) {
081: // don't create extract payload until Users wants it.
082: this .packet = packet;
083: }
084:
085: boolean isPayloadModifed() {
086: return payloadModifed;
087: }
088:
089: Source getModifiedPayload() {
090: if (!payloadModifed)
091: throw new RuntimeException("Payload not modified.");
092: return payloadSrc;
093:
094: }
095:
096: public Source getPayload() {
097: if (!payloadModifed) {
098: payloadSrc = packet.getMessage().readPayloadAsSource();
099: payloadModifed = true;
100: }
101: if (payloadSrc == null)
102: return null;
103: if (payloadSrc instanceof DOMSource) {
104: return payloadSrc;
105: } else {
106: try {
107: Transformer transformer = XmlUtil.newTransformer();
108: DOMResult domResult = new DOMResult();
109: transformer.transform(payloadSrc, domResult);
110: payloadSrc = new DOMSource(domResult.getNode());
111: return payloadSrc;
112: } catch (TransformerException te) {
113: throw new WebServiceException(te);
114: }
115: }
116: /*
117: Source copySrc;
118: if(payloadSrc instanceof DOMSource){
119: copySrc = payloadSrc;
120: } else {
121: copySrc = copy(payloadSrc);
122: }
123: return copySrc;
124: */
125: }
126:
127: public void setPayload(Source payload) {
128: payloadModifed = true;
129: payloadSrc = payload;
130: }
131:
132: /*
133: * Converts to DOMSource and then it unmarshalls this DOMSource
134: * to a jaxb object. Any changes done in jaxb object are lost if
135: * the object isn't set again.
136: */
137: public Object getPayload(JAXBContext context) {
138: try {
139: Source payloadSrc = getPayload();
140: if (payloadSrc == null)
141: return null;
142: Unmarshaller unmarshaller = context.createUnmarshaller();
143: return unmarshaller.unmarshal(payloadSrc);
144: } catch (JAXBException e) {
145: throw new WebServiceException(e);
146: }
147: }
148:
149: public void setPayload(Object payload, JAXBContext context) {
150: payloadModifed = true;
151: try {
152: Marshaller marshaller = context.createMarshaller();
153: marshaller.setProperty("jaxb.fragment", true);
154: DOMResult domResult = new DOMResult();
155: marshaller.marshal(payload, domResult);
156: payloadSrc = new DOMSource(domResult.getNode());
157: } catch (JAXBException e) {
158: throw new WebServiceException(e);
159: }
160: }
161: /*
162: private Source copy(Source src) {
163: if(src instanceof StreamSource){
164: StreamSource origSrc = (StreamSource)src;
165: byte[] payloadbytes;
166: try {
167: payloadbytes = ASCIIUtility.getBytes(origSrc.getInputStream());
168: } catch (IOException e) {
169: throw new WebServiceException(e);
170: }
171: ByteArrayInputStream bis = new ByteArrayInputStream(payloadbytes);
172: origSrc.setInputStream(new ByteArrayInputStream(payloadbytes));
173: StreamSource copySource = new StreamSource(bis, src.getSystemId());
174: return copySource;
175: } else if(src instanceof SAXSource){
176: SAXSource saxSrc = (SAXSource)src;
177: try {
178: XMLStreamBuffer xsb = new XMLStreamBuffer();
179: XMLReader reader = saxSrc.getXMLReader();
180: if(reader == null)
181: reader = new SAXBufferProcessor();
182: saxSrc.setXMLReader(reader);
183: reader.setContentHandler(new SAXBufferCreator(xsb));
184: reader.parse(saxSrc.getInputSource());
185: src = new XMLStreamBufferSource(xsb);
186: return new XMLStreamBufferSource(xsb);
187: } catch (IOException e) {
188: throw new WebServiceException(e);
189: } catch (SAXException e) {
190: throw new WebServiceException(e);
191: }
192: }
193: throw new WebServiceException("Copy is not needed for this Source");
194: }
195: */
196: }
|