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

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

Introduction

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

Prototype

@Override
    public boolean equals(Object o) 

Source Link

Usage

From source file:io.crate.protocols.http.HttpBlobHandler.java

License:Apache License

private void handleBlobRequest(HttpRequest request, @Nullable HttpContent content) throws IOException {
    if (possibleRedirect(request, index, digest)) {
        reset();//w w w .j a  v  a2s .com
        return;
    }

    HttpMethod method = request.method();
    if (method.equals(HttpMethod.GET)) {
        get(request, index, digest);
        reset();
    } else if (method.equals(HttpMethod.HEAD)) {
        head(index, digest);
        reset();
    } else if (method.equals(HttpMethod.PUT)) {
        put(content, index, digest);
    } else if (method.equals(HttpMethod.DELETE)) {
        delete(index, digest);
        reset();
    } else {
        simpleResponse(HttpResponseStatus.METHOD_NOT_ALLOWED);
        reset();
    }
}

From source file:io.jsync.http.impl.DefaultHttpServerRequest.java

License:Open Source License

@Override
public HttpServerRequest expectMultiPart(boolean expect) {
    if (expect) {
        String contentType = request.headers().get(HttpHeaderNames.CONTENT_TYPE);
        if (contentType != null) {
            HttpMethod method = request.method();
            AsciiString lowerCaseContentType = new AsciiString(contentType.toLowerCase());
            boolean isURLEncoded = lowerCaseContentType
                    .startsWith(HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED);
            if ((lowerCaseContentType.startsWith(HttpHeaderValues.MULTIPART_FORM_DATA) || isURLEncoded)
                    && (method.equals(HttpMethod.POST) || method.equals(HttpMethod.PUT)
                            || method.equals(HttpMethod.PATCH))) {
                decoder = new HttpPostRequestDecoder(new DataFactory(), request);
            }/*from  w  w  w  .j  a v a  2  s  .c om*/
        }
    } else {
        decoder = null;
    }
    return this;
}

From source file:io.vertx.core.http.impl.HttpServerRequestImpl.java

License:Open Source License

@Override
public HttpServerRequest setExpectMultipart(boolean expect) {
    synchronized (conn) {
        checkEnded();// w  ww  . j av a  2 s .c o  m
        if (expect) {
            if (decoder == null) {
                String contentType = request.headers().get(HttpHeaders.Names.CONTENT_TYPE);
                if (contentType != null) {
                    HttpMethod method = request.getMethod();
                    String lowerCaseContentType = contentType.toLowerCase();
                    boolean isURLEncoded = lowerCaseContentType
                            .startsWith(HttpHeaders.Values.APPLICATION_X_WWW_FORM_URLENCODED);
                    if ((lowerCaseContentType.startsWith(HttpHeaders.Values.MULTIPART_FORM_DATA)
                            || isURLEncoded)
                            && (method.equals(HttpMethod.POST) || method.equals(HttpMethod.PUT)
                                    || method.equals(HttpMethod.PATCH) || method.equals(HttpMethod.DELETE))) {
                        decoder = new HttpPostRequestDecoder(
                                new NettyFileUploadDataFactory(conn.vertx(), this, () -> uploadHandler),
                                request);
                    }
                }
            }
        } else {
            decoder = null;
        }
        return this;
    }
}

From source file:net.oebs.jalos.netty.HttpHandler.java

License:Open Source License

private FullHttpResponse handleRequest(HttpRequest request) {

    FullHttpResponse response = null;/*from   w  w  w  .  j  a va2s . com*/
    String uri = request.getUri();

    // lookup Handler class by URL
    Class cls = null;
    for (Route route : routes) {
        if (uri.matches(route.path)) {
            cls = route.handler;
            break;
        }
    }

    // no matching handler == 404
    if (cls == null) {
        log.info("No handler match found for uri {}", uri);
        return notFound();
    }

    Handler h;
    try {
        h = (Handler) cls.newInstance();
    } catch (InstantiationException | IllegalAccessException ex) {
        return internalError();
    }

    Map<String, String> params = null;
    HttpMethod method = request.getMethod();

    // dispatch based on request method
    try {
        if (method.equals(HttpMethod.POST)) {
            HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(new DefaultHttpDataFactory(false),
                    request);
            params = httpDataToStringMap(decoder.getBodyHttpDatas());
            response = h.handlePost(uri, params);
        } else if (method.equals(HttpMethod.GET)) {
            params = new HashMap<>();
            response = h.handleGet(uri, params);
        } else {
            response = badRequest();
        }

    } catch (BadRequest ex) {
        response = badRequest();
    } catch (NotFound ex) {
        response = notFound();
    } catch (HandlerError ex) {
        response = internalError();
    }

    return response;
}

From source file:org.apache.activemq.artemis.core.remoting.impl.netty.HttpAcceptorHandler.java

License:Apache License

@Override
public void channelRead(final ChannelHandlerContext ctx, final Object msg) throws Exception {
    FullHttpRequest request = (FullHttpRequest) msg;
    HttpMethod method = request.getMethod();
    // if we are a post then we send upstream, otherwise we are just being prompted for a response.
    if (method.equals(HttpMethod.POST)) {
        ctx.fireChannelRead(ReferenceCountUtil.retain(((FullHttpRequest) msg).content()));
        // add a new response
        responses.put(new ResponseHolder(System.currentTimeMillis() + responseTime,
                new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK)));
        ReferenceCountUtil.release(msg);
        return;/* w  ww. j  a v  a 2s . c o  m*/
    }
    super.channelRead(ctx, msg);
}

From source file:org.janusgraph.graphdb.tinkerpop.gremlin.server.handler.HttpHMACAuthenticationHandler.java

License:Apache License

@Override
public void channelRead(final ChannelHandlerContext ctx, final Object msg) {
    if (msg instanceof FullHttpRequest) {
        final FullHttpRequest req = (FullHttpRequest) msg;
        final HttpMethod method = req.getMethod();
        final Map<String, String> credentialsMap = getCredentialsMap(ctx, req);
        if (credentialsMap == null) {
            sendError(ctx, msg);/*from   w ww .  ja va2  s . c  o m*/
            return;
        }
        if ("/session".equals(req.getUri()) && method.equals(HttpMethod.GET)) {
            try {
                credentialsMap.put(PROPERTY_GENERATE_TOKEN, "true");
                authenticator.authenticate(credentialsMap);
            } catch (Exception e) {
                sendError(ctx, msg);
                return;
            }
            replyWithToken(ctx, msg, credentialsMap.get(PROPERTY_TOKEN));
        } else {
            try {
                authenticator.authenticate(credentialsMap);
                ctx.fireChannelRead(req);
            } catch (Exception e) {
                sendError(ctx, msg);
                return;
            }
        }
    }
}

From source file:org.jooby.internal.netty.NettyRequest.java

License:Apache License

private Multimap<String, String> decodeParams() throws IOException {
    if (params == null) {
        params = ArrayListMultimap.create();
        files = ArrayListMultimap.create();

        query.parameters().forEach((name, values) -> values.forEach(value -> params.put(name, value)));

        HttpMethod method = req.method();
        boolean hasBody = method.equals(HttpMethod.POST) || method.equals(HttpMethod.PUT)
                || method.equals(HttpMethod.PATCH);
        boolean formLike = false;
        if (req.headers().contains("Content-Type")) {
            String contentType = req.headers().get("Content-Type").toLowerCase();
            formLike = (contentType.startsWith(MediaType.multipart.name())
                    || contentType.startsWith(MediaType.form.name()));
        }//from   w ww.  j a v  a2 s .  c  om
        if (hasBody && formLike) {
            HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(new DefaultHttpDataFactory(), req);
            try {
                Function<HttpPostRequestDecoder, Boolean> hasNext = it -> {
                    try {
                        return it.hasNext();
                    } catch (HttpPostRequestDecoder.EndOfDataDecoderException ex) {
                        return false;
                    }
                };
                while (hasNext.apply(decoder)) {
                    HttpData field = (HttpData) decoder.next();
                    try {
                        String name = field.getName();
                        if (field.getHttpDataType() == HttpDataType.FileUpload) {
                            files.put(name, new NettyUpload((FileUpload) field, tmpdir));
                        } else {
                            params.put(name, field.getString());
                        }
                    } finally {
                        field.release();
                    }
                }
            } finally {
                decoder.destroy();
            }
        }
    }
    return params;
}

From source file:org.mitre.svmp.webrtc.http.PeerConnectionServerHandler.java

License:Open Source License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

    //System.out.println(msg.getClass().getName());

    DefaultFullHttpResponse response = null;

    if (msg instanceof HttpRequest) {

        // All HTTP messages used by the peerconnection_client:
        ////from   w  w w. j  a  v  a  2  s .c o  m
        // "GET /sign_in?%s HTTP/1.0\r\n\r\n", client_name_.c_str());
        // "POST /message?peer_id=%i&to=%i HTTP/1.0\r\n"
        // "GET /sign_out?peer_id=%i HTTP/1.0\r\n\r\n", my_id_);
        // "GET /wait?peer_id=%i HTTP/1.0\r\n\r\n", my_id_);

        HttpRequest request = (HttpRequest) msg;

        System.out.println(request.toString());

        HttpMethod method = request.getMethod();
        String uri = request.getUri();
        //        QueryStringDecoder queryDecoder = new QueryStringDecoder(uri);
        //        Map<String, List<String>> params = queryDecoder.parameters();

        System.out.println("Incoming HTTP request:\n" + method + " " + uri);

        if (method.equals(HttpMethod.GET)) {

            // Case: GET /sign_in?%s
            if (uri.startsWith("/sign_in")) {
                // should have a username parameter and it should be "fbstreamer"
                String username = uri.split("[?]")[1];

                // return a comma delimited list of name,peer_id,is_connected
                // where the first line is the info of the peer we're talking to
                // and all subsequent lines are the set of other connected peers

                // since fbstreamer on the HTTP side will always be the first peer to connect
                // lie about the other peer being there

                ByteBuf content = copiedBuffer(
                        username + "," + FBSTREAM_PEER_ID + ",1\n" + "svmpclient," + CLIENT_PEER_ID + ",1",
                        CharsetUtil.US_ASCII);
                response = new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.OK, content);

                response.headers().set(CONTENT_TYPE, "text/plain");
                response.headers().set(CONNECTION, "close");
                response.headers().set(PRAGMA, FBSTREAM_PEER_ID);

                // do we have to set the content-length header ourselves?
                // if so, how do we calculate it?
            }

            // Case: GET /sign_out?peer_id=%i
            else if (uri.startsWith("/sign_out")) {
                // TODO might be worth checking that the peer_id is actually FBSTREAM_PEER_ID
                // doesn't really matter though

                response = new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.OK);
                response.headers().set(CONNECTION, "close");
                response.headers().set(CONTENT_LENGTH, 0);

                // create a BYE message, no other peers to notify
                // add it to the sendQueue
                // TODO
            }

            // Case: GET /wait?peer_id=%i
            else if (uri.startsWith("/wait")) {
                // pull something off receiveQueue
                SVMPProtocol.Request pbMsg = receiveQueue.take();

                // convert it to JSON
                String json = Translator.ProtobufToJSON(pbMsg.getWebrtcMsg());
                ByteBuf content = copiedBuffer(json, CharsetUtil.US_ASCII);

                System.out.println("JSON from client:\n" + json);

                // make HTTP response
                response = new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.OK, content);
                response.headers().set(CONTENT_TYPE, "text/plain");

                // if it's a peer presence info message, set pragma to the client's ID
                // function handleServerNotification(data) {(String)
                //   trace("Server notification: " + data);
                //   var parsed = data.split(',');
                //   if (parseInt(parsed[2]) != 0)
                //   other_peers[parseInt(parsed[1])] = parsed[0];
                // }

                // shouldn't be any presence messages since we're hard coding
                // it above in the sign_in case

                // if it's a message from another peer, set pragma to that peer's ID
                response.headers().set(PRAGMA, CLIENT_PEER_ID);
                // peerconnection_server appears to set "Connection: close" for these
                response.headers().set(CONNECTION, "close");

                // send in HTTP response
                response = null;
            } else {
                // some other bad URL
                response = new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.NOT_FOUND);
            }

            // send response
            System.out.println("HTTP response:\n" + response.toString());
            ctx.write(response).addListener(ChannelFutureListener.CLOSE);

        } else if (method.equals(HttpMethod.POST) && uri.startsWith("/message")) {
            // Case: POST /message?peer_id=%i&to=%i

            isProcessingPost = true;

        } else {
            // some other HTTP request we don't support
            // send an error or something
            response = new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.BAD_REQUEST);

            // send response
            System.out.println("HTTP response:\n" + response.toString());
            ctx.write(response).addListener(ChannelFutureListener.CLOSE);
        }
    } else if (msg instanceof HttpContent && isProcessingPost) {
        // handle body of the POST /message

        // decode the JSON payload
        String content = ((HttpContent) msg).content().toString(CharsetUtil.UTF_8);
        System.out.println("JSON in from fbstream:\n" + content);

        // convert it to protobuf
        SVMPProtocol.Response.Builder pbResp = SVMPProtocol.Response.newBuilder();
        pbResp.setType(ResponseType.WEBRTC);
        pbResp.setWebrtcMsg(Translator.JSONToProtobuf(content));

        // add it to sendQueue
        sendQueue.add(pbResp.build());

        // generate appropriate HTTP response
        response = new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.OK);
        response.headers().set(CONNECTION, "close");

        // send response
        System.out.println("HTTP response:\n" + response.toString());
        ctx.write(response).addListener(ChannelFutureListener.CLOSE);

        isProcessingPost = false;
    }
}

From source file:org.nosceon.titanite.HttpServerHandler.java

License:Apache License

private BodyParser newBodyParser(RoutingResult routing, HttpRequest request) {
    HttpMethod method = request.getMethod();
    if (method.equals(HttpMethod.POST) || method.equals(HttpMethod.PUT) || method.equals(HttpMethod.PATCH)) {

        if (routing.bodyParser() != null) {
            BodyParser bp = routing.bodyParser().get();
            if (bp != null) {
                return bp;
            }/*w  w w .j  a  va  2  s  .  com*/
        }

        String contentType = request.headers().get(HttpHeaders.Names.CONTENT_TYPE);

        if (contentType != null) {
            String lowerCaseContentType = contentType.toLowerCase();
            boolean isURLEncoded = lowerCaseContentType
                    .startsWith(HttpHeaders.Values.APPLICATION_X_WWW_FORM_URLENCODED);
            boolean isMultiPart = lowerCaseContentType.startsWith(HttpHeaders.Values.MULTIPART_FORM_DATA);

            if (isMultiPart) {
                return new FormParamsBodyParser(maxMultipartRequestSize);
            }

            if (isURLEncoded) {
                return new FormParamsBodyParser(maxRequestSize);
            }
        }

        return new RawBodyParser(maxRequestSize);
    } else {
        return new EmptyBodyParser();
    }
}

From source file:org.vertx.java.core.http.impl.DefaultHttpServerRequest.java

License:Open Source License

@Override
public HttpServerRequest expectMultiPart(boolean expect) {
    if (expect) {
        String contentType = request.headers().get(HttpHeaders.Names.CONTENT_TYPE);
        if (contentType != null) {
            HttpMethod method = request.getMethod();
            String lowerCaseContentType = contentType.toLowerCase();
            isURLEncoded = lowerCaseContentType
                    .startsWith(HttpHeaders.Values.APPLICATION_X_WWW_FORM_URLENCODED);
            if ((lowerCaseContentType.startsWith(HttpHeaders.Values.MULTIPART_FORM_DATA) || isURLEncoded)
                    && (method.equals(HttpMethod.POST) || method.equals(HttpMethod.PUT)
                            || method.equals(HttpMethod.PATCH))) {
                decoder = new HttpPostRequestDecoder(new DataFactory(), request);
            }/*from   ww  w.  j a  v a  2 s .co m*/
        }
    } else {
        decoder = null;
    }
    return this;
}