Example usage for javax.mail.internet InternetHeaders setHeader

List of usage examples for javax.mail.internet InternetHeaders setHeader

Introduction

In this page you can find the example usage for javax.mail.internet InternetHeaders setHeader.

Prototype

public void setHeader(String name, String value) 

Source Link

Document

Change the first header line that matches name to have value, adding a new header if no existing header matches.

Usage

From source file:com.adaptris.core.services.aggregator.MimeAggregator.java

private static byte[] encodeData(byte[] data, String encoding, InternetHeaders hdrs)
        throws MessagingException, IOException {
    if (!isBlank(encoding)) {
        hdrs.setHeader(HEADER_CONTENT_ENCODING, encoding);
    }// w w  w . j  av  a  2 s  .  co m
    try (ByteArrayOutputStream out = new ByteArrayOutputStream();
            OutputStream encodedOut = MimeUtility.encode(out, encoding)) {
        encodedOut.write(data);
        return out.toByteArray();
    }
}

From source file:com.adaptris.util.text.mime.MultiPartOutput.java

private static OutputStream wrap(OutputStream original, String encoding, InternetHeaders hdrs)
        throws MessagingException {
    OutputStream encodedOut = original;
    if (encoding != null) {
        encodedOut = MimeUtility.encode(original, encoding);
        hdrs.setHeader(HEADER_CONTENT_ENCODING, encoding);
    }// w  w  w . ja  v  a2s. c  om
    return encodedOut;
}

From source file:com.adaptris.mail.MailSenderImp.java

/**
 * Return the encoded payload./*from   w  ww. j  av  a  2s  . c om*/
 * <p>
 * In the situation where we are directly creating the <code>MimeBodypart</code> with a set of InternetHeaders and a byte payload,
 * we have to encode the payload ourselves, as no encoding appears to be done when the body part is written. This is implied by
 * the documentation for MimeBodyPart.
 * </p>
 *
 * @return the bytes
 * @param unencoded the payload in its raw form.
 * @param header the existing InternetHeaders so that the appropriate content transfer encoding header can be set.
 * @param encoding for this particular part of the message
 * @see MimeBodypart#MimeBodyPart(InternetHeaders, byte[])
 */
private byte[] encode(byte[] unencoded, InternetHeaders header, String encoding)
        throws MessagingException, IOException {

    byte[] encoded = unencoded;
    if (encoding != null) {

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        OutputStream encodedOut = MimeUtility.encode(out, encoding);
        encodedOut.write(unencoded);
        encodedOut.flush();
        encodedOut.close();
        encoded = out.toByteArray();
        header.setHeader(Mail.CONTENT_ENCODING, encoding);
    }
    return encoded;
}

From source file:com.boundlessgeo.geoserver.AppIntegrationTest.java

MimeMultipart appendMultiPartFormContent(MimeMultipart body, String contentDisposition, String contentType,
        byte[] content) throws Exception {
    InternetHeaders headers = new InternetHeaders();
    headers.setHeader("Content-Disposition", contentDisposition);
    headers.setHeader("Content-Type", contentType);
    body.addBodyPart(new MimeBodyPart(headers, content));

    return body;//w w w  .j  av a  2s  . c om
}

From source file:com.boundlessgeo.geoserver.AppIntegrationTest.java

void createMultiPartFormContent(MockHttpServletRequest request, String contentDisposition, String contentType,
        byte[] content) throws Exception {
    MimeMultipart body = new MimeMultipart();
    request.setContentType(body.getContentType());
    InternetHeaders headers = new InternetHeaders();
    headers.setHeader("Content-Disposition", contentDisposition);
    headers.setHeader("Content-Type", contentType);
    body.addBodyPart(new MimeBodyPart(headers, content));

    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    body.writeTo(bout);/*from   ww  w. ja v a2 s.  c  o  m*/
    request.setContent(bout.toByteArray());
}

From source file:org.apache.axis2.datasource.jaxb.JAXBAttachmentMarshaller.java

public String addMtomAttachment(byte[] data, int offset, int length, String mimeType, String namespace,
        String localPart) {//from   ww  w.j a v  a 2s.c om

    if (offset != 0 || length != data.length) {
        int len = length - offset;
        byte[] newData = new byte[len];
        System.arraycopy(data, offset, newData, 0, len);
        data = newData;
    }

    if (mimeType == null || mimeType.length() == 0) {
        mimeType = APPLICATION_OCTET;
    }

    if (log.isDebugEnabled()) {
        log.debug("Adding MTOM/XOP byte array attachment for element: " + "{" + namespace + "}" + localPart);
    }

    String cid = null;

    try {
        // Create MIME Body Part
        final InternetHeaders ih = new InternetHeaders();
        final byte[] dataArray = data;
        ih.setHeader(HTTPConstants.HEADER_CONTENT_TYPE, mimeType);
        final MimeBodyPart mbp = (MimeBodyPart) AccessController.doPrivileged(new PrivilegedAction() {
            public Object run() {
                try {
                    return new MimeBodyPart(ih, dataArray);
                } catch (MessagingException e) {
                    throw new OMException(e);
                }
            }
        });

        //Create a data source for the MIME Body Part
        MimePartDataSource mpds = (MimePartDataSource) AccessController.doPrivileged(new PrivilegedAction() {
            public Object run() {
                return new MimePartDataSource(mbp);
            }
        });
        long dataLength = data.length;
        Integer value = null;
        if (msgContext != null) {
            value = (Integer) msgContext.getProperty(Constants.Configuration.MTOM_THRESHOLD);
        } else if (log.isDebugEnabled()) {
            log.debug(
                    "The msgContext is null so the MTOM threshold value can not be determined; it will default to 0.");
        }

        int optimizedThreshold = (value != null) ? value.intValue() : 0;

        if (optimizedThreshold == 0 || dataLength > optimizedThreshold) {
            DataHandler dataHandler = new DataHandler(mpds);
            cid = addDataHandler(dataHandler, false);
        }

        // Add the content id to the mime body part
        mbp.setHeader(HTTPConstants.HEADER_CONTENT_ID, cid);
    } catch (MessagingException e) {
        throw new OMException(e);
    }

    return cid == null ? null : "cid:" + cid;
}

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

private static DataHandler createDataHandler(Object value, Class cls, String[] mimeTypes, String cid) {
    if (log.isDebugEnabled()) {
        System.out.println("Construct data handler for " + cls + " cid=" + cid);
    }//w w  w.  j av a 2  s .c om
    DataHandler dh = null;
    if (cls.isAssignableFrom(DataHandler.class)) {
        dh = (DataHandler) value;
        if (dh == null) {
            return dh; //return if DataHandler is null
        }

        try {
            Object content = dh.getContent();
            // If the content is a Source, convert to a String due to 
            // problems with the DataContentHandler
            if (content instanceof Source) {
                if (log.isDebugEnabled()) {
                    System.out
                            .println("Converting DataHandler Source content to " + "DataHandlerString content");
                }
                byte[] bytes = (byte[]) ConvertUtils.convert(content, byte[].class);
                String newContent = new String(bytes);
                return new DataHandler(newContent, mimeTypes[0]);
            }
        } catch (Exception e) {
            throw ExceptionFactory.makeWebServiceException(e);
        }
    } else {
        try {
            byte[] bytes = createBytes(value, cls, mimeTypes);
            // Create MIME Body Part
            InternetHeaders ih = new InternetHeaders();
            ih.setHeader(HTTPConstants.HEADER_CONTENT_TYPE, mimeTypes[0]);
            MimeBodyPart mbp = new MimeBodyPart(ih, bytes);

            //Create a data source for the MIME Body Part
            MimePartDataSource ds = new MimePartDataSource(mbp);

            dh = new DataHandler(ds);
            mbp.setHeader(HTTPConstants.HEADER_CONTENT_ID, cid);
        } catch (Exception e) {
            throw ExceptionFactory.makeWebServiceException(e);
        }
    }
    return dh;
}