Author: scheu Date: Thu Jun 10 15:33:03 2010 New Revision: 953352 URL: http://svn.apache.org/viewvc?rev=953352&view=rev Log: AXIS2-4733 Contributor: Phil Adams Contributed WrappedDataHandler to allow Axis2 to set the appropriate content-type on a DataHandler. Also added a validation test.
Added: axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/util/WrappedDataHandler.java axis/axis2/java/core/trunk/modules/kernel/test/org/apache/axis2/util/WrappedDataHandlerTest.java Modified: axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/jaxws/message/impl/MessageFactoryImpl.java Modified: axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/jaxws/message/impl/MessageFactoryImpl.java URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/jaxws/message/impl/MessageFactoryImpl.java?rev=953352&r1=953351&r2=953352&view=diff ============================================================================== --- axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/jaxws/message/impl/MessageFactoryImpl.java (original) +++ axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/jaxws/message/impl/MessageFactoryImpl.java Thu Jun 10 15:33:03 2010 @@ -20,12 +20,7 @@ package org.apache.axis2.jaxws.message.impl; import org.apache.axiom.om.OMElement; -import org.apache.axiom.om.OMNamespace; -import org.apache.axiom.om.OMFactory; -import org.apache.axiom.om.OMAbstractFactory; import org.apache.axiom.om.impl.builder.StAXOMBuilder; -import org.apache.axiom.om.impl.OMNamespaceImpl; -import org.apache.axiom.om.impl.llom.OMSourcedElementImpl; import org.apache.axiom.soap.SOAPEnvelope; import org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder; import org.apache.axis2.jaxws.ExceptionFactory; @@ -37,6 +32,7 @@ import org.apache.axis2.jaxws.message.da import org.apache.axis2.jaxws.message.databinding.DataSourceBlock; import org.apache.axis2.jaxws.message.factory.MessageFactory; import org.apache.axis2.transport.http.HTTPConstants; +import org.apache.axis2.util.WrappedDataHandler; import javax.xml.soap.AttachmentPart; import javax.xml.soap.MimeHeader; @@ -118,7 +114,7 @@ public class MessageFactoryImpl implemen m.setDoingSWA(true); while (it.hasNext()) { AttachmentPart ap = (AttachmentPart)it.next(); - m.addDataHandler(ap.getDataHandler(), ap.getContentId()); + m.addDataHandler(new WrappedDataHandler(ap.getDataHandler(), ap.getContentType()), ap.getContentId()); } } return m; @@ -144,5 +140,4 @@ public class MessageFactoryImpl implemen } return createFrom(block.getXMLStreamReader(true), protocol); } - } Added: axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/util/WrappedDataHandler.java URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/util/WrappedDataHandler.java?rev=953352&view=auto ============================================================================== --- axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/util/WrappedDataHandler.java (added) +++ axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/util/WrappedDataHandler.java Thu Jun 10 15:33:03 2010 @@ -0,0 +1,214 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.axis2.util; + +import java.awt.datatransfer.DataFlavor; +import java.awt.datatransfer.UnsupportedFlavorException; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import javax.activation.CommandInfo; +import javax.activation.CommandMap; +import javax.activation.DataHandler; +import javax.activation.DataSource; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +/** + * This class acts as a wrapper for the javax.activation.DataHandler class. + * It is used to store away a (potentially) user-defined content-type value along with + * the DataHandler instance. We'll delegate all method calls except for getContentType() + * to the real DataHandler instance. + */ +public class WrappedDataHandler extends DataHandler { + + private static final Log log = LogFactory.getLog(WrappedDataHandler.class); + + DataHandler delegate; + String contentType; + + private static FakeDataSource FAKE_DS = new FakeDataSource(); + + // This class is simply used as a fake DataSource implementation so that the + // WrappedDataHandler class can call it's superclass's ctor with a non-null + // value that implements DataSource. The FakeDataSource instance will never + // be used, however. It's simply a placeholder. + private static class FakeDataSource implements DataSource { + + @Override + public String getContentType() { + return "application/octet-stream"; + } + + @Override + public InputStream getInputStream() throws IOException { + throw new UnsupportedOperationException(); + } + + @Override + public String getName() { + return "FakeDataSource"; + } + + @Override + public OutputStream getOutputStream() throws IOException { + throw new UnsupportedOperationException(); + } + } + + /** + * Constructs a new instance of the WrappedDataHandler. + * @param _delegate the real DataHandler instance being wrapped + * @param _contentType the user-defined contentType associated with the DataHandler instance + */ + public WrappedDataHandler(DataHandler _delegate, String _contentType) { + super(FAKE_DS); + + delegate = _delegate; + contentType = _contentType; + + if (log.isDebugEnabled()) { + log.debug("Created instance of WrappedDatahandler: " + this.toString() + ", contentType=" + contentType + + "\nDelegate DataHandler: " + delegate.toString()); + } + } + + /* (non-Javadoc) + * @see javax.activation.DataHandler#getAllCommands() + */ + @Override + public CommandInfo[] getAllCommands() { + return delegate.getAllCommands(); + } + + /* (non-Javadoc) + * @see javax.activation.DataHandler#getBean(javax.activation.CommandInfo) + */ + @Override + public Object getBean(CommandInfo paramCommandInfo) { + return delegate.getBean(paramCommandInfo); + } + + /* (non-Javadoc) + * @see javax.activation.DataHandler#getCommand(java.lang.String) + */ + @Override + public CommandInfo getCommand(String paramString) { + return delegate.getCommand(paramString); + } + + /* (non-Javadoc) + * @see javax.activation.DataHandler#getContent() + */ + @Override + public Object getContent() throws IOException { + return delegate.getContent(); + } + + /* (non-Javadoc) + * @see javax.activation.DataHandler#getContentType() + */ + @Override + public String getContentType() { + return (contentType != null ? contentType : delegate.getContentType()); + } + + /* (non-Javadoc) + * @see javax.activation.DataHandler#getDataSource() + */ + @Override + public DataSource getDataSource() { + return delegate.getDataSource(); + } + + /* (non-Javadoc) + * @see javax.activation.DataHandler#getInputStream() + */ + @Override + public InputStream getInputStream() throws IOException { + return delegate.getInputStream(); + } + + /* (non-Javadoc) + * @see javax.activation.DataHandler#getName() + */ + @Override + public String getName() { + return delegate.getName(); + } + + /* (non-Javadoc) + * @see javax.activation.DataHandler#getOutputStream() + */ + @Override + public OutputStream getOutputStream() throws IOException { + return delegate.getOutputStream(); + } + + /* (non-Javadoc) + * @see javax.activation.DataHandler#getPreferredCommands() + */ + @Override + public CommandInfo[] getPreferredCommands() { + return delegate.getPreferredCommands(); + } + + /* (non-Javadoc) + * @see javax.activation.DataHandler#getTransferData(java.awt.datatransfer.DataFlavor) + */ + @Override + public Object getTransferData(DataFlavor paramDataFlavor) throws UnsupportedFlavorException, IOException { + return delegate.getTransferData(paramDataFlavor); + } + + /* (non-Javadoc) + * @see javax.activation.DataHandler#getTransferDataFlavors() + */ + @Override + public synchronized DataFlavor[] getTransferDataFlavors() { + return delegate.getTransferDataFlavors(); + } + + /* (non-Javadoc) + * @see javax.activation.DataHandler#isDataFlavorSupported(java.awt.datatransfer.DataFlavor) + */ + @Override + public boolean isDataFlavorSupported(DataFlavor paramDataFlavor) { + return delegate.isDataFlavorSupported(paramDataFlavor); + } + + /* (non-Javadoc) + * @see javax.activation.DataHandler#setCommandMap(javax.activation.CommandMap) + */ + @Override + public synchronized void setCommandMap(CommandMap paramCommandMap) { + delegate.setCommandMap(paramCommandMap); + } + + /* (non-Javadoc) + * @see javax.activation.DataHandler#writeTo(java.io.OutputStream) + */ + @Override + public void writeTo(OutputStream paramOutputStream) throws IOException { + delegate.writeTo(paramOutputStream); + } +} Added: axis/axis2/java/core/trunk/modules/kernel/test/org/apache/axis2/util/WrappedDataHandlerTest.java URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/kernel/test/org/apache/axis2/util/WrappedDataHandlerTest.java?rev=953352&view=auto ============================================================================== --- axis/axis2/java/core/trunk/modules/kernel/test/org/apache/axis2/util/WrappedDataHandlerTest.java (added) +++ axis/axis2/java/core/trunk/modules/kernel/test/org/apache/axis2/util/WrappedDataHandlerTest.java Thu Jun 10 15:33:03 2010 @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.axis2.util; + +import java.net.URL; +import javax.activation.DataHandler; +import junit.framework.TestCase; + +/** + * Test the WrappedDataHandler class. + */ +public class WrappedDataHandlerTest extends TestCase { + + /** + * Verify that the Wrapped DataHandler maintains the correct content-type value + * for an XML document attachment. + */ + public void testWrappedDataHandler() throws Exception { + URL xmlAttachment = new URL("file:./test-resources/soapmessage.xml"); + + DataHandler dh = new DataHandler(xmlAttachment); + assertTrue(dh.getContentType().equals("application/xml")); + + WrappedDataHandler wrappedDH = new WrappedDataHandler(dh, "text/xml"); + assertTrue(wrappedDH.getContentType() != null); + assertTrue(wrappedDH.getContentType().equals("text/xml")); + } +} \ No newline at end of file