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:gribbit.http.request.decoder.HttpRequestDecoder.java

License:Open Source License

/** Decode an HTTP message. */
@Override/*from  w  ww.  ja v  a2s . c om*/
public void messageReceived(ChannelHandlerContext ctx, Object msg) {
    try {
        Log.info("Got message of type " + msg.getClass().getName());
        if (msg instanceof HttpRequest) {
            // Got a new HTTP request -- decode HTTP headers
            HttpRequest httpReq = (HttpRequest) msg;
            if (!httpReq.decoderResult().isSuccess()) {
                throw new BadRequestException(null);
            }

            // Free resources to avoid DoS attack by sending repeated unterminated requests
            freeResources();

            // Parse the HttpRequest fields. 
            request = new Request(ctx, httpReq);

            // Handle expect-100-continue
            List<CharSequence> allExpectHeaders = httpReq.headers().getAll(EXPECT);
            for (int i = 0; i < allExpectHeaders.size(); i++) {
                String h = allExpectHeaders.get(i).toString();
                if (h.equalsIgnoreCase("100-continue")) {
                    ctx.writeAndFlush(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
                            HttpResponseStatus.CONTINUE, Unpooled.EMPTY_BUFFER));
                    return;
                }
            }

            if (httpReq.method() == HttpMethod.POST) {
                // Start decoding HttpContent chunks.
                freeResources();
                postRequestDecoder = new HttpPostRequestDecoder(httpDataFactory, httpReq);
            }

        }
        if (msg instanceof LastHttpContent || msg instanceof FullHttpRequest) {
            // Reached end of HTTP request

            if (request != null) {
                // Check for WebSocket upgrade request
                if (!tryWebSocketHandlers(ctx, request.getHttpRequest())) {
                    // This is a regular HTTP request -- find a handler for the request
                    tryHttpRequestHandlers(ctx);
                }
                // After the last content message has been processed, free resources
                freeResources();
            }

        } else if (msg instanceof HttpContent) {
            // Decode HTTP POST body
            HttpContent chunk = (HttpContent) msg;
            if (!chunk.decoderResult().isSuccess()) {
                throw new BadRequestException(null);
            }
            handlePOSTChunk(chunk);

        } else if (msg instanceof WebSocketFrame) {
            // Handle WebSocket frame
            if (webSocketHandler == null) {
                // Connection was never upgraded to websocket
                throw new BadRequestException();
            }
            WebSocketFrame frame = (WebSocketFrame) msg;
            handleWebSocketFrame(ctx, frame);
        }
    } catch (Exception e) {
        exceptionCaught(ctx, e);
    }
}

From source file:inn.eatery.clientTest.ClientHandler.java

License:Apache License

private void getEvents(Channel ch) {
    StringWriter estr = new StringWriter();
    getEvents.write(estr);//  ww  w  . j a  v  a2  s .c  om
    String contentStr = estr.toString();

    FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/events/find",
            ch.alloc().buffer().writeBytes(contentStr.getBytes()));

    request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
    request.headers().set(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP);
    request.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/json");

    request.headers().set(HttpHeaders.Names.CONTENT_LENGTH, request.content().readableBytes());
    // Set some example cookies.
    request.headers().set(HttpHeaders.Names.COOKIE, ClientCookieEncoder
            .encode(new DefaultCookie("userId", "ahanda"), new DefaultCookie("sessStart", "kal")));

    l.info("getevents bytes {}", request.content().readableBytes());

    // Send the HTTP request.
    ch.writeAndFlush(request);
}

From source file:inn.eatery.clientTest.ClientHandler.java

License:Apache License

public static void pubEvent(Channel ch, JSONObject e) {
    // Prepare the HTTP request.
    JSONArray es = new JSONArray();
    es.put(e);/*w ww  .j a v  a  2 s.co m*/

    StringWriter estr = new StringWriter();
    es.write(estr);
    String contentStr = estr.toString();
    FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/events",
            ch.alloc().buffer().writeBytes(contentStr.getBytes()));

    request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
    request.headers().set(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP);
    request.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/json");

    request.headers().set(HttpHeaders.Names.CONTENT_LENGTH, request.content().readableBytes());
    // Set some example cookies.
    request.headers().set(HttpHeaders.Names.COOKIE, ClientCookieEncoder
            .encode(new DefaultCookie("userId", "ahanda"), new DefaultCookie("sessStart", "kal")));

    l.info("readable bytes {}", request.content().readableBytes());

    // Send the HTTP request.
    ch.writeAndFlush(request);
}

From source file:inn.eatery.server.ServerHandler.java

License:Apache License

FullHttpResponse handleRequest(ChannelHandlerContext ctx, FullHttpRequest req) {
    QueryStringDecoder resource = new QueryStringDecoder(req.uri());
    String contentStr = req.content().toString(CharsetUtil.UTF_8);

    if (resource.path().equals("/login")) {
        JSONObject content = new JSONObject(contentStr);
        userId = content.getString("userId");
        authorized = true;/*from w ww.  j  a  v a 2  s .  c o m*/
    }

    if (!authorized) {
        return unauthAccess;
    }

    try {
        dbMgr = MongoDBManager.getInstance();
    } catch (Exception e) {
        l.error("Could not get DB Manager {} {}", e.getStackTrace(), e.getMessage());
    }
    if (resource.path().equals("/events")) {
        if (req.method() == HttpMethod.POST) {
            JSONArray events = new JSONArray(contentStr);
            for (int i = 0, size = events.length(); i < size; i++) {
                dbMgr.insertEvent(events.getJSONObject(i));
                l.info("Handled event publish !! {}", events.getJSONObject(i));
            }
        } else { // get events
            JSONObject dbquery = new JSONObject(contentStr);
        }
    }
    return null;
}

From source file:io.advantageous.conekt.http.impl.HttpClientImpl.java

License:Open Source License

@Override
public HttpClientRequest post(int port, String host, String requestURI) {
    return request(io.advantageous.conekt.http.HttpMethod.POST, port, host, requestURI);
}

From source file:io.advantageous.conekt.http.impl.HttpClientImpl.java

License:Open Source License

@Override
public HttpClientRequest post(int port, String host, String requestURI,
        Handler<HttpClientResponse> responseHandler) {
    return request(io.advantageous.conekt.http.HttpMethod.POST, port, host, requestURI, responseHandler);
}

From source file:io.advantageous.conekt.http.impl.HttpClientImpl.java

License:Open Source License

@Override
public HttpClientRequest post(String requestURI) {
    return request(io.advantageous.conekt.http.HttpMethod.POST, requestURI);
}

From source file:io.advantageous.conekt.http.impl.HttpClientImpl.java

License:Open Source License

@Override
public HttpClientRequest post(String requestURI, Handler<HttpClientResponse> responseHandler) {
    return request(io.advantageous.conekt.http.HttpMethod.POST, requestURI, responseHandler);
}

From source file:io.advantageous.conekt.http.impl.HttpClientImpl.java

License:Open Source License

@Override
public HttpClientRequest postAbs(String absoluteURI) {
    return requestAbs(io.advantageous.conekt.http.HttpMethod.POST, absoluteURI);
}

From source file:io.advantageous.conekt.http.impl.HttpClientImpl.java

License:Open Source License

@Override
public HttpClientRequest postAbs(String absoluteURI, Handler<HttpClientResponse> responseHandler) {
    return requestAbs(io.advantageous.conekt.http.HttpMethod.POST, absoluteURI, responseHandler);
}