Search in sources :

Example 71 with HttpResponse

use of org.jboss.netty.handler.codec.http.HttpResponse 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 72 with HttpResponse

use of org.jboss.netty.handler.codec.http.HttpResponse 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 73 with HttpResponse

use of org.jboss.netty.handler.codec.http.HttpResponse 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)

Example 74 with HttpResponse

use of org.jboss.netty.handler.codec.http.HttpResponse in project load-balancer by RestComm.

the class HttpRequestHandler method writeResponse.

private void writeResponse(MessageEvent e, HttpResponseStatus status, String responseString) {
    // Convert the response content to a ChannelBuffer.
    ChannelBuffer buf = ChannelBuffers.copiedBuffer(responseString, Charset.forName("UTF-8"));
    // Decide whether to close the connection or not.
    boolean close = HttpHeaders.Values.CLOSE.equalsIgnoreCase(request.getHeader(HttpHeaders.Names.CONNECTION)) || request.getProtocolVersion().equals(HttpVersion.HTTP_1_0) && !HttpHeaders.Values.KEEP_ALIVE.equalsIgnoreCase(request.getHeader(HttpHeaders.Names.CONNECTION));
    // Build the response object.
    HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, status);
    response.setContent(buf);
    response.setHeader(HttpHeaders.Names.CONTENT_TYPE, "text/plain; charset=UTF-8");
    if (!close) {
        // There's no need to add 'Content-Length' header
        // if this is the last response.
        response.setHeader(HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(buf.readableBytes()));
    }
    String cookieString = request.getHeader(HttpHeaders.Names.COOKIE);
    if (cookieString != null) {
        CookieDecoder cookieDecoder = new CookieDecoder();
        Set<Cookie> cookies = cookieDecoder.decode(cookieString);
        if (!cookies.isEmpty()) {
            // Reset the cookies if necessary.
            CookieEncoder cookieEncoder = new CookieEncoder(true);
            for (Cookie cookie : cookies) {
                cookieEncoder.addCookie(cookie);
            }
            response.addHeader(HttpHeaders.Names.SET_COOKIE, cookieEncoder.encode());
        }
    }
    // Write the response.
    ChannelFuture future = null;
    if (status.equals(HttpResponseStatus.SERVICE_UNAVAILABLE) || status.equals(HttpResponseStatus.LOCKED))
        future = e.getChannel().write(buf);
    else
        future = e.getChannel().write(response);
    // Close the connection after the write operation is done if necessary.
    if (close) {
        future.addListener(ChannelFutureListener.CLOSE);
    }
}
Also used : Cookie(org.jboss.netty.handler.codec.http.Cookie) ChannelFuture(org.jboss.netty.channel.ChannelFuture) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) CookieEncoder(org.jboss.netty.handler.codec.http.CookieEncoder) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) CookieDecoder(org.jboss.netty.handler.codec.http.CookieDecoder) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 75 with HttpResponse

use of org.jboss.netty.handler.codec.http.HttpResponse in project load-balancer by RestComm.

the class HttpRequestHandler method sendRedirectResponse.

private void sendRedirectResponse(MessageEvent e, HttpRequest request) {
    HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.FOUND);
    String host = request.getHeader("Host");
    host = host.replace(balancerRunner.balancerContext.lbConfig.getHttpConfiguration().getHttpPort().toString(), balancerRunner.balancerContext.lbConfig.getHttpConfiguration().getHttpsPort().toString());
    response.setHeader("Location", "https://" + host + request.getUri());
    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) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse)

Aggregations

HttpResponse (org.jboss.netty.handler.codec.http.HttpResponse)143 DefaultHttpResponse (org.jboss.netty.handler.codec.http.DefaultHttpResponse)111 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)61 HttpChunk (org.jboss.netty.handler.codec.http.HttpChunk)53 Test (org.testng.annotations.Test)51 DefaultHttpChunk (org.jboss.netty.handler.codec.http.DefaultHttpChunk)47 HttpRequest (org.jboss.netty.handler.codec.http.HttpRequest)37 Channel (org.jboss.netty.channel.Channel)34 DefaultHttpChunkTrailer (org.jboss.netty.handler.codec.http.DefaultHttpChunkTrailer)30 HttpChunkTrailer (org.jboss.netty.handler.codec.http.HttpChunkTrailer)28 BootstrapDatabaseTooOldException (com.linkedin.databus2.core.container.request.BootstrapDatabaseTooOldException)25 InetSocketAddress (java.net.InetSocketAddress)25 ChannelFuture (org.jboss.netty.channel.ChannelFuture)25 DefaultHttpRequest (org.jboss.netty.handler.codec.http.DefaultHttpRequest)25 Checkpoint (com.linkedin.databus.core.Checkpoint)24 SocketAddress (java.net.SocketAddress)23 ConditionCheck (com.linkedin.databus2.test.ConditionCheck)20 Test (org.junit.Test)20 Logger (org.apache.log4j.Logger)16 ChannelPipeline (org.jboss.netty.channel.ChannelPipeline)16