Example usage for javax.activation DataHandler DataHandler

List of usage examples for javax.activation DataHandler DataHandler

Introduction

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

Prototype

public DataHandler(URL url) 

Source Link

Document

Create a DataHandler instance referencing a URL.

Usage

From source file:org.taverna.server.master.soap.WrappedWorkflow.java

/**
 * Initialize the contents of this descriptor from the given file and
 * content type./*ww  w  . ja va 2s .c  o m*/
 * 
 * @param workflow
 *            The workflow that is to be reported.
 */
public void setWorkflow(Workflow workflow) {
    workflowData = new DataHandler(new WorkflowSource(workflow));
}

From source file:org.wso2.brs.rule.test.GreetingServiceTestCase.java

@Test(groups = { "wso2.brs" })
public void uploadGreetingService() throws Exception {
    String samplesDir = System.getProperty("samples.dir");
    String greetingServiceAAR = samplesDir + File.separator
            + "greeting.service/service/target/GreetingService.aar";
    log.info(greetingServiceAAR);/*from  w ww. jav  a 2s .  co m*/
    FileDataSource fileDataSource = new FileDataSource(greetingServiceAAR);
    DataHandler dataHandler = new DataHandler(fileDataSource);
    getRuleServiceFileUploadClient().uploadService("GreetingService.aar", dataHandler);
}

From source file:org.jaggeryjs.integration.common.clients.WebAppAdminClient.java

public void warFileUplaoder(String filePath) throws RemoteException {
    File file = new File(filePath);
    String fileName = file.getName();
    URL url = null;/*w  w  w. j  ava 2s .  c o m*/
    try {
        url = new URL("file://" + filePath);
    } catch (MalformedURLException e) {
        log.error("Malformed URL " + e);
    }
    DataHandler dh = new DataHandler(url);
    WebappUploadData webApp;
    webApp = new WebappUploadData();
    webApp.setFileName(fileName);
    webApp.setDataHandler(dh);

    try {
        assert webappAdminStub.uploadWebapp(new WebappUploadData[] { webApp }) : "webapp upload unsuccessful";
    } catch (RemoteException e) {
        log.error("Fail to upload webapp file :" + e);
        throw new RemoteException("Fail to upload webapp file :" + e);
    }
}

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

public void warFileUplaoder(String sessionCookie) {
    File file = new File(filePath);
    String fileName = file.getName();
    URL url = null;//ww  w  .  j  a va 2  s .  c  o  m
    try {
        url = new URL("file://" + filePath);
    } catch (MalformedURLException e) {
        e.printStackTrace(); //TO-DO
    }
    DataHandler dh = new DataHandler(url);
    WebappUploadData webApp;
    webApp = new WebappUploadData();
    webApp.setFileName(fileName);
    webApp.setDataHandler(dh);
    new AuthenticateStub().authenticateStub(sessionCookie, webappAdminStub);
    try {
        Assert.assertTrue("webapp upload unsuccessful",
                webappAdminStub.uploadWebapp(new WebappUploadData[] { webApp }));
    } catch (RemoteException e) {
        log.error("Thrown RemoteException while uploading webapp :" + e.getMessage());
        Assert.fail("Thrown RemoteException while uploading webapp :" + e.getMessage());
    }

}

From source file:org.wso2.bps.integration.tests.bpmn.BPMNCappDeploymentTestCase.java

/**
 * Deploy the uploaded C-App file/*  w w  w .  ja  v  a 2  s .  c  o  m*/
 */

@BeforeClass(alwaysRun = true)
public void deployTask() throws Exception {
    super.init();
    CarbonAppUploaderClient carbonAppUploaderClient = new CarbonAppUploaderClient(backEndUrl, sessionCookie);

    carbonAppUploaderClient.uploadCarbonAppArtifact(carFileName + BPMNTestConstants.CAR_EXTENSION,
            new DataHandler(new URL("file:" + File.separator + File.separator
                    + FrameworkPathUtil.getSystemResourceLocation() + "artifacts" + File.separator + "bpmn"
                    + File.separator + carFileName + BPMNTestConstants.CAR_EXTENSION)));
    isCarFileUploaded = true;
    applicationAdminClient = new ApplicationAdminClient(backEndUrl, sessionCookie);
    boolean result = isCarFileDeployed(carFileName);
    Assert.assertTrue(result, "Car file deployment failed");
}

From source file:com.googlecode.ddom.mime.JavaMailTest.java

private void test(boolean preamble) throws Exception {
    MimeMultipart multipart = new MimeMultipart();

    MimeBodyPart bodyPart1 = new MimeBodyPart();
    StringBuilder buffer = new StringBuilder();
    for (int i = 0; i < 1000; i++) {
        buffer.append('(');
        buffer.append(i);//  www .  j  a va2  s  .c o m
        buffer.append(')');
    }
    String content1 = buffer.toString();
    bodyPart1
            .setDataHandler(new DataHandler(new ByteArrayDataSource(content1.getBytes("UTF-8"), "text/plain")));
    Map<String, String> headers1 = new HashMap<String, String>();
    headers1.put("Content-ID", "<1@example.com>");
    headers1.put("Content-Type", "text/plain; charset=UTF-8");
    setHeaders(bodyPart1, headers1);
    multipart.addBodyPart(bodyPart1);

    MimeBodyPart bodyPart2 = new MimeBodyPart();
    byte[] content2 = new byte[10000];
    new Random().nextBytes(content2);
    bodyPart2.setDataHandler(new DataHandler(new ByteArrayDataSource(content2, "application/octet-stream")));
    Map<String, String> headers2 = new HashMap<String, String>();
    headers2.put("Content-ID", "<2@example.com>");
    headers2.put("Content-Type", "application/octet-stream");
    setHeaders(bodyPart2, headers2);
    multipart.addBodyPart(bodyPart2);

    if (preamble) {
        multipart.setPreamble("This is a MIME multipart.");
    }

    String boundary = new ContentType(multipart.getContentType()).getParameter("boundary");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    multipart.writeTo(baos);
    MultipartReader mpr = new MultipartReader(new ByteArrayInputStream(baos.toByteArray()), boundary);
    assertTrue(mpr.nextPart());
    assertEquals(headers1, readHeaders(mpr));
    assertEquals(content1, IOUtils.toString(mpr.getContent(), "UTF-8"));
    assertTrue(mpr.nextPart());
    assertEquals(headers2, readHeaders(mpr));
    assertArrayEquals(content2, IOUtils.toByteArray(mpr.getContent()));
    assertFalse(mpr.nextPart());
}

From source file:com.yosanai.tutorial.ws.hellowebservice.WebServiceTest.java

@Test
public void callUpload() throws Exception {
    File file = new File();
    java.io.File fileToUpload = new java.io.File("src/test/resources/icons.jpg");
    file.setFileName(fileToUpload.getName());
    FileDataSource fileDataSource = new FileDataSource(fileToUpload);
    DataHandler fileContent = new DataHandler(fileDataSource);
    file.setFileContent(fileContent);/*  ww  w. j  a va2 s. co  m*/
    System.out.println(fileManagement.upload(file));
}

From source file:ebay.dts.client.FileTransferActions.java

public void uploadFile(String xmlFile, String jobId, String fileReferenceId) throws EbayConnectorException {

    String callName = "uploadFile";

    try {//from ww  w.  ja va 2s .c  o m

        String compressedFileName = compressFileToGzip(xmlFile);

        if (compressedFileName == null) {
            logger.error("Failed to compress your XML file into gzip file. Aborted");
            throw new EbayConnectorException("Failed to compress your XML file into gzip file. Aborted");
        }

        FileTransferServicePort port = call.setFTSMessageContext(callName);
        UploadFileRequest request = new UploadFileRequest();
        FileAttachment attachment = new FileAttachment();
        File fileToUpload = new File(compressedFileName);
        DataHandler dh = new DataHandler(new FileDataSource(fileToUpload));
        attachment.setData(dh);
        attachment.setSize(fileToUpload.length());
        String fileFormat = "gzip";
        request.setFileFormat(fileFormat);

        /*
         * For instance, the Bulk Data Exchange Service uses a job ID as a
         * primary identifier, so, if you're using the Bulk Data Exchange
         * Service, enter the job ID as the taskReferenceId.
         */

        request.setTaskReferenceId(jobId);
        request.setFileReferenceId(fileReferenceId);
        request.setFileAttachment(attachment);
        // request.
        if (port != null && request != null) {
            UploadFileResponse response = port.uploadFile(request);
            if (response.getAck().equals(AckValue.SUCCESS)) {
                return;
            } else {
                logger.error(response.getErrorMessage().getError().get(0).getMessage());
                throw new EbayConnectorException(response.getErrorMessage().getError().get(0).getMessage());
            }
        }

    } catch (Exception e) {
        logger.error(e.getMessage());
        throw new EbayConnectorException(e.getMessage());
    }

}

From source file:org.wso2.appserver.integration.common.clients.WebAppAdminClient.java

public void uploadWarFile(String filePath, String hostName) throws RemoteException {
    File file = new File(filePath);
    String fileName = file.getName();
    URL url = null;/* w ww.  j av a2  s .  c om*/
    try {
        url = new URL("file://" + filePath);
    } catch (MalformedURLException e) {
        log.error("Malformed URL " + e);
    }
    DataHandler dh = new DataHandler(url);
    WebappUploadData webApp;
    webApp = new WebappUploadData();
    webApp.setFileName(fileName);
    if (hostName != null) {
        webApp.setHostName(hostName);
    }
    webApp.setDataHandler(dh);

    try {
        assert webappAdminStub.uploadWebapp(new WebappUploadData[] { webApp }) : "webapp upload unsuccessful";
    } catch (RemoteException e) {
        log.error("Fail to upload webapp file :" + e);
        throw new RemoteException("Fail to upload webapp file :" + e);
    }
}

From source file:org.vosao.utils.EmailUtil.java

/**
 * Send email with html content and attachments.
 * @param htmlBody//from  w  ww  .  j a  va2 s.com
 * @param subject
 * @param fromAddress
 * @param fromText
 * @param toAddress
 * @return null if OK or error message.
 */
public static String sendEmail(final String htmlBody, final String subject, final String fromAddress,
        final String fromText, final String toAddress, final List<FileItem> files) {

    Properties props = new Properties();
    Session session = Session.getDefaultInstance(props, null);
    try {
        Multipart mp = new MimeMultipart();
        MimeBodyPart htmlPart = new MimeBodyPart();
        htmlPart.setContent(htmlBody, "text/html");
        htmlPart.setHeader("Content-type", "text/html; charset=UTF-8");
        mp.addBodyPart(htmlPart);
        for (FileItem item : files) {
            MimeBodyPart attachment = new MimeBodyPart();
            attachment.setFileName(item.getFilename());
            String mimeType = MimeType.getContentTypeByExt(FolderUtil.getFileExt(item.getFilename()));
            DataSource ds = new ByteArrayDataSource(item.getData(), mimeType);
            attachment.setDataHandler(new DataHandler(ds));
            mp.addBodyPart(attachment);
        }
        MimeMessage msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(fromAddress, fromText));
        msg.addRecipient(Message.RecipientType.TO, new InternetAddress(toAddress, toAddress));
        msg.setSubject(subject, "UTF-8");
        msg.setContent(mp);
        Transport.send(msg);
        return null;
    } catch (AddressException e) {
        return e.getMessage();
    } catch (MessagingException e) {
        return e.getMessage();
    } catch (UnsupportedEncodingException e) {
        return e.getMessage();
    }
}