Example usage for javax.mail.internet MimeBodyPart addHeader

List of usage examples for javax.mail.internet MimeBodyPart addHeader

Introduction

In this page you can find the example usage for javax.mail.internet MimeBodyPart addHeader.

Prototype

@Override
public void addHeader(String name, String value) throws MessagingException 

Source Link

Document

Add this value to the existing values for this header_name.

Usage

From source file:org.pentaho.di.trans.steps.mail.Mail.java

private void addAttachedFilePart(FileObject file) throws Exception {
    // create a data source

    MimeBodyPart files = new MimeBodyPart();
    // create a data source
    URLDataSource fds = new URLDataSource(file.getURL());
    // get a data Handler to manipulate this file type;
    files.setDataHandler(new DataHandler(fds));
    // include the file in the data source
    files.setFileName(file.getName().getBaseName());
    // insist on base64 to preserve line endings
    files.addHeader("Content-Transfer-Encoding", "base64");
    // add the part with the file in the BodyPart();
    data.parts.addBodyPart(files);//from  w ww  .  j a  v  a 2 s . com
    if (isDetailed()) {
        logDetailed(BaseMessages.getString(PKG, "Mail.Log.AttachedFile", fds.getName()));
    }

}

From source file:voldemort.coordinator.HttpGetAllRequestExecutor.java

public void writeResponse(Map<ByteArray, List<Versioned<byte[]>>> versionedResponses) throws Exception {
    // multiPartKeys is the outer multipart
    MimeMultipart multiPartKeys = new MimeMultipart();
    ByteArrayOutputStream keysOutputStream = new ByteArrayOutputStream();

    for (Entry<ByteArray, List<Versioned<byte[]>>> entry : versionedResponses.entrySet()) {
        ByteArray key = entry.getKey();/*from   ww w . j a va 2 s  .  co  m*/
        String contentLocationKey = "/" + this.storeName + "/" + new String(Base64.encodeBase64(key.get()));

        // Create the individual body part - for each key requested
        MimeBodyPart keyBody = new MimeBodyPart();
        try {
            // Add the right headers
            keyBody.addHeader(CONTENT_TYPE, "multipart/binary");
            keyBody.addHeader(CONTENT_TRANSFER_ENCODING, "binary");
            keyBody.addHeader(CONTENT_LOCATION, contentLocationKey);
        } catch (MessagingException me) {
            logger.error("Exception while constructing key body headers", me);
            keysOutputStream.close();
            throw me;
        }
        // multiPartValues is the inner multipart
        MimeMultipart multiPartValues = new MimeMultipart();
        for (Versioned<byte[]> versionedValue : entry.getValue()) {

            byte[] responseValue = versionedValue.getValue();

            VectorClock vectorClock = (VectorClock) versionedValue.getVersion();
            String serializedVC = CoordinatorUtils.getSerializedVectorClock(vectorClock);

            // Create the individual body part - for each versioned value of
            // a key
            MimeBodyPart valueBody = new MimeBodyPart();
            try {
                // Add the right headers
                valueBody.addHeader(CONTENT_TYPE, "application/octet-stream");
                valueBody.addHeader(CONTENT_TRANSFER_ENCODING, "binary");
                valueBody.addHeader(VoldemortHttpRequestHandler.X_VOLD_VECTOR_CLOCK, serializedVC);
                valueBody.setContent(responseValue, "application/octet-stream");

                multiPartValues.addBodyPart(valueBody);
            } catch (MessagingException me) {
                logger.error("Exception while constructing value body part", me);
                keysOutputStream.close();
                throw me;
            }

        }
        try {
            // Add the inner multipart as the content of the outer body part
            keyBody.setContent(multiPartValues);
            multiPartKeys.addBodyPart(keyBody);
        } catch (MessagingException me) {
            logger.error("Exception while constructing key body part", me);
            keysOutputStream.close();
            throw me;
        }

    }
    try {
        multiPartKeys.writeTo(keysOutputStream);
    } catch (Exception e) {
        logger.error("Exception while writing mutipart to output stream", e);
        throw e;
    }

    ChannelBuffer responseContent = ChannelBuffers.dynamicBuffer();
    responseContent.writeBytes(keysOutputStream.toByteArray());

    // Create the Response object
    HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);

    // Set the right headers
    response.setHeader(CONTENT_TYPE, "multipart/binary");
    response.setHeader(CONTENT_TRANSFER_ENCODING, "binary");

    // Copy the data into the payload
    response.setContent(responseContent);
    response.setHeader(CONTENT_LENGTH, response.getContent().readableBytes());

    // Update the stats
    if (this.coordinatorPerfStats != null) {
        long durationInNs = System.nanoTime() - startTimestampInNs;
        this.coordinatorPerfStats.recordTime(Tracked.GET_ALL, durationInNs);
    }

    // Write the response to the Netty Channel
    this.getRequestMessageEvent.getChannel().write(response);

    keysOutputStream.close();
}

From source file:voldemort.coordinator.HttpGetAllRequestExecutor.java

public void writeResponseOld(Map<ByteArray, Versioned<byte[]>> responseVersioned) {

    // Multipart response
    MimeMultipart mp = new MimeMultipart();
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

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

        for (Entry<ByteArray, Versioned<byte[]>> entry : responseVersioned.entrySet()) {
            Versioned<byte[]> value = entry.getValue();
            ByteArray keyByteArray = entry.getKey();
            String base64Key = new String(Base64.encodeBase64(keyByteArray.get()));
            String contentLocationKey = "/" + this.storeName + "/" + base64Key;

            byte[] responseValue = value.getValue();

            VectorClock vc = (VectorClock) value.getVersion();
            VectorClockWrapper vcWrapper = new VectorClockWrapper(vc);
            ObjectMapper mapper = new ObjectMapper();
            String eTag = "";
            try {
                eTag = mapper.writeValueAsString(vcWrapper);
            } catch (JsonGenerationException e) {
                e.printStackTrace();
            } catch (JsonMappingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            if (logger.isDebugEnabled()) {
                logger.debug("ETAG : " + eTag);
            }

            // Create the individual body part
            MimeBodyPart body = new MimeBodyPart();
            body.addHeader(CONTENT_TYPE, "application/octet-stream");
            body.addHeader(CONTENT_LOCATION, contentLocationKey);
            body.addHeader(CONTENT_TRANSFER_ENCODING, "binary");
            body.addHeader(CONTENT_LENGTH, "" + responseValue.length);
            body.addHeader(ETAG, eTag);
            body.setContent(responseValue, "application/octet-stream");
            mp.addBodyPart(body);
        }

        // At this point we have a complete multi-part response
        mp.writeTo(outputStream);

    } catch (MessagingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    ChannelBuffer responseContent = ChannelBuffers.dynamicBuffer();
    responseContent.writeBytes(outputStream.toByteArray());

    // 1. Create the Response object
    HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);

    // 2. Set the right headers
    response.setHeader(CONTENT_TYPE, "multipart/binary");
    response.setHeader(CONTENT_TRANSFER_ENCODING, "binary");

    // 3. Copy the data into the payload
    response.setContent(responseContent);
    response.setHeader(CONTENT_LENGTH, response.getContent().readableBytes());

    // Update the stats
    if (this.coordinatorPerfStats != null) {
        long durationInNs = System.nanoTime() - startTimestampInNs;
        this.coordinatorPerfStats.recordTime(Tracked.GET_ALL, durationInNs);
    }

    // Write the response to the Netty Channel
    this.getRequestMessageEvent.getChannel().write(response);
}

From source file:voldemort.coordinator.HttpGetRequestExecutor.java

public void writeResponse(List<Versioned<byte[]>> versionedValues) throws Exception {

    MimeMultipart multiPart = new MimeMultipart();
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

    for (Versioned<byte[]> versionedValue : versionedValues) {

        byte[] responseValue = versionedValue.getValue();

        VectorClock vectorClock = (VectorClock) versionedValue.getVersion();
        String serializedVectorClock = CoordinatorUtils.getSerializedVectorClock(vectorClock);

        // Create the individual body part for each versioned value of the
        // requested key
        MimeBodyPart body = new MimeBodyPart();
        try {//www.ja  v a2s .  c o m
            // Add the right headers
            body.addHeader(CONTENT_TYPE, "application/octet-stream");
            body.addHeader(CONTENT_TRANSFER_ENCODING, "binary");
            body.addHeader(VoldemortHttpRequestHandler.X_VOLD_VECTOR_CLOCK, serializedVectorClock);
            body.setContent(responseValue, "application/octet-stream");
            body.addHeader(CONTENT_LENGTH, "" + responseValue.length);

            multiPart.addBodyPart(body);
        } catch (MessagingException me) {
            logger.error("Exception while constructing body part", me);
            outputStream.close();
            throw me;
        }
    }
    try {
        multiPart.writeTo(outputStream);
    } catch (Exception e) {
        logger.error("Exception while writing multipart to output stream", e);
        outputStream.close();
        throw e;
    }
    ChannelBuffer responseContent = ChannelBuffers.dynamicBuffer();
    responseContent.writeBytes(outputStream.toByteArray());

    // Create the Response object
    HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);

    // Set the right headers
    response.setHeader(CONTENT_TYPE, "multipart/binary");
    response.setHeader(CONTENT_TRANSFER_ENCODING, "binary");

    // Copy the data into the payload
    response.setContent(responseContent);
    response.setHeader(CONTENT_LENGTH, response.getContent().readableBytes());

    if (logger.isDebugEnabled()) {
        logger.debug("Response = " + response);
    }

    // Update the stats
    if (this.coordinatorPerfStats != null) {
        long durationInNs = System.nanoTime() - startTimestampInNs;
        this.coordinatorPerfStats.recordTime(Tracked.GET, durationInNs);
    }

    // Write the response to the Netty Channel
    this.getRequestMessageEvent.getChannel().write(response);
}