Example usage for io.netty.handler.codec.http HttpMethod GET

List of usage examples for io.netty.handler.codec.http HttpMethod GET

Introduction

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

Prototype

HttpMethod GET

To view the source code for io.netty.handler.codec.http HttpMethod GET.

Click Source Link

Document

The GET method means retrieve whatever information (in the form of an entity) is identified by the Request-URI.

Usage

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

License:Open Source License

/**
 * Gets the blob with blob ID {@code blobId} and verifies that the headers and content match with what is expected.
 * @param blobId the blob ID of the blob to GET.
 * @param expectedHeaders the expected headers in the response.
 * @param expectedContent the expected content of the blob.
 * @throws ExecutionException/*from www .j  a va 2 s  . c o m*/
 * @throws InterruptedException
 */
private void getBlobAndVerify(String blobId, HttpHeaders expectedHeaders, ByteBuffer expectedContent)
        throws ExecutionException, InterruptedException {
    FullHttpRequest httpRequest = buildRequest(HttpMethod.GET, 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("Content-Type does not match", expectedHeaders.get(RestUtils.Headers.AMBRY_CONTENT_TYPE),
            response.headers().get(HttpHeaders.Names.CONTENT_TYPE));
    assertEquals(RestUtils.Headers.BLOB_SIZE + " does not match",
            expectedHeaders.get(RestUtils.Headers.BLOB_SIZE),
            response.headers().get(RestUtils.Headers.BLOB_SIZE));
    ByteBuffer responseContent = getContent(response, responseParts);
    assertArrayEquals("GET content does not match original content", expectedContent.array(),
            responseContent.array());
    assertTrue("Channel should be active", HttpHeaders.isKeepAlive(response));
}

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

License:Open Source License

/**
 * Gets the blob with blob ID {@code blobId} and verifies that the blob is not returned as blob is not modified
 * @param blobId the blob ID of the blob to GET.
 * @throws Exception//from  www. jav  a  2 s . c  o  m
 */
private void getNotModifiedBlobAndVerify(String blobId) throws Exception {
    HttpHeaders headers = new DefaultHttpHeaders();
    headers.add(RestUtils.Headers.IF_MODIFIED_SINCE, new Date());
    FullHttpRequest httpRequest = buildRequest(HttpMethod.GET, blobId, headers, null);
    Queue<HttpObject> responseParts = nettyClient.sendRequest(httpRequest, null, null).get();
    HttpResponse response = (HttpResponse) responseParts.poll();
    assertEquals("Unexpected response status", HttpResponseStatus.NOT_MODIFIED, response.getStatus());
    assertTrue("No Date header", response.headers().get(RestUtils.Headers.DATE) != null);
    assertNull("No Last-Modified header expected", response.headers().get("Last-Modified"));
    assertNull(RestUtils.Headers.BLOB_SIZE + " should have been null ",
            response.headers().get(RestUtils.Headers.BLOB_SIZE));
    assertNull("Content-Type should have been null", response.headers().get(RestUtils.Headers.CONTENT_TYPE));
    assertNoContent(responseParts);
}

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

License:Open Source License

/**
 * Gets the user metadata 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.
 * @param usermetadata if non-null, this is expected to come as the body.
 * @throws ExecutionException/*from  www. j av a2 s  .com*/
 * @throws InterruptedException
 */
private void getUserMetadataAndVerify(String blobId, HttpHeaders expectedHeaders, byte[] usermetadata)
        throws ExecutionException, InterruptedException {
    FullHttpRequest httpRequest = buildRequest(HttpMethod.GET,
            blobId + "/" + RestUtils.SubResource.UserMetadata, 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());
    verifyUserMetadata(expectedHeaders, response, usermetadata, responseParts);
    assertTrue("Channel should be active", HttpHeaders.isKeepAlive(response));
}

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

License:Open Source License

/**
 * Gets the blob info 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.
 * @param usermetadata if non-null, this is expected to come as the body.
 * @throws ExecutionException/*from   w w w  . j  a v  a 2 s  .  c o  m*/
 * @throws InterruptedException
 */
private void getBlobInfoAndVerify(String blobId, HttpHeaders expectedHeaders, byte[] usermetadata)
        throws ExecutionException, InterruptedException {
    FullHttpRequest httpRequest = buildRequest(HttpMethod.GET, blobId + "/" + RestUtils.SubResource.BlobInfo,
            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());
    verifyBlobProperties(expectedHeaders, response);
    verifyUserMetadata(expectedHeaders, response, usermetadata, responseParts);
    assertTrue("Channel should be active", HttpHeaders.isKeepAlive(response));
}

From source file:com.github.ambry.rest.HealthCheckHandlerTest.java

License:Open Source License

/**
 * Tests for the common case request handling flow for health check requests.
 * @throws java.io.IOException/*from  w  ww . j  a  v a 2 s . c o m*/
 */
@Test
public void requestHandleWithHealthCheckRequestTest() throws IOException {
    // test with keep alive
    testHealthCheckRequest(HttpMethod.GET, true, true);
    testHealthCheckRequest(HttpMethod.GET, false, true);

    // test without keep alive
    testHealthCheckRequest(HttpMethod.GET, true, false);
    testHealthCheckRequest(HttpMethod.GET, false, false);
}

From source file:com.github.ambry.rest.HealthCheckHandlerTest.java

License:Open Source License

/**
 * Tests non health check requests handling
 * @throws java.io.IOException/*from w w w  .  j a  va  2s. com*/
 */
@Test
public void requestHandleWithNonHealthCheckRequestTest() throws IOException {
    testNonHealthCheckRequest(HttpMethod.POST, "POST");
    testNonHealthCheckRequest(HttpMethod.GET, "GET");
    testNonHealthCheckRequest(HttpMethod.DELETE, "DELETE");
}

From source file:com.github.ambry.rest.NettyMessageProcessorTest.java

License:Open Source License

/**
 * Tests for the common case request handling flow.
 * @throws IOException//from   w w  w .  j  a v  a2 s  . co m
 */
@Test
public void requestHandleWithGoodInputTest() throws IOException {
    doRequestHandleWithoutKeepAlive(HttpMethod.GET, RestMethod.GET);
    doRequestHandleWithoutKeepAlive(HttpMethod.DELETE, RestMethod.DELETE);
    doRequestHandleWithoutKeepAlive(HttpMethod.HEAD, RestMethod.HEAD);

    EmbeddedChannel channel = createChannel();
    doRequestHandleWithKeepAlive(channel, HttpMethod.GET, RestMethod.GET);
    doRequestHandleWithKeepAlive(channel, HttpMethod.DELETE, RestMethod.DELETE);
    doRequestHandleWithKeepAlive(channel, HttpMethod.HEAD, RestMethod.HEAD);
}

From source file:com.github.ambry.rest.NettyMessageProcessorTest.java

License:Open Source License

/**
 * Tests for error handling flow when bad input streams are provided to the {@link NettyMessageProcessor}.
 *//*  w ww . ja  va 2  s. co  m*/
@Test
public void requestHandleWithBadInputTest() throws IOException {
    String content = "@@randomContent@@@";
    // content without request.
    EmbeddedChannel channel = createChannel();
    channel.writeInbound(new DefaultLastHttpContent(Unpooled.wrappedBuffer(content.getBytes())));
    HttpResponse response = (HttpResponse) channel.readOutbound();
    assertEquals("Unexpected response status", HttpResponseStatus.BAD_REQUEST, response.getStatus());

    // content without request on a channel that was kept alive
    channel = createChannel();
    // send and receive response for a good request and keep the channel alive
    channel.writeInbound(
            RestTestUtils.createRequest(HttpMethod.GET, MockBlobStorageService.ECHO_REST_METHOD, null));
    channel.writeInbound(LastHttpContent.EMPTY_LAST_CONTENT);
    response = (HttpResponse) channel.readOutbound();
    assertEquals("Unexpected response status", HttpResponseStatus.OK, response.getStatus());
    // drain the content
    while (channel.readOutbound() != null) {
        ;
    }
    assertTrue("Channel is not active", channel.isActive());
    // send content without request
    channel.writeInbound(LastHttpContent.EMPTY_LAST_CONTENT);
    response = (HttpResponse) channel.readOutbound();
    assertEquals("Unexpected response status", HttpResponseStatus.BAD_REQUEST, response.getStatus());

    // content when no content is expected.
    channel = createChannel();
    channel.writeInbound(RestTestUtils.createRequest(HttpMethod.GET, "/", null));
    channel.writeInbound(new DefaultLastHttpContent(Unpooled.wrappedBuffer(content.getBytes())));
    response = (HttpResponse) channel.readOutbound();
    assertEquals("Unexpected response status", HttpResponseStatus.BAD_REQUEST, response.getStatus());

    // wrong HTTPObject.
    channel = createChannel();
    channel.writeInbound(new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK));
    response = (HttpResponse) channel.readOutbound();
    assertEquals("Unexpected response status", HttpResponseStatus.BAD_REQUEST, response.getStatus());
}

From source file:com.github.ambry.rest.NettyMessageProcessorTest.java

License:Open Source License

/**
 * Tests for error handling flow when the {@link RestRequestHandler} throws exceptions.
 *//*w ww.  java  2  s.c om*/
@Test
public void requestHandlerExceptionTest() {
    try {
        // RuntimeException
        Properties properties = new Properties();
        properties.setProperty(MockRestRequestResponseHandler.RUNTIME_EXCEPTION_ON_HANDLE, "true");
        requestHandler.breakdown(new VerifiableProperties(properties));
        doRequestHandlerExceptionTest(HttpMethod.GET, HttpResponseStatus.INTERNAL_SERVER_ERROR);

        // RestServiceException
        properties.clear();
        properties.setProperty(MockRestRequestResponseHandler.REST_EXCEPTION_ON_HANDLE,
                RestServiceErrorCode.InternalServerError.toString());
        requestHandler.breakdown(new VerifiableProperties(properties));
        doRequestHandlerExceptionTest(HttpMethod.GET, HttpResponseStatus.INTERNAL_SERVER_ERROR);

        // ClosedChannelException
        properties.clear();
        properties.setProperty(MockRestRequestResponseHandler.CLOSE_REQUEST_ON_HANDLE, "true");
        requestHandler.breakdown(new VerifiableProperties(properties));
        doRequestHandlerExceptionTest(HttpMethod.POST, HttpResponseStatus.INTERNAL_SERVER_ERROR);
    } finally {
        requestHandler.fix();
    }
}

From source file:com.github.ambry.rest.NettyMultipartRequestTest.java

License:Open Source License

/**
 * Tests instantiation of {@link NettyMultipartRequest} with different {@link HttpMethod} types.
 * </p>/*from   w w  w .  ja v  a 2 s  .c o m*/
 * Only {@link HttpMethod#POST} should succeed.
 * @throws RestServiceException
 */
@Test
public void instantiationTest() throws RestServiceException {
    // POST will succeed.
    HttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
    closeRequestAndValidate(new NettyMultipartRequest(httpRequest, nettyMetrics));

    // Methods that will fail. Can include other methods, but these should be enough.
    HttpMethod[] methods = { HttpMethod.GET, HttpMethod.DELETE, HttpMethod.HEAD };
    for (HttpMethod method : methods) {
        httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, method, "/");
        try {
            new NettyMultipartRequest(httpRequest, nettyMetrics);
            fail("Creation of NettyMultipartRequest should have failed for " + method);
        } catch (IllegalArgumentException e) {
            // expected. Nothing to do.
        }
    }
}