Search in sources :

Example 51 with DefaultHttpResponse

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);
    }
}
Also used : HandlerException(co.cask.cdap.common.HandlerException) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse)

Example 52 with DefaultHttpResponse

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);
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse)

Example 53 with DefaultHttpResponse

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);
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) JsonObject(com.google.gson.JsonObject) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 54 with DefaultHttpResponse

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);
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) HeartbeatResponsePacket(org.mobicents.tools.heartbeat.packets.HeartbeatResponsePacket) ShutdownResponsePacket(org.mobicents.tools.heartbeat.packets.ShutdownResponsePacket) StartResponsePacket(org.mobicents.tools.heartbeat.packets.StartResponsePacket) Packet(org.mobicents.tools.heartbeat.api.Packet) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) StartResponsePacket(org.mobicents.tools.heartbeat.packets.StartResponsePacket) HeartbeatResponsePacket(org.mobicents.tools.heartbeat.packets.HeartbeatResponsePacket) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) ShutdownResponsePacket(org.mobicents.tools.heartbeat.packets.ShutdownResponsePacket) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 55 with DefaultHttpResponse

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);
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) StatisticObject(org.mobicents.tools.sip.balancer.StatisticObject) GsonBuilder(com.google.gson.GsonBuilder) JsonElement(com.google.gson.JsonElement) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Aggregations

DefaultHttpResponse (org.jboss.netty.handler.codec.http.DefaultHttpResponse)117 HttpResponse (org.jboss.netty.handler.codec.http.HttpResponse)92 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)66 Test (org.testng.annotations.Test)54 DefaultHttpChunk (org.jboss.netty.handler.codec.http.DefaultHttpChunk)52 HttpChunk (org.jboss.netty.handler.codec.http.HttpChunk)51 DefaultHttpChunkTrailer (org.jboss.netty.handler.codec.http.DefaultHttpChunkTrailer)38 HttpChunkTrailer (org.jboss.netty.handler.codec.http.HttpChunkTrailer)34 ChannelFuture (org.jboss.netty.channel.ChannelFuture)31 Checkpoint (com.linkedin.databus.core.Checkpoint)25 BootstrapDatabaseTooOldException (com.linkedin.databus2.core.container.request.BootstrapDatabaseTooOldException)25 Channel (org.jboss.netty.channel.Channel)19 DefaultHttpRequest (org.jboss.netty.handler.codec.http.DefaultHttpRequest)19 ConditionCheck (com.linkedin.databus2.test.ConditionCheck)18 HttpRequest (org.jboss.netty.handler.codec.http.HttpRequest)18 InetSocketAddress (java.net.InetSocketAddress)17 SocketAddress (java.net.SocketAddress)16 Logger (org.apache.log4j.Logger)15 Test (org.junit.Test)11 SucceededChannelFuture (org.jboss.netty.channel.SucceededChannelFuture)10