Search in sources :

Example 96 with ByteArrayDataSource

use of javax.mail.util.ByteArrayDataSource in project cxf by apache.

the class SwAServiceImpl method echoData.

public void echoData(Holder<String> text, Holder<DataHandler> data) {
    try {
        InputStream bis = null;
        bis = data.value.getDataSource().getInputStream();
        byte[] b = new byte[6];
        bis.read(b, 0, 6);
        String string = IOUtils.newStringFromBytes(b);
        ByteArrayDataSource source = new ByteArrayDataSource(("test" + string).getBytes(), "application/octet-stream");
        data.value = new DataHandler(source);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : InputStream(java.io.InputStream) DataHandler(javax.activation.DataHandler) IOException(java.io.IOException) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource)

Example 97 with ByteArrayDataSource

use of javax.mail.util.ByteArrayDataSource in project cxf by apache.

the class SwAServiceImpl method echoDataRef.

public void echoDataRef(Holder<DataStruct> data) {
    try {
        InputStream bis = null;
        bis = data.value.getDataRef().getDataSource().getInputStream();
        byte[] b = new byte[6];
        bis.read(b, 0, 6);
        String string = IOUtils.newStringFromBytes(b);
        ByteArrayDataSource source = new ByteArrayDataSource(("test" + string).getBytes(), "application/octet-stream");
        data.value.setDataRef(new DataHandler(source));
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : InputStream(java.io.InputStream) DataHandler(javax.activation.DataHandler) IOException(java.io.IOException) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource)

Example 98 with ByteArrayDataSource

use of javax.mail.util.ByteArrayDataSource in project kernel by exoplatform.

the class MailServiceImpl method sendMessage.

/**
 * {@inheritDoc}
 */
public void sendMessage(Message message) throws Exception {
    MimeMessage mimeMessage = new MimeMessage(getMailSession());
    String FROM = message.getFrom();
    String TO = message.getTo();
    String CC = message.getCC();
    String BCC = message.getBCC();
    String subject = message.getSubject();
    String mimeType = message.getMimeType();
    String body = message.getBody();
    List<Attachment> attachment = message.getAttachment();
    // set From to the message
    if (FROM != null && !FROM.equals("")) {
        InternetAddress sentFrom = new InternetAddress(FROM);
        mimeMessage.setFrom(sentFrom);
    }
    // set To to the message
    InternetAddress[] sendTo = new InternetAddress[getArrs(TO).length];
    for (int i = 0; i < getArrs(TO).length; i++) {
        sendTo[i] = new InternetAddress(getArrs(TO)[i]);
    }
    mimeMessage.setRecipients(javax.mail.Message.RecipientType.TO, sendTo);
    // set CC to the message
    if ((getArrs(CC) != null) && (getArrs(CC).length > 0)) {
        InternetAddress[] copyTo = new InternetAddress[getArrs(CC).length];
        for (int i = 0; i < getArrs(CC).length; i++) {
            copyTo[i] = new InternetAddress(getArrs(CC)[i]);
        }
        mimeMessage.setRecipients(javax.mail.Message.RecipientType.CC, copyTo);
    }
    // set BCC to the message
    if ((getArrs(BCC) != null) && (getArrs(BCC).length > 0)) {
        InternetAddress[] bccTo = new InternetAddress[getArrs(BCC).length];
        for (int i = 0; i < getArrs(BCC).length; i++) {
            bccTo[i] = new InternetAddress(getArrs(BCC)[i]);
        }
        mimeMessage.setRecipients(javax.mail.Message.RecipientType.BCC, bccTo);
    }
    // set Subject to the message
    mimeMessage.setSubject(subject);
    mimeMessage.setSubject(message.getSubject(), "UTF-8");
    mimeMessage.setSentDate(new Date());
    MimeMultipart multipPartRoot = new MimeMultipart("mixed");
    MimeMultipart multipPartContent = new MimeMultipart("alternative");
    if (attachment != null && attachment.size() != 0) {
        MimeBodyPart contentPartRoot = new MimeBodyPart();
        if (mimeType != null && mimeType.indexOf("text/plain") > -1)
            contentPartRoot.setContent(body, "text/plain; charset=utf-8");
        else
            contentPartRoot.setContent(body, "text/html; charset=utf-8");
        MimeBodyPart mimeBodyPart1 = new MimeBodyPart();
        mimeBodyPart1.setContent(body, mimeType);
        multipPartContent.addBodyPart(mimeBodyPart1);
        multipPartRoot.addBodyPart(contentPartRoot);
        for (Attachment att : attachment) {
            InputStream is = att.getInputStream();
            MimeBodyPart mimeBodyPart = new MimeBodyPart();
            ByteArrayDataSource byteArrayDataSource = new ByteArrayDataSource(is, att.getMimeType());
            mimeBodyPart.setDataHandler(new DataHandler(byteArrayDataSource));
            mimeBodyPart.setDisposition(Part.ATTACHMENT);
            if (att.getName() != null)
                mimeBodyPart.setFileName(MimeUtility.encodeText(att.getName(), "utf-8", null));
            multipPartRoot.addBodyPart(mimeBodyPart);
        }
        mimeMessage.setContent(multipPartRoot);
    } else {
        if (mimeType != null && mimeType.indexOf("text/plain") > -1)
            mimeMessage.setContent(body, "text/plain; charset=utf-8");
        else
            mimeMessage.setContent(body, "text/html; charset=utf-8");
    }
    sendMessage(mimeMessage);
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) InputStream(java.io.InputStream) Attachment(org.exoplatform.services.mail.Attachment) DataHandler(javax.activation.DataHandler) Date(java.util.Date) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPart(javax.mail.internet.MimeBodyPart) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource)

Example 99 with ByteArrayDataSource

use of javax.mail.util.ByteArrayDataSource in project VaadinUtils by rlsutton1.

the class JasperManager method run.

@Override
public void run() {
    JRSwapFileVirtualizer fileVirtualizer = null;
    CleanupCallback cleanupCallback = null;
    boolean initialized = false;
    try {
        logger.warn("{} permits are available", concurrentLimit.availablePermits());
        concurrentLimit.acquire();
        initialized = true;
        inQueue = false;
        queueEntry.setStatus("Gathering report data phase 1");
        reportProperties.initDBConnection();
        cleanupCallback = reportProperties.getCleanupCallback();
        List<ReportParameter<?>> extraParams = reportProperties.prepareData(params, reportProperties.getReportFileName(), cleanupCallback);
        compileReport();
        if (reportProperties.getCustomReportParameterMap() != null) {
            boundParams.putAll(reportProperties.getCustomReportParameterMap());
        }
        if (extraParams != null) {
            params.removeAll(extraParams);
            params.addAll(extraParams);
        }
        logger.info("Running report " + reportProperties.getReportFileName());
        for (ReportParameter<?> param : params) {
            for (String parameterName : param.getParameterNames()) {
                bindParameter(param, parameterName);
                if (param.displayInreport()) {
                    // populate dynamically added parameters to display user
                    // friendly parameters on the report
                    boundParams.put("ParamDisplay-" + parameterName, param.getDisplayValue(parameterName));
                }
                logger.info(parameterName + " " + param.getValue(parameterName));
            }
        }
        reportProperties.prepareForOutputFormat(exportMethod);
        queueEntry.setStatus("Gathering report data phase 2");
        // use file virtualizer to prevent out of heap
        String fileName = "/tmp";
        JRSwapFile file = new JRSwapFile(fileName, 100, 10);
        fileVirtualizer = new JRSwapFileVirtualizer(500, file);
        boundParams.put(JRParameter.REPORT_VIRTUALIZER, fileVirtualizer);
        if (stop) {
            return;
        }
        if (exportMethod == OutputFormat.CSV) {
            boundParams.put(JRParameter.IS_IGNORE_PAGINATION, true);
        }
        JasperPrint jasper_print = fillReport(exportMethod);
        queueEntry.setStatus("Waiting for browser to start streaming");
        if (stop) {
            return;
        }
        logger.warn("Output stream is ready");
        progressListener.outputStreamReady();
        logger.warn("Waiting for reader stream");
        if (readerReady.await(10, TimeUnit.SECONDS)) {
            outputStream = new PipedOutputStream(inputStream);
            writerReady.countDown();
            exportMethod.export(jasper_print, outputStream, getPageMonitorConfig(exportMethod.getConfig()), images);
            imagesrcs = (images.size() <= 0) ? null : new DataSource[images.size()];
            if (imagesrcs != null) {
                int xi = 0;
                for (Map.Entry<String, byte[]> entry : images.entrySet()) {
                    ByteArrayDataSource image = new ByteArrayDataSource(entry.getValue(), "image/gif");
                    image.setName(entry.getKey());
                    imagesrcs[xi++] = image;
                }
            }
        } else {
            logger.error("Couldn't attach to reader stream");
        }
        Thread.sleep(750);
        queueEntry.setStatus("Cleaning up");
    } catch (Exception e) {
        logger.error(e, e);
    } finally {
        if (queueEntry != null) {
            jobQueue.remove(queueEntry);
            queueEntry = null;
        }
        try {
            if (outputStream != null) {
                outputStream.close();
            }
        } catch (IOException e) {
            logger.error(e, e);
        }
        if (cleanupCallback != null) {
            try {
                cleanupCallback.cleanup();
            } catch (Exception e) {
                logger.error(e, e);
            }
        }
        if (fileVirtualizer != null) {
            try {
                fileVirtualizer.cleanup();
            } catch (Exception e) {
                logger.error(e, e);
            }
        }
        if (initialized) {
            concurrentLimit.release();
            try {
                reportProperties.closeDBConnection();
            } catch (Exception e) {
                logger.error(e, e);
            }
        }
        completeBarrier.countDown();
        if (progressListener != null) {
            progressListener.completed();
        }
    }
}
Also used : ReportParameter(au.com.vaadinutils.jasper.parameter.ReportParameter) JRSwapFileVirtualizer(net.sf.jasperreports.engine.fill.JRSwapFileVirtualizer) JasperPrint(net.sf.jasperreports.engine.JasperPrint) JRSwapFile(net.sf.jasperreports.engine.util.JRSwapFile) PipedOutputStream(java.io.PipedOutputStream) IOException(java.io.IOException) JasperPrint(net.sf.jasperreports.engine.JasperPrint) FileNotFoundException(java.io.FileNotFoundException) JRException(net.sf.jasperreports.engine.JRException) IOException(java.io.IOException) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) DataSource(javax.activation.DataSource) CleanupCallback(au.com.vaadinutils.jasper.ui.CleanupCallback) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource)

Example 100 with ByteArrayDataSource

use of javax.mail.util.ByteArrayDataSource in project VaadinUtils by rlsutton1.

the class RenderedReport method getBodyAsDataSource.

public DataSource getBodyAsDataSource(String fileName, AttachmentType type) throws IOException {
    final ByteArrayDataSource body = new ByteArrayDataSource(reportBody, type.toString());
    body.setName(fileName);
    return body;
}
Also used : ByteArrayDataSource(javax.mail.util.ByteArrayDataSource)

Aggregations

ByteArrayDataSource (javax.mail.util.ByteArrayDataSource)119 DataHandler (javax.activation.DataHandler)82 MimeMultipart (javax.mail.internet.MimeMultipart)39 InputStream (java.io.InputStream)38 IOException (java.io.IOException)35 MimeBodyPart (javax.mail.internet.MimeBodyPart)35 DataSource (javax.activation.DataSource)32 MimeMessage (javax.mail.internet.MimeMessage)31 ByteArrayOutputStream (java.io.ByteArrayOutputStream)25 Test (org.junit.Test)25 MessagingException (javax.mail.MessagingException)21 ByteArrayInputStream (java.io.ByteArrayInputStream)17 ArrayList (java.util.ArrayList)17 InternetAddress (javax.mail.internet.InternetAddress)16 Session (javax.mail.Session)14 Properties (java.util.Properties)12 Date (java.util.Date)11 List (java.util.List)8 Multipart (javax.mail.Multipart)8 Holder (javax.xml.ws.Holder)8