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.pentaho.platform.repository2.unified.webservices.jaxws.SimpleRepositoryFileDataDto.java

/**
 * Converts SimpleRepositoryFileData to SimpleRepositoryFileDataDto. Does not use ByteArrayDataSource since that
 * implementation reads the entire stream into a byte array.
 *///from www .j  a v a 2s  .  c  o m
public static SimpleRepositoryFileDataDto convert(final SimpleRepositoryFileData simpleData) {
    FileOutputStream fout = null;
    boolean foutClosed = false;
    try {
        SimpleRepositoryFileDataDto simpleJaxWsData = new SimpleRepositoryFileDataDto();
        File tmpFile = File.createTempFile("pentaho-ws", null); //$NON-NLS-1$
        // TODO mlowery this might not delete files soon enough
        tmpFile.deleteOnExit();
        fout = FileUtils.openOutputStream(tmpFile);
        IOUtils.copy(simpleData.getStream(), fout);
        fout.close();
        foutClosed = true;
        simpleJaxWsData.dataHandler = new DataHandler(new FileDataSource(tmpFile));
        simpleJaxWsData.encoding = simpleData.getEncoding();
        simpleJaxWsData.mimeType = simpleData.getMimeType();
        return simpleJaxWsData;
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        try {
            if (fout != null && !foutClosed) {
                fout.close();
            }
        } catch (Exception e) {
            // CHECKSTYLES IGNORE
        }
    }
}

From source file:com.nortal.jroad.model.XTeeAttachment.java

public XTeeAttachment(String cid, String contentType, byte[] data) {
    this.cid = cid;
    this.dataHandler = new DataHandler(new ByteArrayDataSource(contentType, data));
}

From source file:org.apache.axis.encoding.ser.SourceDataHandlerSerializer.java

/**
 * Serialize a Source DataHandler quantity.
 *//*from   w  w  w .  j  av a 2s.c om*/
public void serialize(QName name, Attributes attributes, Object value, SerializationContext context)
        throws IOException {
    if (value != null) {
        if (!(value instanceof StreamSource)) {
            throw new IOException(Messages.getMessage("badSource", value.getClass().getName()));
        }
        DataHandler dh = new DataHandler(new SourceDataSource("source", "text/xml", (StreamSource) value));
        super.serialize(name, attributes, dh, context);
    }
}

From source file:org.ojbc.util.camel.processor.FileAttachmentProcessor.java

@Override
public void process(Exchange exchange) throws Exception {
    String currentXMLFilePath = (String) exchange.getProperty("currentXMLFilePath");
    log.debug("Attaching file.");
    log.debug("currentXMLFilePath=[" + currentXMLFilePath + "]");
    exchange.getIn().addAttachment("Bad.xml",
            new DataHandler(new FileDataSource(new File(currentXMLFilePath))));
}

From source file:org.wso2.carbon.connector.common.ConnectorIntegrationUtil.java

public static void uploadConnector(String repoLocation, MediationLibraryUploaderStub mediationLibUploadStub,
        String strFileName) throws MalformedURLException, RemoteException {

    List<LibraryFileItem> uploadLibraryInfoList = new ArrayList<LibraryFileItem>();
    LibraryFileItem uploadedFileItem = new LibraryFileItem();
    uploadedFileItem/*from  w  w  w .  j  a v a  2s.co  m*/
            .setDataHandler(new DataHandler(new URL("file:" + "///" + repoLocation + "/" + strFileName)));
    uploadedFileItem.setFileName(strFileName);
    uploadedFileItem.setFileType("zip");
    uploadLibraryInfoList.add(uploadedFileItem);
    LibraryFileItem[] uploadServiceTypes = new LibraryFileItem[uploadLibraryInfoList.size()];
    uploadServiceTypes = uploadLibraryInfoList.toArray(uploadServiceTypes);
    mediationLibUploadStub.uploadLibrary(uploadServiceTypes);
}

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

@Override
public void postMe(Holder<String> name, Holder<DataHandler> attachinfo, DataHandler onewayattach) {
    name.value = "Hello from the real world. Request was " + name.value;

    try {// www  . j ava  2  s  .c  o m
        String content = toString(attachinfo.value);
        String onewayContent = toString(onewayattach);
        String contentType = attachinfo.value.getContentType();
        String resultContent = "I received: " + content + "/" + onewayContent;
        attachinfo.value = new DataHandler(new ByteArrayDataSource(resultContent, contentType));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.apachecon.memories.hippocampus.MailConnectionTest.java

@Test
public void testMailEndpoint() throws Exception {
    Mailbox.clearAll();/*from ww  w . j a v a2 s.c o  m*/

    Endpoint endpoint = context.getEndpoint("{{mbox.smtp}}");
    assertNotNull(endpoint);

    Exchange ex1 = endpoint.createExchange();
    Message in = ex1.getIn();
    in.setBody("Hello World");
    in.addAttachment("feather-small.gif",
            new DataHandler(new FileDataSource("src/test/resources/img/feather-small.gif")));
    in.addAttachment("talend-logo",
            new DataHandler(new FileDataSource("src/test/resources/img/talend-logo.jpg")));

    Exchange ex2 = endpoint.createExchange();
    ex2.getIn().setBody("Bye World... without attachments");

    Producer producer = endpoint.createProducer();
    producer.start();
    producer.process(ex1);
    producer.process(ex2);
    producer.stop();
    Thread.sleep(5000);

    Endpoint target = context.getEndpoint("{{mail.target}}");
    if (target instanceof MockEndpoint) {
        MockEndpoint mock = (MockEndpoint) target;

        mock.expectedMessageCount(2);
        mock.assertIsSatisfied();

        assertTrue(mock.assertExchangeReceived(0).getIn().getBody() instanceof DataHandler);
        assertTrue(mock.assertExchangeReceived(1).getIn().getBody() instanceof DataHandler);
    }
}

From source file:org.apache.axis2.jaxbri.mtom.MtomImpl.java

public RetrieveDocumentResponse retrieveDocument(RetrieveDocument retrieveDocument) {
    RetrieveDocumentResponse response = new RetrieveDocumentResponse();
    response.setContent(new DataHandler(
            new ByteArrayDataSource(documents.get(retrieveDocument.getId()), "application/octet-stream")));
    return response;
}

From source file:frameworkcontentspeed.Utils.SendEmailAtachament.java

public static void sendGRUP(String from, String password, String bcc, String sub, String msg, String filename)
        throws Exception {
    //Get properties object    
    Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "465");
    //get Session   
    Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(from, password);
        }//from w ww  . jav  a 2 s . co m
    });
    //compose message    
    try {
        MimeMessage message = new MimeMessage(session);
        //message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
        message.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc));

        message.setSubject(sub);
        message.setText(msg);

        BodyPart messageBodyPart = new MimeBodyPart();

        messageBodyPart.setText("Raport teste automat");

        Multipart multipart = new MimeMultipart();

        multipart.addBodyPart(messageBodyPart);

        messageBodyPart = new MimeBodyPart();

        DataSource source = new FileDataSource(filename);

        messageBodyPart.setDataHandler(new DataHandler(source));

        messageBodyPart.setFileName(filename);

        multipart.addBodyPart(messageBodyPart);

        //message.setContent(multipart);
        StringWriter writer = new StringWriter();
        IOUtils.copy(new FileInputStream(new File(filename)), writer);

        message.setContent(writer.toString(), "text/html");

        //send message  
        Transport.send(message);
        System.out.println("message sent successfully");
    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }

}

From source file:org.wso2.carbon.appfactory.tenant.build.integration.uploder.DirectUploader.java

private static WebappUploadData getWebAppUploadDataItem(File fileToDeploy) {
    DataHandler dataHandler = new DataHandler(new FileDataSource(fileToDeploy));

    WebappUploadData webappUploadData = new WebappUploadData();
    webappUploadData.setDataHandler(dataHandler);
    webappUploadData.setFileName(fileToDeploy.getName());
    return webappUploadData;
}