001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.jaxws.message.databinding.impl;
020:
021: import org.apache.axiom.om.OMElement;
022: import org.apache.axiom.om.util.StAXUtils;
023: import org.apache.axiom.soap.SOAP11Constants;
024: import org.apache.axis2.java.security.AccessController;
025: import org.apache.axis2.jaxws.ExceptionFactory;
026: import org.apache.axis2.jaxws.i18n.Messages;
027: import org.apache.axis2.jaxws.message.databinding.SourceBlock;
028: import org.apache.axis2.jaxws.message.factory.BlockFactory;
029: import org.apache.axis2.jaxws.message.impl.BlockImpl;
030: import org.apache.axis2.jaxws.message.util.Reader2Writer;
031: import org.apache.axis2.jaxws.utility.ConvertUtils;
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034:
035: import javax.xml.bind.util.JAXBSource;
036: import javax.xml.namespace.QName;
037: import javax.xml.stream.XMLInputFactory;
038: import javax.xml.stream.XMLStreamException;
039: import javax.xml.stream.XMLStreamReader;
040: import javax.xml.stream.XMLStreamWriter;
041: import javax.xml.transform.Source;
042: import javax.xml.transform.dom.DOMSource;
043: import javax.xml.transform.sax.SAXSource;
044: import javax.xml.transform.stream.StreamSource;
045: import javax.xml.ws.WebServiceException;
046:
047: import java.io.ByteArrayInputStream;
048: import java.io.ByteArrayOutputStream;
049: import java.io.StringReader;
050: import java.lang.reflect.Constructor;
051: import java.security.PrivilegedActionException;
052: import java.security.PrivilegedExceptionAction;
053:
054: /**
055: * SourceBlock
056: * <p/>
057: * Block containing a business object that is a javax.xml.transform.Source.
058: * <p/>
059: * The javax.xml.transform.Source is an interface. The actual concrete class may be one of the
060: * following: - StreamSource - DOMSource - JAXBSource - SAXSource - StAXSource
061: * <p/>
062: * During processing of the block, the block is free to change the representation from one source
063: * to another. (i.e. if you initially seed this with a SAXSource, but a later access may give you
064: * a StAXSource).
065: * <p/>
066: * A Source is consumed when read. The block will make a copy of the source if a non-consumable
067: * request is made.
068: */
069: public class SourceBlockImpl extends BlockImpl implements SourceBlock {
070:
071: private static final Log log = LogFactory
072: .getLog(SourceBlockImpl.class);
073: private static Class staxSource = null;
074:
075: static {
076: try {
077: // Dynamically discover if StAXSource is available
078: staxSource = forName("javax.xml.transform.stax.StAXSource");
079: } catch (Exception e) {
080: }
081: try {
082: // Woodstox does not work with StAXSource
083: if (XMLInputFactory.newInstance().getClass().getName()
084: .indexOf("wstx") != -1) {
085: staxSource = null;
086: }
087: } catch (Exception e) {
088: }
089: }
090:
091: /**
092: * Constructor called from factory
093: *
094: * @param busObject
095: * @param qName
096: * @param factory
097: */
098: SourceBlockImpl(Source busObject, QName qName, BlockFactory factory)
099: throws WebServiceException {
100: super (busObject, null, qName, factory);
101:
102: // Check validity of Source
103: if (busObject instanceof DOMSource
104: || busObject instanceof SAXSource
105: || busObject instanceof StreamSource
106: || (busObject.getClass().equals(staxSource))
107: || busObject instanceof JAXBSource) {
108: // Okay, these are supported Source objects
109: } else {
110: throw ExceptionFactory.makeWebServiceException(Messages
111: .getMessage("SourceNotSupported", busObject
112: .getClass().getName()));
113: }
114: }
115:
116: /**
117: * Constructor called from factory
118: *
119: * @param reader
120: * @param qName
121: * @param factory
122: */
123: public SourceBlockImpl(OMElement omElement, QName qName,
124: BlockFactory factory) {
125: super (omElement, null, qName, factory);
126: }
127:
128: @Override
129: protected Object _getBOFromReader(XMLStreamReader reader,
130: Object busContext) throws XMLStreamException {
131:
132: // Best solution is to use a StAXSource
133: if (staxSource != null) {
134: try {
135: // TODO Constructor should be statically cached for performance
136: Constructor c = staxSource
137: .getDeclaredConstructor(new Class[] { XMLStreamReader.class });
138: return c.newInstance(new Object[] { reader });
139: } catch (Exception e) {
140: }
141: }
142:
143: // TODO StreamSource is not performant...work is needed here to make this faster
144: Reader2Writer r2w = new Reader2Writer(reader);
145: String text = r2w.getAsString();
146: StringReader sr = new StringReader(text);
147: return new StreamSource(sr);
148:
149: }
150:
151: @Override
152: protected XMLStreamReader _getReaderFromBO(Object busObj,
153: Object busContext) throws XMLStreamException,
154: WebServiceException {
155: try {
156: // TODO not sure if this is always the most performant way to do this.
157: /* The following code failed in some (CTS) environments.
158: if (busObj instanceof DOMSource) {
159: // Let's use our own DOMReader for now...
160: Element element = null;
161:
162: // Business Object msut be a Document or Element
163: Node node = ((DOMSource)busObj).getNode();
164: if(node instanceof Document){
165: element = ((Document)node).getDocumentElement();
166: }else{
167: element = (Element) ((DOMSource)busObj).getNode();
168: }
169:
170: // We had some problems with testers producing DOMSources w/o Namespaces.
171: // It's easy to catch this here.
172: if (element.getLocalName() == null) {
173: throw new XMLStreamException(ExceptionFactory.
174: makeWebServiceException(Messages.getMessage("JAXBSourceNamespaceErr")));
175: }
176:
177: return new DOMReader(element);
178: }
179: */
180:
181: if (busObj instanceof StreamSource) {
182: XMLInputFactory f = StAXUtils.getXMLInputFactory();
183:
184: XMLStreamReader reader = f
185: .createXMLStreamReader((Source) busObj);
186: StAXUtils.releaseXMLInputFactory(f);
187: return reader;
188: }
189: //TODO: For GM we need to only use this approach when absolutely necessary.
190: // For example, we don't want to do this if this is a (1.6) StaxSource or if the
191: // installed parser provides a better solution.
192: //TODO: Uncomment this code if woodstock parser handles
193: // JAXBSource and SAXSource correctly.
194: //return inputFactory.createXMLStreamReader((Source) busObj);
195: return _slow_getReaderFromSource((Source) busObj);
196: } catch (Exception e) {
197: String className = (busObj == null) ? "none" : busObj
198: .getClass().getName();
199: throw ExceptionFactory.makeWebServiceException(Messages
200: .getMessage("SourceReadErr", className), e);
201: }
202: }
203:
204: /** Creates an XMLStreamReader from a Source using a slow but proven algorithm. */
205: private XMLStreamReader _slow_getReaderFromSource(Source src)
206: throws XMLStreamException {
207: byte[] bytes = (byte[]) ConvertUtils.convert(src, byte[].class);
208: ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
209: return StAXUtils.createXMLStreamReader(bais);
210: }
211:
212: @Override
213: protected void _outputFromBO(Object busObject, Object busContext,
214: XMLStreamWriter writer) throws XMLStreamException,
215: WebServiceException {
216: // There is no fast way to output the Source to a writer, so get the reader
217: // and pass use the default reader->writer.
218: XMLStreamReader reader = _getReaderFromBO(busObject, busContext);
219: _outputFromReader(reader, writer);
220: // REVIEW Should we call close() on the Source ?
221: }
222:
223: @Override
224: protected Object _getBOFromBO(Object busObject, Object busContext,
225: boolean consume) {
226: if (consume) {
227: return busObject;
228: } else {
229: // TODO Missing Impl
230: throw ExceptionFactory.makeWebServiceException(Messages
231: .getMessage("SourceMissingSupport", busObject
232: .getClass().getName()));
233: }
234: }
235:
236: public boolean isElementData() {
237: return false; // The source could be a text or element etc.
238: }
239:
240: /**
241: * Return the class for this name
242: * @return Class
243: */
244: private static Class forName(final String className)
245: throws ClassNotFoundException {
246: // NOTE: This method must remain private because it uses AccessController
247: Class cl = null;
248: try {
249: cl = (Class) AccessController
250: .doPrivileged(new PrivilegedExceptionAction() {
251: public Object run()
252: throws ClassNotFoundException {
253: return Class.forName(className);
254: }
255: });
256: } catch (PrivilegedActionException e) {
257: if (log.isDebugEnabled()) {
258: log.debug("Exception thrown from AccessController: "
259: + e);
260: }
261: throw (ClassNotFoundException) e.getException();
262: }
263:
264: return cl;
265: }
266:
267: /* (non-Javadoc)
268: * @see org.apache.axis2.jaxws.message.Block#getBusinessObject(boolean)
269: */
270: public Object getBusinessObject(boolean consume)
271: throws XMLStreamException, WebServiceException {
272: if (consumed) {
273: throw ExceptionFactory.makeWebServiceException(Messages
274: .getMessage("BlockImplErr1", this .getClass()
275: .getName()));
276: }
277:
278: if (busObject != null) {
279: busObject = _getBOFromBO(busObject, busContext, consume);
280: } else {
281: // If the message is a fault, there are some special gymnastics that we have to do
282: // to get this working for all of the handler scenarios.
283: boolean hasFault = false;
284: if ((parent != null && parent.isFault())
285: || omElement.getQName().getLocalPart().equals(
286: SOAP11Constants.SOAPFAULT_LOCAL_NAME)) {
287: hasFault = true;
288: }
289:
290: // Transform reader into business object
291: if (!hasFault) {
292: XMLStreamReader reader;
293: if (omElement.getBuilder() != null
294: && !omElement.getBuilder().isCompleted()) {
295: reader = omElement
296: .getXMLStreamReaderWithoutCaching();
297: } else {
298: reader = omElement.getXMLStreamReader();
299: }
300: busObject = _getBOFromReader(reader, busContext);
301: } else {
302: ByteArrayOutputStream baos = new ByteArrayOutputStream();
303: omElement.serialize(baos);
304:
305: ByteArrayInputStream bais = new ByteArrayInputStream(
306: baos.toByteArray());
307: busObject = new StreamSource(bais);
308: }
309:
310: omElement = null;
311: }
312:
313: // Save the businessObject in a local variable
314: // so that we can reset the Block if consume was indicated
315: Object newBusObject = busObject;
316: setConsumed(consume);
317: return newBusObject;
318: }
319: }
|