List of usage examples for io.netty.handler.codec.http HttpRequest setMethod
HttpRequest setMethod(HttpMethod method);
From source file:com.digisky.innerproxy.testclient.HttpSnoopClient.java
License:Apache License
public static void test(Channel ch, String uri, String sjson) { HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/"); request.headers().set(HttpHeaders.Names.HOST, "127.0.0.1"); request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE); request.headers().set(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP); request.setMethod(HttpMethod.POST); request.setUri("/" + uri); HttpPostRequestEncoder bodyRequestEncoder = null; try {/*w ww . j av a 2 s.c om*/ bodyRequestEncoder = new HttpPostRequestEncoder(request, false); } catch (ErrorDataEncoderException e) { // TODO Auto-generated catch block e.printStackTrace(); } // false => not multipart //*********************************************************************** try { bodyRequestEncoder.addBodyAttribute("val", sjson); } catch (ErrorDataEncoderException e) { // TODO Auto-generated catch block e.printStackTrace(); } //*********************************************************************** try { request = bodyRequestEncoder.finalizeRequest(); } catch (ErrorDataEncoderException e) { // TODO Auto-generated catch block e.printStackTrace(); } ch.writeAndFlush(request); try { ch.closeFuture().sync(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:com.digisky.outerproxy.testclient.HttpSnoopClient.java
License:Apache License
public static void testWithEncode(Channel ch, String uri, String sjson) { HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/"); request.headers().set(HttpHeaders.Names.HOST, "127.0.0.1"); request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE); request.headers().set(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP); request.setMethod(HttpMethod.POST); request.setUri("/" + uri); HttpPostRequestEncoder bodyRequestEncoder = null; try {/*from w ww. j a v a2 s .c o m*/ bodyRequestEncoder = new HttpPostRequestEncoder(request, false); } catch (ErrorDataEncoderException e) { // TODO Auto-generated catch block e.printStackTrace(); } // false => not multipart //*********************************************************************** ByteBuf b = Unpooled.buffer(); b.writeBytes("{}".getBytes()); try { bodyRequestEncoder.addBodyAttribute("val", sjson); } catch (ErrorDataEncoderException e) { // TODO Auto-generated catch block e.printStackTrace(); } //*********************************************************************** try { request = bodyRequestEncoder.finalizeRequest(); } catch (ErrorDataEncoderException e) { // TODO Auto-generated catch block e.printStackTrace(); } ch.writeAndFlush(request); try { ch.closeFuture().sync(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:com.litgh.RouterTest.java
License:Open Source License
public void testRouterAPI() { Router router = new Router(); final Map<String, Boolean> test = new HashMap<String, Boolean>(); router.GET("/GET", new Handler() { public void handle(HttpRequest req, FullHttpResponse resp, List<Param> params) { test.put("GET", true); }/*from www . j a va2 s .c o m*/ }); router.POST("/POST", new Handler() { public void handle(HttpRequest req, FullHttpResponse resp, List<Param> params) { test.put("POST", true); } }); router.PUT("/PUT", new Handler() { public void handle(HttpRequest req, FullHttpResponse resp, List<Param> params) { test.put("PUT", true); } }); router.DELETE("/DELETE", new Handler() { public void handle(HttpRequest req, FullHttpResponse resp, List<Param> params) { test.put("DELETE", true); } }); router.HEAD("/HEAD", new Handler() { public void handle(HttpRequest req, FullHttpResponse resp, List<Param> params) { test.put("HEAD", true); } }); router.PATCH("/PATCH", new Handler() { public void handle(HttpRequest req, FullHttpResponse resp, List<Param> params) { test.put("PATCH", true); } }); HttpRequest req = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/GET"); FullHttpResponse resp = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); router.serverHttp(req, resp); req.setMethod(HttpMethod.POST); req.setUri("/POST"); router.serverHttp(req, resp); req.setMethod(HttpMethod.PUT); req.setUri("/PUT"); router.serverHttp(req, resp); req.setMethod(HttpMethod.DELETE); req.setUri("/DELETE"); router.serverHttp(req, resp); req.setMethod(HttpMethod.HEAD); req.setUri("/HEAD"); router.serverHttp(req, resp); req.setMethod(HttpMethod.PATCH); req.setUri("/PATCH"); router.serverHttp(req, resp); assertEquals("routing GET failed", Boolean.TRUE, test.get("GET")); assertEquals("routing POST failed", Boolean.TRUE, test.get("POST")); assertEquals("routing PUT failed", Boolean.TRUE, test.get("PUT")); assertEquals("routing DELETE failed", Boolean.TRUE, test.get("DELETE")); assertEquals("routing HEAD failed", Boolean.TRUE, test.get("HEAD")); assertEquals("routing PATCH failed", Boolean.TRUE, test.get("PATCH")); }
From source file:io.reactivex.netty.protocol.http.client.DefaultRedirectHandler.java
License:Apache License
protected HttpClientRequest<I> createRedirectRequest(HttpClientRequest<I> original, URI redirectLocation, int redirectStatus) { HttpRequest nettyRequest = original.getNettyRequest(); nettyRequest.setUri(getNettyRequestUri(redirectLocation)); HttpClientRequest<I> newRequest = new HttpClientRequest<I>(nettyRequest, original); if (redirectStatus == 303) { // according to HTTP spec, 303 mandates the change of request type to GET nettyRequest.setMethod(HttpMethod.GET); // If it is a get, then the content is not to be sent. newRequest.removeContent();// w w w . j a v a 2 s . c o m } return newRequest; }
From source file:io.reactivex.netty.protocol.http.client.FollowRedirectHttpClientImpl.java
License:Apache License
private static <I> HttpClientRequest<I> createRedirectRequest(HttpClientRequest<I> original, String newURI, int statusCode) { HttpRequest nettyRequest = original.getNettyRequest(); nettyRequest.setUri(newURI);/*from w w w . j ava 2 s . c o m*/ if (statusCode == 303) { // according to HTTP spec, 303 mandates the change of request type to GET nettyRequest.setMethod(HttpMethod.GET); } HttpClientRequest<I> newRequest = new HttpClientRequest<I>(nettyRequest); if (statusCode != 303) { // if status code is 303, we can just leave the content factory to be null newRequest.contentFactory = original.contentFactory; newRequest.rawContentFactory = original.rawContentFactory; } return newRequest; }
From source file:org.apache.camel.component.netty4.http.DefaultNettyHttpBinding.java
License:Apache License
@Override public HttpRequest toNettyRequest(Message message, String uri, NettyHttpConfiguration configuration) throws Exception { LOG.trace("toNettyRequest: {}", message); // the message body may already be a Netty HTTP response if (message.getBody() instanceof HttpRequest) { return (HttpRequest) message.getBody(); }// w w w . java2s .c o m // just assume GET for now, we will later change that to the actual method to use HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uri); Object body = message.getBody(); if (body != null) { // support bodies as native Netty ByteBuf buffer; if (body instanceof ByteBuf) { buffer = (ByteBuf) body; } else { // try to convert to buffer first buffer = message.getBody(ByteBuf.class); if (buffer == null) { // fallback to byte array as last resort byte[] data = message.getMandatoryBody(byte[].class); buffer = NettyConverter.toByteBuffer(data); } } if (buffer != null) { request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, uri, buffer); int len = buffer.readableBytes(); // set content-length request.headers().set(HttpHeaders.Names.CONTENT_LENGTH, len); LOG.trace("Content-Length: {}", len); } else { // we do not support this kind of body throw new NoTypeConversionAvailableException(body, ByteBuf.class); } } // update HTTP method accordingly as we know if we have a body or not HttpMethod method = NettyHttpHelper.createMethod(message, body != null); request.setMethod(method); TypeConverter tc = message.getExchange().getContext().getTypeConverter(); // if we bridge endpoint then we need to skip matching headers with the HTTP_QUERY to avoid sending // duplicated headers to the receiver, so use this skipRequestHeaders as the list of headers to skip Map<String, Object> skipRequestHeaders = null; if (configuration.isBridgeEndpoint()) { String queryString = message.getHeader(Exchange.HTTP_QUERY, String.class); if (queryString != null) { skipRequestHeaders = URISupport.parseQuery(queryString, false, true); } // Need to remove the Host key as it should be not used message.getHeaders().remove("host"); } // append headers // must use entrySet to ensure case of keys is preserved for (Map.Entry<String, Object> entry : message.getHeaders().entrySet()) { String key = entry.getKey(); Object value = entry.getValue(); // we should not add headers for the parameters in the uri if we bridge the endpoint // as then we would duplicate headers on both the endpoint uri, and in HTTP headers as well if (skipRequestHeaders != null && skipRequestHeaders.containsKey(key)) { continue; } // use an iterator as there can be multiple values. (must not use a delimiter) final Iterator<?> it = ObjectHelper.createIterator(value, null, true); while (it.hasNext()) { String headerValue = tc.convertTo(String.class, it.next()); if (headerValue != null && headerFilterStrategy != null && !headerFilterStrategy .applyFilterToCamelHeaders(key, headerValue, message.getExchange())) { LOG.trace("HTTP-Header: {}={}", key, headerValue); request.headers().add(key, headerValue); } } } // set the content type in the response. String contentType = MessageHelper.getContentType(message); if (contentType != null) { // set content-type request.headers().set(HttpHeaders.Names.CONTENT_TYPE, contentType); LOG.trace("Content-Type: {}", contentType); } // must include HOST header as required by HTTP 1.1 // use URI as its faster than URL (no DNS lookup) URI u = new URI(uri); String host = u.getHost(); request.headers().set(HttpHeaders.Names.HOST, host); LOG.trace("Host: {}", host); // configure connection to accordingly to keep alive configuration // favor using the header from the message String connection = message.getHeader(HttpHeaders.Names.CONNECTION, String.class); if (connection == null) { // fallback and use the keep alive from the configuration if (configuration.isKeepAlive()) { connection = HttpHeaders.Values.KEEP_ALIVE; } else { connection = HttpHeaders.Values.CLOSE; } } request.headers().set(HttpHeaders.Names.CONNECTION, connection); LOG.trace("Connection: {}", connection); return request; }