Example usage for javax.mail.internet MimeMultipart writeTo

List of usage examples for javax.mail.internet MimeMultipart writeTo

Introduction

In this page you can find the example usage for javax.mail.internet MimeMultipart writeTo.

Prototype

@Override
public synchronized void writeTo(OutputStream os) throws IOException, MessagingException 

Source Link

Document

Iterates through all the parts and outputs each MIME part separated by a boundary.

Usage

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 {//from  w  w w  . ja  va 2 s .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);
}