Search in sources :

Example 21 with HttpPostRequestEncoder

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.HttpPostRequestEncoder in project ambry by linkedin.

the class NettyMessageProcessorTest method createEncoder.

// multipartPostTest() helpers.
/**
 * Creates a {@link HttpPostRequestEncoder} that encodes the given {@code request} and {@code blobContent}.
 * @param request the {@link HttpRequest} containing headers and other metadata about the request.
 * @param blobContent the {@link ByteBuffer} that represents the content of the blob.
 * @return a {@link HttpPostRequestEncoder} that can encode the {@code request} and {@code blobContent}.
 * @throws HttpPostRequestEncoder.ErrorDataEncoderException
 * @throws IOException
 */
private HttpPostRequestEncoder createEncoder(HttpRequest request, ByteBuffer blobContent) throws HttpPostRequestEncoder.ErrorDataEncoderException, IOException {
    HttpDataFactory httpDataFactory = new DefaultHttpDataFactory(false);
    HttpPostRequestEncoder encoder = new HttpPostRequestEncoder(httpDataFactory, request, true);
    FileUpload fileUpload = new MemoryFileUpload(RestUtils.MultipartPost.BLOB_PART, RestUtils.MultipartPost.BLOB_PART, "application/octet-stream", "", Charset.forName("UTF-8"), blobContent.remaining());
    fileUpload.setContent(Unpooled.wrappedBuffer(blobContent));
    encoder.addBodyHttpData(fileUpload);
    return encoder;
}
Also used : HttpPostRequestEncoder(io.netty.handler.codec.http.multipart.HttpPostRequestEncoder) DefaultHttpDataFactory(io.netty.handler.codec.http.multipart.DefaultHttpDataFactory) MemoryFileUpload(io.netty.handler.codec.http.multipart.MemoryFileUpload) MemoryFileUpload(io.netty.handler.codec.http.multipart.MemoryFileUpload) FileUpload(io.netty.handler.codec.http.multipart.FileUpload) HttpDataFactory(io.netty.handler.codec.http.multipart.HttpDataFactory) DefaultHttpDataFactory(io.netty.handler.codec.http.multipart.DefaultHttpDataFactory)

Example 22 with HttpPostRequestEncoder

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.HttpPostRequestEncoder in project ambry by linkedin.

the class NettyMessageProcessorTest method multipartPostTest.

/**
 * Tests the case where multipart upload is used.
 * @throws Exception
 */
@Test
public void multipartPostTest() throws Exception {
    Random random = new Random();
    ByteBuffer content = ByteBuffer.wrap(TestUtils.getRandomBytes(random.nextInt(128) + 128));
    HttpRequest httpRequest = RestTestUtils.createRequest(HttpMethod.POST, "/", null);
    httpRequest.headers().set(RestUtils.Headers.SERVICE_ID, "rawBytesPostTest");
    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(PooledByteBufAllocator.DEFAULT).content().nioBuffer());
    }
    ByteBuffer receivedContent = doPostTest(postRequest, contents);
    compareContent(receivedContent, Collections.singletonList(content));
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) Random(java.util.Random) HttpPostRequestEncoder(io.netty.handler.codec.http.multipart.HttpPostRequestEncoder) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Aggregations

HttpPostRequestEncoder (io.netty.handler.codec.http.multipart.HttpPostRequestEncoder)21 HttpRequest (io.netty.handler.codec.http.HttpRequest)13 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)10 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)8 DefaultHttpDataFactory (io.netty.handler.codec.http.multipart.DefaultHttpDataFactory)6 HttpDataFactory (io.netty.handler.codec.http.multipart.HttpDataFactory)6 MemoryFileUpload (io.netty.handler.codec.http.multipart.MemoryFileUpload)5 Test (org.junit.Test)5 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)4 HttpContent (io.netty.handler.codec.http.HttpContent)4 FileUpload (io.netty.handler.codec.http.multipart.FileUpload)4 ResponseParts (com.github.ambry.rest.NettyClient.ResponseParts)3 DefaultHttpContent (io.netty.handler.codec.http.DefaultHttpContent)3 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)3 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)3 ByteBuffer (java.nio.ByteBuffer)3 Account (com.github.ambry.account.Account)2 Container (com.github.ambry.account.Container)2 NettyConfig (com.github.ambry.config.NettyConfig)2 Channel (io.netty.channel.Channel)2