Example usage for io.netty.handler.codec.http HttpHeaders getContentLength

List of usage examples for io.netty.handler.codec.http HttpHeaders getContentLength

Introduction

In this page you can find the example usage for io.netty.handler.codec.http HttpHeaders getContentLength.

Prototype

@Deprecated
public static long getContentLength(HttpMessage message) 

Source Link

Usage

From source file:com.bloom.zerofs.rest.NettyResponseChannel.java

License:Open Source License

@Override
public Future<Long> write(ByteBuffer src, Callback<Long> callback) {
    long writeProcessingStartTime = System.currentTimeMillis();
    if (!responseMetadataWriteInitiated.get()) {
        maybeWriteResponseMetadata(responseMetadata, new ResponseMetadataWriteListener());
    }//ww  w  .jav  a2 s  . c  o m
    Chunk chunk = new Chunk(src, callback);
    chunksToWrite.add(chunk);
    if (!isOpen()) {
        // the isOpen() check is not before addition to the queue because chunks need to be acknowledged in the order
        // they were received. If we don't add it to the queue and clean up, chunks may be acknowledged out of order.
        logger.debug("Scheduling a chunk cleanup on channel {} because response channel is closed",
                ctx.channel());
        writeFuture.addListener(new CleanupCallback(new ClosedChannelException()));
    } else if (finalResponseMetadata instanceof FullHttpResponse) {
        logger.debug("Scheduling a chunk cleanup on channel {} because Content-Length is 0", ctx.channel());
        Exception exception = null;
        // this is only allowed to be a 0 sized buffer.
        if (src.remaining() > 0) {
            exception = new IllegalStateException(
                    "Provided non zero size content after setting Content-Length to 0");
            if (!writeFuture.isDone()) {
                writeFuture.setFailure(exception);
            }
        }
        writeFuture.addListener(new CleanupCallback(exception));
    } else if (HttpHeaders.isContentLengthSet(finalResponseMetadata)
            && totalBytesReceived.get() > HttpHeaders.getContentLength(finalResponseMetadata)) {
        Exception exception = new IllegalStateException("Size of provided content [" + totalBytesReceived.get()
                + "] is greater than Content-Length set [" + HttpHeaders.getContentLength(finalResponseMetadata)
                + "]");
        if (!writeFuture.isDone()) {
            writeFuture.setFailure(exception);
        }
        writeFuture.addListener(new CleanupCallback(exception));
    } else {
        chunkedWriteHandler.resumeTransfer();
    }

    long writeProcessingTime = System.currentTimeMillis() - writeProcessingStartTime;
    nettyMetrics.writeProcessingTimeInMs.update(writeProcessingTime);
    if (request != null) {
        request.getMetricsTracker().nioMetricsTracker.addToResponseProcessingTime(writeProcessingTime);
    }
    return chunk.future;
}

From source file:com.bloom.zerofs.rest.NettyResponseChannel.java

License:Open Source License

/**
 * Writes response metadata to the channel if not already written previously and channel is active.
 * @param responseMetadata the {@link HttpResponse} that needs to be written.
 * @param listener the {@link GenericFutureListener} that needs to be attached to the write.
 * @return {@code true} if response metadata was written to the channel in this call. {@code false} otherwise.
 *///from  w  ww.ja  v a2  s . com
private boolean maybeWriteResponseMetadata(HttpResponse responseMetadata,
        GenericFutureListener<ChannelFuture> listener) {
    long writeProcessingStartTime = System.currentTimeMillis();
    boolean writtenThisTime = false;
    if (responseMetadataWriteInitiated.compareAndSet(false, true) && ctx.channel().isActive()) {
        // we do some manipulation here for chunking. According to the HTTP spec, we can have either a Content-Length
        // or Transfer-Encoding:chunked, never both. So we check for Content-Length - if it is not there, we add
        // Transfer-Encoding:chunked. Note that sending HttpContent chunks data anyway - we are just explicitly specifying
        // this in the header.
        if (!HttpHeaders.isContentLengthSet(responseMetadata)) {
            // This makes sure that we don't stomp on any existing transfer-encoding.
            HttpHeaders.setTransferEncodingChunked(responseMetadata);
        } else if (HttpHeaders.getContentLength(responseMetadata) == 0
                && !(responseMetadata instanceof FullHttpResponse)) {
            // if the Content-Length is 0, we can send a FullHttpResponse since there is no content expected.
            FullHttpResponse fullHttpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
                    responseMetadata.getStatus());
            fullHttpResponse.headers().set(responseMetadata.headers());
            responseMetadata = fullHttpResponse;
        }
        logger.trace("Sending response with status {} on channel {}", responseMetadata.getStatus(),
                ctx.channel());
        finalResponseMetadata = responseMetadata;
        ChannelPromise writePromise = ctx.newPromise().addListener(listener);
        ctx.writeAndFlush(responseMetadata, writePromise);
        writtenThisTime = true;
        long writeProcessingTime = System.currentTimeMillis() - writeProcessingStartTime;
        nettyMetrics.responseMetadataProcessingTimeInMs.update(writeProcessingTime);
    }
    return writtenThisTime;
}

From source file:com.github.ambry.admin.AdminIntegrationTest.java

License:Open Source License

/**
 * Posts a blob with the given {@code headers} and {@code content}.
 * @param headers the headers required.//from   ww  w .j a v  a  2  s . c  o m
 * @param content the content of the blob.
 * @return the blob ID of the blob.
 * @throws ExecutionException
 * @throws InterruptedException
 */
private String postBlobAndVerify(HttpHeaders headers, ByteBuffer content)
        throws ExecutionException, InterruptedException {
    FullHttpRequest httpRequest = buildRequest(HttpMethod.POST, "/", headers, content);
    Queue<HttpObject> responseParts = nettyClient.sendRequest(httpRequest, null, null).get();
    HttpResponse response = (HttpResponse) responseParts.poll();
    discardContent(responseParts, 1);
    assertEquals("Unexpected response status", HttpResponseStatus.CREATED, response.getStatus());
    assertTrue("No Date header", HttpHeaders.getDateHeader(response, HttpHeaders.Names.DATE, null) != null);
    assertTrue("No " + RestUtils.Headers.CREATION_TIME,
            HttpHeaders.getHeader(response, RestUtils.Headers.CREATION_TIME, null) != null);
    assertEquals("Content-Length is not 0", 0, HttpHeaders.getContentLength(response));
    String blobId = HttpHeaders.getHeader(response, HttpHeaders.Names.LOCATION, null);

    if (blobId == null) {
        fail("postBlobAndVerify did not return a blob ID");
    }
    return blobId;
}

From source file:com.github.ambry.admin.AdminIntegrationTest.java

License:Open Source License

/**
 * Gets the headers of the blob with blob ID {@code blobId} and verifies them against what is expected.
 * @param blobId the blob ID of the blob to HEAD.
 * @param expectedHeaders the expected headers in the response.
 * @throws ExecutionException//from  w w  w. ja  va  2s . c  o m
 * @throws InterruptedException
 */
private void getHeadAndVerify(String blobId, HttpHeaders expectedHeaders)
        throws ExecutionException, InterruptedException {
    FullHttpRequest httpRequest = buildRequest(HttpMethod.HEAD, blobId, null, null);
    Queue<HttpObject> responseParts = nettyClient.sendRequest(httpRequest, null, null).get();
    HttpResponse response = (HttpResponse) responseParts.poll();
    discardContent(responseParts, 1);
    assertEquals("Unexpected response status", HttpResponseStatus.OK, response.getStatus());
    checkCommonGetHeadHeaders(response.headers(), expectedHeaders);
    assertEquals("Content-Length does not match blob size",
            Long.parseLong(expectedHeaders.get(RestUtils.Headers.BLOB_SIZE)),
            HttpHeaders.getContentLength(response));
    assertEquals("Blob size does not match", expectedHeaders.get(RestUtils.Headers.BLOB_SIZE),
            HttpHeaders.getHeader(response, RestUtils.Headers.BLOB_SIZE));
    assertEquals(RestUtils.Headers.SERVICE_ID + " does not match",
            expectedHeaders.get(RestUtils.Headers.SERVICE_ID),
            HttpHeaders.getHeader(response, RestUtils.Headers.SERVICE_ID));
    assertEquals(RestUtils.Headers.PRIVATE + " does not match", expectedHeaders.get(RestUtils.Headers.PRIVATE),
            HttpHeaders.getHeader(response, RestUtils.Headers.PRIVATE));
    assertEquals(RestUtils.Headers.AMBRY_CONTENT_TYPE + " does not match",
            expectedHeaders.get(RestUtils.Headers.AMBRY_CONTENT_TYPE),
            HttpHeaders.getHeader(response, RestUtils.Headers.AMBRY_CONTENT_TYPE));
    assertTrue("No " + RestUtils.Headers.CREATION_TIME,
            HttpHeaders.getHeader(response, RestUtils.Headers.CREATION_TIME, null) != null);
    if (Long.parseLong(expectedHeaders.get(RestUtils.Headers.TTL)) != Utils.Infinite_Time) {
        assertEquals(RestUtils.Headers.TTL + " does not match", expectedHeaders.get(RestUtils.Headers.TTL),
                HttpHeaders.getHeader(response, RestUtils.Headers.TTL));
    }
    if (expectedHeaders.contains(RestUtils.Headers.OWNER_ID)) {
        assertEquals(RestUtils.Headers.OWNER_ID + " does not match",
                expectedHeaders.get(RestUtils.Headers.OWNER_ID),
                HttpHeaders.getHeader(response, RestUtils.Headers.OWNER_ID));
    }
}

From source file:com.github.ambry.frontend.FrontendIntegrationTest.java

License:Open Source License

/**
 * Posts a blob with the given {@code headers} and {@code content}.
 * @param headers the headers required.//w  w  w  . j ava2s  .com
 * @param content the content of the blob.
 * @return the blob ID of the blob.
 * @throws ExecutionException
 * @throws InterruptedException
 */
private String postBlobAndVerify(HttpHeaders headers, ByteBuffer content)
        throws ExecutionException, InterruptedException {
    FullHttpRequest httpRequest = buildRequest(HttpMethod.POST, "/", headers, content);
    Queue<HttpObject> responseParts = nettyClient.sendRequest(httpRequest, null, null).get();
    HttpResponse response = (HttpResponse) responseParts.poll();
    assertEquals("Unexpected response status", HttpResponseStatus.CREATED, response.getStatus());
    assertTrue("No Date header", HttpHeaders.getDateHeader(response, HttpHeaders.Names.DATE, null) != null);
    assertTrue("No " + RestUtils.Headers.CREATION_TIME,
            HttpHeaders.getHeader(response, RestUtils.Headers.CREATION_TIME, null) != null);
    assertEquals("Content-Length is not 0", 0, HttpHeaders.getContentLength(response));
    String blobId = HttpHeaders.getHeader(response, HttpHeaders.Names.LOCATION, null);

    if (blobId == null) {
        fail("postBlobAndVerify did not return a blob ID");
    }
    discardContent(responseParts, 1);
    assertTrue("Channel should be active", HttpHeaders.isKeepAlive(response));
    return blobId;
}

From source file:com.github.ambry.frontend.FrontendIntegrationTest.java

License:Open Source License

/**
 * Gets the headers of the blob with blob ID {@code blobId} and verifies them against what is expected.
 * @param blobId the blob ID of the blob to HEAD.
 * @param expectedHeaders the expected headers in the response.
 * @throws ExecutionException/*  www .ja va2 s .  com*/
 * @throws InterruptedException
 */
private void getHeadAndVerify(String blobId, HttpHeaders expectedHeaders)
        throws ExecutionException, InterruptedException {
    FullHttpRequest httpRequest = buildRequest(HttpMethod.HEAD, blobId, null, null);
    Queue<HttpObject> responseParts = nettyClient.sendRequest(httpRequest, null, null).get();
    HttpResponse response = (HttpResponse) responseParts.poll();
    assertEquals("Unexpected response status", HttpResponseStatus.OK, response.getStatus());
    checkCommonGetHeadHeaders(response.headers());
    assertEquals(RestUtils.Headers.CONTENT_LENGTH + " does not match " + RestUtils.Headers.BLOB_SIZE,
            Long.parseLong(expectedHeaders.get(RestUtils.Headers.BLOB_SIZE)),
            HttpHeaders.getContentLength(response));
    assertEquals(RestUtils.Headers.CONTENT_TYPE + " does not match " + RestUtils.Headers.AMBRY_CONTENT_TYPE,
            expectedHeaders.get(RestUtils.Headers.AMBRY_CONTENT_TYPE),
            HttpHeaders.getHeader(response, HttpHeaders.Names.CONTENT_TYPE));
    verifyBlobProperties(expectedHeaders, response);
    discardContent(responseParts, 1);
    assertTrue("Channel should be active", HttpHeaders.isKeepAlive(response));
}

From source file:com.github.ambry.frontend.FrontendIntegrationTest.java

License:Open Source License

/**
 * Verifies User metadata headers from output, to that sent in during input
 * @param expectedHeaders the expected headers in the response.
 * @param response the {@link HttpResponse} which contains the headers of the response.
 * @param usermetadata if non-null, this is expected to come as the body.
 * @param content the content accompanying the response.
 *//*from w  w w  . ja  v  a 2  s  .  c o  m*/
private void verifyUserMetadata(HttpHeaders expectedHeaders, HttpResponse response, byte[] usermetadata,
        Queue<HttpObject> content) {
    if (usermetadata == null) {
        assertEquals("Content-Length is not 0", 0, HttpHeaders.getContentLength(response));
        for (Map.Entry<String, String> header : expectedHeaders) {
            String key = header.getKey();
            if (key.startsWith(RestUtils.Headers.USER_META_DATA_HEADER_PREFIX)) {
                assertEquals("Value for " + key + " does not match in user metadata", header.getValue(),
                        HttpHeaders.getHeader(response, key));
            }
        }
        for (Map.Entry<String, String> header : response.headers()) {
            String key = header.getKey();
            if (key.startsWith(RestUtils.Headers.USER_META_DATA_HEADER_PREFIX)) {
                assertTrue("Key " + key + " does not exist in expected headers", expectedHeaders.contains(key));
            }
        }
        discardContent(content, 1);
    } else {
        assertEquals("Content-Length is not as expected", usermetadata.length,
                HttpHeaders.getContentLength(response));
        byte[] receivedMetadata = getContent(response, content).array();
        assertArrayEquals("User metadata does not match original", usermetadata, receivedMetadata);
    }
}

From source file:com.github.ambry.frontend.FrontendIntegrationTest.java

License:Open Source License

/**
 * Posts a blob with the given {@code headers} and {@code content}.
 * @param headers the headers required.//from w ww .  ja  v  a  2 s .c om
 * @param content the content of the blob.
 * @param usermetadata the {@link ByteBuffer} that represents user metadata
 * @return the blob ID of the blob.
 * @throws Exception
 */
private String multipartPostBlobAndVerify(HttpHeaders headers, ByteBuffer content, ByteBuffer usermetadata)
        throws Exception {
    HttpRequest httpRequest = RestTestUtils.createRequest(HttpMethod.POST, "/", headers);
    HttpPostRequestEncoder encoder = createEncoder(httpRequest, content, usermetadata);
    Queue<HttpObject> responseParts = nettyClient.sendRequest(encoder.finalizeRequest(), encoder, null).get();
    HttpResponse response = (HttpResponse) responseParts.poll();
    assertEquals("Unexpected response status", HttpResponseStatus.CREATED, response.getStatus());
    assertTrue("No Date header", HttpHeaders.getDateHeader(response, HttpHeaders.Names.DATE, null) != null);
    assertTrue("No " + RestUtils.Headers.CREATION_TIME,
            HttpHeaders.getHeader(response, RestUtils.Headers.CREATION_TIME, null) != null);
    assertEquals("Content-Length is not 0", 0, HttpHeaders.getContentLength(response));
    String blobId = HttpHeaders.getHeader(response, HttpHeaders.Names.LOCATION, null);

    if (blobId == null) {
        fail("postBlobAndVerify did not return a blob ID");
    }
    discardContent(responseParts, 1);
    assertTrue("Channel should be active", HttpHeaders.isKeepAlive(response));
    return blobId;
}

From source file:com.titilink.camel.rest.client.HttpClientHandler.java

License:LGPL

@Override
public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
    //?/*from  w  w  w. j  a  va  2s.  c o m*/
    boolean isReadOverMsg = false;
    if (msg instanceof HttpResponse) {
        HttpResponse response = (HttpResponse) msg;
        if (HttpHeaders.isContentLengthSet(response)) {
            int pkLength = (int) HttpHeaders.getContentLength(response);
            if (pkLength > maxPacketSize) {
                LOGGER.error(
                        "http response packet size exceeds the system limit threshold, realSize={},maxPacketSize={}.",
                        new Object[] { pkLength, maxPacketSize });
                isOverSizePackage = true;
            } else {
                isOverSizePackage = false;
            }
        }
        LOGGER.debug("status: {} , version: {}", response.getStatus(), response.getProtocolVersion());
        if (firstHeader) {
            // ???500??
            Status status = isOverSizePackage ? Status.SERVER_ERROR_INTERNAL
                    : Status.valueOf(((HttpResponse) msg).getStatus().code());
            result = new RestResponse(status);
            if (isOverSizePackage) {
                //??
                result.setText("{\"code\":\"" + HTTP_DOWN_PACKAGE_SIZE_OVER
                        + "\",\"message\":\"http response packet size exceeds the system limit threshold\"}");
            }
            firstHeader = false;
        }
        if (!response.headers().isEmpty()) {
            for (String name : response.headers().names()) {
                for (String value : response.headers().getAll(name)) {
                    result.addHeader(name, value);
                }
            }
        }
    }
    if (msg instanceof HttpContent) {
        HttpContent content = (HttpContent) msg;
        //????contentBufBuilder?
        if (!isOverSizePackage) {
            String sContent = content.content().toString(CharsetUtil.UTF_8);
            contentBufBuilder.append(sContent);
            if (content instanceof LastHttpContent) {
                if (null != result && HttpResponseStatus.NO_CONTENT.code() != result.getStatus().getCode()) {
                    result.setText(contentBufBuilder.toString());
                }
                isReadOverMsg = true;
            }
        }
    }
    /**
     * ?  ??result
     */
    if ((!isReadOverMsg && isOverSizePackage) || (isReadOverMsg && !isOverSizePackage)) {
        if (!queue.offer(result)) {
            LOGGER.error("channelRead0. add result to queue failed");
        }
        reset();
    }
}

From source file:com.xx_dev.speed_test.SpeedTestHttpClientHandler.java

License:Apache License

@Override
public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
    long currentTime = System.currentTimeMillis();
    boolean isPrintLog = false;

    if (msg instanceof HttpResponse) {
        HttpResponse response = (HttpResponse) msg;
        totalSize = HttpHeaders.getContentLength(response);
        lastReadTime = currentTime;/*from www.j a v  a 2 s.  c o  m*/
    }

    if (msg instanceof HttpContent) {
        long currentReadSize = ((HttpContent) msg).content().readableBytes();

        currentReadedSize += currentReadSize;
        readedSize += currentReadSize;

        if (currentReadedSize > 1024 * 500 && (currentTime - lastReadTime) > 1000) {

            isPrintLog = true;
        }
    }

    if (msg instanceof LastHttpContent) {
        if ((currentTime - lastReadTime) > 0) {
            isPrintLog = true;
        }
    }

    if (isPrintLog) {

        String s = "" + readedSize + "," + df.format(readedSize * 100.0 / totalSize) + ","
                + df.format(currentReadedSize / ((currentTime - lastReadTime) * 1.024)) + ","
                + (currentTime - lastReadTime) + "," + currentReadedSize;

        if (resultOut != null) {
            resultOut.println(s);
            resultOut.flush();
        }

        logger.info(s);

        currentReadedSize = 0;
        lastReadTime = currentTime;
    }

    if (msg instanceof LastHttpContent) {
        ctx.close();
    }

}