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

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

Introduction

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

Prototype

HttpMethod POST

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

Click Source Link

Document

The POST method is used to request that the origin server accept the entity enclosed in the request as a new subordinate of the resource identified by the Request-URI in the Request-Line.

Usage

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

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    // ?/*ww w. j ava 2s.  c  o  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);
}

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

License:Apache License

@Test
public void testChannelReadPacket() throws Exception {
    ByteBuf content = Unpooled.copiedBuffer("d=3:::{\"greetings\":\"Hello World!\"}", CharsetUtil.UTF_8);
    HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST,
            "/socket.io/1/jsonp-polling", content);
    String origin = "http://localhost:8080";
    HttpHeaders.addHeader(request, HttpHeaders.Names.ORIGIN, origin);
    LastOutboundHandler lastOutboundHandler = new LastOutboundHandler();
    EmbeddedChannel channel = new EmbeddedChannel(lastOutboundHandler, jsonpPollingHandler);
    channel.writeInbound(request);/*from   www .  j a v  a2  s.  co  m*/
    Object object = channel.readInbound();
    Assert.assertTrue(object instanceof Packet);
    Packet packet = (Packet) object;
    Assert.assertEquals(origin, packet.getOrigin());
    Assert.assertEquals("{\"greetings\":\"Hello World!\"}", packet.getData().toString(CharsetUtil.UTF_8));
    channel.finish();
}

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

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    // ?/*w w w. j  av  a2s .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 XHR-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)) {
                SocketAddress clientIp = PipelineUtils.getHeaderClientIPParamValue(req, remoteAddressHeader);

                // Process polling request from client
                final ConnectPacket packet = new ConnectPacket(sessionId, origin);
                packet.setTransportType(TransportType.XHR_POLLING);
                packet.setRemoteAddress(clientIp);

                ctx.fireChannelRead(packet);
            } else if (HttpMethod.POST.equals(requestMethod)) {
                // Process message request from client
                List<Packet> packets = PacketFramer.decodePacketsFrame(req.content());
                for (Packet packet : packets) {
                    packet.setSessionId(sessionId);
                    packet.setOrigin(origin);
                    ctx.fireChannelRead(packet);
                }
            } else {
                log.warn("Can't process HTTP XHR-Polling request. Unknown request method: {} from channel: {}",
                        requestMethod, ctx.channel());
            }
            ReferenceCountUtil.release(msg);
            return;
        }
    }

    ctx.fireChannelRead(msg);
}

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

License:Apache License

@Test
public void testChannelReadPacket() throws Exception {
    ByteBuf content = Unpooled.copiedBuffer("3:::{\"greetings\":\"Hello World!\"}", CharsetUtil.UTF_8);
    HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST,
            "/socket.io/1/xhr-polling", content);
    String origin = "http://localhost:8080";
    HttpHeaders.addHeader(request, HttpHeaders.Names.ORIGIN, origin);
    LastOutboundHandler lastOutboundHandler = new LastOutboundHandler();
    EmbeddedChannel channel = new EmbeddedChannel(lastOutboundHandler, xhrPollingHandler);
    channel.writeInbound(request);/*www .j  a v  a2  s  .  c  o  m*/
    Object object = channel.readInbound();
    Assert.assertTrue(object instanceof Packet);
    Packet packet = (Packet) object;
    Assert.assertEquals(origin, packet.getOrigin());
    Assert.assertEquals("{\"greetings\":\"Hello World!\"}", packet.getData().toString(CharsetUtil.UTF_8));
    channel.finish();
}

From source file:io.selendroid.driver.MultipleWebviewHandlingTests.java

License:Apache License

@Test
public void shouldSwitchContext() throws Exception {
    openMultipleWebViewActivity();/*from  w  w w . jav a2s  . c  om*/
    SelendroidDriver driver = driver();
    String uri = "/wd/hub/session/" + driver.getSessionId() + "/context";

    HttpClientUtil.parseJsonResponse(
            HttpClientUtil.executeRequestWithPayload(uri, 8080, HttpMethod.POST, "{'name':'WEBVIEW_0'}"));
    String getContextUri = "http://localhost:8080/wd/hub/session/" + driver.getSessionId() + "/context";

    JSONObject response = HttpClientUtil
            .parseJsonResponse(HttpClientUtil.executeRequest(getContextUri, HttpMethod.GET));
    Assert.assertEquals("WEBVIEW_0", response.getString("value"));
}

From source file:io.selendroid.server.BaseTest.java

License:Apache License

public HttpResponse executeRequest(String url, HttpMethod method) throws Exception {
    HttpRequestBase request;// w  w  w. ja va 2  s .  co  m
    if (HttpMethod.GET.equals(method)) {
        request = new HttpGet(url);
    } else if (HttpMethod.POST.equals(method)) {
        request = new HttpPost(url);
    } else if (HttpMethod.DELETE.equals(method)) {
        request = new HttpDelete(url);
    } else {
        throw new RuntimeException("Provided HttpMethod not supported");
    }
    return executeRequest(request);
}

From source file:io.selendroid.server.BaseTest.java

License:Apache License

protected HttpResponse executeCreateSessionRequest() throws Exception {
    String url = "http://" + host + ":" + port + "/wd/hub/session";
    return executeRequestWithPayload(url, HttpMethod.POST, getCapabilityPayload().toString());
}

From source file:io.selendroid.server.FindElementHandlerTest.java

License:Apache License

public void assertThatFindElementResponseHasCorrectFormat() throws Exception {
    HttpResponse response = executeCreateSessionRequest();
    SelendroidAssert.assertResponseIsRedirect(response);
    JSONObject session = parseJsonResponse(response);
    String sessionId = session.getString("sessionId");
    Assert.assertFalse(sessionId.isEmpty());

    JSONObject payload = new JSONObject();
    payload.put("using", "id");
    payload.put("value", "my_button_bar");

    String url = "http://" + host + ":" + port + "/wd/hub/session/" + sessionId + "/element";
    HttpResponse element = executeRequestWithPayload(url, HttpMethod.POST, payload.toString());
    SelendroidAssert.assertResponseIsOk(element);
}

From source file:io.selendroid.server.GetStatusTest.java

License:Apache License

@Test
public void assertThatGetStatusHandlerIsNotRegisteredForPost() throws Exception {
    String url = "http://" + host + ":" + server.getPort() + "/wd/hub/status";
    HttpResponse response = executeRequest(url, HttpMethod.POST);
    SelendroidAssert.assertResponseIsResourceNotFound(response);
}

From source file:io.selendroid.server.handler.RequestRedirectHandler.java

License:Apache License

private JSONObject redirectRequest(HttpRequest request, ActiveSession session, String url, String method)
        throws Exception {

    HttpResponse r = null;//from w  w  w . j a v  a  2  s  .  c  o m
    if ("get".equalsIgnoreCase(method)) {
        log.info("GET redirect to: " + url);
        r = HttpClientUtil.executeRequest(url, HttpMethod.GET);
    } else if ("post".equalsIgnoreCase(method)) {
        log.info("POST redirect to: " + url);
        JSONObject payload = getPayload(request);
        log.info("Payload? " + payload);
        r = HttpClientUtil.executeRequestWithPayload(url, session.getSelendroidServerPort(), HttpMethod.POST,
                payload.toString());
    } else if ("delete".equalsIgnoreCase(method)) {
        log.info("DELETE redirect to: " + url);
        r = HttpClientUtil.executeRequest(url, HttpMethod.DELETE);
    } else {
        throw new SelendroidException("HTTP method not supported: " + method);
    }
    return HttpClientUtil.parseJsonResponse(r);
}