Example usage for javax.activation DataHandler getName

List of usage examples for javax.activation DataHandler getName

Introduction

In this page you can find the example usage for javax.activation DataHandler getName.

Prototype

public String getName() 

Source Link

Document

Return the name of the data object.

Usage

From source file:org.wso2.carbon.attachment.mgt.core.dao.impl.jpa.openjpa.entity.AttachmentDAOImpl.java

@Override
public void setContent(DataHandler content) throws AttachmentMgtException {
    try {// w w  w  .  j a  v  a2  s. c  o  m
        //Here we are giving priority to setContentType() and setName(). If those values are already assigned,
        // then name and contentType in the content DataHandler will be ignored.
        if (this.name == null) {
            setName(content.getName());
        }
        if (this.contentType == null) {
            setContentType(content.getContentType());
        }

        this.content = IOUtils.toByteArray(content.getInputStream());
    } catch (IOException e) {
        log.error(e.getLocalizedMessage(), e);
        throw new AttachmentMgtException(e.getLocalizedMessage(), e);
    }
}

From source file:org.hubiquitus.hubotsdk.adapters.HHttpAdapterInbox.java

@Override
public void process(Exchange exchange) throws Exception {
    Message in = exchange.getIn();/*from w  w  w .  ja  v  a2  s.co m*/

    HttpServletRequest request = exchange.getIn().getBody(HttpServletRequest.class);

    //Gather data to send through an hmessage
    byte[] rawBody = (byte[]) in.getBody(byte[].class);
    Map<String, Object> headers = in.getHeaders();
    Map<String, DataHandler> attachments = in.getAttachments();

    String method = request.getMethod().toLowerCase();
    String queryArgs = request.getQueryString();
    String queryPath = request.getRequestURI();
    String serverName = request.getServerName();
    Integer serverPort = request.getServerPort();

    //create message to send
    HMessage message = new HMessage();
    message.setAuthor(this.actor);
    if (headers != null) {
        JSONObject jsonHeaders = new JSONObject();
        for (String key : headers.keySet()) {
            Object header = headers.get(key);
            String value = null;
            if (header != null) {
                value = header.toString();
            }
            jsonHeaders.put(key, value);
        }
    }

    message.setPublished(new DateTime());

    message.setType("hHttpData");
    //create payload
    HHttpData httpData = new HHttpData();
    httpData.setMethod(method);
    httpData.setQueryArgs(queryArgs);
    httpData.setQueryPath(queryPath);
    httpData.setServerName(serverName);
    httpData.setServerPort(serverPort);
    httpData.setRawBody(rawBody);

    //create attachements
    JSONObject hattachements = new JSONObject();
    for (String key : attachments.keySet()) {
        DataHandler attachement = attachments.get(key);
        if (attachement != null) {
            HHttpAttachement hattachement = new HHttpAttachement();
            hattachement.setContentType(attachement.getContentType());
            hattachement.setName(attachement.getName());

            //read attachement
            byte[] buffer = new byte[8 * 1024];
            InputStream input = attachement.getInputStream();

            ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
            int bytesRead;
            while ((bytesRead = input.read(buffer)) != -1) {
                byteOutputStream.write(buffer, 0, bytesRead);
            }
            hattachement.setData(byteOutputStream.toByteArray());
            try {
                hattachements.put(key, hattachement);
            } catch (JSONException e) {
                logger.debug(e.toString());
            }
        } else {
            //if attachment is null, do not put the key in hattachments.
        }
    }

    httpData.setAttachments(hattachements);
    message.setPayload(httpData);

    //finally send message to actor
    this.put(message);
}

From source file:org.apache.camel.component.jetty.MultiPartFormTestWithCustomFilter.java

protected RouteBuilder createRouteBuilder() throws Exception {
    return new RouteBuilder() {
        public void configure() throws Exception {
            // START SNIPPET: e1
            // Set the jetty temp directory which store the file for multi part form
            // camel-jetty will clean up the file after it handled the request.
            // The option works rightly from Camel 2.4.0
            getContext().getProperties().put("CamelJettyTempDir", "target");

            from("jetty://http://localhost:9080/test?multipartFilterRef=myMultipartFilter")
                    .process(new Processor() {
                        public void process(Exchange exchange) throws Exception {
                            Message in = exchange.getIn();
                            assertEquals("Get a wrong attachement size", 1, in.getAttachments().size());
                            // The file name is attachment id
                            DataHandler data = in.getAttachment("NOTICE.txt");

                            assertNotNull("Should get the DataHandle NOTICE.txt", data);
                            assertEquals("Get a wrong content type", "text/plain", data.getContentType());
                            assertEquals("Got the wrong name", "NOTICE.txt", data.getName());

                            assertTrue("We should get the data from the DataHandle",
                                    data.getDataSource().getInputStream().available() > 0);

                            // The other form date can be get from the message header
                            exchange.getOut().setBody(in.getHeader("comment"));
                        }/*from w  w  w  .  j  a  v  a 2 s .  c o  m*/
                    });
            // END SNIPPET: e1

            // Test to ensure that setting a multipartFilterRef overrides the enableMultipartFilter=false parameter
            from("jetty://http://localhost:9080/test2?multipartFilterRef=myMultipartFilter&enableMultipartFilter=false")
                    .process(new Processor() {
                        public void process(Exchange exchange) throws Exception {
                            Message in = exchange.getIn();
                            assertEquals("Get a wrong attachement size", 1, in.getAttachments().size());
                            DataHandler data = in.getAttachment("NOTICE.txt");

                            assertNotNull("Should get the DataHandle NOTICE.txt", data);
                            // The other form date can be get from the message header
                            exchange.getOut().setBody(in.getHeader("comment"));
                        }
                    });
        }
    };
}

From source file:se.inera.axel.shs.camel.CamelShsDataPartConverterTest.java

@DirtiesContext
@Test/*from   w  w  w  .  ja v  a 2 s  . c  om*/
public void convertCamelMessageToDataPartWithoutFilename() throws Exception {

    resultEndpoint.expectedMessageCount(1);

    String message = DEFAULT_TEST_BODY;

    Map<String, Object> headers = new HashMap<String, Object>();
    headers.put(ShsHeaders.DATAPART_TRANSFERENCODING, DEFAULT_TEST_DATAPART_TRANSFERENCODING);
    headers.put(ShsHeaders.DATAPART_CONTENTTYPE, DEFAULT_TEST_DATAPART_CONTENTTYPE);
    headers.put(ShsHeaders.DATAPART_CONTENTLENGTH, message.length());
    headers.put(ShsHeaders.DATAPART_TYPE, DEFAULT_TEST_DATAPART_TYPE);

    template.sendBodyAndHeaders("direct:camelToShsConverter", message, headers);

    resultEndpoint.assertIsSatisfied();
    List<Exchange> exchanges = resultEndpoint.getExchanges();
    Exchange exchange = exchanges.get(0);
    Message in = exchange.getIn();
    DataPart datapart = in.getMandatoryBody(DataPart.class);
    assertNotNull(datapart);

    assertEquals((long) datapart.getContentLength(), message.length());
    assertEquals(datapart.getContentType(), DEFAULT_TEST_DATAPART_CONTENTTYPE);
    assertEquals(datapart.getDataPartType(), DEFAULT_TEST_DATAPART_TYPE);
    assertNull(datapart.getFileName());
    assertEquals(datapart.getTransferEncoding(), DEFAULT_TEST_DATAPART_TRANSFERENCODING);
    assertNotNull(datapart.getDataHandler());

    DataHandler dataHandler = datapart.getDataHandler();
    assertEquals(dataHandler.getContentType(), DEFAULT_TEST_DATAPART_CONTENTTYPE);
    assertNull(dataHandler.getName());
    assertEquals(dataHandler.getContent(), DEFAULT_TEST_BODY);
}

From source file:se.inera.axel.shs.camel.CamelShsDataPartConverterTest.java

@DirtiesContext
@Test/*from   ww  w .  j  a v  a 2 s.c o m*/
public void convertCamelMessageToDataPart() throws Exception {

    resultEndpoint.expectedMessageCount(1);

    String message = DEFAULT_TEST_BODY;

    Map<String, Object> headers = new HashMap<String, Object>();
    headers.put(ShsHeaders.DATAPART_TRANSFERENCODING, DEFAULT_TEST_DATAPART_TRANSFERENCODING);
    headers.put(ShsHeaders.DATAPART_CONTENTTYPE, DEFAULT_TEST_DATAPART_CONTENTTYPE);
    headers.put(ShsHeaders.DATAPART_FILENAME, DEFAULT_TEST_DATAPART_FILENAME);
    headers.put(ShsHeaders.DATAPART_CONTENTLENGTH, message.length());
    headers.put(ShsHeaders.DATAPART_TYPE, DEFAULT_TEST_DATAPART_TYPE);

    template.sendBodyAndHeaders("direct:camelToShsConverter", message, headers);

    resultEndpoint.assertIsSatisfied();
    List<Exchange> exchanges = resultEndpoint.getExchanges();
    Exchange exchange = exchanges.get(0);
    Message in = exchange.getIn();
    DataPart datapart = in.getMandatoryBody(DataPart.class);
    assertNotNull(datapart);

    assertEquals((long) datapart.getContentLength(), message.length());
    assertEquals(datapart.getContentType(), DEFAULT_TEST_DATAPART_CONTENTTYPE);
    assertEquals(datapart.getDataPartType(), DEFAULT_TEST_DATAPART_TYPE);
    assertEquals(datapart.getFileName(), DEFAULT_TEST_DATAPART_FILENAME);
    assertEquals(datapart.getTransferEncoding(), DEFAULT_TEST_DATAPART_TRANSFERENCODING);
    assertNotNull(datapart.getDataHandler());

    DataHandler dataHandler = datapart.getDataHandler();
    assertEquals(dataHandler.getContentType(), DEFAULT_TEST_DATAPART_CONTENTTYPE);
    assertEquals(dataHandler.getName(), DEFAULT_TEST_DATAPART_FILENAME);
    assertEquals(dataHandler.getContent(), DEFAULT_TEST_BODY);
}

From source file:se.inera.axel.shs.camel.CamelShsDataPartConverterTest.java

@DirtiesContext
@Test//  w w w .  j  av a 2  s  . c  o m
public void convertCamelMessageToDataPartWithBase64() throws Exception {

    resultEndpoint.expectedMessageCount(1);

    String message = DEFAULT_TEST_BODY;

    Map<String, Object> headers = new HashMap<String, Object>();
    headers.put(ShsHeaders.DATAPART_TRANSFERENCODING, "base64");
    headers.put(ShsHeaders.DATAPART_CONTENTTYPE, DEFAULT_TEST_DATAPART_CONTENTTYPE);
    headers.put(ShsHeaders.DATAPART_FILENAME, DEFAULT_TEST_DATAPART_FILENAME);
    headers.put(ShsHeaders.DATAPART_CONTENTLENGTH, message.length());
    headers.put(ShsHeaders.DATAPART_TYPE, DEFAULT_TEST_DATAPART_TYPE);

    template.sendBodyAndHeaders("direct:camelToShsConverter", message, headers);

    resultEndpoint.assertIsSatisfied();
    List<Exchange> exchanges = resultEndpoint.getExchanges();
    Exchange exchange = exchanges.get(0);
    Message in = exchange.getIn();
    DataPart datapart = in.getMandatoryBody(DataPart.class);
    assertNotNull(datapart);

    assertEquals((long) datapart.getContentLength(), message.length());
    assertEquals(datapart.getContentType(), DEFAULT_TEST_DATAPART_CONTENTTYPE);
    assertEquals(datapart.getDataPartType(), DEFAULT_TEST_DATAPART_TYPE);
    assertEquals(datapart.getFileName(), DEFAULT_TEST_DATAPART_FILENAME);
    assertEquals(datapart.getTransferEncoding(), "base64");

    assertNotNull(datapart.getDataHandler());
    DataHandler dataHandler = datapart.getDataHandler();
    assertEquals(dataHandler.getContentType(), DEFAULT_TEST_DATAPART_CONTENTTYPE);
    assertEquals(dataHandler.getName(), DEFAULT_TEST_DATAPART_FILENAME);
    assertEquals(dataHandler.getContent(), DEFAULT_TEST_BODY);
}

From source file:se.inera.axel.shs.camel.CamelShsDataPartConverterTest.java

@DirtiesContext
@Test(expectedExceptions = IllegalDatapartContentException.class)
public void convertCamelMessageToDataPartWithNoDataPartTypeShouldThrow() throws Throwable {

    resultEndpoint.expectedMessageCount(1);

    String message = DEFAULT_TEST_BODY;

    Map<String, Object> headers = new HashMap<String, Object>();
    headers.put(ShsHeaders.DATAPART_TRANSFERENCODING, "base64");
    headers.put(ShsHeaders.DATAPART_CONTENTTYPE, DEFAULT_TEST_DATAPART_CONTENTTYPE);
    headers.put(ShsHeaders.DATAPART_FILENAME, DEFAULT_TEST_DATAPART_FILENAME);
    headers.put(ShsHeaders.DATAPART_CONTENTLENGTH, message.length());

    try {/*  w w w  . j  a v a  2  s .c  o m*/
        template.sendBodyAndHeaders("direct:camelToShsConverter", message, headers);
    } catch (CamelExecutionException e) {
        throw e.getCause();
    }

    resultEndpoint.assertIsSatisfied();
    List<Exchange> exchanges = resultEndpoint.getExchanges();
    Exchange exchange = exchanges.get(0);
    Message in = exchange.getIn();
    DataPart datapart = in.getMandatoryBody(DataPart.class);
    assertNotNull(datapart);

    assertEquals((long) datapart.getContentLength(), message.length());
    assertEquals(datapart.getContentType(), DEFAULT_TEST_DATAPART_CONTENTTYPE);
    assertEquals(datapart.getDataPartType(), DEFAULT_TEST_DATAPART_TYPE);
    assertEquals(datapart.getFileName(), DEFAULT_TEST_DATAPART_FILENAME);
    assertEquals(datapart.getTransferEncoding(), "base64");

    assertNotNull(datapart.getDataHandler());
    DataHandler dataHandler = datapart.getDataHandler();
    assertEquals(dataHandler.getContentType(), DEFAULT_TEST_DATAPART_CONTENTTYPE);
    assertEquals(dataHandler.getName(), DEFAULT_TEST_DATAPART_FILENAME);
    assertEquals(dataHandler.getContent(), DEFAULT_TEST_BODY);
}

From source file:edu.mit.broad.genepattern.gp.services.GenePatternClientImpl.java

private HashMap<String, DataHandler> getFileDataHandlers(ParameterInfo[] parameters) {
    HashMap<String, DataHandler> handlers = new HashMap<String, DataHandler>();
    for (ParameterInfo parameter : parameters) {
        if (isFileParameter(parameter)) {
            DataHandler handler = getFileDataHandler(parameter);
            handlers.put(handler.getName(), handler);
        }/*from w  w w .  j a  v a 2  s . c  o  m*/
    }
    return handlers;
}

From source file:org.wso2.carbon.admin.service.AdminServiceJARServiceUploader.java

public void uploadJARServiceFile(String sessionCookie, String serviceGroup, DataHandler dhJar,
        DataHandler dhWsdl) throws JarUploadExceptionException, RemoteException,
        DuplicateServiceGroupExceptionException, DuplicateServiceExceptionException {

    Resource resourceData;/*  w ww  . ja va  2  s  . c o  m*/
    Resource resourceDataWsdl;
    UploadArtifactsResponse uploadArtifactsResponse;

    new AuthenticateStub().authenticateStub(sessionCookie, jarServiceCreatorAdminStub);

    resourceData = new Resource();
    resourceData.setFileName(dhJar.getName().substring(dhJar.getName().lastIndexOf('/') + 1));
    resourceData.setDataHandler(dhJar);

    if (dhWsdl != null) {
        resourceDataWsdl = new Resource();
        resourceDataWsdl.setFileName(dhWsdl.getName().substring(dhWsdl.getName().lastIndexOf('/') + 1));
        resourceDataWsdl.setDataHandler(dhWsdl);
    } else {
        resourceDataWsdl = null;
    }

    uploadArtifactsResponse = jarServiceCreatorAdminStub.upload(serviceGroup, resourceDataWsdl,
            new Resource[] { resourceData });

    Service[] service = uploadArtifactsResponse.getServices();
    for (int i = 0; i < service.length; i++) {
        Service temp = service[i];
        temp.setUseOriginalWsdl(false);
        temp.setDeploymentScope("request");
    }

    jarServiceCreatorAdminStub.createAndDeployService(uploadArtifactsResponse.getResourcesDirPath(), "",
            serviceGroup, service);
    log.info("Artifact uploaded");

}

From source file:se.inera.axel.shs.camel.CamelShsDataPartConverterTest.java

@DirtiesContext
@Test//from  w ww. j av  a 2s  .  co  m
public void convertCamelMessageToDataPartWithFilenameInContentType() throws Exception {

    resultEndpoint.expectedMessageCount(1);

    String message = DEFAULT_TEST_BODY;

    Map<String, Object> headers = new HashMap<String, Object>();
    headers.put(ShsHeaders.DATAPART_TRANSFERENCODING, DEFAULT_TEST_DATAPART_TRANSFERENCODING);
    headers.put(ShsHeaders.DATAPART_CONTENTTYPE,
            DEFAULT_TEST_DATAPART_CONTENTTYPE + ";name=" + DEFAULT_TEST_DATAPART_FILENAME);
    headers.put(ShsHeaders.DATAPART_CONTENTLENGTH, message.length());
    headers.put(ShsHeaders.DATAPART_TYPE, DEFAULT_TEST_DATAPART_TYPE);

    template.sendBodyAndHeaders("direct:camelToShsConverter", message, headers);

    resultEndpoint.assertIsSatisfied();
    List<Exchange> exchanges = resultEndpoint.getExchanges();
    Exchange exchange = exchanges.get(0);
    Message in = exchange.getIn();
    DataPart datapart = in.getMandatoryBody(DataPart.class);
    assertNotNull(datapart);

    assertEquals((long) datapart.getContentLength(), message.length());
    assertEquals(datapart.getContentType(),
            DEFAULT_TEST_DATAPART_CONTENTTYPE + ";name=" + DEFAULT_TEST_DATAPART_FILENAME);
    assertEquals(datapart.getDataPartType(), DEFAULT_TEST_DATAPART_TYPE);
    assertEquals(datapart.getFileName(), DEFAULT_TEST_DATAPART_FILENAME);
    assertEquals(datapart.getTransferEncoding(), DEFAULT_TEST_DATAPART_TRANSFERENCODING);

    assertNotNull(datapart.getDataHandler());
    DataHandler dataHandler = datapart.getDataHandler();
    assertEquals(dataHandler.getContentType(),
            DEFAULT_TEST_DATAPART_CONTENTTYPE + ";name=" + DEFAULT_TEST_DATAPART_FILENAME);
    assertEquals(dataHandler.getName(), DEFAULT_TEST_DATAPART_FILENAME);
    assertEquals(dataHandler.getContent(), DEFAULT_TEST_BODY);

}