Search in sources :

Example 61 with DataHandler

use of javax.activation.DataHandler in project webservices-axiom by apache.

the class MTOMSample method retrieveContent.

// START SNIPPET: retrieveContent
public void retrieveContent(URL serviceURL, String id, OutputStream result) throws Exception {
    // Build the SOAP request
    SOAPFactory soapFactory = OMAbstractFactory.getSOAP11Factory();
    SOAPEnvelope request = soapFactory.getDefaultEnvelope();
    OMElement retrieveContent = soapFactory.createOMElement(new QName("urn:test", "retrieveContent"), request.getBody());
    OMElement fileId = soapFactory.createOMElement(new QName("fileId"), retrieveContent);
    fileId.setText(id);
    // Use the java.net.URL API to connect to the service
    URLConnection connection = serviceURL.openConnection();
    connection.setDoOutput(true);
    connection.addRequestProperty("Content-Type", "text/xml; charset=UTF-8");
    OutputStream out = connection.getOutputStream();
    // Send the request
    OMOutputFormat format = new OMOutputFormat();
    format.setCharSetEncoding("UTF-8");
    request.serialize(out, format);
    out.close();
    // Get the SOAP response
    InputStream in = connection.getInputStream();
    MultipartBody multipartBody = MultipartBody.builder().setInputStream(in).setContentType(connection.getContentType()).build();
    SOAPEnvelope response = OMXMLBuilderFactory.createSOAPModelBuilder(multipartBody).getSOAPEnvelope();
    OMElement retrieveContentResponse = response.getBody().getFirstElement();
    OMElement content = retrieveContentResponse.getFirstElement();
    // Extract the DataHandler representing the optimized binary data
    DataHandler dh = ((OMText) content.getFirstOMChild()).getDataHandler();
    // Stream the content of the MIME part
    InputStream contentStream = ((PartDataHandler) dh).getPart().getInputStream(false);
    // Write the content to the result stream
    IOUtils.copy(contentStream, result);
    contentStream.close();
    in.close();
}
Also used : QName(javax.xml.namespace.QName) InputStream(java.io.InputStream) MultipartBody(org.apache.axiom.mime.MultipartBody) OutputStream(java.io.OutputStream) OMText(org.apache.axiom.om.OMText) OMElement(org.apache.axiom.om.OMElement) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) DataHandler(javax.activation.DataHandler) PartDataHandler(org.apache.axiom.mime.PartDataHandler) OMOutputFormat(org.apache.axiom.om.OMOutputFormat) SOAPFactory(org.apache.axiom.soap.SOAPFactory) URLConnection(java.net.URLConnection)

Example 62 with DataHandler

use of javax.activation.DataHandler in project webservices-axiom by apache.

the class MTOMStAXSOAPModelBuilderTest method testCreateOMElement.

public void testCreateOMElement() throws Exception {
    OMElement root = createTestMTOMMessage();
    OMElement body = (OMElement) root.getFirstOMChild();
    OMElement data = (OMElement) body.getFirstOMChild();
    Iterator childIt = data.getChildren();
    OMElement child = (OMElement) childIt.next();
    OMText blob = (OMText) child.getFirstOMChild();
    /*
         * Following is the procedure the user has to follow to read objects in
         * OBBlob User has to know the object type & whether it is serializable.
         * If it is not he has to use a Custom Defined DataSource to get the
         * Object.
         */
    byte[] expectedObject = new byte[] { 13, 56, 65, 32, 12, 12, 7, -3, -2, -1, 98 };
    DataHandler actualDH;
    actualDH = blob.getDataHandler();
//ByteArrayInputStream object = (ByteArrayInputStream) actualDH
//.getContent();
//byte[] actualObject= null;
//  object.read(actualObject,0,10);
//  assertEquals("Object check", expectedObject[5],actualObject[5] );
}
Also used : Iterator(java.util.Iterator) OMText(org.apache.axiom.om.OMText) OMElement(org.apache.axiom.om.OMElement) DataHandler(javax.activation.DataHandler)

Example 63 with DataHandler

use of javax.activation.DataHandler in project webservices-axiom by apache.

the class DataHandlerTest method test.

private void test(String feature) {
    DataHandler dh = new DataHandler(new TestDataSource('x', 1000));
    SOAPEnvelope envelope = OMAbstractFactory.getMetaFactory(feature).getSOAP11Factory().createDefaultSOAPMessage().getSOAPEnvelope();
    PayloadHelper.setBinaryPayload(envelope, dh);
    assertThat(PayloadHelper.getBinaryPayload(envelope)).isSameAs(dh);
}
Also used : TestDataSource(org.apache.axiom.testutils.activation.TestDataSource) DataHandler(javax.activation.DataHandler) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope)

Example 64 with DataHandler

use of javax.activation.DataHandler in project webservices-axiom by apache.

the class TestDataHandlerSerializationWithMTOM method runTest.

@Override
protected void runTest() throws Throwable {
    SOAPFactory factory = metaFactory.getSOAP11Factory();
    JAXBContext context = JAXBContext.newInstance(DocumentBean.class);
    // Construct the original message
    DocumentBean object = new DocumentBean();
    object.setId("123456");
    object.setContent(new DataHandler("some content", "text/plain; charset=utf-8"));
    SOAPEnvelope orgEnvelope = factory.getDefaultEnvelope();
    OMSourcedElement element = factory.createOMElement(new JAXBOMDataSource(context, object));
    orgEnvelope.getBody().addChild(element);
    // Serialize the message
    OMOutputFormat format = new OMOutputFormat();
    format.setDoOptimize(true);
    MemoryBlob blob = Blobs.createMemoryBlob();
    OutputStream out = blob.getOutputStream();
    orgEnvelope.serialize(out, format);
    out.close();
    assertFalse(element.isExpanded());
    // Parse the serialized message
    MultipartBody mb = MultipartBody.builder().setInputStream(blob.getInputStream()).setContentType(format.getContentType()).build();
    assertEquals(2, mb.getPartCount());
    SOAPEnvelope envelope = OMXMLBuilderFactory.createSOAPModelBuilder(metaFactory, mb).getSOAPEnvelope();
    OMElement contentElement = envelope.getBody().getFirstElement().getFirstChildWithName(new QName("http://ws.apache.org/axiom/test/jaxb", "content"));
    OMText content = (OMText) contentElement.getFirstOMChild();
    assertTrue(content.isBinary());
    assertTrue(content.isOptimized());
    DataHandler dh = content.getDataHandler();
    assertEquals("some content", dh.getContent());
}
Also used : MemoryBlob(org.apache.axiom.blob.MemoryBlob) QName(javax.xml.namespace.QName) OutputStream(java.io.OutputStream) JAXBContext(javax.xml.bind.JAXBContext) OMElement(org.apache.axiom.om.OMElement) DataHandler(javax.activation.DataHandler) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) OMSourcedElement(org.apache.axiom.om.OMSourcedElement) SOAPFactory(org.apache.axiom.soap.SOAPFactory) JAXBOMDataSource(org.apache.axiom.om.ds.jaxb.JAXBOMDataSource) MultipartBody(org.apache.axiom.mime.MultipartBody) DocumentBean(org.apache.axiom.ts.jaxb.beans.DocumentBean) OMText(org.apache.axiom.om.OMText) OMOutputFormat(org.apache.axiom.om.OMOutputFormat)

Example 65 with DataHandler

use of javax.activation.DataHandler in project webservices-axiom by apache.

the class TestBase64StreamingWithGetSAXSource method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    OMElement elem = factory.createOMElement("test", null);
    // Create a data source that would eat up all memory when loaded. If the test
    // doesn't fail with an OutOfMemoryError, we know that the OMText implementation
    // supports streaming.
    DataSource ds = new RandomDataSource(654321L, Runtime.getRuntime().maxMemory());
    OMText text = factory.createOMText(new DataHandler(ds), false);
    elem.addChild(text);
    SAXSource saxSource = elem.getSAXSource(true);
    XMLReader xmlReader = saxSource.getXMLReader();
    xmlReader.setContentHandler(new Base64Comparator(ds.getInputStream()));
    xmlReader.parse(saxSource.getInputSource());
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) RandomDataSource(org.apache.axiom.testutils.activation.RandomDataSource) SAXSource(javax.xml.transform.sax.SAXSource) OMText(org.apache.axiom.om.OMText) OMElement(org.apache.axiom.om.OMElement) DataHandler(javax.activation.DataHandler) XMLReader(org.xml.sax.XMLReader) RandomDataSource(org.apache.axiom.testutils.activation.RandomDataSource) DataSource(javax.activation.DataSource)

Aggregations

DataHandler (javax.activation.DataHandler)270 ByteArrayDataSource (javax.mail.util.ByteArrayDataSource)61 Test (org.junit.Test)59 InputStream (java.io.InputStream)57 DataSource (javax.activation.DataSource)53 MimeBodyPart (javax.mail.internet.MimeBodyPart)50 FileDataSource (javax.activation.FileDataSource)47 IOException (java.io.IOException)43 Exchange (org.apache.camel.Exchange)39 ByteArrayOutputStream (java.io.ByteArrayOutputStream)34 MimeMultipart (javax.mail.internet.MimeMultipart)33 ByteArrayInputStream (java.io.ByteArrayInputStream)32 MimeMessage (javax.mail.internet.MimeMessage)30 MessagingException (javax.mail.MessagingException)27 File (java.io.File)24 Message (org.apache.camel.Message)21 OMElement (org.apache.axiom.om.OMElement)20 QName (javax.xml.namespace.QName)19 URL (java.net.URL)17 ArrayList (java.util.ArrayList)17