Search in sources :

Example 56 with DefaultHttpResponse

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

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

Example 58 with DefaultHttpResponse

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

the class HttpRequestHandler method writeInfoResponse.

private void writeInfoResponse(MessageEvent e) {
    GsonBuilder builder = new GsonBuilder();
    Gson gson = builder.setPrettyPrinting().create();
    JsonElement je = gson.toJsonTree(new NodesInfoObject(balancerRunner));
    JsonObject jo = new JsonObject();
    jo.add("Nodes info", je);
    String output = jo.toString();
    HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
    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) NodesInfoObject(org.mobicents.tools.sip.balancer.NodesInfoObject) 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 DefaultHttpResponse

use of org.jboss.netty.handler.codec.http.DefaultHttpResponse in project traccar by tananaev.

the class Mta6ProtocolDecoder method sendContinue.

private void sendContinue(Channel channel) {
    HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.CONTINUE);
    channel.write(response);
}
Also used : 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 60 with DefaultHttpResponse

use of org.jboss.netty.handler.codec.http.DefaultHttpResponse in project camel by apache.

the class HttpServerMultiplexChannelHandler method messageReceived.

@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent messageEvent) throws Exception {
    // store request, as this channel handler is created per pipeline
    HttpRequest request = (HttpRequest) messageEvent.getMessage();
    LOG.debug("Message received: {}", request);
    HttpServerChannelHandler handler = getHandler(request);
    if (handler != null) {
        // store handler as attachment
        ctx.setAttachment(handler);
        handler.messageReceived(ctx, messageEvent);
    } else {
        // this resource is not found, so send empty response back
        HttpResponse response = new DefaultHttpResponse(HTTP_1_1, NOT_FOUND);
        response.headers().set(Exchange.CONTENT_TYPE, "text/plain");
        response.headers().set(Exchange.CONTENT_LENGTH, 0);
        response.setContent(ChannelBuffers.copiedBuffer(new byte[] {}));
        messageEvent.getChannel().write(response).syncUninterruptibly();
        // close the channel after send error message
        messageEvent.getChannel().close();
    }
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) 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

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