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:org.elasticsearch.http.netty4.Netty4HttpChannelTests.java

License:Apache License

public void testHeadersSet() {
    Settings settings = Settings.builder().build();
    try (Netty4HttpServerTransport httpServerTransport = new Netty4HttpServerTransport(settings, networkService,
            bigArrays, threadPool)) {//from   w  w  w  . j a  va2s.c om
        httpServerTransport.start();
        final FullHttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET,
                "/");
        httpRequest.headers().add(HttpHeaderNames.ORIGIN, "remote");
        final WriteCapturingChannel writeCapturingChannel = new WriteCapturingChannel();
        Netty4HttpRequest request = new Netty4HttpRequest(httpRequest, writeCapturingChannel);

        // send a response
        Netty4HttpChannel channel = new Netty4HttpChannel(httpServerTransport, request, null, randomBoolean(),
                threadPool.getThreadContext());
        TestResponse resp = new TestResponse();
        final String customHeader = "custom-header";
        final String customHeaderValue = "xyz";
        resp.addHeader(customHeader, customHeaderValue);
        channel.sendResponse(resp);

        // inspect what was written
        List<Object> writtenObjects = writeCapturingChannel.getWrittenObjects();
        assertThat(writtenObjects.size(), is(1));
        HttpResponse response = (HttpResponse) writtenObjects.get(0);
        assertThat(response.headers().get("non-existent-header"), nullValue());
        assertThat(response.headers().get(customHeader), equalTo(customHeaderValue));
        assertThat(response.headers().get(HttpHeaderNames.CONTENT_LENGTH),
                equalTo(Integer.toString(resp.content().length())));
        assertThat(response.headers().get(HttpHeaderNames.CONTENT_TYPE), equalTo(resp.contentType()));
    }
}

From source file:org.elasticsearch.http.netty4.Netty4HttpChannelTests.java

License:Apache License

private FullHttpResponse executeRequest(final Settings settings, final String originValue, final String host) {
    // construct request and send it over the transport layer
    try (Netty4HttpServerTransport httpServerTransport = new Netty4HttpServerTransport(settings, networkService,
            bigArrays, threadPool)) {//w w w .  j av  a 2  s . com
        httpServerTransport.start();
        final FullHttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET,
                "/");
        if (originValue != null) {
            httpRequest.headers().add(HttpHeaderNames.ORIGIN, originValue);
        }
        httpRequest.headers().add(HttpHeaderNames.HOST, host);
        final WriteCapturingChannel writeCapturingChannel = new WriteCapturingChannel();
        final Netty4HttpRequest request = new Netty4HttpRequest(httpRequest, writeCapturingChannel);

        Netty4HttpChannel channel = new Netty4HttpChannel(httpServerTransport, request, null, randomBoolean(),
                threadPool.getThreadContext());
        channel.sendResponse(new TestResponse());

        // get the response
        List<Object> writtenObjects = writeCapturingChannel.getWrittenObjects();
        assertThat(writtenObjects.size(), is(1));
        return (FullHttpResponse) writtenObjects.get(0);
    }
}

From source file:org.elasticsearch.http.netty4.Netty4HttpClient.java

License:Apache License

public Collection<FullHttpResponse> get(SocketAddress remoteAddress, String... uris)
        throws InterruptedException {
    Collection<HttpRequest> requests = new ArrayList<>(uris.length);
    for (int i = 0; i < uris.length; i++) {
        final HttpRequest httpRequest = new DefaultFullHttpRequest(HTTP_1_1, HttpMethod.GET, uris[i]);
        httpRequest.headers().add(HOST, "localhost");
        httpRequest.headers().add("X-Opaque-ID", String.valueOf(i));
        requests.add(httpRequest);//from w w w  .  j  a v  a 2s.c om
    }
    return sendRequests(remoteAddress, requests);
}

From source file:org.elasticsearch.http.netty4.Netty4HttpPipeliningHandlerTests.java

License:Apache License

private FullHttpRequest createHttpRequest(String uri) {
    return new DefaultFullHttpRequest(HTTP_1_1, HttpMethod.GET, uri);
}

From source file:org.elasticsearch.http.netty4.pipelining.Netty4HttpPipeliningHandlerTests.java

License:Apache License

public void testThatPipeliningWorksWithChunkedRequests() throws InterruptedException {
    final int numberOfRequests = randomIntBetween(2, 128);
    final EmbeddedChannel embeddedChannel = new EmbeddedChannel(new AggregateUrisAndHeadersHandler(),
            new HttpPipeliningHandler(numberOfRequests), new WorkEmulatorHandler());

    for (int i = 0; i < numberOfRequests; i++) {
        final DefaultHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET,
                "/" + i);
        embeddedChannel.writeInbound(request);
        embeddedChannel.writeInbound(LastHttpContent.EMPTY_LAST_CONTENT);
    }//from  w  w w .  jav  a 2 s . c om

    final List<CountDownLatch> latches = new ArrayList<>();
    for (int i = numberOfRequests - 1; i >= 0; i--) {
        latches.add(finishRequest(Integer.toString(i)));
    }

    for (final CountDownLatch latch : latches) {
        latch.await();
    }

    embeddedChannel.flush();

    for (int i = 0; i < numberOfRequests; i++) {
        assertReadHttpMessageHasContent(embeddedChannel, Integer.toString(i));
    }

    assertTrue(embeddedChannel.isOpen());
}

From source file:org.elasticsearch.http.nio.HttpReadWriteHandlerTests.java

License:Apache License

public void testSuccessfulDecodeHttpRequest() throws IOException {
    String uri = "localhost:9090/" + randomAlphaOfLength(8);
    io.netty.handler.codec.http.HttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1,
            HttpMethod.GET, uri);

    ByteBuf buf = requestEncoder.encode(httpRequest);
    int slicePoint = randomInt(buf.writerIndex() - 1);
    ByteBuf slicedBuf = buf.retainedSlice(0, slicePoint);
    ByteBuf slicedBuf2 = buf.retainedSlice(slicePoint, buf.writerIndex());
    try {//from  w ww  . ja v  a  2s .  c om
        handler.consumeReads(toChannelBuffer(slicedBuf));

        verify(transport, times(0)).incomingRequest(any(HttpRequest.class), any(NioHttpChannel.class));

        handler.consumeReads(toChannelBuffer(slicedBuf2));

        ArgumentCaptor<HttpRequest> requestCaptor = ArgumentCaptor.forClass(HttpRequest.class);
        verify(transport).incomingRequest(requestCaptor.capture(), any(NioHttpChannel.class));

        HttpRequest nioHttpRequest = requestCaptor.getValue();
        assertEquals(HttpRequest.HttpVersion.HTTP_1_1, nioHttpRequest.protocolVersion());
        assertEquals(RestRequest.Method.GET, nioHttpRequest.method());
    } finally {
        handler.close();
        buf.release();
        slicedBuf.release();
        slicedBuf2.release();
    }
}

From source file:org.elasticsearch.http.nio.HttpReadWriteHandlerTests.java

License:Apache License

public void testDecodeHttpRequestError() throws IOException {
    String uri = "localhost:9090/" + randomAlphaOfLength(8);
    io.netty.handler.codec.http.HttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1,
            HttpMethod.GET, uri);

    ByteBuf buf = requestEncoder.encode(httpRequest);
    try {// w  w  w .ja  va 2s .  c o  m
        buf.setByte(0, ' ');
        buf.setByte(1, ' ');
        buf.setByte(2, ' ');

        handler.consumeReads(toChannelBuffer(buf));

        ArgumentCaptor<Exception> exceptionCaptor = ArgumentCaptor.forClass(Exception.class);
        verify(transport).incomingRequestError(any(HttpRequest.class), any(NioHttpChannel.class),
                exceptionCaptor.capture());

        assertTrue(exceptionCaptor.getValue() instanceof IllegalArgumentException);
    } finally {
        buf.release();
    }
}

From source file:org.elasticsearch.http.nio.HttpReadWriteHandlerTests.java

License:Apache License

@SuppressWarnings("unchecked")
public void testEncodeHttpResponse() throws IOException {
    prepareHandlerForResponse(handler);/*from   w  ww .  ja  va  2s.c  o  m*/

    DefaultFullHttpRequest nettyRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/");
    NioHttpRequest nioHttpRequest = new NioHttpRequest(nettyRequest, 0);
    NioHttpResponse httpResponse = nioHttpRequest.createResponse(RestStatus.OK, BytesArray.EMPTY);
    httpResponse.addHeader(HttpHeaderNames.CONTENT_LENGTH.toString(), "0");

    SocketChannelContext context = mock(SocketChannelContext.class);
    HttpWriteOperation writeOperation = new HttpWriteOperation(context, httpResponse, mock(BiConsumer.class));
    List<FlushOperation> flushOperations = handler.writeToBytes(writeOperation);
    FlushOperation operation = flushOperations.get(0);
    FullHttpResponse response = responseDecoder.decode(Unpooled.wrappedBuffer(operation.getBuffersToWrite()));
    ((ChannelPromise) operation.getListener()).setSuccess();
    try {
        assertEquals(HttpResponseStatus.OK, response.status());
        assertEquals(HttpVersion.HTTP_1_1, response.protocolVersion());
    } finally {
        response.release();
    }
}

From source file:org.elasticsearch.http.nio.HttpReadWriteHandlerTests.java

License:Apache License

private FullHttpResponse executeCorsRequest(final Settings settings, final String originValue,
        final String host) throws IOException {
    HttpHandlingSettings httpHandlingSettings = HttpHandlingSettings.fromSettings(settings);
    NioCorsConfig nioCorsConfig = NioHttpServerTransport.buildCorsConfig(settings);
    HttpReadWriteHandler handler = new HttpReadWriteHandler(nioHttpChannel, transport, httpHandlingSettings,
            nioCorsConfig);//from w  w  w .jav a 2  s .c o m
    prepareHandlerForResponse(handler);
    DefaultFullHttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/");
    if (originValue != null) {
        httpRequest.headers().add(HttpHeaderNames.ORIGIN, originValue);
    }
    httpRequest.headers().add(HttpHeaderNames.HOST, host);
    NioHttpRequest nioHttpRequest = new NioHttpRequest(httpRequest, 0);
    BytesArray content = new BytesArray("content");
    HttpResponse response = nioHttpRequest.createResponse(RestStatus.OK, content);
    response.addHeader("Content-Length", Integer.toString(content.length()));

    SocketChannelContext context = mock(SocketChannelContext.class);
    List<FlushOperation> flushOperations = handler
            .writeToBytes(handler.createWriteOperation(context, response, (v, e) -> {
            }));
    handler.close();
    FlushOperation flushOperation = flushOperations.get(0);
    ((ChannelPromise) flushOperation.getListener()).setSuccess();
    return responseDecoder.decode(Unpooled.wrappedBuffer(flushOperation.getBuffersToWrite()));
}

From source file:org.elasticsearch.http.nio.HttpReadWriteHandlerTests.java

License:Apache License

private NioHttpRequest prepareHandlerForResponse(HttpReadWriteHandler handler) throws IOException {
    HttpMethod method = randomBoolean() ? HttpMethod.GET : HttpMethod.HEAD;
    HttpVersion version = randomBoolean() ? HttpVersion.HTTP_1_0 : HttpVersion.HTTP_1_1;
    String uri = "http://localhost:9090/" + randomAlphaOfLength(8);

    io.netty.handler.codec.http.HttpRequest request = new DefaultFullHttpRequest(version, method, uri);
    ByteBuf buf = requestEncoder.encode(request);
    try {/*w  ww .  j a va  2s.c o m*/
        handler.consumeReads(toChannelBuffer(buf));
    } finally {
        buf.release();
    }

    ArgumentCaptor<NioHttpRequest> requestCaptor = ArgumentCaptor.forClass(NioHttpRequest.class);
    verify(transport, atLeastOnce()).incomingRequest(requestCaptor.capture(), any(HttpChannel.class));

    NioHttpRequest nioHttpRequest = requestCaptor.getValue();
    assertNotNull(nioHttpRequest);
    assertEquals(method.name(), nioHttpRequest.method().name());
    if (version == HttpVersion.HTTP_1_1) {
        assertEquals(HttpRequest.HttpVersion.HTTP_1_1, nioHttpRequest.protocolVersion());
    } else {
        assertEquals(HttpRequest.HttpVersion.HTTP_1_0, nioHttpRequest.protocolVersion());
    }
    assertEquals(nioHttpRequest.uri(), uri);
    return nioHttpRequest;
}