Example usage for io.netty.handler.codec.http HttpRequest getMethod

List of usage examples for io.netty.handler.codec.http HttpRequest getMethod

Introduction

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

Prototype

@Deprecated
HttpMethod getMethod();

Source Link

Usage

From source file:br.unifei.edu.eco009.steamlansync.proxy.SteamActivityTracker.java

public void requestSentToServer(FullFlowContext flowContext, HttpRequest httpRequest) {
    if (httpRequest.getMethod().equals(HttpMethod.GET)
            && httpRequest.getUri().contains("steampowered.com/depot/")) {
        System.out.println(httpRequest.getUri());
        System.out.println(UriParser.getAppId(httpRequest));
    }//  w w w.  ja  va 2 s  . c  o  m

}

From source file:br.unifei.edu.eco009.steamlansync.proxy.SteamDepotFilter.java

public HttpFilters filterRequest(HttpRequest originalRequest, ChannelHandlerContext ctx) {
    if (originalRequest.getMethod().equals(HttpMethod.GET)
            && originalRequest.getUri().contains("steampowered.com/depot/")
            && originalRequest.getUri().contains("chunk")) {

        // alterar o precedimento da
        // request:
        return new HttpFiltersAdapter(originalRequest) {
            boolean store = false;
            String chunkId = UriParser.getChunkId(originalRequest);
            HttpChunkContents cachedChunk;

            @Override//  w ww  .  j  a v a 2  s .c  o  m
            public HttpResponse clientToProxyRequest(HttpObject httpObject) {
                //                        return chunks.get(getChunkId(originalRequest)).copy().retain();
                cachedChunk = SteamCache.getChunk(chunkId);
                if (cachedChunk == null) {
                    store = true;
                    System.out.println("cache MISS");
                    return super.clientToProxyRequest(httpObject);
                }
                System.out.println("cache HIT");
                return cachedChunk.getFullHttpResponse().retain();

            }

            @Override
            public HttpObject proxyToClientResponse(HttpObject httpObject) {
                FullHttpResponse resp = (FullHttpResponse) super.proxyToClientResponse(httpObject);
                //                        chunks.put(getChunkId(originalRequest), resp.copy());
                if (store) {
                    SteamCache.putChunk(chunkId, new HttpChunkContents(resp));
                }
                return resp;
            }

        };
    }
    return null;
}

From source file:cf.component.VcapComponent.java

License:Open Source License

public VcapComponent(CfNats nats, SimpleHttpServer httpServer, String type, List<VarzUpdater> varzUpdaters) {
    this.nats = nats;
    this.type = type;
    this.varzUpdaters = varzUpdaters;

    nats.subscribe(ComponentDiscover.class, new PublicationHandler<ComponentDiscover, ComponentAnnounce>() {
        @Override//from   www  . j  a v  a 2 s  . co  m
        public void onMessage(Publication<ComponentDiscover, ComponentAnnounce> publication) {
            publication.reply(buildComponentAnnounce());
        }
    });

    nats.publish(buildComponentAnnounce());

    httpServer.addHandler(Pattern.compile("/healthz"), new AuthenticatedJsonTextResponseRequestHandler() {
        @Override
        public String handleAuthenticatedRequest(HttpRequest request, Matcher uriMatcher, ByteBuf body)
                throws RequestException {
            if (!request.getMethod().equals(HttpMethod.GET)) {
                throw new RequestException(HttpResponseStatus.METHOD_NOT_ALLOWED);
            }
            return "ok\n";
        }
    });

    httpServer.addHandler(Pattern.compile("/varz"), new AuthenticatedJsonTextResponseRequestHandler() {
        @Override
        protected String handleAuthenticatedRequest(HttpRequest request, Matcher uriMatcher, ByteBuf body)
                throws RequestException {
            if (!request.getMethod().equals(HttpMethod.GET)) {
                throw new RequestException(HttpResponseStatus.METHOD_NOT_ALLOWED);
            }
            final Varz varz = buildVarz();
            try {
                return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(varz);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    });
}

From source file:cf.service.NettyBrokerServer.java

License:Open Source License

public NettyBrokerServer(SimpleHttpServer server, Provisioner provisioner, String authToken) {
    super(provisioner, authToken);
    server.addHandler(Pattern.compile("/+gateway/v1/configurations/(.*?)/handles(/(.*))?"),
            new RequestHandler() {
                @Override//from   w w  w. j  ava2  s .com
                public HttpResponse handleRequest(HttpRequest request, Matcher uriMatcher, ByteBuf body)
                        throws RequestException {
                    validateAuthToken(request);
                    // Bind service
                    if (request.getMethod() == HttpMethod.POST) {
                        final BindRequest bindRequest = decode(BindRequest.class, body);
                        final BindResponse bindResponse = bindService(bindRequest);
                        return encodeResponse(bindResponse);
                    }
                    // Unbind service
                    if (request.getMethod() == HttpMethod.DELETE) {
                        if (uriMatcher.groupCount() != 3) {
                            throw new RequestException(HttpResponseStatus.NOT_FOUND);
                        }
                        final String serviceInstanceId = uriMatcher.group(1);
                        final String handleId = uriMatcher.group(3);
                        unbindService(serviceInstanceId, handleId);
                        return encodeResponse(EMPTY_JSON_OBJECT);
                    }
                    throw new RequestException(HttpResponseStatus.METHOD_NOT_ALLOWED);
                }
            });
    server.addHandler(Pattern.compile("/+gateway/v1/configurations(/(.*))?"), new RequestHandler() {
        @Override
        public HttpResponse handleRequest(HttpRequest request, Matcher uriMatcher, ByteBuf body)
                throws RequestException {
            validateAuthToken(request);
            // Create service
            if (request.getMethod() == HttpMethod.POST) {
                final CreateRequest createRequest = decode(CreateRequest.class, body);
                final CreateResponse createResponse = createService(createRequest);
                return encodeResponse(createResponse);
            }
            // Delete service
            if (request.getMethod() == HttpMethod.DELETE) {
                if (uriMatcher.groupCount() != 2) {
                    throw new RequestException(HttpResponseStatus.NOT_FOUND);
                }
                final String serviceInstanceId = uriMatcher.group(2);
                deleteService(serviceInstanceId);
                return encodeResponse(EMPTY_JSON_OBJECT);
            }
            throw new RequestException(HttpResponseStatus.METHOD_NOT_ALLOWED);
        }
    });
}

From source file:co.freeside.betamax.proxy.netty.PredicatedHttpFilters.java

License:Apache License

public static Predicate<HttpRequest> httpMethodPredicate(final HttpMethod method) {
    return new Predicate<HttpRequest>() {
        @Override/*from w  ww .  j  a va  2 s  . co m*/
        public boolean apply(HttpRequest input) {
            return method.equals(input.getMethod());
        }
    };
}

From source file:com.alibaba.dubbo.qos.command.decoder.HttpCommandDecoder.java

License:Apache License

public static CommandContext decode(HttpRequest request) {
    CommandContext commandContext = null;
    if (request != null) {
        QueryStringDecoder queryStringDecoder = new QueryStringDecoder(request.getUri());
        String path = queryStringDecoder.path();
        String[] array = path.split("/");
        if (array.length == 2) {
            String name = array[1];

            // process GET request and POST request separately. Check url for GET, and check body for POST
            if (request.getMethod() == HttpMethod.GET) {
                if (queryStringDecoder.parameters().isEmpty()) {
                    commandContext = CommandContextFactory.newInstance(name);
                    commandContext.setHttp(true);
                } else {
                    List<String> valueList = new ArrayList<String>();
                    for (List<String> values : queryStringDecoder.parameters().values()) {
                        valueList.addAll(values);
                    }//  ww w  . java 2 s.c  o  m
                    commandContext = CommandContextFactory.newInstance(name, valueList.toArray(new String[] {}),
                            true);
                }
            } else if (request.getMethod() == HttpMethod.POST) {
                HttpPostRequestDecoder httpPostRequestDecoder = new HttpPostRequestDecoder(request);
                List<String> valueList = new ArrayList<String>();
                for (InterfaceHttpData interfaceHttpData : httpPostRequestDecoder.getBodyHttpDatas()) {
                    if (interfaceHttpData.getHttpDataType() == InterfaceHttpData.HttpDataType.Attribute) {
                        Attribute attribute = (Attribute) interfaceHttpData;
                        try {
                            valueList.add(attribute.getValue());
                        } catch (IOException ex) {
                            throw new RuntimeException(ex);
                        }
                    }
                }
                if (valueList.isEmpty()) {
                    commandContext = CommandContextFactory.newInstance(name);
                    commandContext.setHttp(true);
                } else {
                    commandContext = CommandContextFactory.newInstance(name, valueList.toArray(new String[] {}),
                            true);
                }
            }
        }
    }

    return commandContext;
}

From source file:com.buildria.mocking.stub.Call.java

License:Open Source License

public static Call fromRequest(HttpRequest req) {
    Objects.requireNonNull(req);//w  ww .j a va  2 s .c om
    Call call = new Call();

    call.method = req.getMethod().name();
    QueryStringDecoder decoder = new QueryStringDecoder(req.getUri());
    call.path = QueryStringDecoder.decodeComponent(decoder.path());

    Map<String, List<String>> params = decoder.parameters();
    for (String name : params.keySet()) {
        List<String> values = params.get(name);
        for (String value : values) {
            call.parameters.add(new Pair(name, value));
        }
    }

    HttpHeaders headers = req.headers();
    for (String name : headers.names()) {
        List<String> values = headers.getAll(name);
        for (String value : values) {
            call.headers.add(new Pair(name, value));
        }
        if (CONTENT_TYPE.equalsIgnoreCase(name)) {
            call.contentType = MediaType.parse(headers.get(CONTENT_TYPE));
        }
    }

    if (req instanceof ByteBufHolder) {
        ByteBuf buf = ((ByteBufHolder) req).content();
        if (buf != null) {
            call.body = new byte[buf.readableBytes()];
            buf.readBytes(call.body);
        }
    }

    return call;
}

From source file:com.buildria.mocking.stub.CallTest.java

License:Open Source License

@Test
public void testFromRequestNoContent() throws Exception {
    HttpRequest req = mock(HttpRequest.class);

    when(req.getUri()).thenReturn("/api/p?name=%E3%81%82");
    when(req.getMethod()).thenReturn(GET);

    HttpHeaders headers = new DefaultHttpHeaders();
    headers.add("key", "value");
    when(req.headers()).thenReturn(headers);

    Call call = Call.fromRequest(req);// w  w  w  . ja v  a  2  s .c  o  m

    assertThat(call.getBody().length, is(0));
}

From source file:com.chenyang.proxy.http.HttpSchemaHandler.java

License:Apache License

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

    if (msg instanceof HttpRequest) {
        HttpRequest httpRequest = (HttpRequest) msg;

        String originalHost = HostNamePortUtil.getHostName(httpRequest);
        int originalPort = HostNamePortUtil.getPort(httpRequest);
        HttpRemote apnProxyRemote = new HttpRemote(originalHost, originalPort);

        if (!HostAuthenticationUtil.isValidAddress(apnProxyRemote.getInetSocketAddress())) {
            HttpErrorUtil.writeAndFlush(uaChannelCtx.channel(), HttpResponseStatus.FORBIDDEN);
            return;
        }//www.  jav  a 2  s .c  om

        Channel uaChannel = uaChannelCtx.channel();

        HttpConnectionAttribute apnProxyConnectionAttribute = HttpConnectionAttribute.build(
                uaChannel.remoteAddress().toString(), httpRequest.getMethod().name(), httpRequest.getUri(),
                httpRequest.getProtocolVersion().text(),
                httpRequest.headers().get(HttpHeaders.Names.USER_AGENT), apnProxyRemote);

        uaChannelCtx.attr(HttpConnectionAttribute.ATTRIBUTE_KEY).set(apnProxyConnectionAttribute);
        uaChannel.attr(HttpConnectionAttribute.ATTRIBUTE_KEY).set(apnProxyConnectionAttribute);

        if (httpRequest.getMethod().equals(HttpMethod.CONNECT)) {
            if (uaChannelCtx.pipeline().get(HttpUserAgentForwardHandler.HANDLER_NAME) != null) {
                uaChannelCtx.pipeline().remove(HttpUserAgentForwardHandler.HANDLER_NAME);
            }
            if (uaChannelCtx.pipeline().get(HttpUserAgentTunnelHandler.HANDLER_NAME) == null) {
                uaChannelCtx.pipeline().addLast(HttpUserAgentTunnelHandler.HANDLER_NAME,
                        new HttpUserAgentTunnelHandler());
            }
        } else {
            if (uaChannelCtx.pipeline().get(HttpUserAgentForwardHandler.HANDLER_NAME) == null) {
                uaChannelCtx.pipeline().addLast(HttpUserAgentForwardHandler.HANDLER_NAME,
                        new HttpUserAgentForwardHandler());
            }
        }
    }

    uaChannelCtx.fireChannelRead(msg);
}

From source file:com.chenyang.proxy.http.HttpUserAgentForwardHandler.java

License:Apache License

private HttpRequest constructRequestForProxy(HttpRequest httpRequest, HttpRemote apnProxyRemote) {

    String uri = httpRequest.getUri();
    uri = this.getPartialUrl(uri);
    HttpRequest _httpRequest = new DefaultHttpRequest(httpRequest.getProtocolVersion(), httpRequest.getMethod(),
            uri);//  w  w  w  .  ja  v  a  2  s.c om
    Set<String> headerNames = httpRequest.headers().names();
    for (String headerName : headerNames) {
        if (StringUtils.equalsIgnoreCase(headerName, "Proxy-Connection")) {
            _httpRequest.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
        } else {
            _httpRequest.headers().add(headerName, httpRequest.headers().getAll(headerName));
        }
    }
    Iterator<Entry<String, String>> iterator = _httpRequest.headers().iterator();
    while (iterator.hasNext()) {
        Entry<String, String> entry = iterator.next();
        logger.info(" heard : {} {}", entry.getKey(), entry.getValue());
    }
    return _httpRequest;
}