Author: veithen Date: Tue Jun 8 22:06:21 2010 New Revision: 952842 URL: http://svn.apache.org/viewvc?rev=952842&view=rev Log: WSCOMMONS-518 / AXIS2-4603: Only allow unwrapping of selected XMLStreamReader wrappers and use a specialized API (XOPUtils#getXOPEncodedStream) to unwrap XOPDecodingStreamReader.
Added: axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/datasource/jaxb/AbstractJAXBAttachmentUnmarshaller.java - copied, changed from r952771, axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/datasource/jaxb/JAXBAttachmentUnmarshaller.java Modified: axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/datasource/jaxb/JAXBAttachmentUnmarshaller.java axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/datasource/jaxb/JAXBDSContext.java axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/jaxws/message/attachments/JAXBAttachmentUnmarshaller.java axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/jaxws/message/databinding/JAXBBlockContext.java Copied: axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/datasource/jaxb/AbstractJAXBAttachmentUnmarshaller.java (from r952771, axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/datasource/jaxb/JAXBAttachmentUnmarshaller.java) URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/datasource/jaxb/AbstractJAXBAttachmentUnmarshaller.java?p2=axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/datasource/jaxb/AbstractJAXBAttachmentUnmarshaller.java&p1=axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/datasource/jaxb/JAXBAttachmentUnmarshaller.java&r1=952771&r2=952842&rev=952842&view=diff ============================================================================== --- axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/datasource/jaxb/JAXBAttachmentUnmarshaller.java (original) +++ axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/datasource/jaxb/AbstractJAXBAttachmentUnmarshaller.java Tue Jun 8 22:06:21 2010 @@ -19,42 +19,38 @@ package org.apache.axis2.datasource.jaxb; -import org.apache.axiom.om.OMAttachmentAccessor; import org.apache.axiom.om.OMException; -import org.apache.axiom.util.stax.XMLStreamReaderUtils; -import org.apache.axis2.context.MessageContext; +import org.apache.axiom.util.stax.xop.MimePartProvider; import org.apache.axis2.jaxws.i18n.Messages; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import javax.activation.DataHandler; import javax.xml.bind.attachment.AttachmentUnmarshaller; -import javax.xml.stream.XMLStreamReader; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; /** - * JAXBAttachmentUnmarshaller - * <p/> - * An implementation of the <link>javax.xml.bind.attachment.AttachmentUnmarshaller</link> that is - * used for deserializing XOP elements into their corresponding binary data packages. + * Abstract base class for {...@link JAXBAttachmentUnmarshaller} and + * {...@link org.apache.axis2.jaxws.message.attachments.JAXBAttachmentUnmarshaller}. This is a partial + * implementation of the {...@link AttachmentUnmarshaller} class. It handles the attachments provided + * by Axiom through the {...@link MimePartProvider} interface. It should be noted that while Axiom only + * handles attachments referenced using XOP, {...@link AttachmentUnmarshaller} is also used to retrieve + * attachments from SwA messages. Hence the {...@link #getDataHandlerForSwA(String)} method. */ -public class JAXBAttachmentUnmarshaller extends AttachmentUnmarshaller { +public abstract class AbstractJAXBAttachmentUnmarshaller extends AttachmentUnmarshaller { - private static final Log log = LogFactory.getLog(JAXBAttachmentUnmarshaller.class); + private static final Log log = LogFactory.getLog(AbstractJAXBAttachmentUnmarshaller.class); - private MessageContext msgContext; - private XMLStreamReader xmlStreamReader; + private final MimePartProvider mimePartProvider; - public JAXBAttachmentUnmarshaller(MessageContext msgContext, - XMLStreamReader xmlStreamReader) { - this.msgContext = msgContext; - this.xmlStreamReader = xmlStreamReader; + public AbstractJAXBAttachmentUnmarshaller(MimePartProvider mimePartProvider) { + this.mimePartProvider = mimePartProvider; } - public boolean isXOPPackage() { + public final boolean isXOPPackage() { // Any message that is received might contain MTOM. // So always return true. @@ -66,7 +62,7 @@ public class JAXBAttachmentUnmarshaller return value; } - public byte[] getAttachmentAsByteArray(String cid) { + public final byte[] getAttachmentAsByteArray(String cid) { if (log.isDebugEnabled()) { log.debug("Attempting to retrieve attachment [" + cid + "] as a byte[]"); } @@ -87,7 +83,7 @@ public class JAXBAttachmentUnmarshaller return null; } - public DataHandler getAttachmentAsDataHandler(String cid) { + public final DataHandler getAttachmentAsDataHandler(String cid) { if (log.isDebugEnabled()) { log.debug("Attempting to retrieve attachment [" + cid + "] as a DataHandler"); } @@ -156,32 +152,27 @@ public class JAXBAttachmentUnmarshaller return baos.toByteArray(); } - protected DataHandler getDataHandler(String cid) { + private DataHandler getDataHandler(String cid) { String blobcid = cid; if (blobcid.startsWith("cid:")) { blobcid = blobcid.substring(4); } - // Get the attachment from the messagecontext using the blob cid - if (msgContext != null) { - DataHandler dh = msgContext.getAttachment(blobcid); - if (dh != null) { - JAXBAttachmentUnmarshallerMonitor.addBlobCID(blobcid); - } - return dh; + DataHandler dh; + try { + dh = mimePartProvider.getDataHandler(blobcid); + } catch (IllegalArgumentException ex) { + dh = null; + } catch (IOException ex) { + throw new OMException("Failed to load attachment with content ID " + blobcid, ex); } - XMLStreamReader attachmentAccessor = - XMLStreamReaderUtils.getWrappedXMLStreamReader(xmlStreamReader, OMAttachmentAccessor.class); - - if (attachmentAccessor != null && - attachmentAccessor instanceof OMAttachmentAccessor) { - - DataHandler dh = - ((OMAttachmentAccessor) attachmentAccessor).getDataHandler(blobcid); - if (dh != null) { - JAXBAttachmentUnmarshallerMonitor.addBlobCID(blobcid); - } - return dh; + if (dh == null) { + dh = getDataHandlerForSwA(blobcid); } - return null; + if (dh != null) { + JAXBAttachmentUnmarshallerMonitor.addBlobCID(blobcid); + } + return dh; } + + protected abstract DataHandler getDataHandlerForSwA(String blobcid); } Modified: axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/datasource/jaxb/JAXBAttachmentUnmarshaller.java URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/datasource/jaxb/JAXBAttachmentUnmarshaller.java?rev=952842&r1=952841&r2=952842&view=diff ============================================================================== --- axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/datasource/jaxb/JAXBAttachmentUnmarshaller.java (original) +++ axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/datasource/jaxb/JAXBAttachmentUnmarshaller.java Tue Jun 8 22:06:21 2010 @@ -19,21 +19,10 @@ package org.apache.axis2.datasource.jaxb; -import org.apache.axiom.om.OMAttachmentAccessor; -import org.apache.axiom.om.OMException; -import org.apache.axiom.util.stax.XMLStreamReaderUtils; +import org.apache.axiom.util.stax.xop.MimePartProvider; import org.apache.axis2.context.MessageContext; -import org.apache.axis2.jaxws.i18n.Messages; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; import javax.activation.DataHandler; -import javax.xml.bind.attachment.AttachmentUnmarshaller; -import javax.xml.stream.XMLStreamReader; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; /** * JAXBAttachmentUnmarshaller @@ -41,147 +30,17 @@ import java.io.InputStream; * An implementation of the <link>javax.xml.bind.attachment.AttachmentUnmarshaller</link> that is * used for deserializing XOP elements into their corresponding binary data packages. */ -public class JAXBAttachmentUnmarshaller extends AttachmentUnmarshaller { - - private static final Log log = LogFactory.getLog(JAXBAttachmentUnmarshaller.class); - - private MessageContext msgContext; - private XMLStreamReader xmlStreamReader; +public class JAXBAttachmentUnmarshaller extends AbstractJAXBAttachmentUnmarshaller { + private final MessageContext msgContext; - public JAXBAttachmentUnmarshaller(MessageContext msgContext, - XMLStreamReader xmlStreamReader) { + public JAXBAttachmentUnmarshaller(MimePartProvider mimePartProvider, + MessageContext msgContext) { + super(mimePartProvider); this.msgContext = msgContext; - this.xmlStreamReader = xmlStreamReader; } - public boolean isXOPPackage() { - - // Any message that is received might contain MTOM. - // So always return true. - boolean value = true; - - if (log.isDebugEnabled()){ - log.debug("isXOPPackage returns " + value); - } - return value; - } - - public byte[] getAttachmentAsByteArray(String cid) { - if (log.isDebugEnabled()) { - log.debug("Attempting to retrieve attachment [" + cid + "] as a byte[]"); - } - DataHandler dh = getAttachmentAsDataHandler(cid); - if (dh != null) { - try { - return convert(dh); - } catch (IOException ioe) { - if (log.isDebugEnabled()) { - log.debug("Exception occurred while getting the byte[] " + ioe); - } - throw new OMException(ioe); - } - } - if (log.isDebugEnabled()) { - log.debug("returning null byte[]"); - } - return null; - } - - public DataHandler getAttachmentAsDataHandler(String cid) { - if (log.isDebugEnabled()) { - log.debug("Attempting to retrieve attachment [" + cid + "] as a DataHandler"); - } - - DataHandler dh = getDataHandler(cid); - if (dh != null) { - return dh; - } else { - String cid2 = getNewCID(cid); - if (log.isDebugEnabled()) { - log.debug("A dataHandler was not found for [" + cid + "] trying [" + cid2 + "]"); - } - dh = getDataHandler(cid2); - if (dh != null) { - return dh; - } - } - // No Data Handler found - throw new OMException(Messages.getMessage("noDataHandler", cid)); - } - - /** - * @param cid - * @return cid with translated characters - */ - private String getNewCID(String cid) { - String cid2 = cid; - - try { - cid2 = java.net.URLDecoder.decode(cid, "UTF-8"); - } catch (Exception e) { - if (log.isDebugEnabled()) { - log.debug("getNewCID decoding " + cid + " as UTF-8 decoding error: " + e); - } - } - return cid2; - } - - /** - * Read the bytes from the DataHandler - * - * @param dh - * @return byte[] - * @throws IOException - */ - private byte[] convert(DataHandler dh) throws IOException { - if (log.isDebugEnabled()) { - log.debug("Reading byte[] from DataHandler " + dh); - } - InputStream is = dh.getInputStream(); - if (log.isDebugEnabled()) { - log.debug("DataHandler InputStream " + is); - } - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - byte[] b = new byte[1024]; - int num = is.read(b); - if (log.isDebugEnabled()) { - if (num <= 0) { - log.debug("DataHandler InputStream contains no data. num=" + num); - } - } - while (num > 0) { - baos.write(b, 0, num); - num = is.read(b); - } - return baos.toByteArray(); - } - - protected DataHandler getDataHandler(String cid) { - String blobcid = cid; - if (blobcid.startsWith("cid:")) { - blobcid = blobcid.substring(4); - } - // Get the attachment from the messagecontext using the blob cid - if (msgContext != null) { - DataHandler dh = msgContext.getAttachment(blobcid); - if (dh != null) { - JAXBAttachmentUnmarshallerMonitor.addBlobCID(blobcid); - } - return dh; - } - XMLStreamReader attachmentAccessor = - XMLStreamReaderUtils.getWrappedXMLStreamReader(xmlStreamReader, OMAttachmentAccessor.class); - - if (attachmentAccessor != null && - attachmentAccessor instanceof OMAttachmentAccessor) { - - DataHandler dh = - ((OMAttachmentAccessor) attachmentAccessor).getDataHandler(blobcid); - if (dh != null) { - JAXBAttachmentUnmarshallerMonitor.addBlobCID(blobcid); - } - return dh; - } - return null; + @Override + protected DataHandler getDataHandlerForSwA(String blobcid) { + return msgContext.getAttachment(blobcid); } } Modified: axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/datasource/jaxb/JAXBDSContext.java URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/datasource/jaxb/JAXBDSContext.java?rev=952842&r1=952841&r2=952842&view=diff ============================================================================== --- axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/datasource/jaxb/JAXBDSContext.java (original) +++ axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/datasource/jaxb/JAXBDSContext.java Tue Jun 8 22:06:21 2010 @@ -22,6 +22,9 @@ package org.apache.axis2.datasource.jaxb import org.apache.axiom.om.OMException; import org.apache.axiom.om.impl.MTOMXMLStreamWriter; import org.apache.axiom.util.stax.XMLStreamReaderUtils; +import org.apache.axiom.util.stax.xop.MimePartProvider; +import org.apache.axiom.util.stax.xop.XOPEncodedStream; +import org.apache.axiom.util.stax.xop.XOPUtils; import org.apache.axis2.context.MessageContext; import org.apache.axis2.java.security.AccessController; import org.apache.axis2.jaxws.message.OccurrenceArray; @@ -286,8 +289,8 @@ public class JAXBDSContext { * Create an Attachment unmarshaller for unmarshalling MTOM/SWA Attachments * @return AttachmentUnmarshaller */ - protected AttachmentUnmarshaller createAttachmentUnmarshaller(XMLStreamReader reader) { - return new JAXBAttachmentUnmarshaller(getMessageContext(), reader); + protected AttachmentUnmarshaller createAttachmentUnmarshaller(MimePartProvider mimePartProvider) { + return new JAXBAttachmentUnmarshaller(mimePartProvider, getMessageContext()); } /** @@ -302,10 +305,10 @@ public class JAXBDSContext { String clsText = (inputReader !=null) ? inputReader.getClass().toString() : "null"; log.debug("unmarshal with inputReader=" + clsText); } - XMLStreamReader reader = XMLStreamReaderUtils.getOriginalXMLStreamReader(inputReader); - if (reader == null) { - reader = inputReader; - } + // See the Javadoc of the CustomBuilder interface for a complete explanation of + // the following two instructions: + XOPEncodedStream xopEncodedStream = XOPUtils.getXOPEncodedStream(inputReader); + XMLStreamReader reader = XMLStreamReaderUtils.getOriginalXMLStreamReader(xopEncodedStream.getReader()); if (DEBUG_ENABLED) { String clsText = (reader !=null) ? reader.getClass().toString() : "null"; log.debug(" originalReader=" + clsText); @@ -318,7 +321,7 @@ public class JAXBDSContext { // Create an attachment unmarshaller - AttachmentUnmarshaller aum = createAttachmentUnmarshaller(reader); + AttachmentUnmarshaller aum = createAttachmentUnmarshaller(xopEncodedStream.getMimePartProvider()); if (aum != null) { if (DEBUG_ENABLED) { Modified: axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/jaxws/message/attachments/JAXBAttachmentUnmarshaller.java URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/jaxws/message/attachments/JAXBAttachmentUnmarshaller.java?rev=952842&r1=952841&r2=952842&view=diff ============================================================================== --- axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/jaxws/message/attachments/JAXBAttachmentUnmarshaller.java (original) +++ axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/jaxws/message/attachments/JAXBAttachmentUnmarshaller.java Tue Jun 8 22:06:21 2010 @@ -19,13 +19,11 @@ package org.apache.axis2.jaxws.message.attachments; -import org.apache.axiom.om.OMAttachmentAccessor; -import org.apache.axiom.util.stax.XMLStreamReaderUtils; -import org.apache.axis2.datasource.jaxb.JAXBAttachmentUnmarshallerMonitor; +import org.apache.axiom.util.stax.xop.MimePartProvider; +import org.apache.axis2.datasource.jaxb.AbstractJAXBAttachmentUnmarshaller; import org.apache.axis2.jaxws.message.Message; import javax.activation.DataHandler; -import javax.xml.stream.XMLStreamReader; /** * JAXBAttachmentUnmarshaller @@ -33,41 +31,17 @@ import javax.xml.stream.XMLStreamReader; * An implementation of the <link>javax.xml.bind.attachment.AttachmentUnmarshaller</link> that is * used for deserializing XOP elements into their corresponding binary data packages. */ -public class JAXBAttachmentUnmarshaller extends org.apache.axis2.datasource.jaxb.JAXBAttachmentUnmarshaller { +public class JAXBAttachmentUnmarshaller extends AbstractJAXBAttachmentUnmarshaller { - private Message message; - private XMLStreamReader xmlStreamReader; + private final Message message; - public JAXBAttachmentUnmarshaller(Message message, XMLStreamReader xmlStreamReader) { - super(null, xmlStreamReader); + public JAXBAttachmentUnmarshaller(MimePartProvider mimePartProvider, Message message) { + super(mimePartProvider); this.message = message; } - protected DataHandler getDataHandler(String cid) { - - // Get the attachment from the message using the cid - if (message != null) { - DataHandler dh = message.getDataHandler(cid); - if (dh != null) { - JAXBAttachmentUnmarshallerMonitor.addBlobCID(cid); - } - return dh; - } - - XMLStreamReader attachmentAccessor = - XMLStreamReaderUtils.getWrappedXMLStreamReader(xmlStreamReader, OMAttachmentAccessor.class); - - if (attachmentAccessor != null && - attachmentAccessor instanceof OMAttachmentAccessor) { - DataHandler dh = - ((OMAttachmentAccessor) attachmentAccessor).getDataHandler(cid); - if (dh != null) { - JAXBAttachmentUnmarshallerMonitor.addBlobCID(cid); - } - return dh; - } - return null; + @Override + protected DataHandler getDataHandlerForSwA(String blobcid) { + return message.getDataHandler(blobcid); } - - } Modified: axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/jaxws/message/databinding/JAXBBlockContext.java URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/jaxws/message/databinding/JAXBBlockContext.java?rev=952842&r1=952841&r2=952842&view=diff ============================================================================== --- axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/jaxws/message/databinding/JAXBBlockContext.java (original) +++ axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/jaxws/message/databinding/JAXBBlockContext.java Tue Jun 8 22:06:21 2010 @@ -19,6 +19,7 @@ package org.apache.axis2.jaxws.message.databinding; +import org.apache.axiom.util.stax.xop.MimePartProvider; import org.apache.axis2.context.MessageContext; import org.apache.axis2.datasource.jaxb.JAXBDSContext; import org.apache.axis2.jaxws.message.Message; @@ -29,7 +30,6 @@ import org.apache.axis2.jaxws.spi.Consta import javax.xml.bind.JAXBContext; import javax.xml.bind.attachment.AttachmentMarshaller; import javax.xml.bind.attachment.AttachmentUnmarshaller; -import javax.xml.stream.XMLStreamReader; import javax.xml.stream.XMLStreamWriter; import java.util.TreeSet; @@ -94,8 +94,8 @@ public class JAXBBlockContext extends JA } @Override - protected AttachmentUnmarshaller createAttachmentUnmarshaller(XMLStreamReader reader) { - return new JAXBAttachmentUnmarshaller(getMessage(), reader); + protected AttachmentUnmarshaller createAttachmentUnmarshaller(MimePartProvider mimePartProvider) { + return new JAXBAttachmentUnmarshaller(mimePartProvider, getMessage()); } public ClassLoader getClassLoader() {