Search in sources :

Example 56 with ChannelFuture

use of org.jboss.netty.channel.ChannelFuture 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 57 with ChannelFuture

use of org.jboss.netty.channel.ChannelFuture 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 58 with ChannelFuture

use of org.jboss.netty.channel.ChannelFuture 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 59 with ChannelFuture

use of org.jboss.netty.channel.ChannelFuture 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 60 with ChannelFuture

use of org.jboss.netty.channel.ChannelFuture 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

ChannelFuture (org.jboss.netty.channel.ChannelFuture)122 DefaultHttpResponse (org.jboss.netty.handler.codec.http.DefaultHttpResponse)36 Channel (org.jboss.netty.channel.Channel)33 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)29 ChannelFutureListener (org.jboss.netty.channel.ChannelFutureListener)26 HttpResponse (org.jboss.netty.handler.codec.http.HttpResponse)25 InetSocketAddress (java.net.InetSocketAddress)22 HttpRequest (org.jboss.netty.handler.codec.http.HttpRequest)22 DefaultHttpRequest (org.jboss.netty.handler.codec.http.DefaultHttpRequest)19 SucceededChannelFuture (org.jboss.netty.channel.SucceededChannelFuture)13 Test (org.junit.Test)13 ClientBootstrap (org.jboss.netty.bootstrap.ClientBootstrap)12 InvocationOnMock (org.mockito.invocation.InvocationOnMock)11 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)10 NioClientSocketChannelFactory (org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory)8 Test (org.testng.annotations.Test)8 ConnectException (java.net.ConnectException)7 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6