Skip to content
This repository was archived by the owner on Dec 19, 2025. It is now read-only.
Open
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
13 changes: 7 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ subprojects {

dependencies {
compile 'org.slf4j:slf4j-api:1.7.2'
compile 'io.netty:netty:3.6.1.Final'
compile 'io.netty:netty-all:4.0.26.Final'
compile 'com.netflix.servo:servo-core:0.4.32'
compile 'com.google.guava:guava:11.0.2'
compile 'com.google.inject:guice:3.0'
compile 'com.netflix.archaius:archaius-core:0.5.4'
compile 'com.netflix.netflix-commons:netflix-commons-util:0.1.1'
compile 'javax.ws.rs:jsr311-api:1.1.1'
compile 'commons-collections:commons-collections:3.2.1'
compile 'com.sun.jersey:jersey-server:1.13'
compile 'com.sun.jersey:jersey-core:1.13'
compile 'com.sun.jersey:jersey-bundle:1.13'
compile 'com.sun.jersey.contribs:jersey-guice:1.13'
compile 'com.sun.jersey:jersey-server:1.19'
compile 'com.sun.jersey:jersey-core:1.19'
compile 'com.sun.jersey:jersey-bundle:1.19'
compile 'com.sun.jersey.contribs:jersey-guice:1.19'
compile 'com.netflix.eureka:eureka-client:1.1.73'
compile 'com.netflix.blitz4j:blitz4j:1.21'
compile 'jetty:jetty:6.0.2'
Expand All @@ -64,6 +64,7 @@ subprojects {

project(':rss-core') {
apply plugin: 'java'
apply plugin : 'eclipse'
dependencies {
compile 'com.netflix.netflix-commons:netflix-statistics:0.1.1'
}
Expand All @@ -79,7 +80,7 @@ project(':rss-middletier') {
compile 'asm:asm-all:3.2'
compile 'commons-io:commons-io:2.4'
compile 'commons-configuration:commons-configuration:1.9'
compile 'com.sun.jersey:jersey-server:1.9.1'
compile 'com.sun.jersey:jersey-server:1.19'
}

jar {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,33 @@
import com.sun.jersey.spi.container.ContainerResponse;
import com.sun.jersey.spi.container.ContainerResponseWriter;
import com.sun.jersey.spi.container.WebApplication;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBufferInputStream;
import org.jboss.netty.buffer.ChannelBufferOutputStream;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.*;
import org.jboss.netty.channel.ChannelHandler.Sharable;
import org.jboss.netty.handler.codec.http.*;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufInputStream;
import io.netty.buffer.ByteBufOutputStream;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpMessage;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Sharable
public class NettyHandlerContainer extends SimpleChannelUpstreamHandler {
public class NettyHandlerContainer extends ChannelInboundHandlerAdapter {
public static final String PROPERTY_BASE_URI = "com.sun.jersey.server.impl.container.netty.baseUri";

private final WebApplication application;
Expand All @@ -63,60 +73,61 @@ public class NettyHandlerContainer extends SimpleChannelUpstreamHandler {
}

private final static class Writer implements ContainerResponseWriter {
private final Channel channel;
private HttpResponse response;
private final ChannelHandlerContext ctx;
private DefaultFullHttpResponse response;

private Writer(Channel channel) {
this.channel = channel;
private Writer(ChannelHandlerContext ctx) {
this.ctx = ctx;
}

public OutputStream writeStatusAndHeaders(long contentLength, ContainerResponse cResponse) throws IOException {

response = new DefaultHttpResponse(HttpVersion.HTTP_1_0, HttpResponseStatus.valueOf(cResponse.getStatus()));
ByteBuf buffer = Unpooled.buffer();
response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_0, HttpResponseStatus.valueOf(cResponse
.getStatus()), buffer);
for (Map.Entry<String, List<Object>> e : cResponse.getHttpHeaders().entrySet()) {
List<String> values = new ArrayList<String>();
for (Object v : e.getValue())
values.add(ContainerResponse.getHeaderValue(v));
response.setHeader(e.getKey(), values);
response.headers().set(e.getKey(), values);
}
ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
response.setContent(buffer);
return new ChannelBufferOutputStream(buffer);
return new ByteBufOutputStream(buffer);
}

public void finish() throws IOException {
// Streaming is not supported. Entire response will be written
// downstream once finish() is called.
channel.write(response).addListener(ChannelFutureListener.CLOSE);
final ChannelFuture f = ctx.writeAndFlush(response);
f.addListener(ChannelFutureListener.CLOSE);
}
}

@Override
public void messageReceived(ChannelHandlerContext context, MessageEvent e) throws Exception {
HttpRequest request = (HttpRequest) e.getMessage();
String base = getBaseUri(request);
URI baseUri = new URI(base);
URI requestUri = new URI(base.substring(0, base.length() - 1) + request.getUri());
ContainerRequest cRequest = new ContainerRequest(application, request
.getMethod().getName(), baseUri, requestUri,
getHeaders(request), new ChannelBufferInputStream(
request.getContent()));
application.handleRequest(cRequest, new Writer(e.getChannel()));
public void channelRead(ChannelHandlerContext ctx, Object msg) throws URISyntaxException, IOException {
if (msg instanceof FullHttpRequest) {
FullHttpRequest httpRequest = (FullHttpRequest) msg;
String base = getBaseUri(httpRequest);
URI baseUri = new URI(base);
URI requestUri = new URI(base.substring(0, base.length() - 1) + httpRequest.getUri());
ContainerRequest cRequest = new ContainerRequest(application, httpRequest.getMethod().name(), baseUri,
requestUri, getHeaders(httpRequest), new ByteBufInputStream(httpRequest.content()));
application.handleRequest(cRequest, new Writer(ctx));
}
}

private String getBaseUri(HttpRequest request) {
private String getBaseUri(HttpMessage httpMessage) {
if (baseUri != null) {
return baseUri;
}

return "http://" + request.getHeader(HttpHeaders.Names.HOST) + "/";
return "http://" + HttpHeaders.getHost(httpMessage) + "/";
}

private InBoundHeaders getHeaders(HttpRequest request) {
private InBoundHeaders getHeaders(FullHttpRequest httpRequest) {
InBoundHeaders headers = new InBoundHeaders();
for (String name : request.getHeaderNames()) {
headers.put(name, request.getHeaders(name));
HttpHeaders httpHeaders = httpRequest.headers();
for (String name : httpHeaders.names()) {
headers.put(name, httpHeaders.getAll(name));
}
return headers;
}

}
Loading