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.server.HttpServerRequestUriTest.java

License:Apache License

@Test
public void testRequestUri() throws Exception {
    String path = "a/b/c";
    String qp1Name = "qp1";
    String qp1Val = "qp1Val";
    String qp2Name = "qp2";
    String qp2Val = "qp2Val";
    String qp2Val2 = "qp2Val222";
    String queryString = qp1Name + '=' + qp1Val + '&' + qp2Name + '=' + qp2Val + '&' + qp2Name + '=' + qp2Val2;
    String uri = path + '?' + queryString;
    DefaultHttpRequest nettyRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uri);
    HttpServerRequest<ByteBuf> request = newServerRequest(nettyRequest);
    Assert.assertEquals("Unexpected uri string", uri, request.getUri());
    Assert.assertEquals("Unexpected query string", queryString, request.getQueryString());
    Assert.assertEquals("Unexpected path string", path, request.getPath());
    Map<String, List<String>> qpsGot = request.getQueryParameters();
    Assert.assertNotNull("Got null query parameters", qpsGot);
    Assert.assertEquals("Unexpected number of query parameters", 2, qpsGot.size());
    List<String> qp1Got = qpsGot.get(qp1Name);
    Assert.assertNotNull("Got no query parameters with name: " + qp1Name, qp1Got);
    Assert.assertEquals("Unexpected number of query parameters with name: " + qp1Name, 1, qp1Got.size());
    Assert.assertEquals("Unexpected query parameter value with name: " + qp1Name, qp1Val, qp1Got.get(0));

    List<String> qp2Got = qpsGot.get(qp2Name);
    Assert.assertNotNull("Got no query parameters with name: " + qp2Name, qp2Got);
    Assert.assertEquals("Unexpected number of query parameters with name: " + qp2Name, 2, qp2Got.size());
    Assert.assertEquals("Unexpected query parameter value with name: " + qp2Name, qp2Got.get(0), qp2Val);
    Assert.assertEquals("Unexpected query parameter second value with name: " + qp2Name, qp2Got.get(1),
            qp2Val2);//from w ww .  j a va  2 s.  c om
}

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

License:Apache License

@Test
public void testEmptyQueryString() throws Exception {
    String path = "a/b/c";
    String uri = path + '?';
    DefaultHttpRequest nettyRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uri);
    HttpServerRequest<ByteBuf> request = newServerRequest(nettyRequest);
    Assert.assertEquals("Unexpected uri string", uri, request.getUri());
    Assert.assertEquals("Unexpected query string", "", request.getQueryString());
}

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

License:Apache License

@Test
public void testAbsentQueryString() throws Exception {
    String path = "a/b/c";
    String uri = path;//  w  w w.  j  a v  a 2s  .  c o  m
    DefaultHttpRequest nettyRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uri);
    HttpServerRequest<ByteBuf> request = newServerRequest(nettyRequest);
    Assert.assertEquals("Unexpected uri string", uri, request.getUri());
    Assert.assertEquals("Unexpected query string", "", request.getQueryString());
}

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

License:Apache License

@Test
public void testRequestUri() throws Exception {
    String path = "a/b/c";
    String qp1Name = "qp1";
    String qp1Val = "qp1Val";
    String qp2Name = "qp2";
    String qp2Val = "qp2Val";
    String qp2Val2 = "qp2Val222";
    String queryString = qp1Name + '=' + qp1Val + '&' + qp2Name + '=' + qp2Val + '&' + qp2Name + '=' + qp2Val2;
    String uri = path + '?' + queryString;
    DefaultHttpRequest nettyRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uri);
    HttpServerRequest<ByteBuf> request = new HttpServerRequest<ByteBuf>(nettyRequest,
            PublishSubject.<ByteBuf>create());
    Assert.assertEquals("Unexpected uri string", uri, request.getUri());
    Assert.assertEquals("Unexpected query string", queryString, request.getQueryString());
    Assert.assertEquals("Unexpected path string", path, request.getPath());
    Map<String, List<String>> qpsGot = request.getQueryParameters();
    Assert.assertNotNull("Got null query parameters", qpsGot);
    Assert.assertEquals("Unexpected number of query parameters", 2, qpsGot.size());
    List<String> qp1Got = qpsGot.get(qp1Name);
    Assert.assertNotNull("Got no query parameters with name: " + qp1Name, qp1Got);
    Assert.assertEquals("Unexpected number of query parameters with name: " + qp1Name, 1, qp1Got.size());
    Assert.assertEquals("Unexpected query parameter value with name: " + qp1Name, qp1Val, qp1Got.get(0));

    List<String> qp2Got = qpsGot.get(qp2Name);
    Assert.assertNotNull("Got no query parameters with name: " + qp2Name, qp2Got);
    Assert.assertEquals("Unexpected number of query parameters with name: " + qp2Name, 2, qp2Got.size());
    Assert.assertEquals("Unexpected query parameter value with name: " + qp2Name, qp2Got.get(0), qp2Val);
    Assert.assertEquals("Unexpected query parameter second value with name: " + qp2Name, qp2Got.get(1),
            qp2Val2);//from ww  w .jav a 2  s. co  m
}

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

License:Apache License

@Test
public void testEmptyQueryString() throws Exception {
    String path = "a/b/c";
    String uri = path + '?';
    DefaultHttpRequest nettyRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uri);
    HttpServerRequest<ByteBuf> request = new HttpServerRequest<ByteBuf>(nettyRequest,
            PublishSubject.<ByteBuf>create());
    Assert.assertEquals("Unexpected uri string", uri, request.getUri());
    Assert.assertEquals("Unexpected query string", "", request.getQueryString());
}

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

License:Apache License

@Test
public void testAbsentQueryString() throws Exception {
    String path = "a/b/c";
    String uri = path;// w  ww .  j  ava2s .  c  o  m
    DefaultHttpRequest nettyRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uri);
    HttpServerRequest<ByteBuf> request = new HttpServerRequest<ByteBuf>(nettyRequest,
            PublishSubject.<ByteBuf>create());
    Assert.assertEquals("Unexpected uri string", uri, request.getUri());
    Assert.assertEquals("Unexpected query string", "", request.getQueryString());
}

From source file:io.scalecube.socketio.pipeline.HandshakeHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    // /* ww  w.ja va2s . c om*/
    if (msg instanceof HttpRequest) {
        final HttpRequest req = (HttpRequest) msg;
        final HttpMethod requestMethod = req.getMethod();
        final QueryStringDecoder queryDecoder = new QueryStringDecoder(req.getUri());
        final String requestPath = queryDecoder.path();

        if (!requestPath.startsWith(handshakePath)) {
            log.warn("Received HTTP bad request: {} {} from channel: {}", requestMethod, requestPath,
                    ctx.channel());

            HttpResponse res = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.BAD_REQUEST);
            ChannelFuture f = ctx.channel().writeAndFlush(res);
            f.addListener(ChannelFutureListener.CLOSE);
            ReferenceCountUtil.release(req);
            return;
        }

        if (HttpMethod.GET.equals(requestMethod) && requestPath.equals(handshakePath)) {
            if (log.isDebugEnabled())
                log.debug("Received HTTP handshake request: {} {} from channel: {}", requestMethod, requestPath,
                        ctx.channel());

            handshake(ctx, req, queryDecoder);
            ReferenceCountUtil.release(req);
            return;
        }
    }

    super.channelRead(ctx, msg);
}

From source file:io.scalecube.socketio.pipeline.HandshakeHandlerTest.java

License:Apache License

@Test
public void testChannelRead() throws Exception {
    HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/socket.io/1/");

    LastOutboundHandler lastOutboundHandler = new LastOutboundHandler();
    EmbeddedChannel channel = new EmbeddedChannel(lastOutboundHandler, handshakeHandler);
    channel.writeInbound(request);//ww  w.  j a  v a2  s . c om
    Object outboundMessage = lastOutboundHandler.getOutboundMessages().poll();
    Assert.assertTrue(outboundMessage instanceof FullHttpResponse);
    FullHttpResponse res = (FullHttpResponse) outboundMessage;
    Assert.assertEquals(HttpVersion.HTTP_1_1, res.getProtocolVersion());
    Assert.assertEquals(HttpResponseStatus.OK, res.getStatus());
    ByteBuf content = res.content();
    Assert.assertTrue(content.toString(CharsetUtil.UTF_8)
            .endsWith("60:60:websocket,flashsocket,xhr-polling,jsonp-polling"));
    channel.finish();
}

From source file:io.scalecube.socketio.pipeline.HandshakeHandlerTest.java

License:Apache License

@Test
public void testChannelReadError() throws Exception {
    HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/wrongPath/");

    LastOutboundHandler lastOutboundHandler = new LastOutboundHandler();
    EmbeddedChannel channel = new EmbeddedChannel(lastOutboundHandler, handshakeHandler);
    channel.writeInbound(request);/*from w  w  w . ja  v a2  s  .c om*/
    Object outboundMessage = lastOutboundHandler.getOutboundMessages().poll();
    Assert.assertTrue(outboundMessage instanceof HttpResponse);
    HttpResponse res = (HttpResponse) outboundMessage;
    Assert.assertEquals(HttpVersion.HTTP_1_1, res.getProtocolVersion());
    Assert.assertEquals(HttpResponseStatus.BAD_REQUEST, res.getStatus());
    channel.finish();
}

From source file:io.scalecube.socketio.pipeline.JsonpPollingHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    // ?//from  w  w  w  . j  a  v a2 s.co m
    if (msg instanceof FullHttpRequest) {
        final FullHttpRequest req = (FullHttpRequest) msg;
        final HttpMethod requestMethod = req.getMethod();
        final QueryStringDecoder queryDecoder = new QueryStringDecoder(req.getUri());
        final String requestPath = queryDecoder.path();

        if (requestPath.startsWith(connectPath)) {
            if (log.isDebugEnabled())
                log.debug("Received HTTP JSONP-Polling request: {} {} from channel: {}", requestMethod,
                        requestPath, ctx.channel());

            final String sessionId = PipelineUtils.getSessionId(requestPath);
            final String origin = PipelineUtils.getOrigin(req);

            if (HttpMethod.GET.equals(requestMethod)) {
                // Process polling request from client
                SocketAddress clientIp = PipelineUtils.getHeaderClientIPParamValue(req, remoteAddressHeader);

                String jsonpIndexParam = PipelineUtils.extractParameter(queryDecoder, "i");
                final ConnectPacket packet = new ConnectPacket(sessionId, origin);
                packet.setTransportType(TransportType.JSONP_POLLING);
                packet.setJsonpIndexParam(jsonpIndexParam);
                packet.setRemoteAddress(clientIp);

                ctx.fireChannelRead(packet);
            } else if (HttpMethod.POST.equals(requestMethod)) {
                // Process message request from client
                ByteBuf buffer = req.content();
                String content = buffer.toString(CharsetUtil.UTF_8);
                if (content.startsWith("d=")) {
                    QueryStringDecoder queryStringDecoder = new QueryStringDecoder(content, CharsetUtil.UTF_8,
                            false);
                    content = PipelineUtils.extractParameter(queryStringDecoder, "d");
                    content = preprocessJsonpContent(content);
                    ByteBuf buf = PipelineUtils.copiedBuffer(ctx.alloc(), content);
                    List<Packet> packets = PacketFramer.decodePacketsFrame(buf);
                    buf.release();
                    for (Packet packet : packets) {
                        packet.setSessionId(sessionId);
                        packet.setOrigin(origin);
                        ctx.fireChannelRead(packet);
                    }
                } else {
                    log.warn(
                            "Can't process HTTP JSONP-Polling message. Incorrect content format: {} from channel: {}",
                            content, ctx.channel());
                }
            } else {
                log.warn(
                        "Can't process HTTP JSONP-Polling request. Unknown request method: {} from channel: {}",
                        requestMethod, ctx.channel());
            }
            ReferenceCountUtil.release(msg);
            return;
        }
    }
    super.channelRead(ctx, msg);
}