Example usage for javax.activation DataHandler getContentType

List of usage examples for javax.activation DataHandler getContentType

Introduction

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

Prototype

public String getContentType() 

Source Link

Document

Return the MIME type of this object as retrieved from the source object.

Usage

From source file:org.shredzone.cilla.admin.AbstractImageBean.java

/**
 * Creates a {@link StreamedContent} for the given {@link DataHandler}.
 *
 * @param dh//from www  .  ja  va2s. c o m
 *            {@link DataHandler} to stream
 * @return {@link StreamedContent} containing that image
 */
protected StreamedContent createStreamedContent(DataHandler dh) {
    if (dh != null) {
        try {
            return new DefaultStreamedContent(dh.getInputStream(), dh.getContentType());
        } catch (IOException ex) {
            log.error("Exception while streaming content", ex);
        }
    }
    return createEmptyStreamedContent();
}

From source file:org.apache.axis2.jaxws.marshaller.impl.alt.Attachment.java

/**
 * Constructor used to set Attachment from wire unmarshalling
 * @param dh//from   ww  w  .  ja  v a  2s.com
 * @param cid
 */
public Attachment(DataHandler dh, String cid) {
    if (log.isDebugEnabled()) {
        System.out.println("Construct with dh=" + dh.getContentType() + " cid=" + cid);
    }
    this.dh = dh;
    this.cid = cid;
}

From source file:org.openehealth.ipf.platform.camel.lbs.cxf.process.CxfPojoResourceHandler.java

private ResourceDataSource extractFromDataHandler(String subUnit, DataHandler handler, int paramIdx)
        throws IOException {
    InputStream inputStream = handler.getInputStream();
    try {//  w w w . j  a v  a2s  .  c om
        String contentType = handler.getContentType();
        String id = RESOURCE_ID_PARAM_PREFIX + paramIdx;
        return resourceFactory.createResource(subUnit, contentType, null, id, inputStream);
    } finally {
        inputStream.close();
    }
}

From source file:org.wso2.carbon.application.mgt.ui.ApplicationAdminClient.java

public void downloadCappArchive(String filename, HttpServletResponse response)
        throws IOException, ApplicationAdminExceptionException {

    ServletOutputStream out = response.getOutputStream();
    DataHandler dataHandler = stub.downloadCappArchive(filename);
    if (dataHandler != null) {
        response.setHeader("Content-Disposition", "fileName=" + filename + ".car");
        response.setContentType(dataHandler.getContentType());
        InputStream in = dataHandler.getDataSource().getInputStream();
        int nextChar;
        while ((nextChar = in.read()) != -1) {
            out.write((char) nextChar);
        }//from  w  ww.j a  v a2  s  . com
        out.flush();
        in.close();
        out.close();
    } else {
        out.write("The requested capp archive was not found on the server".getBytes());
    }

}

From source file:org.wso2.carbon.mediation.library.ui.LibraryAdminClient.java

public void downloadCappArchive(String filename, HttpServletResponse response)
        throws IOException, MediationLibraryAdminServiceException {

    ServletOutputStream out = response.getOutputStream();
    DataHandler dataHandler = stub.downloadLibraryArchive(filename);
    if (dataHandler != null) {
        response.setHeader("Content-Disposition", "fileName=" + filename + ".zip");
        response.setContentType(dataHandler.getContentType());
        InputStream in = dataHandler.getDataSource().getInputStream();
        int nextChar;
        while ((nextChar = in.read()) != -1) {
            out.write((char) nextChar);
        }// www .j  av a  2  s  .c om
        out.flush();
        in.close();
        out.close();
    } else {
        out.write("The requested library archive was not found on the server".getBytes());
    }

}

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  www  .j ava2s  .com*/
                    });
            // 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:org.wso2.carbon.ui.util.FileDownloadUtil.java

public synchronized boolean acquireResource(ConfigurationContextService configCtxService,
        HttpServletRequest request, HttpServletResponse response) throws CarbonException {

    OutputStream out;/*from   w w  w. j av  a 2  s .  co m*/
    try {
        out = response.getOutputStream();
    } catch (IOException e) {
        String msg = "Unable to retrieve file ";
        log.error(msg, e);
        throw new CarbonException(msg, e);
    }
    String fileID = request.getParameter("id");
    String fileName = getFileName(configCtxService, request, fileID);

    if (fileName == null) {
        String serverURL = CarbonUIUtil.getServerURL(request.getSession().getServletContext(),
                request.getSession());

        String serviceEPR = serverURL + "FileDownloadService";
        try {

            FileDownloadServiceStub stub;
            if (CarbonUtils.isRunningOnLocalTransportMode()) {
                stub = new FileDownloadServiceStub(configCtxService.getServerConfigContext(), serviceEPR);
            } else {
                stub = new FileDownloadServiceStub(configCtxService.getClientConfigContext(), serviceEPR);
            }
            DataHandler dataHandler = stub.downloadFile(fileID);
            if (dataHandler != null) {
                response.setHeader("Content-Disposition", "filename=" + fileID);
                response.setContentType(dataHandler.getContentType());
                InputStream in = dataHandler.getDataSource().getInputStream();
                int nextChar;
                while ((nextChar = in.read()) != -1) {
                    out.write((char) nextChar);
                }
                out.flush();
                out.close();
                in.close();
                return true;
            }
            out.write("The requested file was not found on the server".getBytes());
            out.flush();
            out.close();
        } catch (IOException e) {
            String msg = "Unable to write output to HttpServletResponse OutputStream ";
            log.error(msg, e);
            throw new CarbonException(msg, e);
        }
        return false;
    }

    try {
        File file = new File(fileName);
        FileInputStream in = new FileInputStream(file);
        byte[] b = new byte[(int) file.length()];
        response.setContentType(mimeMap.getMIMEType(file));
        response.setContentLength((int) file.length());
        response.setHeader("Content-Disposition", "filename=" + file.getName());
        int lengthRead = in.read(b);
        if (lengthRead != -1) {
            out.write(b);
        }
        out.flush();
        out.close();
        in.close();
        return true;
    } catch (IOException e) {
        String msg = "Unable to retrieve file ";
        log.error(msg, e);
        throw new CarbonException(msg, e);
    }
}

From source file:org.mule.module.http.functional.listener.HttpListenerAttachmentsTestCase.java

private MuleMessage getResponseWithExpectedAttachmentFrom(String path) throws MuleException, IOException {
    MuleMessage response = muleContext.getClient().send(getUrl(path), getTestMuleMessage());
    assertThat(response.getInboundAttachmentNames().size(), is(1));
    DataHandler attachment = response.getInboundAttachment(TEXT_BODY_FIELD_NAME);
    assertThat((String) attachment.getContent(), is(TEXT_BODY_FIELD_VALUE));
    assertThat(attachment.getContentType(), is(TEXT_PLAIN));
    return response;
}

From source file:org.wso2.carbon.humantask.ui.clients.HumanTaskPackageManagementServiceClient.java

/**
 * Download package archive.//w w  w  .j av  a  2s .  c o m
 *
 * @param humanTaskPackageName : The package name.
 * @param response             :
 * @throws PackageManagementException :
 * @throws java.io.IOException        :
 */
public void downloadHumanTaskPackage(String humanTaskPackageName, HttpServletResponse response)
        throws PackageManagementException, IOException {
    try {
        ServletOutputStream out = response.getOutputStream();

        HumanTaskPackageDownloadData downloadData = stub.downloadHumanTaskPackage(humanTaskPackageName);
        if (downloadData != null) {
            DataHandler handler = downloadData.getPackageFileData();
            response.setHeader("Content-Disposition", "fileName=" + downloadData.getPackageName());
            response.setContentType(handler.getContentType());
            InputStream in = handler.getDataSource().getInputStream();
            int nextChar;
            while ((nextChar = in.read()) != -1) {
                out.write((char) nextChar);
            }
            out.flush();
            in.close();
        } else {
            out.write("The requested package archive was not found on the server".getBytes());
        }
    } catch (RemoteException e) {
        log.error(e);
        throw e;
    } catch (IOException e) {
        log.error(e);
        throw e;
    }
}

From source file:org.mule.module.http.functional.listener.HttpListenerAttachmentsTestCase.java

@Test
public void respondWithSeveralAttachments() throws Exception {
    MuleMessage response = muleContext.getClient().send(getUrl(filePath.getValue()), getTestMuleMessage());
    assertThat(response.getInboundAttachmentNames().size(), is(2));

    DataHandler attachment1 = response.getInboundAttachment(FILE_BODY_FIELD_NAME);
    HttpPart part = ((HttpPartDataSource) attachment1.getDataSource()).getPart();
    assertThat(part.getName(), is(FILE_BODY_FIELD_NAME));
    assertThat(part.getFileName(), is(FIELD_BDOY_FILE_NAME));
    assertThat(part.getContentType(), is("application/octet-stream"));
    assertThat(IOUtils.toString(part.getInputStream()), is(FILE_BODY_FIELD_VALUE));

    DataHandler attachment2 = response.getInboundAttachment(TEXT_BODY_FIELD_NAME);
    assertThat((String) attachment2.getContent(), is(TEXT_BODY_FIELD_VALUE));
    assertThat(attachment2.getContentType(), is(TEXT_PLAIN));
}