Search in sources :

Example 76 with HttpResponse

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

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

the class HttpResponseHandler method handleHttpResponse.

private void handleHttpResponse(ChannelHandlerContext ctx, final MessageEvent e) throws Exception {
    if (e.getMessage() instanceof HttpChunkTrailer) {
        HttpChunkTrailer chunk = (HttpChunkTrailer) e.getMessage();
        balancerRunner.balancerContext.httpBytesToClient.addAndGet(chunk.getContent().capacity());
        if (chunk.isLast())
            readingChunks = false;
        AdvancedChannel ac = HttpChannelAssociations.channels.get(new AdvancedChannel(e.getChannel()));
        Channel channel = null;
        if (ac != null)
            channel = ac.getChannel();
        if (channel != null) {
            if (logger.isDebugEnabled())
                logger.debug("Send chunked response from : " + e.getChannel().getRemoteAddress() + " to : " + channel.getRemoteAddress() + " capacity : " + chunk.getContent().capacity());
            channel.write(chunk);
        }
    } else if (!readingChunks || !(e.getMessage() instanceof DefaultHttpChunk)) {
        response = (HttpResponse) e.getMessage();
        int stsusCode = response.getStatus().getCode();
        if (stsusCode > 399 && stsusCode < 600) {
            AdvancedChannel ac = HttpChannelAssociations.channels.get(new AdvancedChannel(e.getChannel()));
            if (ac != null && ac.isCheckNeed()) {
                InetSocketAddress address = (InetSocketAddress) e.getChannel().getRemoteAddress();
                InvocationContext invocationContext = balancerRunner.getLatestInvocationContext();
                KeySip keySip = new KeySip(address.getHostString(), address.getPort(), false);
                Node currNode = invocationContext.sipNodeMap(false).get(keySip);
                if (currNode != null) {
                    currNode.setBad(true);
                    String instanseId = currNode.getProperties().get(Protocol.RESTCOMM_INSTANCE_ID);
                    if (instanseId != null)
                        invocationContext.httpNodeMap.get(new KeyHttp(currNode.getProperties().get(Protocol.RESTCOMM_INSTANCE_ID))).setBad(true);
                    logger.error("Error code [" + stsusCode + "] detected in HTTP response. From  node : " + currNode + ". This node will marked as bad.");
                    String currInstanceId = (String) currNode.getProperties().get("Restcomm-Instance-Id");
                    if (currInstanceId != null)
                        logger.warn("Node : " + invocationContext.httpNodeMap.remove(new KeyHttp(currInstanceId)) + " from httpNodeMap");
                    // invocationContext.badSipNodeMap(false).put(keySip, currNode);
                    invocationContext.balancerAlgorithm.nodeRemoved(currNode);
                }
            // TODO CHECK REQUEST AND REMOVE NODE
            }
        }
        updateStatistic(response);
        balancerRunner.balancerContext.httpBytesToClient.addAndGet(response.getContent().capacity());
        if (response.isChunked()) {
            readingChunks = true;
        }
        AdvancedChannel ac = HttpChannelAssociations.channels.get(new AdvancedChannel(e.getChannel()));
        Channel channel = null;
        if (ac != null)
            channel = ac.getChannel();
        if (channel != null) {
            if (logger.isDebugEnabled())
                logger.debug("Send response from : " + e.getChannel().getRemoteAddress() + " to : " + channel.getRemoteAddress() + " capacity : " + response.getContent().capacity());
            channel.write(response);
        }
        Set<String> headers = response.getHeaderNames();
        if (headers.contains("Sec-WebSocket-Protocol")) {
            if (response.getHeader("Sec-WebSocket-Protocol").equalsIgnoreCase("sip")) {
                if (logger.isDebugEnabled()) {
                    logger.debug("WebSocket response");
                }
                wsVersion = response.getHeader(Names.SEC_WEBSOCKET_VERSION);
                // Modify the Server pipeline
                ChannelPipeline p = channel.getPipeline();
                websocketModifyServerPipelineFactory = new WebsocketModifyServerPipelineFactory();
                websocketModifyServerPipelineFactory.upgradeServerPipelineFactory(p, wsVersion);
            }
        }
    } else {
        HttpChunk chunk = (HttpChunk) e.getMessage();
        balancerRunner.balancerContext.httpBytesToClient.addAndGet(chunk.getContent().capacity());
        if (chunk.isLast())
            readingChunks = false;
        AdvancedChannel ac = HttpChannelAssociations.channels.get(new AdvancedChannel(e.getChannel()));
        Channel channel = null;
        if (ac != null)
            channel = ac.getChannel();
        if (channel != null) {
            if (logger.isDebugEnabled())
                logger.debug("Send chunked response from : " + e.getChannel().getRemoteAddress() + " to : " + channel.getRemoteAddress() + " capacity : " + chunk.getContent().capacity());
            channel.write(chunk);
        }
    }
}
Also used : Set(java.util.Set) DefaultHttpChunk(org.jboss.netty.handler.codec.http.DefaultHttpChunk) InetSocketAddress(java.net.InetSocketAddress) Channel(org.jboss.netty.channel.Channel) Node(org.mobicents.tools.heartbeat.api.Node) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline) HttpChunkTrailer(org.jboss.netty.handler.codec.http.HttpChunkTrailer) KeySip(org.mobicents.tools.sip.balancer.KeySip) InvocationContext(org.mobicents.tools.sip.balancer.InvocationContext) KeyHttp(org.mobicents.tools.sip.balancer.KeyHttp) DefaultHttpChunk(org.jboss.netty.handler.codec.http.DefaultHttpChunk) HttpChunk(org.jboss.netty.handler.codec.http.HttpChunk)

Example 78 with HttpResponse

use of org.jboss.netty.handler.codec.http.HttpResponse in project hive by apache.

the class ResponseCookieHandler method handleResponse.

@Override
public ClientResponse<Intermediate> handleResponse(HttpResponse httpResponse) {
    try {
        final HttpHeaders headers = httpResponse.headers();
        manager.put(uri, Maps.asMap(headers.names(), input -> headers.getAll(input)));
    } catch (IOException e) {
        log.error("Error while processing Cookies from header", e);
    } finally {
        return delegate.handleResponse(httpResponse);
    }
}
Also used : HttpHeaders(org.jboss.netty.handler.codec.http.HttpHeaders) ClientResponse(com.metamx.http.client.response.ClientResponse) Logger(org.slf4j.Logger) HttpResponseHandler(com.metamx.http.client.response.HttpResponseHandler) CookieManager(java.net.CookieManager) HttpChunk(org.jboss.netty.handler.codec.http.HttpChunk) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) URI(java.net.URI) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) Maps(com.google.common.collect.Maps) HttpHeaders(org.jboss.netty.handler.codec.http.HttpHeaders) IOException(java.io.IOException)

Example 79 with HttpResponse

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

use of org.jboss.netty.handler.codec.http.HttpResponse in project vcell by virtualcell.

the class HttpResponseHandler method messageReceived.

@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
    if (clientTaskStatusSupport != null && clientTaskStatusSupport.isInterrupted()) {
        ctx.getChannel().close();
        return;
    }
    if (!readingChunks) {
        HttpResponse response = (HttpResponse) e.getMessage();
        if (!response.getHeaderNames().isEmpty()) {
            if (clientTaskStatusSupport != null) {
                clientTaskStatusSupport.setMessage("downloading " + NumberUtils.formatNumBytes(contentLength) + " from " + serverHost);
                clientTaskStatusSupport.setProgress(0);
            } else {
                System.out.println("downloading " + contentLength + " bytes from " + serverHost);
            }
        }
        if (response.isChunked()) {
            readingChunks = true;
        } else {
            ChannelBuffer content = response.getContent();
            if (content.readable()) {
                responseContent.append(content.toString(CharsetUtil.UTF_8));
                clientTaskStatusSupport.setProgress(100);
            }
        }
    } else {
        HttpChunk chunk = (HttpChunk) e.getMessage();
        if (chunk.isLast()) {
            readingChunks = false;
        } else {
            responseContent.append(chunk.getContent().toString(CharsetUtil.UTF_8));
            if (clientTaskStatusSupport != null) {
                clientTaskStatusSupport.setMessage("downloaded " + NumberUtils.formatNumBytes(responseContent.length()) + " from " + serverHost);
            } else {
                System.out.println("downloaded " + responseContent.length() + " of " + NumberUtils.formatNumBytes(responseContent.length()) + " from " + serverHost);
            }
        }
    }
}
Also used : HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) HttpChunk(org.jboss.netty.handler.codec.http.HttpChunk)

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