Author: dkulp Date: Mon Apr 21 09:05:31 2008 New Revision: 650181 URL: http://svn.apache.org/viewvc?rev=650181&view=rev Log: [CXF-1524] Workaround bug in sun SAAJ implementation that is causing attachments to not work well with Dispatch clients
Modified: incubator/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/interceptors/DispatchInDatabindingInterceptor.java incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/swa/ClientServerSwaTest.java Modified: incubator/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/interceptors/DispatchInDatabindingInterceptor.java URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/interceptors/DispatchInDatabindingInterceptor.java?rev=650181&r1=650180&r2=650181&view=diff ============================================================================== --- incubator/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/interceptors/DispatchInDatabindingInterceptor.java (original) +++ incubator/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/interceptors/DispatchInDatabindingInterceptor.java Mon Apr 21 09:05:31 2008 @@ -115,6 +115,16 @@ if (message instanceof SoapMessage) { SOAPMessage soapMessage = newSOAPMessage(is, (SoapMessage)message); + //workaround bugs in SAAJ + //calling getSOAPBody does wacky things with the InputStream so + //attachements can be lost. Count them first to make sure they + //are properly sucked in. + soapMessage.countAttachments(); + + //This seems to be a problem in SAAJ. Envelope might not be initialized + //properly without calling getEnvelope() + soapMessage.getSOAPPart().getEnvelope(); + if (soapMessage.getSOAPBody().hasFault()) { Endpoint ep = message.getExchange().get(Endpoint.class); message.getInterceptorChain().abort(); Modified: incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/swa/ClientServerSwaTest.java URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/swa/ClientServerSwaTest.java?rev=650181&r1=650180&r2=650181&view=diff ============================================================================== --- incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/swa/ClientServerSwaTest.java (original) +++ incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/swa/ClientServerSwaTest.java Mon Apr 21 09:05:31 2008 @@ -25,10 +25,18 @@ import javax.activation.DataHandler; import javax.imageio.ImageIO; import javax.mail.util.ByteArrayDataSource; +import javax.xml.namespace.QName; +import javax.xml.soap.AttachmentPart; +import javax.xml.soap.MessageFactory; +import javax.xml.soap.SOAPBody; +import javax.xml.soap.SOAPMessage; import javax.xml.transform.Source; import javax.xml.transform.stream.StreamSource; +import javax.xml.ws.Dispatch; import javax.xml.ws.Holder; +import javax.xml.ws.Service; +import org.apache.cxf.helpers.IOUtils; import org.apache.cxf.swa.SwAService; import org.apache.cxf.swa.SwAServiceInterface; import org.apache.cxf.swa.types.DataStruct; @@ -170,4 +178,68 @@ assertNotNull(response); } + @Test + public void testSwaTypesWithDispatchAPI() throws Exception { + if (Boolean.getBoolean("java.awt.headless")) { + System.out.println("Running headless. Skipping test as Images may not work."); + return; + } + + URL url1 = this.getClass().getResource("resources/attach.text"); + URL url2 = this.getClass().getResource("resources/attach.html"); + URL url3 = this.getClass().getResource("resources/attach.xml"); + URL url4 = this.getClass().getResource("resources/attach.jpeg1"); + URL url5 = this.getClass().getResource("resources/attach.jpeg2"); + + + byte[] bytes = IOUtils.readBytesFromStream(url1.openStream()); + byte[] bigBytes = new byte[bytes.length * 50]; + for (int x = 0; x < 50; x++) { + System.arraycopy(bytes, 0, bigBytes, x * bytes.length, bytes.length); + } + + DataHandler dh1 = new DataHandler(new ByteArrayDataSource(bigBytes, "text/plain")); + DataHandler dh2 = new DataHandler(url2); + DataHandler dh3 = new DataHandler(url3); + DataHandler dh4 = new DataHandler(url4); + DataHandler dh5 = new DataHandler(url5); + + SwAService service = new SwAService(); + + Dispatch<SOAPMessage> disp = service + .createDispatch(SwAService.SwAServiceHttpPort, + SOAPMessage.class, + Service.Mode.MESSAGE); + + + SOAPMessage msg = MessageFactory.newInstance().createMessage(); + SOAPBody body = msg.getSOAPPart().getEnvelope().getBody(); + body.addBodyElement(new QName("http://cxf.apache.org/swa/types", + "VoidRequest")); + + AttachmentPart att = msg.createAttachmentPart(dh1); + att.setContentId("<[EMAIL PROTECTED]>"); + msg.addAttachmentPart(att); + + att = msg.createAttachmentPart(dh2); + att.setContentId("<[EMAIL PROTECTED]>"); + msg.addAttachmentPart(att); + + att = msg.createAttachmentPart(dh3); + att.setContentId("<[EMAIL PROTECTED]>"); + msg.addAttachmentPart(att); + + att = msg.createAttachmentPart(dh4); + att.setContentId("<[EMAIL PROTECTED]>"); + msg.addAttachmentPart(att); + + att = msg.createAttachmentPart(dh5); + att.setContentId("<[EMAIL PROTECTED]>"); + msg.addAttachmentPart(att); + + //Test for CXF- + msg = disp.invoke(msg); + assertEquals(5, msg.countAttachments()); + + } }