-
Notifications
You must be signed in to change notification settings - Fork 888
/
RequestHandler.java
108 lines (94 loc) · 3.58 KB
/
RequestHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package org.jboss.resteasy.plugins.server.netty;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.ChannelFutureListener;
import org.jboss.netty.channel.ChannelHandler.Sharable;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
import org.jboss.netty.handler.codec.frame.TooLongFrameException;
import org.jboss.netty.handler.codec.http.DefaultHttpResponse;
import org.jboss.netty.handler.codec.http.HttpResponse;
import org.jboss.netty.handler.codec.http.HttpResponseStatus;
import org.jboss.resteasy.logging.Logger;
import org.jboss.resteasy.spi.Failure;
import static org.jboss.netty.handler.codec.http.HttpResponseStatus.CONTINUE;
import static org.jboss.netty.handler.codec.http.HttpVersion.HTTP_1_1;
/**
* {@link SimpleChannelUpstreamHandler} which handles the requests and dispatch them.
*
* This class is {@link Sharable}.
*
* @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
* @author Andy Taylor (andy.taylor@jboss.org)
* @author <a href="http://gleamynode.net/">Trustin Lee</a>
* @author Norman Maurer
* @version $Rev: 2368 $, $Date: 2010-10-18 17:19:03 +0900 (Mon, 18 Oct 2010) $
*/
@Sharable
public class RequestHandler extends SimpleChannelUpstreamHandler
{
protected final RequestDispatcher dispatcher;
private final static Logger logger = Logger.getLogger(RequestHandler.class);
public RequestHandler(RequestDispatcher dispatcher)
{
this.dispatcher = dispatcher;
}
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception
{
if (e.getMessage() instanceof NettyHttpRequest) {
NettyHttpRequest request = (NettyHttpRequest) e.getMessage();
if (request.is100ContinueExpected())
{
send100Continue(e);
}
NettyHttpResponse response = request.getResponse();
try
{
dispatcher.service(request, response, true);
}
catch (Failure e1)
{
response.reset();
response.setStatus(e1.getErrorCode());
}
catch (Exception ex)
{
response.reset();
response.setStatus(500);
logger.error("Unexpected", ex);
}
// Write the response.
ChannelFuture future = e.getChannel().write(response);
//NETTY-391
NettyJaxrsServer.allChannels.add(e.getChannel());
// Close the non-keep-alive connection after the write operation is done.
if (!request.isKeepAlive())
{
future.addListener(ChannelFutureListener.CLOSE);
}
}
}
private void send100Continue(MessageEvent e)
{
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, CONTINUE);
e.getChannel().write(response);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
throws Exception
{
// handle the case of to big requests.
if (e.getCause() instanceof TooLongFrameException)
{
DefaultHttpResponse response = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE);
e.getChannel().write(response).addListener(ChannelFutureListener.CLOSE);
}
else
{
e.getCause().printStackTrace();
e.getChannel().close();
}
}
}