Search in sources :

Example 66 with HttpChunk

use of org.jboss.netty.handler.codec.http.HttpChunk in project databus by linkedin.

the class TestResponseProcessors method testTargetSCNBootstrapTooOldException.

@Test
public void testTargetSCNBootstrapTooOldException() throws Exception {
    TestAbstractQueue queue = new TestAbstractQueue();
    TestConnectionStateMessage stateMsg = new TestConnectionStateMessage();
    DefaultHttpResponse httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
    TestRemoteExceptionHandler remoteExHandler = new TestRemoteExceptionHandler();
    remoteExHandler._exType = ExceptionType.BOOTSTRAP_TOO_OLD_EXCEPTION;
    Checkpoint cp = new Checkpoint();
    cp.setConsumptionMode(DbusClientMode.BOOTSTRAP_CATCHUP);
    BootstrapTargetScnHttpResponseProcessor processor = new BootstrapTargetScnHttpResponseProcessor(null, queue, stateMsg, cp, remoteExHandler, null);
    ChannelBuffer buf = getScnResponse();
    HttpChunk httpChunk = new DefaultHttpChunk(buf);
    HttpChunkTrailer httpChunkTrailer = new DefaultHttpChunkTrailer();
    processor.startResponse(httpResponse);
    processor.addChunk(httpChunk);
    processor.addTrailer(httpChunkTrailer);
    processor.finishResponse();
    Assert.assertEquals("Error Handled", false, processor._errorHandled);
    Assert.assertEquals("Processor Response State", AbstractHttpResponseProcessorDecorator.ResponseStatus.CHUNKS_FINISHED, processor._responseStatus);
    Assert.assertEquals("Actor Queue Size", 1, queue.getMessages().size());
    Assert.assertEquals("Expected ConnectionStateMessage", "TestConnectionStateMessage", queue.getMessages().get(0).getClass().getSimpleName());
    TestConnectionStateMessage gotMsg = (TestConnectionStateMessage) (queue.getMessages().get(0));
    Assert.assertEquals("Expected ConnectionStateMessage State", TestConnectionStateMessage.State.TARGETSCN_RESPONSE_ERROR, gotMsg._state);
    Assert.assertEquals("No response", true, (null == stateMsg._cp));
}
Also used : Checkpoint(com.linkedin.databus.core.Checkpoint) DefaultHttpChunkTrailer(org.jboss.netty.handler.codec.http.DefaultHttpChunkTrailer) HttpChunkTrailer(org.jboss.netty.handler.codec.http.HttpChunkTrailer) DefaultHttpChunk(org.jboss.netty.handler.codec.http.DefaultHttpChunk) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) DefaultHttpChunkTrailer(org.jboss.netty.handler.codec.http.DefaultHttpChunkTrailer) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) DefaultHttpChunk(org.jboss.netty.handler.codec.http.DefaultHttpChunk) HttpChunk(org.jboss.netty.handler.codec.http.HttpChunk) Test(org.testng.annotations.Test)

Example 67 with HttpChunk

use of org.jboss.netty.handler.codec.http.HttpChunk in project databus by linkedin.

the class TestResponseProcessors method testStartSCNExceptionAfterFinish.

@Test
public void testStartSCNExceptionAfterFinish() throws Exception {
    TestAbstractQueue queue = new TestAbstractQueue();
    TestConnectionStateMessage stateMsg = new TestConnectionStateMessage();
    HttpResponse httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
    TestRemoteExceptionHandler remoteExHandler = new TestRemoteExceptionHandler();
    Checkpoint cp = new Checkpoint();
    cp.setConsumptionMode(DbusClientMode.BOOTSTRAP_SNAPSHOT);
    BootstrapStartScnHttpResponseProcessor processor = new BootstrapStartScnHttpResponseProcessor(null, queue, stateMsg, cp, remoteExHandler, null);
    ChannelBuffer buf = getScnResponse();
    HttpChunk httpChunk = new DefaultHttpChunk(buf);
    HttpChunkTrailer httpChunkTrailer = new DefaultHttpChunkTrailer();
    processor.startResponse(httpResponse);
    processor.addChunk(httpChunk);
    processor.addTrailer(httpChunkTrailer);
    processor.finishResponse();
    processor.channelException(new Exception("dummy exception"));
    Assert.assertEquals("Error Handled", true, processor._errorHandled);
    Assert.assertEquals("Processor Response State", AbstractHttpResponseProcessorDecorator.ResponseStatus.CHUNKS_EXCEPTION, processor._responseStatus);
    Assert.assertEquals("Actor Queue Size", 1, queue.getMessages().size());
    Assert.assertEquals("Expected ConnectionStateMessage", "TestConnectionStateMessage", queue.getMessages().get(0).getClass().getSimpleName());
    TestConnectionStateMessage gotMsg = (TestConnectionStateMessage) (queue.getMessages().get(0));
    Assert.assertEquals("Expected ConnectionStateMessage State", TestConnectionStateMessage.State.STARTSCN_RESPONSE_SUCCESS, gotMsg._state);
    Assert.assertEquals("StartSCN Response Id Check", new Long(5678912), cp.getBootstrapStartScn());
}
Also used : DefaultHttpChunk(org.jboss.netty.handler.codec.http.DefaultHttpChunk) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) BootstrapDatabaseTooOldException(com.linkedin.databus2.core.container.request.BootstrapDatabaseTooOldException) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) Checkpoint(com.linkedin.databus.core.Checkpoint) DefaultHttpChunkTrailer(org.jboss.netty.handler.codec.http.DefaultHttpChunkTrailer) HttpChunkTrailer(org.jboss.netty.handler.codec.http.HttpChunkTrailer) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) DefaultHttpChunkTrailer(org.jboss.netty.handler.codec.http.DefaultHttpChunkTrailer) DefaultHttpChunk(org.jboss.netty.handler.codec.http.DefaultHttpChunk) HttpChunk(org.jboss.netty.handler.codec.http.HttpChunk) Test(org.testng.annotations.Test)

Example 68 with HttpChunk

use of org.jboss.netty.handler.codec.http.HttpChunk in project voldemort by voldemort.

the class CoordinatorAdminRequestHandler method messageReceived.

@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent messageEvent) throws Exception {
    if (!readingChunks) {
        HttpRequest request = this.request = (HttpRequest) messageEvent.getMessage();
        String requestURI = this.request.getUri();
        if (logger.isDebugEnabled()) {
            logger.debug("Admin Request URI: " + requestURI);
        }
        if (request.isChunked()) {
            readingChunks = true;
        } else {
            String[] requestUriSegments = requestURI.split("/");
            HttpResponse response;
            if (requestUriSegments.length > 0 && requestUriSegments[1].equals(STORE_OPS_NAMESPACE)) {
                List<String> storeList = Lists.newArrayList();
                if (requestUriSegments.length > 2) {
                    String csvStoreList = requestUriSegments[2];
                    String[] storeArray = csvStoreList.split(",");
                    storeList = Lists.newArrayList(storeArray);
                }
                HttpMethod httpMethod = request.getMethod();
                if (httpMethod.equals(HttpMethod.GET)) {
                    response = handleGet(storeList);
                } else if (httpMethod.equals(HttpMethod.POST)) {
                    Map<String, Properties> configsToPut = Maps.newHashMap();
                    ChannelBuffer content = this.request.getContent();
                    if (content != null) {
                        byte[] postBody = new byte[content.capacity()];
                        content.readBytes(postBody);
                        configsToPut = ClientConfigUtil.readMultipleClientConfigAvro(new String(postBody));
                    }
                    response = handlePut(configsToPut);
                } else if (httpMethod.equals(HttpMethod.DELETE)) {
                    if (storeList.size() == 0) {
                        response = handleBadRequest("Cannot delete config for all stores. Please specify at least one store name.");
                    } else {
                        response = handleDelete(storeList);
                    }
                } else {
                    // Bad HTTP method
                    response = handleBadRequest("Unsupported HTTP method. Only GET, POST and DELETE are supported.");
                }
            } else {
                // Bad namespace
                response = handleBadRequest("Unsupported namespace. Only /" + STORE_OPS_NAMESPACE + "/ is supported.");
            }
            messageEvent.getChannel().write(response);
        }
    } else {
        HttpChunk chunk = (HttpChunk) messageEvent.getMessage();
        if (chunk.isLast()) {
            readingChunks = false;
        }
    }
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) Map(java.util.Map) HttpMethod(org.jboss.netty.handler.codec.http.HttpMethod) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) HttpChunk(org.jboss.netty.handler.codec.http.HttpChunk)

Example 69 with HttpChunk

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

the class HttpServerRequestHandler method writeResponse.

@SuppressWarnings("deprecation")
private void writeResponse(MessageEvent e, HttpResponseStatus status, String responseString) {
    // Convert the response content to a ChannelBuffer.
    if (chunk)
        for (int i = 0; i < 1000; i++) responseString += "HOW MUCH IS THE FISH";
    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);
    if (!chunk)
        response.setContent(buf);
    response.setHeader(HttpHeaders.Names.CONTENT_TYPE, "text/plain; charset=UTF-8");
    if (chunk)
        response.setHeader(HttpHeaders.Names.TRANSFER_ENCODING, "chunked");
    if (!chunk)
        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 = e.getChannel().write(response);
    if (chunk) {
        while (buf.readableBytes() > 0) {
            int maxBytes = 1000;
            if (buf.readableBytes() < 1000)
                maxBytes = buf.readableBytes();
            HttpChunk currChunk = new DefaultHttpChunk(buf.readBytes(maxBytes));
            future = e.getChannel().write(currChunk);
        }
        HttpChunk currChunk = new DefaultHttpChunk(buf);
        future = e.getChannel().write(currChunk);
    }
    // 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) DefaultHttpChunk(org.jboss.netty.handler.codec.http.DefaultHttpChunk) CookieEncoder(org.jboss.netty.handler.codec.http.CookieEncoder) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) CookieDecoder(org.jboss.netty.handler.codec.http.CookieDecoder) DefaultHttpChunk(org.jboss.netty.handler.codec.http.DefaultHttpChunk) HttpChunk(org.jboss.netty.handler.codec.http.HttpChunk)

Example 70 with HttpChunk

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

the class HttpServerRequestHandler method handle.

public void handle(ChannelHandlerContext ctx, MessageEvent e) throws IOException {
    if (!readingChunks) {
        request = (HttpRequest) e.getMessage();
        requests.add(request.getUri());
        if (request.isChunked())
            readingChunks = true;
        else {
            requestCount.incrementAndGet();
            try {
                String response = createResponseFromQueryParams(new URI(request.getUri()));
                if (!badServer)
                    writeResponse(e, HttpResponseStatus.OK, response);
                else
                    writeResponse(e, HttpResponseStatus.BAD_REQUEST, response);
            } catch (Exception ex) {
            }
        }
    } else {
        HttpChunk chunk = (HttpChunk) e.getMessage();
        if (chunk.isLast())
            readingChunks = false;
    }
}
Also used : URI(java.net.URI) IOException(java.io.IOException) DefaultHttpChunk(org.jboss.netty.handler.codec.http.DefaultHttpChunk) HttpChunk(org.jboss.netty.handler.codec.http.HttpChunk)

Aggregations

HttpChunk (org.jboss.netty.handler.codec.http.HttpChunk)70 DefaultHttpChunk (org.jboss.netty.handler.codec.http.DefaultHttpChunk)56 DefaultHttpResponse (org.jboss.netty.handler.codec.http.DefaultHttpResponse)52 HttpResponse (org.jboss.netty.handler.codec.http.HttpResponse)52 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)48 Test (org.testng.annotations.Test)46 DefaultHttpChunkTrailer (org.jboss.netty.handler.codec.http.DefaultHttpChunkTrailer)38 HttpChunkTrailer (org.jboss.netty.handler.codec.http.HttpChunkTrailer)38 BootstrapDatabaseTooOldException (com.linkedin.databus2.core.container.request.BootstrapDatabaseTooOldException)25 Checkpoint (com.linkedin.databus.core.Checkpoint)22 InetSocketAddress (java.net.InetSocketAddress)16 Channel (org.jboss.netty.channel.Channel)16 ConditionCheck (com.linkedin.databus2.test.ConditionCheck)15 SocketAddress (java.net.SocketAddress)13 Logger (org.apache.log4j.Logger)12 HttpRequest (org.jboss.netty.handler.codec.http.HttpRequest)12 ChannelPipeline (org.jboss.netty.channel.ChannelPipeline)9 RegisterResponseEntry (com.linkedin.databus2.core.container.request.RegisterResponseEntry)8 SimpleObjectCaptureHandler (com.linkedin.databus2.test.container.SimpleObjectCaptureHandler)7 DatabusRelayConnectionStateMessage (com.linkedin.databus.client.DatabusRelayConnectionStateMessage)6