Search in sources :

Example 81 with HttpResponse

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

Example 82 with HttpResponse

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

the class NettyHttpAccessHttpRequestAndResponseBeanTest method myTransformer.

/**
     * We can use both a netty http request and response type for transformation
     */
public static HttpResponse myTransformer(HttpRequest request) {
    String in = request.getContent().toString(Charset.forName("UTF-8"));
    String reply = "Bye " + in;
    HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
    response.setContent(ChannelBuffers.copiedBuffer(reply.getBytes()));
    response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, reply.length());
    return response;
}
Also used : DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse)

Example 83 with HttpResponse

use of org.jboss.netty.handler.codec.http.HttpResponse in project cdap by caskdata.

the class HttpStatusRequestHandler method messageReceived.

@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent event) throws Exception {
    Object msg = event.getMessage();
    if (msg instanceof HttpRequest) {
        HttpRequest request = (HttpRequest) msg;
        // be served by the router itself without it talking to any downstream services
        if (request.getUri().equals(Constants.EndPoints.STATUS)) {
            String statusString = Constants.Monitor.STATUS_OK;
            ChannelBuffer responseContent = ChannelBuffers.wrappedBuffer(Charsets.UTF_8.encode(statusString));
            HttpResponse httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
            httpResponse.setHeader(HttpHeaders.Names.CONTENT_TYPE, "text/plain");
            httpResponse.setHeader(HttpHeaders.Names.CONTENT_LENGTH, responseContent.readableBytes());
            httpResponse.setContent(responseContent);
            ChannelFuture writeFuture = Channels.future(event.getChannel());
            Channels.write(ctx, writeFuture, httpResponse);
            writeFuture.addListener(ChannelFutureListener.CLOSE);
            return;
        }
    }
    super.messageReceived(ctx, event);
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) 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) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 84 with HttpResponse

use of org.jboss.netty.handler.codec.http.HttpResponse in project cdap by caskdata.

the class IdleEventProcessor method messageReceived.

@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
    Object message = e.getMessage();
    if (message instanceof HttpResponse) {
        HttpResponse response = (HttpResponse) message;
        if (!response.isChunked()) {
            requestInProgress = false;
        }
    } else if (message instanceof HttpChunk) {
        HttpChunk chunk = (HttpChunk) message;
        if (chunk.isLast()) {
            requestInProgress = false;
        }
    }
    ctx.sendUpstream(e);
}
Also used : HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) HttpChunk(org.jboss.netty.handler.codec.http.HttpChunk)

Example 85 with HttpResponse

use of org.jboss.netty.handler.codec.http.HttpResponse in project NabAlive by jcheype.

the class Response method write.

public void write(ChannelBuffer buffer) {
    HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
    response.setHeader(HttpHeaders.Names.SERVER, "nuvoos server");
    setContentLength(response, buffer.readableBytes());
    response.setContent(buffer);
    if (isKeepAlive(request)) {
        response.setHeader(HttpHeaders.Names.CONNECTION, "Keep-Alive");
    }
    final ChannelFuture future = ctx.getChannel().write(response);
    if (!isKeepAlive(request)) {
        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