use of org.jboss.netty.handler.codec.http.DefaultHttpResponse in project cdap by caskdata.
the class HttpRequestHandler method exceptionCaught.
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
Throwable cause = e.getCause();
// avoid handling exception more than once from a handler, to avoid a possible infinite recursion
switch(exceptionsHandled.incrementAndGet()) {
case 1:
// if this is the first error, break and handle the error normally (below)
break;
case 2:
// if its the second time, log and return
LOG.error("Not handling exception due to already having handled an exception in Request Handler {}", ctx.getChannel(), cause);
// fall through
default:
// in an exception and cause recursion
return;
}
if (cause instanceof HandlerException && ((HandlerException) cause).getFailureStatus() != HttpResponseStatus.INTERNAL_SERVER_ERROR) {
LOG.debug("Exception raised in Request Handler {}", ctx.getChannel(), cause);
} else {
LOG.error("Exception raised in Request Handler {}", ctx.getChannel(), cause);
}
if (ctx.getChannel().isConnected() && !channelClosed) {
HttpResponse response = cause instanceof HandlerException ? ((HandlerException) cause).createFailureResponse() : new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR);
Channels.write(ctx, e.getFuture(), response);
e.getFuture().addListener(ChannelFutureListener.CLOSE);
}
}
use of org.jboss.netty.handler.codec.http.DefaultHttpResponse in project cdap by caskdata.
the class AuthenticationChannelHandler method exceptionCaught.
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
LOG.error("Got exception: ", e.getCause());
ChannelFuture future = Channels.future(ctx.getChannel());
future.addListener(ChannelFutureListener.CLOSE);
// TODO: add WWW-Authenticate header for 401 response - REACTOR-900
HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.UNAUTHORIZED);
Channels.write(ctx, future, response);
}
use of org.jboss.netty.handler.codec.http.DefaultHttpResponse in project load-balancer by RestComm.
the class TestHA method writeResponse.
private void writeResponse(MessageEvent e, HttpResponseStatus status, String command) {
JsonObject jo = new JsonObject();
jo.addProperty(command, Protocol.OK);
ChannelBuffer buf = ChannelBuffers.copiedBuffer(jo.toString(), Charset.forName("UTF-8"));
HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, status);
response.setHeader(HttpHeaders.Names.CONTENT_TYPE, APPLICATION_JSON);
response.setHeader(HttpHeaders.Names.CONTENT_LENGTH, buf.readableBytes());
response.setContent(buf);
ChannelFuture future = e.getChannel().write(response);
future.addListener(ChannelFutureListener.CLOSE);
}
use of org.jboss.netty.handler.codec.http.DefaultHttpResponse in project load-balancer by RestComm.
the class TestNodeRegister method writeResponse.
private void writeResponse(MessageEvent e, HttpResponseStatus status, String command) {
Packet packet = null;
switch(command) {
case Protocol.HEARTBEAT:
packet = new HeartbeatResponsePacket(Protocol.OK);
break;
case Protocol.START:
packet = new StartResponsePacket(Protocol.OK);
break;
case Protocol.SHUTDOWN:
packet = new ShutdownResponsePacket(Protocol.OK);
break;
}
ChannelBuffer buf = ChannelBuffers.copiedBuffer(gson.toJson(packet), Charset.forName("UTF-8"));
HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, status);
response.setHeader(HttpHeaders.Names.CONTENT_TYPE, APPLICATION_JSON);
response.setHeader(HttpHeaders.Names.CONTENT_LENGTH, buf.readableBytes());
response.setContent(buf);
ChannelFuture future = e.getChannel().write(response);
future.addListener(ChannelFutureListener.CLOSE);
}
use of org.jboss.netty.handler.codec.http.DefaultHttpResponse in project load-balancer by RestComm.
the class HttpRequestHandler method writeStatisticResponse.
private void writeStatisticResponse(MessageEvent e) {
GsonBuilder builder = new GsonBuilder();
Gson gson = builder.setPrettyPrinting().create();
JsonElement je = gson.toJsonTree(new StatisticObject(balancerRunner));
JsonObject jo = new JsonObject();
jo.add("Metrics", je);
String output = jo.toString();
HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
response.setHeader(HttpHeaders.Names.CONTENT_TYPE, APPLICATION_JSON);
ChannelBuffer buf = ChannelBuffers.copiedBuffer(output, Charset.forName("UTF-8"));
response.setContent(buf);
ChannelFuture future = e.getChannel().write(response);
future.addListener(ChannelFutureListener.CLOSE);
}
Aggregations