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:io.reactivex.netty.protocol.http.client.CookieTest.java

License:Apache License

@Test
public void testSetCookie() throws Exception {
    DefaultHttpRequest nettyRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "");
    String cookie1Name = "PREF";
    String cookie1Value = "ID=a95756377b78e75e:FF=0:TM=1392709628:LM=1392709628:S=a5mOVvTB7DBkexgi";
    String cookie1Domain = ".google.com";
    String cookie1Path = "/";
    Cookie cookie = new DefaultCookie(cookie1Name, cookie1Value);
    cookie.setPath(cookie1Path);/*from  w  w w. jav a 2  s  .  c  om*/
    cookie.setDomain(cookie1Domain);
    new HttpClientRequest<ByteBuf>(nettyRequest).withCookie(cookie);
    String cookieHeader = nettyRequest.headers().get(HttpHeaders.Names.COOKIE);
    Assert.assertNotNull("No cookie header found.", cookieHeader);
    Set<Cookie> decodeCookies = CookieDecoder.decode(cookieHeader);
    Assert.assertNotNull("No cookie found with name.", decodeCookies);
    Assert.assertEquals("Unexpected number of cookies.", 1, decodeCookies.size());
    Cookie decodedCookie = decodeCookies.iterator().next();
    Assert.assertEquals("Unexpected cookie name.", cookie1Name, decodedCookie.getName());
    Assert.assertEquals("Unexpected cookie path.", cookie1Path, decodedCookie.getPath());
    Assert.assertEquals("Unexpected cookie domain.", cookie1Domain, decodedCookie.getDomain());
}

From source file:io.reactivex.netty.protocol.http.client.DefaultRedirectHandler.java

License:Apache License

protected HttpClientRequest<I> createRedirectRequest(HttpClientRequest<I> original, URI redirectLocation,
        int redirectStatus) {
    HttpRequest nettyRequest = original.getNettyRequest();
    nettyRequest.setUri(getNettyRequestUri(redirectLocation));

    HttpClientRequest<I> newRequest = new HttpClientRequest<I>(nettyRequest, original);

    if (redirectStatus == 303) {
        // according to HTTP spec, 303 mandates the change of request type to GET
        nettyRequest.setMethod(HttpMethod.GET);
        // If it is a get, then the content is not to be sent.
        newRequest.removeContent();/*w ww  . j  ava 2 s  .c  o  m*/
    }
    return newRequest;
}

From source file:io.reactivex.netty.protocol.http.client.FollowRedirectHttpClientImpl.java

License:Apache License

private static <I> HttpClientRequest<I> createRedirectRequest(HttpClientRequest<I> original, String newURI,
        int statusCode) {
    HttpRequest nettyRequest = original.getNettyRequest();
    nettyRequest.setUri(newURI);//from   www.j av  a 2  s.  co  m
    if (statusCode == 303) {
        // according to HTTP spec, 303 mandates the change of request type to GET
        nettyRequest.setMethod(HttpMethod.GET);
    }
    HttpClientRequest<I> newRequest = new HttpClientRequest<I>(nettyRequest);
    if (statusCode != 303) {
        // if status code is 303, we can just leave the content factory to be null
        newRequest.contentFactory = original.contentFactory;
        newRequest.rawContentFactory = original.rawContentFactory;
    }
    return newRequest;
}

From source file:io.reactivex.netty.protocol.http.client.HttpClientImpl.java

License:Apache License

protected boolean shouldFollowRedirectForRequest(HttpClientConfig config, HttpClientRequest<I> request) {
    HttpClientConfig.RedirectsHandling redirectsHandling = config.getFollowRedirect();

    switch (redirectsHandling) {
    case Enable:/*from   w  w w  . ja  v  a  2s.c o  m*/
        return true;
    case Disable:
        return false;
    case Undefined:
        return request.getMethod() == HttpMethod.HEAD || request.getMethod() == HttpMethod.GET;
    default:
        return false;
    }
}

From source file:io.reactivex.netty.protocol.http.client.HttpClientRequest.java

License:Apache License

public static HttpClientRequest<ByteBuf> createGet(String uri) {
    return create(HttpMethod.GET, uri);
}

From source file:io.reactivex.netty.protocol.http.client.RequestProcessor.java

License:Apache License

@Override
public Observable<Void> handle(HttpServerRequest<ByteBuf> request, HttpServerResponse<ByteBuf> response) {
    String uri = request.getUri();
    if (uri.startsWith("/") && uri.length() > 1) {
        uri = uri.substring(1);//from w  w  w  . j  a v a 2 s  . co  m
    }
    if ("/".equals(uri) || uri.contains("test/singleEntity")) {
        // in case of redirect, uri starts with /test/singleEntity 
        return handleSingleEntity(response);
    } else if (uri.startsWith("test/stream")) {
        return handleStream(response);
    } else if (uri.startsWith("test/nochunk_stream")) {
        return handleStreamWithoutChunking(response);
    } else if (uri.startsWith("test/largeStream")) {
        return handleLargeStream(response);
    } else if (uri.startsWith("test/timeout")) {
        return simulateTimeout(request, response);
    } else if (uri.contains("test/post")) {
        return handlePost(request, response);
    } else if (uri.startsWith("test/closeConnection")) {
        return handleCloseConnection(response);
    } else if (uri.startsWith("test/keepAliveTimeout")) {
        return handleKeepAliveTimeout(response);
    } else if (uri.startsWith("test/redirectInfinite")) {
        return redirectCustom(request, response);
    } else if (uri.startsWith("test/redirectLoop")) {
        return redirectCustom(request, response);
    } else if (uri.startsWith("test/redirectLimited")) {
        return redirectCustom(request, response);
    } else if (uri.startsWith("test/redirect") && request.getHttpMethod().equals(HttpMethod.GET)) {
        return redirectGet(request, response);
    } else if (uri.startsWith("test/redirectPost") && request.getHttpMethod().equals(HttpMethod.POST)) {
        return redirectPost(request, response);
    } else {
        response.setStatus(HttpResponseStatus.NOT_FOUND);
        return response.flush();
    }
}

From source file:io.reactivex.netty.protocol.http.server.CookieTest.java

License:Apache License

@Test
public void testGetCookie() throws Exception {
    DefaultHttpRequest nettyRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "");
    String cookie1Name = "PREF";
    String cookie1Value = "ID=a95756377b78e75e:FF=0:TM=1392709628:LM=1392709628:S=a5mOVvTB7DBkexgi";
    String cookie1Domain = ".google.com";
    String cookie1Path = "/";
    String cookie1Header = cookie1Name + '=' + cookie1Value + "; expires=Thu, 18-Feb-2016 07:47:08 GMT; path="
            + cookie1Path + "; domain=" + cookie1Domain;
    nettyRequest.headers().add(HttpHeaders.Names.COOKIE, cookie1Header);
    HttpServerRequest<ByteBuf> request = new HttpServerRequest<ByteBuf>(nettyRequest,
            PublishSubject.<ByteBuf>create());
    Map<String, Set<Cookie>> cookies = request.getCookies();
    Assert.assertEquals("Unexpected number of cookies.", 1, cookies.size());
    Set<Cookie> cookies1 = cookies.get(cookie1Name);
    Assert.assertNotNull("No cookie found with name: " + cookie1Name, cookies1);
    Assert.assertEquals("Unexpected number of cookies with name: " + cookie1Name, 1, cookies1.size());
    Cookie cookie = cookies1.iterator().next();
    Assert.assertEquals("Unexpected cookie name.", cookie1Name, cookie.getName());
    Assert.assertEquals("Unexpected cookie path.", cookie1Path, cookie.getPath());
}

From source file:io.reactivex.netty.protocol.http.server.Http10Test.java

License:Apache License

@Test
public void testHttp1_0Response() throws Exception {
    tearDown(); // ugly but used to shutdown the existing server.
    mockServer = new HttpServerBuilder<ByteBuf, ByteBuf>(0, new RequestHandler<ByteBuf, ByteBuf>() {
        @Override//from w  w  w  . j a va  2 s.  c o m
        public Observable<Void> handle(HttpServerRequest<ByteBuf> request,
                HttpServerResponse<ByteBuf> response) {
            return response.writeStringAndFlush(WELCOME_SERVER_MSG);
        }
    }, true).build().start();
    HttpClientRequest<ByteBuf> request = HttpClientRequest.create(HttpVersion.HTTP_1_0, HttpMethod.GET, "/");
    HttpClientResponse<ByteBuf> response = RxNetty
            .<ByteBuf, ByteBuf>newHttpClientBuilder("localhost", mockServer.getServerPort())
            .enableWireLogging(LogLevel.ERROR).build().submit(request).toBlocking().toFuture()
            .get(1, TimeUnit.MINUTES);
    HttpVersion httpVersion = response.getHttpVersion();
    Assert.assertEquals("Unexpected HTTP version.", HttpVersion.HTTP_1_0, httpVersion);
    Assert.assertFalse("Unexpected Connection header.", response.getHeaders().isKeepAlive());
    Assert.assertFalse("Unexpected Transfer encoding.", response.getHeaders().isTransferEncodingChunked());
}

From source file:io.reactivex.netty.protocol.http.server.Http10Test.java

License:Apache License

@Test
public void testHttp1_0Request() throws Exception {
    HttpClientRequest<ByteBuf> request = HttpClientRequest.create(HttpVersion.HTTP_1_0, HttpMethod.GET, "/");
    HttpClientResponse<ByteBuf> response = RxNetty
            .<ByteBuf, ByteBuf>newHttpClientBuilder("localhost", mockServerPort)
            .enableWireLogging(LogLevel.ERROR).build().submit(request).toBlocking().toFuture()
            .get(1, TimeUnit.MINUTES);
    HttpVersion httpVersion = response.getHttpVersion();
    Assert.assertEquals("Unexpected HTTP version.", HttpVersion.HTTP_1_1, httpVersion);
    Assert.assertFalse("Unexpected Connection header.", response.getHeaders().isKeepAlive());
    Assert.assertFalse("Unexpected Transfer encoding.", response.getHeaders().isTransferEncodingChunked());
}

From source file:io.reactivex.netty.protocol.http.server.Http10Test.java

License:Apache License

@Test
public void testHttp1_0RequestWithContent() throws Exception {
    HttpClientRequest<ByteBuf> request = HttpClientRequest.create(HttpVersion.HTTP_1_0, HttpMethod.GET, "/");
    final ByteBuf response = RxNetty.<ByteBuf, ByteBuf>newHttpClientBuilder("localhost", mockServerPort)
            .enableWireLogging(LogLevel.ERROR).build().submit(request)
            .flatMap(new Func1<HttpClientResponse<ByteBuf>, Observable<ByteBuf>>() {
                @Override/*from w  w w . j a  v a2s.  c  o  m*/
                public Observable<ByteBuf> call(HttpClientResponse<ByteBuf> response) {
                    return response.getContent();
                }
            }).map(new Func1<ByteBuf, ByteBuf>() {
                @Override
                public ByteBuf call(ByteBuf byteBuf) {
                    return byteBuf.retain();
                }
            }).toBlocking().toFuture().get(1, TimeUnit.MINUTES);
    Assert.assertEquals("Unexpected Content.", WELCOME_SERVER_MSG, response.toString(Charset.defaultCharset()));
    response.release();
}