Java Code Examples for javax.activation.DataHandler#writeTo()

The following examples show how to use javax.activation.DataHandler#writeTo() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
protected String getMessage(MimeMessage mimeMessage) throws MessagingException, IOException {
  DataHandler dataHandler = mimeMessage.getDataHandler();
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  dataHandler.writeTo(baos);
  baos.flush();
  return baos.toString();
}
 
Example 2
public static String getMessage(MimeMessage mimeMessage) throws MessagingException, IOException {
  DataHandler dataHandler = mimeMessage.getDataHandler();
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  dataHandler.writeTo(baos);
  baos.flush();
  return baos.toString();
}
 
Example 3
protected String getMessage(MimeMessage mimeMessage) throws MessagingException, IOException {
  DataHandler dataHandler = mimeMessage.getDataHandler();
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  dataHandler.writeTo(baos);
  baos.flush();
  return baos.toString();
}
 
Example 4
public static String getMessage(MimeMessage mimeMessage) throws MessagingException, IOException {
  DataHandler dataHandler = mimeMessage.getDataHandler();
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  dataHandler.writeTo(baos);
  baos.flush();
  return baos.toString();
}
 
Example 5
public static String getMessage(MimeMessage mimeMessage) throws MessagingException, IOException {
  DataHandler dataHandler = mimeMessage.getDataHandler();
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  dataHandler.writeTo(baos);
  baos.flush();
  return baos.toString();
}
 
Example 6
protected String getMessage(MimeMessage mimeMessage) throws MessagingException, IOException {
    DataHandler dataHandler = mimeMessage.getDataHandler();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    dataHandler.writeTo(baos);
    baos.flush();
    return baos.toString();
}
 
Example 7
protected String getMessage(MimeMessage mimeMessage) {
    try {
        DataHandler dataHandler = mimeMessage.getDataHandler();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        dataHandler.writeTo(baos);
        baos.flush();
        return baos.toString();
    } catch (Exception e) {
        throw new RuntimeException("Couldn't get message", e);
    }
}
 
Example 8
public static String getMessage(MimeMessage mimeMessage) throws MessagingException, IOException {
    DataHandler dataHandler = mimeMessage.getDataHandler();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    dataHandler.writeTo(baos);
    baos.flush();
    return baos.toString();
}
 
Example 9
Source Project: jplag   File: JplagClient.java    License: GNU General Public License v3.0 5 votes vote down vote up
private File extractPart(MimeMultipart inputZipFile, String path) {
	File result = new File(path + "/jplagResult.zip");
	try {
		if (inputZipFile == null)
			return null;
		MimeBodyPart bdp = (MimeBodyPart) inputZipFile.getBodyPart(0);
		System.out.println("Content Type  " + bdp.getContentType());

		DataHandler dh = bdp.getDataHandler();
		FileOutputStream os = new FileOutputStream(result);
		dh.writeTo(os);
		os.close();

	} catch (Exception e) {
		if (e instanceof JPlagException) {
			JPlagException jex = (JPlagException) e;
			System.out.println(jex.getExceptionType());
			System.out.println(jex.getDescription());
			System.out.println(jex.getRepair());
		}
		e.printStackTrace();
		System.exit(-1);
	}
	String report = "\n"
			+ "\n"
			+ ((result == null) ? "compareSource was not successfull  sorry"
					: "WAOOOOOOOUUUUU  ******** CompareSource was succesfull**********") + "\n" + "\n";

	System.out.println(report);
	return result;

}
 
Example 10
Source Project: cxf   File: DataHandlerType.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected byte[] getBytes(Object object) {
    DataHandler handler = (DataHandler) object;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        handler.writeTo(baos);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return baos.toByteArray();
}
 
Example 11
Source Project: webcurator   File: LogReaderSOAPClient.java    License: Apache License 2.0 5 votes vote down vote up
public File retrieveAQAFile(String aJob, String aFilename) {
	try {
		WCTSoapCall call = new WCTSoapCall(host, port, service, "retrieveAQAFile");
		call.regTypes(DataHandler.class);
		DataHandler dh = (DataHandler) call.invoke(aJob, aFilename);
		
           File f = File.createTempFile("wct", "tmp");
        dh.writeTo(new FileOutputStream(f));
        return f;	        
	}
	catch(Exception ex) {
           throw new WCTRuntimeException("Failed to retrieve aqa file " + aFilename + " for " + aJob + ": " + ex.getMessage(), ex);
	}
}
 
Example 12
Source Project: micro-integrator   File: MTOMSwAClient.java    License: Apache License 2.0 4 votes vote down vote up
public static MessageContext sendUsingSwA(String fileName, String targetEPR) throws IOException {

        Options options = new Options();
        options.setTo(new EndpointReference(targetEPR));
        options.setAction("urn:uploadFileUsingSwA");
        options.setProperty(Constants.Configuration.ENABLE_SWA, Constants.VALUE_TRUE);

        ServiceClient sender = createServiceClient();
        sender.setOptions(options);
        OperationClient mepClient = sender.createClient(ServiceClient.ANON_OUT_IN_OP);

        MessageContext mc = new MessageContext();

        System.out.println("Sending file : " + fileName + " as SwA");
        FileDataSource fileDataSource = new FileDataSource(new File(fileName));
        DataHandler dataHandler = new DataHandler(fileDataSource);
        String attachmentID = mc.addAttachment(dataHandler);


        SOAPFactory factory = OMAbstractFactory.getSOAP11Factory();
        SOAPEnvelope env = factory.getDefaultEnvelope();
        OMNamespace ns = factory.createOMNamespace("http://services.samples", "m0");
        OMElement payload = factory.createOMElement("uploadFileUsingSwA", ns);
        OMElement request = factory.createOMElement("request", ns);
        OMElement imageId = factory.createOMElement("imageId", ns);
        imageId.setText(attachmentID);
        request.addChild(imageId);
        payload.addChild(request);
        env.getBody().addChild(payload);
        mc.setEnvelope(env);

        mepClient.addMessageContext(mc);
        mepClient.execute(true);
        MessageContext response = mepClient.getMessageContext(WSDLConstants.MESSAGE_LABEL_IN_VALUE);

        SOAPBody body = response.getEnvelope().getBody();
        String imageContentId = body.
                getFirstChildWithName(new QName("http://services.samples", "uploadFileUsingSwAResponse")).
                getFirstChildWithName(new QName("http://services.samples", "response")).
                getFirstChildWithName(new QName("http://services.samples", "imageId")).
                getText();

        Attachments attachment = response.getAttachmentMap();
        dataHandler = attachment.getDataHandler(imageContentId);
        File tempFile = File.createTempFile("swa-", ".gif");
        FileOutputStream fos = new FileOutputStream(tempFile);
        dataHandler.writeTo(fos);
        fos.flush();
        fos.close();

        System.out.println("Saved response to file : " + tempFile.getAbsolutePath());

        return response;
    }
 
Example 13
Source Project: micro-integrator   File: MTOMSwAClient.java    License: Apache License 2.0 4 votes vote down vote up
public static MessageContext sendUsingSwA(String fileName, String targetEPR) throws IOException {

        Options options = new Options();
        options.setTo(new EndpointReference(targetEPR));
        options.setAction("urn:uploadFileUsingSwA");
        options.setProperty(Constants.Configuration.ENABLE_SWA, Constants.VALUE_TRUE);

        ServiceClient sender = createServiceClient();
        sender.setOptions(options);
        OperationClient mepClient = sender.createClient(ServiceClient.ANON_OUT_IN_OP);

        MessageContext mc = new MessageContext();

        System.out.println("Sending file : " + fileName + " as SwA");
        FileDataSource fileDataSource = new FileDataSource(new File(fileName));
        DataHandler dataHandler = new DataHandler(fileDataSource);
        String attachmentID = mc.addAttachment(dataHandler);


        SOAPFactory factory = OMAbstractFactory.getSOAP11Factory();
        SOAPEnvelope env = factory.getDefaultEnvelope();
        OMNamespace ns = factory.createOMNamespace("http://services.samples", "m0");
        OMElement payload = factory.createOMElement("uploadFileUsingSwA", ns);
        OMElement request = factory.createOMElement("request", ns);
        OMElement imageId = factory.createOMElement("imageId", ns);
        imageId.setText(attachmentID);
        request.addChild(imageId);
        payload.addChild(request);
        env.getBody().addChild(payload);
        mc.setEnvelope(env);

        mepClient.addMessageContext(mc);
        mepClient.execute(true);
        MessageContext response = mepClient.getMessageContext(WSDLConstants.MESSAGE_LABEL_IN_VALUE);

        SOAPBody body = response.getEnvelope().getBody();
        String imageContentId = body.
                getFirstChildWithName(new QName("http://services.samples", "uploadFileUsingSwAResponse")).
                getFirstChildWithName(new QName("http://services.samples", "response")).
                getFirstChildWithName(new QName("http://services.samples", "imageId")).
                getText();

        Attachments attachment = response.getAttachmentMap();
        dataHandler = attachment.getDataHandler(imageContentId);
        File tempFile = File.createTempFile("swa-", ".gif");
        FileOutputStream fos = new FileOutputStream(tempFile);
        dataHandler.writeTo(fos);
        fos.flush();
        fos.close();

        System.out.println("Saved response to file : " + tempFile.getAbsolutePath());

        return response;
    }
 
Example 14
Source Project: Moss   File: MailNotifierTest.java    License: Apache License 2.0 4 votes vote down vote up
private String extractBody(DataHandler dataHandler) throws IOException {
    ByteArrayOutputStream os = new ByteArrayOutputStream(4096);
    dataHandler.writeTo(os);
    return os.toString(StandardCharsets.UTF_8.name())
             .replaceAll("\\r?\\n", "\n");
}
 
Example 15
public void downloadVersionContent(String sid, long docId, String version, File out) throws Exception {
	DataHandler data = client.getVersionContent(sid, docId, version);
	data.writeTo(new FileOutputStream(out));
}
 
Example 16
public void downloadResourceContent(String sid, long docId, String fileVersion, String suffix, File out)
		throws Exception {
	DataHandler data = client.getResource(sid, docId, fileVersion, suffix);
	data.writeTo(new FileOutputStream(out));
}
 
Example 17
Source Project: product-ei   File: MTOMSwAClient.java    License: Apache License 2.0 4 votes vote down vote up
public static MessageContext sendUsingSwA(String fileName, String targetEPR) throws IOException {

        Options options = new Options();
        options.setTo(new EndpointReference(targetEPR));
        options.setAction("urn:uploadFileUsingSwA");
        options.setProperty(Constants.Configuration.ENABLE_SWA, Constants.VALUE_TRUE);

        ServiceClient sender = createServiceClient();
        sender.setOptions(options);
        OperationClient mepClient = sender.createClient(ServiceClient.ANON_OUT_IN_OP);

        MessageContext mc = new MessageContext();

        System.out.println("Sending file : " + fileName + " as SwA");
        FileDataSource fileDataSource = new FileDataSource(new File(fileName));
        DataHandler dataHandler = new DataHandler(fileDataSource);
        String attachmentID = mc.addAttachment(dataHandler);


        SOAPFactory factory = OMAbstractFactory.getSOAP11Factory();
        SOAPEnvelope env = factory.getDefaultEnvelope();
        OMNamespace ns = factory.createOMNamespace("http://services.samples", "m0");
        OMElement payload = factory.createOMElement("uploadFileUsingSwA", ns);
        OMElement request = factory.createOMElement("request", ns);
        OMElement imageId = factory.createOMElement("imageId", ns);
        imageId.setText(attachmentID);
        request.addChild(imageId);
        payload.addChild(request);
        env.getBody().addChild(payload);
        mc.setEnvelope(env);

        mepClient.addMessageContext(mc);
        mepClient.execute(true);
        MessageContext response = mepClient.getMessageContext(WSDLConstants.MESSAGE_LABEL_IN_VALUE);

        SOAPBody body = response.getEnvelope().getBody();
        String imageContentId = body.
                getFirstChildWithName(new QName("http://services.samples", "uploadFileUsingSwAResponse")).
                getFirstChildWithName(new QName("http://services.samples", "response")).
                getFirstChildWithName(new QName("http://services.samples", "imageId")).
                getText();

        Attachments attachment = response.getAttachmentMap();
        dataHandler = attachment.getDataHandler(imageContentId);
        File tempFile = File.createTempFile("swa-", ".gif");
        FileOutputStream fos = new FileOutputStream(tempFile);
        dataHandler.writeTo(fos);
        fos.flush();
        fos.close();

        System.out.println("Saved response to file : " + tempFile.getAbsolutePath());

        return response;
    }
 
Example 18
Source Project: carbon-commons   File: BDReportServlet.java    License: Apache License 2.0 4 votes vote down vote up
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws
                                                                                        Exception {
    String dataSource = request.getParameter("dataSource");
    String reportName = request.getParameter("reportName");
    String reportType = request.getParameter("reportType");
    String params = request.getParameter("hidden_param");

    String downloadFileName = null;

    if (reportType.equals("pdf")) {
        response.setContentType("application/pdf");
        downloadFileName = reportName + ".pdf";
    } else if (reportType.equals("excel")) {
        response.setContentType("application/vnd.ms-excel");
        downloadFileName = reportName + ".xls";
    } else if (reportType.equals("html")) {
        response.setContentType("text/html");
    } else {
        throw new ReportingException("requested report type can not be support");
    }

    if (downloadFileName != null) {
        response.setHeader("Content-Disposition", "attachment; filename=\"" + downloadFileName + "\"");
    }
    ReportBean reportBean = new ReportBean();
    reportBean.setTemplateName(reportName);
    reportBean.setReportType(reportType);
    reportBean.setDataSourceName(dataSource);


    String serverURL = CarbonUIUtil.getServerURL(request.getSession().getServletContext(), request.getSession());
    ConfigurationContext configurationContext = (ConfigurationContext) request.getSession().getServletContext().
            getAttribute(CarbonConstants.CONFIGURATION_CONTEXT);
    String cookie = (String) request.getSession().getAttribute(ServerConstants.ADMIN_SERVICE_COOKIE);
    DBReportingServiceClient dbReportingServiceClient = null;
    try {
        dbReportingServiceClient = new DBReportingServiceClient(cookie, serverURL, configurationContext);
    } catch (AxisFault axisFault) {
        axisFault.printStackTrace();
    }
    List<ReportParamMap> reportParamMapsList = new ArrayList<ReportParamMap>();
    String[] parmCollection = params.split("\\|");

    for (String inputParam : parmCollection) {
        if (inputParam != null && !"".equals(inputParam)) {
            ReportParamMap reportParamMap = new ReportParamMap();
            String[] input = inputParam.split("\\=");
            reportParamMap.setParamKey(input[0]);
            reportParamMap.setParamValue(input[1]);
            reportParamMapsList.add(reportParamMap);
        }
    }

    try {
        DataHandler dataHandler = null;
        if (dbReportingServiceClient != null) {
            dataHandler = dbReportingServiceClient.getReport(reportBean,reportParamMapsList.toArray(new ReportParamMap[reportParamMapsList.size()]));
        }
        ServletOutputStream outputStream = response.getOutputStream();
        if (dataHandler != null) {
            dataHandler.writeTo(outputStream);
        }
    } catch (Exception e) {
        log.error("Failed to handle report request ",e);
        throw e;
    }

}
 
Example 19
private String extractBody(DataHandler dataHandler) throws IOException {
	ByteArrayOutputStream os = new ByteArrayOutputStream(4096);
	dataHandler.writeTo(os);
	return os.toString(StandardCharsets.UTF_8.name()).replaceAll("\\r?\\n", "\n");
}
 
Example 20
Source Project: commons-email   File: AbstractEmailTest.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * Gets the byte making up the body of the message.
 *
 * @param mimeMessage
 *            The mime message from which to extract the body.
 * @return A byte array representing the message body
 * @throws IOException
 *             Thrown while serializing the body from
 *             {@link DataHandler#writeTo(java.io.OutputStream)}.
 * @throws MessagingException
 *             Thrown while getting the body content from
 *             {@link MimeMessage#getDataHandler()}
 * @since 1.1
 */
private byte[] getMessageBodyBytes(final MimeMessage mimeMessage)
        throws IOException, MessagingException
{
    final DataHandler dataHandler = mimeMessage.getDataHandler();
    final ByteArrayOutputStream byteArrayOutStream = new ByteArrayOutputStream();
    final BufferedOutputStream buffOs = new BufferedOutputStream(
            byteArrayOutStream);
    dataHandler.writeTo(buffOs);
    buffOs.flush();

    return byteArrayOutStream.toByteArray();
}