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.eucalyptus.ws.IoMessage.java
License:Open Source License
/** * Create a new message for outbound http use. * * @param endpoint The http endpoint/*from w w w. j a va2 s. co m*/ * @param message The message to be sent * @return The new message */ @Nonnull public static IoMessage<FullHttpRequest> httpRequest(@Nonnull final URI endpoint, @Nonnull final BaseMessage message) { Parameters.checkParamNotNull("endpoint", endpoint); Parameters.checkParamNotNull("message", message); final FullHttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, endpoint.getPath()); httpRequest.headers().add(HttpHeaders.Names.HOST, endpoint.getHost() + ":" + endpoint.getPort()); final IoMessage<FullHttpRequest> ioMessage = new IoMessage<>(); ioMessage.setHttpMessage(httpRequest); ioMessage.setMessage(message); ioMessage.setCorrelationId(message.getCorrelationId()); return ioMessage; }
From source file:com.github.ambry.admin.AdminIntegrationTest.java
License:Open Source License
/** * Posts a blob with the given {@code headers} and {@code content}. * @param headers the headers required./*from w w w .ja va 2 s . c o m*/ * @param content the content of the blob. * @return the blob ID of the blob. * @throws ExecutionException * @throws InterruptedException */ private String postBlobAndVerify(HttpHeaders headers, ByteBuffer content) throws ExecutionException, InterruptedException { FullHttpRequest httpRequest = buildRequest(HttpMethod.POST, "/", headers, content); Queue<HttpObject> responseParts = nettyClient.sendRequest(httpRequest, null, null).get(); HttpResponse response = (HttpResponse) responseParts.poll(); discardContent(responseParts, 1); assertEquals("Unexpected response status", HttpResponseStatus.CREATED, response.getStatus()); assertTrue("No Date header", HttpHeaders.getDateHeader(response, HttpHeaders.Names.DATE, null) != null); assertTrue("No " + RestUtils.Headers.CREATION_TIME, HttpHeaders.getHeader(response, RestUtils.Headers.CREATION_TIME, null) != null); assertEquals("Content-Length is not 0", 0, HttpHeaders.getContentLength(response)); String blobId = HttpHeaders.getHeader(response, HttpHeaders.Names.LOCATION, null); if (blobId == null) { fail("postBlobAndVerify did not return a blob ID"); } return blobId; }
From source file:com.github.ambry.frontend.FrontendIntegrationTest.java
License:Open Source License
/** * Posts a blob with the given {@code headers} and {@code content}. * @param headers the headers required./*from ww w .ja v a2 s . c o m*/ * @param content the content of the blob. * @return the blob ID of the blob. * @throws ExecutionException * @throws InterruptedException */ private String postBlobAndVerify(HttpHeaders headers, ByteBuffer content) throws ExecutionException, InterruptedException { FullHttpRequest httpRequest = buildRequest(HttpMethod.POST, "/", headers, content); Queue<HttpObject> responseParts = nettyClient.sendRequest(httpRequest, null, null).get(); HttpResponse response = (HttpResponse) responseParts.poll(); assertEquals("Unexpected response status", HttpResponseStatus.CREATED, response.getStatus()); assertTrue("No Date header", HttpHeaders.getDateHeader(response, HttpHeaders.Names.DATE, null) != null); assertTrue("No " + RestUtils.Headers.CREATION_TIME, HttpHeaders.getHeader(response, RestUtils.Headers.CREATION_TIME, null) != null); assertEquals("Content-Length is not 0", 0, HttpHeaders.getContentLength(response)); String blobId = HttpHeaders.getHeader(response, HttpHeaders.Names.LOCATION, null); if (blobId == null) { fail("postBlobAndVerify did not return a blob ID"); } discardContent(responseParts, 1); assertTrue("Channel should be active", HttpHeaders.isKeepAlive(response)); return blobId; }
From source file:com.github.ambry.frontend.FrontendIntegrationTest.java
License:Open Source License
/** * Posts a blob with the given {@code headers} and {@code content}. * @param headers the headers required./*from w w w. ja v a 2 s.com*/ * @param content the content of the blob. * @param usermetadata the {@link ByteBuffer} that represents user metadata * @return the blob ID of the blob. * @throws Exception */ private String multipartPostBlobAndVerify(HttpHeaders headers, ByteBuffer content, ByteBuffer usermetadata) throws Exception { HttpRequest httpRequest = RestTestUtils.createRequest(HttpMethod.POST, "/", headers); HttpPostRequestEncoder encoder = createEncoder(httpRequest, content, usermetadata); Queue<HttpObject> responseParts = nettyClient.sendRequest(encoder.finalizeRequest(), encoder, null).get(); HttpResponse response = (HttpResponse) responseParts.poll(); assertEquals("Unexpected response status", HttpResponseStatus.CREATED, response.getStatus()); assertTrue("No Date header", HttpHeaders.getDateHeader(response, HttpHeaders.Names.DATE, null) != null); assertTrue("No " + RestUtils.Headers.CREATION_TIME, HttpHeaders.getHeader(response, RestUtils.Headers.CREATION_TIME, null) != null); assertEquals("Content-Length is not 0", 0, HttpHeaders.getContentLength(response)); String blobId = HttpHeaders.getHeader(response, HttpHeaders.Names.LOCATION, null); if (blobId == null) { fail("postBlobAndVerify did not return a blob ID"); } discardContent(responseParts, 1); assertTrue("Channel should be active", HttpHeaders.isKeepAlive(response)); return blobId; }
From source file:com.github.ambry.rest.HealthCheckHandlerTest.java
License:Open Source License
/** * Tests non health check requests handling * @throws java.io.IOException/*from ww w.j a v a 2 s.com*/ */ @Test public void requestHandleWithNonHealthCheckRequestTest() throws IOException { testNonHealthCheckRequest(HttpMethod.POST, "POST"); testNonHealthCheckRequest(HttpMethod.GET, "GET"); testNonHealthCheckRequest(HttpMethod.DELETE, "DELETE"); }
From source file:com.github.ambry.rest.NettyMessageProcessorTest.java
License:Open Source License
/** * Tests the case where raw bytes are POSTed as chunks. * @throws InterruptedException//w w w . j ava 2 s . co m */ @Test public void rawBytesPostTest() throws InterruptedException { Random random = new Random(); // request also contains content. ByteBuffer content = ByteBuffer.wrap(RestTestUtils.getRandomBytes(random.nextInt(128) + 128)); HttpRequest postRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/", Unpooled.wrappedBuffer(content)); HttpHeaders.setHeader(postRequest, RestUtils.Headers.SERVICE_ID, "rawBytesPostTest"); HttpHeaders.setHeader(postRequest, RestUtils.Headers.BLOB_SIZE, content.remaining()); postRequest = ReferenceCountUtil.retain(postRequest); ByteBuffer receivedContent = doPostTest(postRequest, null); compareContent(receivedContent, Collections.singletonList(content)); // request and content separate. final int NUM_CONTENTS = 5; postRequest = RestTestUtils.createRequest(HttpMethod.POST, "/", null); List<ByteBuffer> contents = new ArrayList<ByteBuffer>(NUM_CONTENTS); int blobSize = 0; for (int i = 0; i < NUM_CONTENTS; i++) { ByteBuffer buffer = ByteBuffer.wrap(RestTestUtils.getRandomBytes(random.nextInt(128) + 128)); blobSize += buffer.remaining(); contents.add(i, buffer); } HttpHeaders.setHeader(postRequest, RestUtils.Headers.SERVICE_ID, "rawBytesPostTest"); HttpHeaders.setHeader(postRequest, RestUtils.Headers.BLOB_SIZE, blobSize); receivedContent = doPostTest(postRequest, contents); compareContent(receivedContent, contents); }
From source file:com.github.ambry.rest.NettyMessageProcessorTest.java
License:Open Source License
/** * Tests the case where multipart upload is used. * @throws Exception//from w w w . j av a 2s.co m */ @Test public void multipartPostTest() throws Exception { Random random = new Random(); ByteBuffer content = ByteBuffer.wrap(RestTestUtils.getRandomBytes(random.nextInt(128) + 128)); HttpRequest httpRequest = RestTestUtils.createRequest(HttpMethod.POST, "/", null); HttpHeaders.setHeader(httpRequest, RestUtils.Headers.SERVICE_ID, "rawBytesPostTest"); HttpHeaders.setHeader(httpRequest, RestUtils.Headers.BLOB_SIZE, content.remaining()); HttpPostRequestEncoder encoder = createEncoder(httpRequest, content); HttpRequest postRequest = encoder.finalizeRequest(); List<ByteBuffer> contents = new ArrayList<ByteBuffer>(); while (!encoder.isEndOfInput()) { // Sending null for ctx because the encoder is OK with that. contents.add(encoder.readChunk(null).content().nioBuffer()); } ByteBuffer receivedContent = doPostTest(postRequest, contents); compareContent(receivedContent, Collections.singletonList(content)); }
From source file:com.github.ambry.rest.NettyMessageProcessorTest.java
License:Open Source License
/** * Tests for error handling flow when the {@link RestRequestHandler} throws exceptions. *//*from ww w. j a va 2 s.c o m*/ @Test public void requestHandlerExceptionTest() { try { // RuntimeException Properties properties = new Properties(); properties.setProperty(MockRestRequestResponseHandler.RUNTIME_EXCEPTION_ON_HANDLE, "true"); requestHandler.breakdown(new VerifiableProperties(properties)); doRequestHandlerExceptionTest(HttpMethod.GET, HttpResponseStatus.INTERNAL_SERVER_ERROR); // RestServiceException properties.clear(); properties.setProperty(MockRestRequestResponseHandler.REST_EXCEPTION_ON_HANDLE, RestServiceErrorCode.InternalServerError.toString()); requestHandler.breakdown(new VerifiableProperties(properties)); doRequestHandlerExceptionTest(HttpMethod.GET, HttpResponseStatus.INTERNAL_SERVER_ERROR); // ClosedChannelException properties.clear(); properties.setProperty(MockRestRequestResponseHandler.CLOSE_REQUEST_ON_HANDLE, "true"); requestHandler.breakdown(new VerifiableProperties(properties)); doRequestHandlerExceptionTest(HttpMethod.POST, HttpResponseStatus.INTERNAL_SERVER_ERROR); } finally { requestHandler.fix(); } }
From source file:com.github.ambry.rest.NettyMultipartRequestTest.java
License:Open Source License
/** * Tests instantiation of {@link NettyMultipartRequest} with different {@link HttpMethod} types. * </p>/*from w ww .j av a2 s. c o m*/ * Only {@link HttpMethod#POST} should succeed. * @throws RestServiceException */ @Test public void instantiationTest() throws RestServiceException { // POST will succeed. HttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/"); closeRequestAndValidate(new NettyMultipartRequest(httpRequest, nettyMetrics)); // Methods that will fail. Can include other methods, but these should be enough. HttpMethod[] methods = { HttpMethod.GET, HttpMethod.DELETE, HttpMethod.HEAD }; for (HttpMethod method : methods) { httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, method, "/"); try { new NettyMultipartRequest(httpRequest, nettyMetrics); fail("Creation of NettyMultipartRequest should have failed for " + method); } catch (IllegalArgumentException e) { // expected. Nothing to do. } } }
From source file:com.github.ambry.rest.NettyMultipartRequestTest.java
License:Open Source License
/** * Tests different scenarios with {@link NettyMultipartRequest#prepare()}. * Currently tests:/*from w ww . ja v a 2 s . c om*/ * 1. Idempotency of {@link NettyMultipartRequest#prepare()}. * 2. Exception scenarios of {@link NettyMultipartRequest#prepare()}. * @throws Exception */ @Test public void prepareTest() throws Exception { // prepare half baked data HttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/"); HttpPostRequestEncoder encoder = createEncoder(httpRequest, null); NettyMultipartRequest request = new NettyMultipartRequest(encoder.finalizeRequest(), nettyMetrics); assertTrue("Request channel is not open", request.isOpen()); // insert random data HttpContent httpContent = new DefaultHttpContent(Unpooled.wrappedBuffer(RestTestUtils.getRandomBytes(10))); request.addContent(httpContent); // prepare should fail try { request.prepare(); fail("Preparing request should have failed"); } catch (HttpPostRequestDecoder.NotEnoughDataDecoderException e) { assertEquals("Reference count is not as expected", 1, httpContent.refCnt()); } finally { closeRequestAndValidate(request); } // more than one blob part HttpHeaders httpHeaders = new DefaultHttpHeaders(); httpHeaders.set(RestUtils.Headers.BLOB_SIZE, 256); InMemoryFile[] files = new InMemoryFile[2]; files[0] = new InMemoryFile(RestUtils.MultipartPost.BLOB_PART, ByteBuffer.wrap(RestTestUtils.getRandomBytes(256))); files[1] = new InMemoryFile(RestUtils.MultipartPost.BLOB_PART, ByteBuffer.wrap(RestTestUtils.getRandomBytes(256))); request = createRequest(httpHeaders, files); assertEquals("Request size does not match", 256, request.getSize()); try { request.prepare(); fail("Prepare should have failed because there was more than one " + RestUtils.MultipartPost.BLOB_PART); } catch (RestServiceException e) { assertEquals("Unexpected RestServiceErrorCode", RestServiceErrorCode.MalformedRequest, e.getErrorCode()); } finally { closeRequestAndValidate(request); } // more than one part named "part-1" files = new InMemoryFile[2]; files[0] = new InMemoryFile("Part-1", ByteBuffer.wrap(RestTestUtils.getRandomBytes(256))); files[1] = new InMemoryFile("Part-1", ByteBuffer.wrap(RestTestUtils.getRandomBytes(256))); request = createRequest(null, files); try { request.prepare(); fail("Prepare should have failed because there was more than one part named Part-1"); } catch (RestServiceException e) { assertEquals("Unexpected RestServiceErrorCode", RestServiceErrorCode.MalformedRequest, e.getErrorCode()); } finally { closeRequestAndValidate(request); } // size of blob does not match the advertized size httpHeaders = new DefaultHttpHeaders(); httpHeaders.set(RestUtils.Headers.BLOB_SIZE, 256); files = new InMemoryFile[1]; files[0] = new InMemoryFile(RestUtils.MultipartPost.BLOB_PART, ByteBuffer.wrap(RestTestUtils.getRandomBytes(128))); request = createRequest(httpHeaders, files); try { request.prepare(); fail("Prepare should have failed because there was more than one " + RestUtils.MultipartPost.BLOB_PART); } catch (RestServiceException e) { assertEquals("Unexpected RestServiceErrorCode", RestServiceErrorCode.BadRequest, e.getErrorCode()); } finally { closeRequestAndValidate(request); } // non fileupload (file attribute present) httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/"); HttpHeaders.setHeader(httpRequest, RestUtils.Headers.BLOB_SIZE, 256); files = new InMemoryFile[1]; files[0] = new InMemoryFile(RestUtils.MultipartPost.BLOB_PART, ByteBuffer.wrap(RestTestUtils.getRandomBytes(256))); encoder = createEncoder(httpRequest, files); encoder.addBodyAttribute("dummyKey", "dummyValue"); request = new NettyMultipartRequest(encoder.finalizeRequest(), nettyMetrics); assertTrue("Request channel is not open", request.isOpen()); while (!encoder.isEndOfInput()) { // Sending null for ctx because the encoder is OK with that. request.addContent(encoder.readChunk(null)); } try { request.prepare(); fail("Prepare should have failed because there was non fileupload"); } catch (RestServiceException e) { assertEquals("Unexpected RestServiceErrorCode", RestServiceErrorCode.BadRequest, e.getErrorCode()); } finally { closeRequestAndValidate(request); } }