Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion src/main/java/org/elasticsearch/http/netty/NettyHttpChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,36 @@
import org.jboss.netty.handler.codec.http.*;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.Set;
import java.util.TimeZone;

/**
*
*/
public class NettyHttpChannel implements HttpChannel {

public static final String HTTP_DATE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss zzz";
public static final String HTTP_DATE_GMT_TIMEZONE = "GMT";

private final NettyHttpServerTransport transport;
private final Channel channel;
private final org.jboss.netty.handler.codec.http.HttpRequest request;
private boolean cacheable;

public NettyHttpChannel(NettyHttpServerTransport transport, Channel channel, org.jboss.netty.handler.codec.http.HttpRequest request) {
this.transport = transport;
this.channel = channel;
this.request = request;
}

public void setCacheable(boolean cacheable) {
this.cacheable = cacheable;
}

@Override
public void sendResponse(RestResponse response) {

Expand Down Expand Up @@ -131,6 +145,21 @@ public void sendResponse(RestResponse response) {
resp.setHeader(HttpHeaders.Names.CONTENT_TYPE, response.contentType());

resp.setHeader(HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(buf.readableBytes()));

// ETag handling, check if checksums matches, and deliver timestamped HTTP 304 "not modified" if so
if (cacheable) {
long checksum = response.contentChecksum();
resp.addHeader(HttpHeaders.Names.ETAG, checksum);
String etag = request.getHeader("If-None-Match");
if (etag != null && Long.parseLong(etag) == checksum) {
resp.setContent(null);
resp.setStatus(HttpResponseStatus.NOT_MODIFIED);
SimpleDateFormat dateFormatter = new SimpleDateFormat(HTTP_DATE_FORMAT, Locale.US);
dateFormatter.setTimeZone(TimeZone.getTimeZone(HTTP_DATE_GMT_TIMEZONE));
Calendar time = new GregorianCalendar();
resp.setHeader(HttpHeaders.Names.DATE, dateFormatter.format(time.getTime()));
}
}

if (transport.resetCookies) {
String cookieString = request.getHeader(HttpHeaders.Names.COOKIE);
Expand All @@ -147,7 +176,7 @@ public void sendResponse(RestResponse response) {
}
}
}

// Write the response.
ChannelFuture future = channel.write(resp);
if (releaseContentListener != null) {
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/org/elasticsearch/rest/AbstractRestResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@

package org.elasticsearch.rest;

import java.io.IOException;
import java.util.zip.Adler32;

/**
*
*/
public abstract class AbstractRestResponse implements RestResponse {

private long checksum = -1L;

@Override
public byte[] prefixContent() {
return null;
Expand Down Expand Up @@ -53,4 +58,23 @@ public int suffixContentLength() {
public int suffixContentOffset() {
return 0;
}

@Override
public long contentChecksum() {
if (checksum == -1L) {
checksum = createChecksum();
}
return checksum;
}

private long createChecksum() {
Adler32 adler = new Adler32();
try {
byte[] b = content();
adler.update(b,0,b.length);
} catch (IOException e) {
}
return adler.getValue();
}

}
2 changes: 2 additions & 0 deletions src/main/java/org/elasticsearch/rest/RestChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@
public interface RestChannel {

void sendResponse(RestResponse response);

void setCacheable(boolean enabled);
}
1 change: 1 addition & 0 deletions src/main/java/org/elasticsearch/rest/RestController.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ public void dispatchRequest(final RestRequest request, final RestChannel channel
void executeHandler(RestRequest request, RestChannel channel) {
final RestHandler handler = getHandler(request);
if (handler != null) {
channel.setCacheable(settings.getAsBoolean("http.cache.etag."+ handler.getClass().getSimpleName().toLowerCase(), false));
handler.handleRequest(request, channel);
} else {
if (request.method() == RestRequest.Method.OPTIONS) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/elasticsearch/rest/RestResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public interface RestResponse {
int contentLength() throws IOException;

int contentOffset() throws IOException;

long contentChecksum();

byte[] prefixContent();

Expand Down