Search in sources :

Example 91 with ByteArrayDataSource

use of javax.mail.util.ByteArrayDataSource in project cxf by apache.

the class SwANoMimeServiceImpl method echoDataRef.

public void echoDataRef(Holder<DataStruct> data) {
    try {
        InputStream bis = null;
        bis = data.value.getDataRef().getDataSource().getInputStream();
        byte[] b = new byte[6];
        bis.read(b, 0, 6);
        String string = IOUtils.newStringFromBytes(b);
        ByteArrayDataSource source = new ByteArrayDataSource(("test" + string).getBytes(), "application/octet-stream");
        data.value.setDataRef(new DataHandler(source));
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : InputStream(java.io.InputStream) DataHandler(javax.activation.DataHandler) IOException(java.io.IOException) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource)

Example 92 with ByteArrayDataSource

use of javax.mail.util.ByteArrayDataSource in project cxf by apache.

the class ClientMtomXopWithJMSTest method testMtomXop.

@Test
public void testMtomXop() throws Exception {
    TestMtom mtomPort = createPort(MTOM_SERVICE, MTOM_PORT, TestMtom.class, true);
    InputStream pre = this.getClass().getResourceAsStream("/wsdl/mtom_xop.wsdl");
    int fileSize = 0;
    for (int i = pre.read(); i != -1; i = pre.read()) {
        fileSize++;
    }
    Holder<DataHandler> param = new Holder<>();
    int count = 50;
    byte[] data = new byte[fileSize * count];
    for (int x = 0; x < count; x++) {
        this.getClass().getResourceAsStream("/wsdl/mtom_xop.wsdl").read(data, fileSize * x, fileSize);
    }
    param.value = new DataHandler(new ByteArrayDataSource(data, "application/octet-stream"));
    Holder<String> name = new Holder<>("call detail");
    mtomPort.testXop(name, param);
    // TODO Why should it fail here?
    // Assert.fail("Expect the exception here !");
    Assert.assertEquals("name unchanged", "return detail + call detail", name.value);
    Assert.assertNotNull(param.value);
    param.value.getInputStream().close();
}
Also used : InputStream(java.io.InputStream) Holder(javax.xml.ws.Holder) TestMtom(org.apache.cxf.mime.TestMtom) DataHandler(javax.activation.DataHandler) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) Endpoint(javax.xml.ws.Endpoint) Test(org.junit.Test)

Example 93 with ByteArrayDataSource

use of javax.mail.util.ByteArrayDataSource in project cxf by apache.

the class ClientMtomXopTest method testMtomWithValidationErrorOnServer.

@Test
public void testMtomWithValidationErrorOnServer() throws Exception {
    TestMtom mtomPort = createPort(MTOM_SERVICE, MTOM_PORT, TestMtom.class, true, true);
    try {
        Holder<DataHandler> param = new Holder<>();
        Holder<String> name;
        InputStream pre = this.getClass().getResourceAsStream("/wsdl/mtom_xop.wsdl");
        int fileSize = 0;
        for (int i = pre.read(); i != -1; i = pre.read()) {
            fileSize++;
        }
        int count = 1;
        byte[] data = new byte[fileSize * count];
        for (int x = 0; x < count; x++) {
            this.getClass().getResourceAsStream("/wsdl/mtom_xop.wsdl").read(data, fileSize * x, fileSize);
        }
        param.value = new DataHandler(new ByteArrayDataSource(data, "application/octet-stream"));
        // name length > 80 to break the schema
        // will throw exception on server side
        name = new Holder<>("break schema");
        ClientProxy.getClient(mtomPort).getInInterceptors().add(new LoggingInInterceptor());
        ClientProxy.getClient(mtomPort).getOutInterceptors().add(new LoggingOutInterceptor());
        ((HTTPConduit) ClientProxy.getClient(mtomPort).getConduit()).getClient().setReceiveTimeout(60000);
        mtomPort.testXop(name, param);
        fail("should throw javax.xml.ws.soap.SOAPFaultException");
    } catch (javax.xml.ws.soap.SOAPFaultException ex) {
        assertTrue(ex.getMessage().contains("cvc-maxLength-valid"));
    } catch (Exception ex) {
        fail("should throw javax.xml.ws.soap.SOAPFaultException");
    }
}
Also used : InputStream(java.io.InputStream) Holder(javax.xml.ws.Holder) TestMtom(org.apache.cxf.mime.TestMtom) DataHandler(javax.activation.DataHandler) Endpoint(org.apache.cxf.endpoint.Endpoint) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) LoggingOutInterceptor(org.apache.cxf.ext.logging.LoggingOutInterceptor) LoggingInInterceptor(org.apache.cxf.ext.logging.LoggingInInterceptor) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) Test(org.junit.Test)

Example 94 with ByteArrayDataSource

use of javax.mail.util.ByteArrayDataSource in project cxf by apache.

the class ClientMtomXopTest method testMtomXopProvider.

@Test
public void testMtomXopProvider() throws Exception {
    TestMtom mtomPort = createPort(MTOM_SERVICE, MTOM_PORT_PROVIDER, TestMtom.class, true, true);
    try {
        Holder<DataHandler> param = new Holder<>();
        Holder<String> name;
        byte[] bytes;
        InputStream in;
        InputStream pre = this.getClass().getResourceAsStream("/wsdl/mtom_xop.wsdl");
        int fileSize = 0;
        for (int i = pre.read(); i != -1; i = pre.read()) {
            fileSize++;
        }
        int count = 50;
        byte[] data = new byte[fileSize * count];
        for (int x = 0; x < count; x++) {
            this.getClass().getResourceAsStream("/wsdl/mtom_xop.wsdl").read(data, fileSize * x, fileSize);
        }
        Object[] validationTypes = new Object[] { Boolean.TRUE, SchemaValidationType.IN, SchemaValidationType.BOTH };
        for (Object validationType : validationTypes) {
            ((BindingProvider) mtomPort).getRequestContext().put(Message.SCHEMA_VALIDATION_ENABLED, validationType);
            param.value = new DataHandler(new ByteArrayDataSource(data, "application/octet-stream"));
            name = new Holder<>("call detail");
            mtomPort.testXop(name, param);
            assertEquals("name unchanged", "return detail + call detail", name.value);
            assertNotNull(param.value);
            in = param.value.getInputStream();
            bytes = IOUtils.readBytesFromStream(in);
            assertEquals(data.length, bytes.length);
            in.close();
            param.value = new DataHandler(new ByteArrayDataSource(data, "application/octet-stream"));
            name = new Holder<>("call detail");
            mtomPort.testXop(name, param);
            assertEquals("name unchanged", "return detail + call detail", name.value);
            assertNotNull(param.value);
            in = param.value.getInputStream();
            bytes = IOUtils.readBytesFromStream(in);
            assertEquals(data.length, bytes.length);
            in.close();
        }
        validationTypes = new Object[] { Boolean.FALSE, SchemaValidationType.OUT, SchemaValidationType.NONE };
        for (Object validationType : validationTypes) {
            ((BindingProvider) mtomPort).getRequestContext().put(Message.SCHEMA_VALIDATION_ENABLED, validationType);
            SAAJOutInterceptor saajOut = new SAAJOutInterceptor();
            SAAJInInterceptor saajIn = new SAAJInInterceptor();
            param.value = new DataHandler(new ByteArrayDataSource(data, "application/octet-stream"));
            name = new Holder<>("call detail");
            mtomPort.testXop(name, param);
            assertEquals("name unchanged", "return detail + call detail", name.value);
            assertNotNull(param.value);
            in = param.value.getInputStream();
            bytes = IOUtils.readBytesFromStream(in);
            assertEquals(data.length, bytes.length);
            in.close();
            ClientProxy.getClient(mtomPort).getInInterceptors().add(saajIn);
            ClientProxy.getClient(mtomPort).getInInterceptors().add(saajOut);
            param.value = new DataHandler(new ByteArrayDataSource(data, "application/octet-stream"));
            name = new Holder<>("call detail");
            mtomPort.testXop(name, param);
            assertEquals("name unchanged", "return detail + call detail", name.value);
            assertNotNull(param.value);
            in = param.value.getInputStream();
            bytes = IOUtils.readBytesFromStream(in);
            assertEquals(data.length, bytes.length);
            in.close();
            ClientProxy.getClient(mtomPort).getInInterceptors().remove(saajIn);
            ClientProxy.getClient(mtomPort).getInInterceptors().remove(saajOut);
        }
    } catch (UndeclaredThrowableException ex) {
        throw (Exception) ex.getCause();
    } catch (Exception ex) {
        System.out.println(System.getProperties());
        throw ex;
    }
}
Also used : SAAJOutInterceptor(org.apache.cxf.binding.soap.saaj.SAAJOutInterceptor) InputStream(java.io.InputStream) Holder(javax.xml.ws.Holder) TestMtom(org.apache.cxf.mime.TestMtom) DataHandler(javax.activation.DataHandler) Endpoint(org.apache.cxf.endpoint.Endpoint) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) SAAJInInterceptor(org.apache.cxf.binding.soap.saaj.SAAJInInterceptor) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) Test(org.junit.Test)

Example 95 with ByteArrayDataSource

use of javax.mail.util.ByteArrayDataSource in project cxf by apache.

the class TestMtomProviderImpl method invoke.

public SOAPMessage invoke(final SOAPMessage request) {
    try {
        System.out.println("=== Received client request ===");
        // create the SOAPMessage
        SOAPMessage message = MessageFactory.newInstance().createMessage();
        SOAPPart part = message.getSOAPPart();
        SOAPEnvelope envelope = part.getEnvelope();
        SOAPBody body = envelope.getBody();
        SOAPBodyElement testResponse = body.addBodyElement(envelope.createName("testXopResponse", null, "http://cxf.apache.org/mime/types"));
        SOAPElement name = testResponse.addChildElement("name", null, "http://cxf.apache.org/mime/types");
        name.setTextContent("return detail + call detail");
        SOAPElement attachinfo = testResponse.addChildElement("attachinfo", null, "http://cxf.apache.org/mime/types");
        SOAPElement include = attachinfo.addChildElement("Include", "xop", "http://www.w3.org/2004/08/xop/include");
        int fileSize = 0;
        try (InputStream pre = this.getClass().getResourceAsStream("/wsdl/mtom_xop.wsdl")) {
            for (int i = pre.read(); i != -1; i = pre.read()) {
                fileSize++;
            }
        }
        int count = 50;
        byte[] data = new byte[fileSize * count];
        for (int x = 0; x < count; x++) {
            this.getClass().getResourceAsStream("/wsdl/mtom_xop.wsdl").read(data, fileSize * x, fileSize);
        }
        DataHandler dh = new DataHandler(new ByteArrayDataSource(data, "application/octet-stream"));
        // create the image attachment
        AttachmentPart attachment = message.createAttachmentPart(dh);
        attachment.setContentId("mtom_xop.wsdl");
        message.addAttachmentPart(attachment);
        System.out.println("Adding attachment: " + attachment.getContentId() + ":" + attachment.getSize());
        // add the reference to the image attachment
        include.addAttribute(envelope.createName("href"), "cid:" + attachment.getContentId());
        return message;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : InputStream(java.io.InputStream) AttachmentPart(javax.xml.soap.AttachmentPart) SOAPEnvelope(javax.xml.soap.SOAPEnvelope) DataHandler(javax.activation.DataHandler) SOAPMessage(javax.xml.soap.SOAPMessage) SOAPBody(javax.xml.soap.SOAPBody) SOAPPart(javax.xml.soap.SOAPPart) SOAPElement(javax.xml.soap.SOAPElement) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) SOAPBodyElement(javax.xml.soap.SOAPBodyElement)

Aggregations

ByteArrayDataSource (javax.mail.util.ByteArrayDataSource)119 DataHandler (javax.activation.DataHandler)82 MimeMultipart (javax.mail.internet.MimeMultipart)39 InputStream (java.io.InputStream)38 IOException (java.io.IOException)35 MimeBodyPart (javax.mail.internet.MimeBodyPart)35 DataSource (javax.activation.DataSource)32 MimeMessage (javax.mail.internet.MimeMessage)31 ByteArrayOutputStream (java.io.ByteArrayOutputStream)25 Test (org.junit.Test)25 MessagingException (javax.mail.MessagingException)21 ByteArrayInputStream (java.io.ByteArrayInputStream)17 ArrayList (java.util.ArrayList)17 InternetAddress (javax.mail.internet.InternetAddress)16 Session (javax.mail.Session)14 Properties (java.util.Properties)12 Date (java.util.Date)11 List (java.util.List)8 Multipart (javax.mail.Multipart)8 Holder (javax.xml.ws.Holder)8