List of usage examples for io.netty.handler.codec.http HttpMethod POST
HttpMethod POST
To view the source code for io.netty.handler.codec.http HttpMethod POST.
Click Source Link
From source file:com.couchbase.client.core.endpoint.config.ConfigHandlerTest.java
License:Apache License
@Test public void shouldEncodeFlushRequest() { FlushRequest request = new FlushRequest("bucket", "password"); channel.writeOutbound(request);//from ww w .j a v a 2 s. c o m HttpRequest outbound = (HttpRequest) channel.readOutbound(); assertEquals(HttpMethod.POST, outbound.getMethod()); assertEquals(HttpVersion.HTTP_1_1, outbound.getProtocolVersion()); assertEquals("/pools/default/buckets/bucket/controller/doFlush", outbound.getUri()); assertTrue(outbound.headers().contains(HttpHeaders.Names.AUTHORIZATION)); assertTrue(outbound.headers().contains(HttpHeaders.Names.HOST)); assertEquals("Basic YnVja2V0OnBhc3N3b3Jk", outbound.headers().get(HttpHeaders.Names.AUTHORIZATION)); assertEquals("Couchbase Client Mock", outbound.headers().get(HttpHeaders.Names.USER_AGENT)); }
From source file:com.couchbase.client.core.endpoint.query.QueryHandler.java
License:Apache License
@Override protected HttpRequest encodeRequest(final ChannelHandlerContext ctx, final QueryRequest msg) throws Exception { FullHttpRequest request;/*from w w w . j a v a2s. c o m*/ if (msg instanceof GenericQueryRequest) { GenericQueryRequest queryRequest = (GenericQueryRequest) msg; request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/query"); request.headers().set(HttpHeaders.Names.USER_AGENT, env().userAgent()); if (queryRequest.isJsonFormat()) { request.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/json"); } ByteBuf query = ctx.alloc().buffer(((GenericQueryRequest) msg).query().length()); query.writeBytes(((GenericQueryRequest) msg).query().getBytes(CHARSET)); request.headers().add(HttpHeaders.Names.CONTENT_LENGTH, query.readableBytes()); request.headers().set(HttpHeaders.Names.HOST, remoteHttpHost(ctx)); request.content().writeBytes(query); query.release(); } else if (msg instanceof KeepAliveRequest) { request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/admin/ping"); request.headers().set(HttpHeaders.Names.USER_AGENT, env().userAgent()); request.headers().set(HttpHeaders.Names.HOST, remoteHttpHost(ctx)); return request; } else { throw new IllegalArgumentException("Unknown incoming QueryRequest type " + msg.getClass()); } addHttpBasicAuth(ctx, request, msg.bucket(), msg.password()); return request; }
From source file:com.couchbase.client.core.endpoint.query.QueryHandlerTest.java
License:Apache License
private void assertGenericQueryRequest(GenericQueryRequest request, boolean jsonExpected) { channel.writeOutbound(request);/*from www . j av a 2 s.c o m*/ HttpRequest outbound = (HttpRequest) channel.readOutbound(); assertEquals(HttpMethod.POST, outbound.getMethod()); assertEquals(HttpVersion.HTTP_1_1, outbound.getProtocolVersion()); assertEquals("/query", outbound.getUri()); assertTrue(outbound.headers().contains(HttpHeaders.Names.AUTHORIZATION)); assertEquals("Couchbase Client Mock", outbound.headers().get(HttpHeaders.Names.USER_AGENT)); assertTrue(outbound instanceof FullHttpRequest); assertEquals("query", ((FullHttpRequest) outbound).content().toString(CharsetUtil.UTF_8)); if (jsonExpected) { assertEquals("application/json", outbound.headers().get(HttpHeaders.Names.CONTENT_TYPE)); } else { assertNotEquals("application/json", outbound.headers().get(HttpHeaders.Names.CONTENT_TYPE)); } assertTrue(outbound.headers().contains(HttpHeaders.Names.HOST)); }
From source file:com.couchbase.client.core.endpoint.search.SearchHandler.java
License:Apache License
@Override protected HttpRequest encodeRequest(ChannelHandlerContext ctx, SearchRequest msg) throws Exception { HttpMethod httpMethod = HttpMethod.GET; if (msg instanceof UpsertSearchIndexRequest) { httpMethod = HttpMethod.PUT;/*from ww w . j a v a 2 s . com*/ } else if (msg instanceof RemoveSearchIndexRequest) { httpMethod = HttpMethod.DELETE; } else if (msg instanceof SearchQueryRequest) { httpMethod = HttpMethod.POST; } ByteBuf content; if (msg instanceof UpsertSearchIndexRequest) { content = Unpooled.copiedBuffer(((UpsertSearchIndexRequest) msg).payload(), CharsetUtil.UTF_8); } else if (msg instanceof SearchQueryRequest) { content = Unpooled.copiedBuffer(((SearchQueryRequest) msg).payload(), CharsetUtil.UTF_8); } else { content = Unpooled.EMPTY_BUFFER; } FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, httpMethod, msg.path(), content); request.headers().set(HttpHeaders.Names.USER_AGENT, env().userAgent()); if (msg instanceof UpsertSearchIndexRequest || msg instanceof SearchQueryRequest) { request.headers().set(HttpHeaders.Names.ACCEPT, "*/*"); request.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/json"); } request.headers().set(HttpHeaders.Names.CONTENT_LENGTH, content.readableBytes()); request.headers().set(HttpHeaders.Names.HOST, remoteHttpHost(ctx)); addHttpBasicAuth(ctx, request, msg.bucket(), msg.password()); return request; }
From source file:com.couchbase.client.core.endpoint.view.ViewHandler.java
License:Apache License
@Override protected HttpRequest encodeRequest(final ChannelHandlerContext ctx, final ViewRequest msg) throws Exception { if (msg instanceof KeepAliveRequest) { FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.HEAD, "/", Unpooled.EMPTY_BUFFER);/*from w ww.j ava 2 s . co m*/ request.headers().set(HttpHeaders.Names.USER_AGENT, env().userAgent()); request.headers().set(HttpHeaders.Names.CONTENT_LENGTH, 0); return request; } StringBuilder path = new StringBuilder(); HttpMethod method = HttpMethod.GET; ByteBuf content = null; if (msg instanceof ViewQueryRequest) { ViewQueryRequest queryMsg = (ViewQueryRequest) msg; path.append("/").append(msg.bucket()).append("/_design/"); path.append(queryMsg.development() ? "dev_" + queryMsg.design() : queryMsg.design()); if (queryMsg.spatial()) { path.append("/_spatial/"); } else { path.append("/_view/"); } path.append(queryMsg.view()); int queryLength = queryMsg.query() == null ? 0 : queryMsg.query().length(); int keysLength = queryMsg.keys() == null ? 0 : queryMsg.keys().length(); boolean hasQuery = queryLength > 0; boolean hasKeys = keysLength > 0; if (hasQuery || hasKeys) { if (queryLength + keysLength < MAX_GET_LENGTH) { //the query is short enough for GET //it has query, query+keys or keys only if (hasQuery) { path.append("?").append(queryMsg.query()); if (hasKeys) { path.append("&keys=").append(encodeKeysGet(queryMsg.keys())); } } else { //it surely has keys if not query path.append("?keys=").append(encodeKeysGet(queryMsg.keys())); } } else { //the query is too long for GET, use the keys as JSON body if (hasQuery) { path.append("?").append(queryMsg.query()); } String keysContent = encodeKeysPost(queryMsg.keys()); //switch to POST method = HttpMethod.POST; //body is "keys" but in JSON content = ctx.alloc().buffer(keysContent.length()); content.writeBytes(keysContent.getBytes(CHARSET)); } } } else if (msg instanceof GetDesignDocumentRequest) { GetDesignDocumentRequest queryMsg = (GetDesignDocumentRequest) msg; path.append("/").append(msg.bucket()).append("/_design/"); path.append(queryMsg.development() ? "dev_" + queryMsg.name() : queryMsg.name()); } else if (msg instanceof UpsertDesignDocumentRequest) { method = HttpMethod.PUT; UpsertDesignDocumentRequest queryMsg = (UpsertDesignDocumentRequest) msg; path.append("/").append(msg.bucket()).append("/_design/"); path.append(queryMsg.development() ? "dev_" + queryMsg.name() : queryMsg.name()); content = Unpooled.copiedBuffer(queryMsg.body(), CHARSET); } else if (msg instanceof RemoveDesignDocumentRequest) { method = HttpMethod.DELETE; RemoveDesignDocumentRequest queryMsg = (RemoveDesignDocumentRequest) msg; path.append("/").append(msg.bucket()).append("/_design/"); path.append(queryMsg.development() ? "dev_" + queryMsg.name() : queryMsg.name()); } else { throw new IllegalArgumentException("Unknown incoming ViewRequest type " + msg.getClass()); } if (content == null) { content = Unpooled.buffer(0); } FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, method, path.toString(), content); request.headers().set(HttpHeaders.Names.USER_AGENT, env().userAgent()); request.headers().set(HttpHeaders.Names.CONTENT_LENGTH, content.readableBytes()); request.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/json"); request.headers().set(HttpHeaders.Names.HOST, remoteHttpHost(ctx)); addHttpBasicAuth(ctx, request, msg.bucket(), msg.password()); return request; }
From source file:com.couchbase.client.core.endpoint.view.ViewHandlerTest.java
License:Apache License
@Test public void shouldEncodeLongViewQueryRequestWithPOST() { String keys = Resources.read("key_many.json", this.getClass()); String query = "stale=false&endKey=test"; ViewQueryRequest request = new ViewQueryRequest("design", "view", true, query, keys, "bucket", "password"); channel.writeOutbound(request);//from ww w .j a v a2s .c o m DefaultFullHttpRequest outbound = (DefaultFullHttpRequest) channel.readOutbound(); assertEquals(HttpMethod.POST, outbound.getMethod()); assertFalse(outbound.getUri().contains("keys=")); assertTrue(outbound.getUri().endsWith("?stale=false&endKey=test")); String content = outbound.content().toString(CharsetUtil.UTF_8); assertTrue(content.startsWith("{\"keys\":[")); assertTrue(content.endsWith("]}")); ReferenceCountUtil.releaseLater(outbound); }
From source file:com.couchbase.client.io.QueryEncoder.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext ctx, QueryEvent ev, List<Object> out) throws Exception { ByteBuf queryBuf = Unpooled.copiedBuffer(ev.getQuery(), CharsetUtil.UTF_8); HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/query", queryBuf); request.headers().set("Content-Length", queryBuf.readableBytes()); request.headers().set("Content-Type", "text/plain"); out.add(request);//from w w w . jav a2s . c o m queue.add(ev); }
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 {/*from w ww . jav a2s . co m*/ 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 {// ww w .j a v a 2 s. c om 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.ebay.jetstream.http.netty.client.HttpClient.java
License:MIT License
public void post(URI uri, Object content, Map<String, String> headers, ResponseFuture responsefuture) throws Exception { if (m_shutDown.get()) { throw new IOException("IO has been Shutdown = "); }//from w w w. j a v a 2 s . c o m if (uri == null) throw new NullPointerException("uri is null or incorrect"); if (!m_started.get()) { synchronized (this) { if (!m_started.get()) { start(); m_started.set(true); } } } ByteBuf byteBuf = Unpooled.buffer(m_config.getInitialRequestBufferSize()); ByteBufOutputStream outputStream = new ByteBufOutputStream(byteBuf); // transform to json try { m_mapper.writeValue(outputStream, content); } catch (JsonGenerationException e) { throw e; } catch (JsonMappingException e) { throw e; } catch (IOException e) { throw e; } finally { outputStream.close(); } EmbeddedChannel encoder; String contenteEncoding; if ("gzip".equals(m_config.getCompressEncoder())) { encoder = new EmbeddedChannel(ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP, 1)); contenteEncoding = "gzip"; } else if ("deflate".equals(m_config.getCompressEncoder())) { encoder = new EmbeddedChannel(ZlibCodecFactory.newZlibEncoder(ZlibWrapper.ZLIB, 1)); contenteEncoding = "deflate"; } else { encoder = null; contenteEncoding = null; } if (encoder != null) { encoder.config().setAllocator(UnpooledByteBufAllocator.DEFAULT); encoder.writeOutbound(byteBuf); encoder.finish(); byteBuf = (ByteBuf) encoder.readOutbound(); encoder.close(); } DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, uri.toString(), byteBuf); if (contenteEncoding != null) { HttpHeaders.setHeader(request, HttpHeaders.Names.CONTENT_ENCODING, contenteEncoding); } HttpHeaders.setHeader(request, HttpHeaders.Names.ACCEPT_ENCODING, "gzip, deflate"); HttpHeaders.setHeader(request, HttpHeaders.Names.CONTENT_TYPE, "application/json"); HttpHeaders.setContentLength(request, byteBuf.readableBytes()); if (isKeepAlive()) HttpHeaders.setHeader(request, HttpHeaders.Names.CONNECTION, "keep-alive"); if (headers != null) { @SuppressWarnings("rawtypes") Iterator it = headers.entrySet().iterator(); while (it.hasNext()) { @SuppressWarnings("rawtypes") Map.Entry pairs = (Map.Entry) it.next(); HttpHeaders.setHeader(request, (String) pairs.getKey(), pairs.getValue()); } } if (responsefuture != null) { RequestId reqid = RequestId.newRequestId(); m_responseDispatcher.add(reqid, responsefuture); HttpHeaders.setHeader(request, "X_EBAY_REQ_ID", reqid.toString()); // we expect this to be echoed in the response used for correlation. } if (m_dataQueue.size() < m_workQueueCapacity) { ProcessHttpWorkRequest workRequest = new ProcessHttpWorkRequest(this, uri, request); if (!m_dataQueue.offer(workRequest)) { if (responsefuture != null) { responsefuture.setFailure(); m_responseDispatcher.remove(request.headers().get("X_EBAY_REQ_ID")); } } } else { throw new IOException("downstream Queue is full "); } }