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);
}
}
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();
}
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);
}
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);
}
}
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);
}
Aggregations