Search in sources :

Example 96 with DefaultHttpResponse

use of org.jboss.netty.handler.codec.http.DefaultHttpResponse in project voldemort by voldemort.

the class DeleteResponseSender method sendResponse.

@Override
public void sendResponse(StoreStats performanceStats, boolean isFromLocalZone, long startTimeInMs) {
    // Create the Response object
    HttpResponse response = new DefaultHttpResponse(HTTP_1_1, NO_CONTENT);
    // Set the right headers
    response.setHeader(CONTENT_LENGTH, "0");
    // Write the response to the Netty Channel
    if (logger.isDebugEnabled()) {
        String keyStr = RestUtils.getKeyHexString(key);
        debugLog("DELETE", this.storeName, keyStr, startTimeInMs, System.currentTimeMillis(), 0);
    }
    this.messageEvent.getChannel().write(response);
    if (performanceStats != null && isFromLocalZone) {
        recordStats(performanceStats, startTimeInMs, Tracked.DELETE);
    }
}
Also used : DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse)

Example 97 with DefaultHttpResponse

use of org.jboss.netty.handler.codec.http.DefaultHttpResponse in project voldemort by voldemort.

the class GetResponseSender method sendResponse.

/**
     * Sends a multipart response. Each body part represents a versioned value
     * of the given key.
     *
     * @throws IOException
     * @throws MessagingException
     */
@Override
public void sendResponse(StoreStats performanceStats, boolean isFromLocalZone, long startTimeInMs) throws Exception {
    /*
         * Pay attention to the code below. Note that in this method we wrap a multiPart object with a mimeMessage.
         * However when writing to the outputStream we only send the multiPart object and not the entire
         * mimeMessage. This is intentional.
         *
         * In the earlier version of this code we used to create a multiPart object and just send that multiPart
         * across the wire.
         *
         * However, we later discovered that upon setting the content of a MimeBodyPart, JavaMail internally creates
         * a DataHandler object wrapping the object you passed in. The part's Content-Type header is not updated
         * immediately. In order to get the headers updated, one needs to to call MimeMessage.saveChanges() on the
         * enclosing message, which cascades down the MIME structure into a call to MimeBodyPart.updateHeaders()
         * on the body part. It's this updateHeaders call that transfers the content type from the
         * DataHandler to the part's MIME Content-Type header.
         *
         * To make sure that the Content-Type headers are being updated (without changing too much code), we decided
         * to wrap the multiPart in a mimeMessage, call mimeMessage.saveChanges() and then just send the multiPart.
         * This is to make sure multiPart's headers are updated accurately.
         */
    MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties()));
    MimeMultipart multiPart = new MimeMultipart();
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    String base64Key = RestUtils.encodeVoldemortKey(key.get());
    String contentLocationKey = "/" + this.storeName + "/" + base64Key;
    for (Versioned<byte[]> versionedValue : versionedValues) {
        byte[] responseValue = versionedValue.getValue();
        VectorClock vectorClock = (VectorClock) versionedValue.getVersion();
        String eTag = RestUtils.getSerializedVectorClock(vectorClock);
        numVectorClockEntries += vectorClock.getVersionMap().size();
        // Create the individual body part for each versioned value of the
        // requested key
        MimeBodyPart body = new MimeBodyPart();
        try {
            // Add the right headers
            body.addHeader(CONTENT_TYPE, "application/octet-stream");
            body.addHeader(CONTENT_TRANSFER_ENCODING, "binary");
            body.addHeader(RestMessageHeaders.X_VOLD_VECTOR_CLOCK, eTag);
            body.setContent(responseValue, "application/octet-stream");
            body.addHeader(RestMessageHeaders.CONTENT_LENGTH, Integer.toString(responseValue.length));
            multiPart.addBodyPart(body);
        } catch (MessagingException me) {
            logger.error("Exception while constructing body part", me);
            outputStream.close();
            throw me;
        }
    }
    message.setContent(multiPart);
    message.saveChanges();
    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");
    response.setHeader(CONTENT_LOCATION, contentLocationKey);
    // Copy the data into the payload
    response.setContent(responseContent);
    response.setHeader(CONTENT_LENGTH, response.getContent().readableBytes());
    // Write the response to the Netty Channel
    if (logger.isDebugEnabled()) {
        String keyStr = RestUtils.getKeyHexString(this.key);
        debugLog("GET", this.storeName, keyStr, startTimeInMs, System.currentTimeMillis(), numVectorClockEntries);
    }
    this.messageEvent.getChannel().write(response);
    if (performanceStats != null && isFromLocalZone) {
        recordStats(performanceStats, startTimeInMs, Tracked.GET);
    }
    outputStream.close();
}
Also used : MessagingException(javax.mail.MessagingException) VectorClock(voldemort.versioning.VectorClock) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) Properties(java.util.Properties) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Example 98 with DefaultHttpResponse

use of org.jboss.netty.handler.codec.http.DefaultHttpResponse in project voldemort by voldemort.

the class GetVersionResponseSender method sendResponse.

@Override
public void sendResponse(StoreStats performanceStats, boolean isFromLocalZone, long startTimeInMs) throws Exception {
    String base64Key = RestUtils.encodeVoldemortKey(key.get());
    String contentLocationKey = "/" + this.storeName + "/" + base64Key;
    List<VectorClock> vectorClocks = new ArrayList<VectorClock>();
    for (Version versionedValue : versionedValues) {
        VectorClock vectorClock = (VectorClock) versionedValue;
        vectorClocks.add(vectorClock);
        numVectorClockEntries += vectorClock.getVersionMap().size();
    }
    String eTags = RestUtils.getSerializedVectorClocks(vectorClocks);
    byte[] responseContent = eTags.getBytes();
    ChannelBuffer responseContentBuffer = ChannelBuffers.dynamicBuffer(responseContent.length);
    responseContentBuffer.writeBytes(responseContent);
    // Create the Response object
    HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
    // Set the right headers
    response.setHeader(CONTENT_TYPE, "binary");
    response.setHeader(CONTENT_TRANSFER_ENCODING, "binary");
    response.setHeader(CONTENT_LOCATION, contentLocationKey);
    // Copy the data into the payload
    response.setContent(responseContentBuffer);
    response.setHeader(CONTENT_LENGTH, response.getContent().readableBytes());
    // Write the response to the Netty Channel
    this.messageEvent.getChannel().write(response);
}
Also used : Version(voldemort.versioning.Version) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) VectorClock(voldemort.versioning.VectorClock) ArrayList(java.util.ArrayList) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 99 with DefaultHttpResponse

use of org.jboss.netty.handler.codec.http.DefaultHttpResponse in project voldemort by voldemort.

the class PutResponseSender method sendResponse.

@Override
public void sendResponse(StoreStats performanceStats, boolean isFromLocalZone, long startTimeInMs) {
    // Create the Response object
    HttpResponse response = new DefaultHttpResponse(HTTP_1_1, CREATED);
    // Set the right headers
    response.setHeader(CONTENT_LENGTH, 0);
    if (this.successfulPutVC != null) {
        numVectorClockEntries += successfulPutVC.getVersionMap().size();
        String serializedVC = RestUtils.getSerializedVectorClock(successfulPutVC);
        response.setHeader(RestMessageHeaders.X_VOLD_VECTOR_CLOCK, serializedVC);
    }
    // Write the response to the Netty Channel
    if (logger.isDebugEnabled()) {
        String keyStr = RestUtils.getKeyHexString(key);
        debugLog("PUT", this.storeName, keyStr, startTimeInMs, System.currentTimeMillis(), numVectorClockEntries);
    }
    this.messageEvent.getChannel().write(response);
    if (performanceStats != null && isFromLocalZone) {
        recordStats(performanceStats, startTimeInMs, Tracked.PUT);
    }
}
Also used : DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse)

Example 100 with DefaultHttpResponse

use of org.jboss.netty.handler.codec.http.DefaultHttpResponse in project cdap by caskdata.

the class HttpStatusRequestHandler method messageReceived.

@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent event) throws Exception {
    Object msg = event.getMessage();
    if (msg instanceof HttpRequest) {
        HttpRequest request = (HttpRequest) msg;
        // be served by the router itself without it talking to any downstream services
        if (request.getUri().equals(Constants.EndPoints.STATUS)) {
            String statusString = Constants.Monitor.STATUS_OK;
            ChannelBuffer responseContent = ChannelBuffers.wrappedBuffer(Charsets.UTF_8.encode(statusString));
            HttpResponse httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
            httpResponse.setHeader(HttpHeaders.Names.CONTENT_TYPE, "text/plain");
            httpResponse.setHeader(HttpHeaders.Names.CONTENT_LENGTH, responseContent.readableBytes());
            httpResponse.setContent(responseContent);
            ChannelFuture writeFuture = Channels.future(event.getChannel());
            Channels.write(ctx, writeFuture, httpResponse);
            writeFuture.addListener(ChannelFutureListener.CLOSE);
            return;
        }
    }
    super.messageReceived(ctx, event);
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) ChannelFuture(org.jboss.netty.channel.ChannelFuture) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Aggregations

DefaultHttpResponse (org.jboss.netty.handler.codec.http.DefaultHttpResponse)101 HttpResponse (org.jboss.netty.handler.codec.http.HttpResponse)76 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)55 Test (org.testng.annotations.Test)54 DefaultHttpChunk (org.jboss.netty.handler.codec.http.DefaultHttpChunk)51 HttpChunk (org.jboss.netty.handler.codec.http.HttpChunk)50 DefaultHttpChunkTrailer (org.jboss.netty.handler.codec.http.DefaultHttpChunkTrailer)38 HttpChunkTrailer (org.jboss.netty.handler.codec.http.HttpChunkTrailer)34 Checkpoint (com.linkedin.databus.core.Checkpoint)25 BootstrapDatabaseTooOldException (com.linkedin.databus2.core.container.request.BootstrapDatabaseTooOldException)25 ChannelFuture (org.jboss.netty.channel.ChannelFuture)20 Channel (org.jboss.netty.channel.Channel)19 DefaultHttpRequest (org.jboss.netty.handler.codec.http.DefaultHttpRequest)19 ConditionCheck (com.linkedin.databus2.test.ConditionCheck)18 InetSocketAddress (java.net.InetSocketAddress)17 HttpRequest (org.jboss.netty.handler.codec.http.HttpRequest)17 SocketAddress (java.net.SocketAddress)16 Logger (org.apache.log4j.Logger)15 Test (org.junit.Test)11 SucceededChannelFuture (org.jboss.netty.channel.SucceededChannelFuture)10