Search in sources :

Example 6 with HttpPostRequestEncoder

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

the class RestClient method createRequest.

private static Request createRequest(String targetAddress, String targetUrl, HttpMethod httpMethod, ByteBuf jsonPayload, Collection<FileUpload> fileUploads) throws IOException {
    if (fileUploads.isEmpty()) {
        HttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, httpMethod, targetUrl, jsonPayload);
        httpRequest.headers().set(HttpHeaders.Names.HOST, targetAddress).set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE).add(HttpHeaders.Names.CONTENT_LENGTH, jsonPayload.capacity()).add(HttpHeaders.Names.CONTENT_TYPE, RestConstants.REST_CONTENT_TYPE);
        return new SimpleRequest(httpRequest);
    } else {
        HttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, httpMethod, targetUrl);
        httpRequest.headers().set(HttpHeaders.Names.HOST, targetAddress).set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
        // takes care of splitting the request into multiple parts
        HttpPostRequestEncoder bodyRequestEncoder;
        try {
            // we could use mixed attributes here but we have to ensure that the minimum size is
            // greater than
            // any file as the upload otherwise fails
            DefaultHttpDataFactory httpDataFactory = new DefaultHttpDataFactory(true);
            // the FileUploadHandler explicitly checks for multipart headers
            bodyRequestEncoder = new HttpPostRequestEncoder(httpDataFactory, httpRequest, true);
            Attribute requestAttribute = new MemoryAttribute(FileUploadHandler.HTTP_ATTRIBUTE_REQUEST);
            requestAttribute.setContent(jsonPayload);
            bodyRequestEncoder.addBodyHttpData(requestAttribute);
            int fileIndex = 0;
            for (FileUpload fileUpload : fileUploads) {
                Path path = fileUpload.getFile();
                if (Files.isDirectory(path)) {
                    throw new IllegalArgumentException("Upload of directories is not supported. Dir=" + path);
                }
                File file = path.toFile();
                LOG.trace("Adding file {} to request.", file);
                bodyRequestEncoder.addBodyFileUpload("file_" + fileIndex, file, fileUpload.getContentType(), false);
                fileIndex++;
            }
        } catch (HttpPostRequestEncoder.ErrorDataEncoderException e) {
            throw new IOException("Could not encode request.", e);
        }
        try {
            httpRequest = bodyRequestEncoder.finalizeRequest();
        } catch (HttpPostRequestEncoder.ErrorDataEncoderException e) {
            throw new IOException("Could not finalize request.", e);
        }
        return new MultipartRequest(httpRequest, bodyRequestEncoder);
    }
}
Also used : HttpRequest(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpRequest) DefaultFullHttpRequest(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpRequest) Path(java.nio.file.Path) DefaultFullHttpRequest(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpRequest) MemoryAttribute(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.MemoryAttribute) Attribute(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.Attribute) HttpPostRequestEncoder(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.HttpPostRequestEncoder) IOException(java.io.IOException) MemoryAttribute(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.MemoryAttribute) DefaultHttpDataFactory(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.DefaultHttpDataFactory) File(java.io.File)

Example 7 with HttpPostRequestEncoder

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

the class FrontendIntegrationTest method multipartPostGetHeadUpdateDeleteUndeleteTest.

/**
 * Tests multipart POST and verifies it via GET operations.
 * @throws Exception
 */
@Test
public void multipartPostGetHeadUpdateDeleteUndeleteTest() throws Exception {
    Account refAccount = ACCOUNT_SERVICE.createAndAddRandomAccount();
    Container refContainer = refAccount.getContainerById(Container.DEFAULT_PUBLIC_CONTAINER_ID);
    doPostGetHeadUpdateDeleteUndeleteTest(0, refAccount, refContainer, refAccount.getName(), !refContainer.isCacheable(), refAccount.getName(), refContainer.getName(), true);
    doPostGetHeadUpdateDeleteUndeleteTest((int) FRONTEND_CONFIG.chunkedGetResponseThresholdInBytes * 3, refAccount, refContainer, refAccount.getName(), !refContainer.isCacheable(), refAccount.getName(), refContainer.getName(), true);
    // failure case
    // size of content being POSTed is higher than what is allowed via multipart/form-data
    long maxAllowedSizeBytes = new NettyConfig(FRONTEND_VERIFIABLE_PROPS).nettyMultipartPostMaxSizeBytes;
    ByteBuffer content = ByteBuffer.wrap(TestUtils.getRandomBytes((int) maxAllowedSizeBytes + 1));
    HttpHeaders headers = new DefaultHttpHeaders();
    setAmbryHeadersForPut(headers, TTL_SECS, !refContainer.isCacheable(), refAccount.getName(), "application/octet-stream", null, refAccount.getName(), refContainer.getName());
    HttpRequest httpRequest = RestTestUtils.createRequest(HttpMethod.POST, "/", headers);
    HttpPostRequestEncoder encoder = createEncoder(httpRequest, content, ByteBuffer.allocate(0));
    ResponseParts responseParts = nettyClient.sendRequest(encoder.finalizeRequest(), encoder, null).get();
    HttpResponse response = getHttpResponse(responseParts);
    assertEquals("Unexpected response status", HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE, response.status());
    assertTrue("No Date header", response.headers().getTimeMillis(HttpHeaderNames.DATE, -1) != -1);
    assertFalse("Channel should not be active", HttpUtil.isKeepAlive(response));
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) Account(com.github.ambry.account.Account) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) Container(com.github.ambry.account.Container) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) HttpPostRequestEncoder(io.netty.handler.codec.http.multipart.HttpPostRequestEncoder) HttpResponse(io.netty.handler.codec.http.HttpResponse) ResponseParts(com.github.ambry.rest.NettyClient.ResponseParts) NettyConfig(com.github.ambry.config.NettyConfig) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 8 with HttpPostRequestEncoder

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

the class FrontendIntegrationTestBase method multipartPostBlobAndVerify.

/**
 * Posts a blob with the given {@code headers} and {@code content}.
 * @param headers the headers required.
 * @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
 */
String multipartPostBlobAndVerify(HttpHeaders headers, ByteBuffer content, ByteBuffer usermetadata) throws Exception {
    HttpRequest httpRequest = RestTestUtils.createRequest(HttpMethod.POST, "/", headers);
    HttpPostRequestEncoder encoder = createEncoder(httpRequest, content, usermetadata);
    NettyClient.ResponseParts responseParts = nettyClient.sendRequest(encoder.finalizeRequest(), encoder, null).get();
    return verifyPostAndReturnBlobId(responseParts, content.capacity(), false);
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) NettyClient(com.github.ambry.rest.NettyClient) HttpPostRequestEncoder(io.netty.handler.codec.http.multipart.HttpPostRequestEncoder)

Example 9 with HttpPostRequestEncoder

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

the class NettyMultipartRequestTest method createRequest.

// helpers
// general
/**
 * Creates a {@link NettyMultipartRequest} with the given {@code headers} and {@code parts}.
 * @param headers the {@link HttpHeaders} that need to be added to the request.
 * @param parts the files that will form the parts of the request.
 * @return a {@link NettyMultipartRequest} containing all the {@code headers} and {@code parts}.
 * @throws Exception
 */
private NettyMultipartRequest createRequest(HttpHeaders headers, InMemoryFile[] parts) throws Exception {
    HttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
    if (headers != null) {
        httpRequest.headers().set(headers);
    }
    HttpPostRequestEncoder encoder = createEncoder(httpRequest, parts);
    NettyMultipartRequest request = new NettyMultipartRequest(encoder.finalizeRequest(), new MockChannel(), NETTY_METRICS, Collections.emptySet(), Long.MAX_VALUE);
    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(PooledByteBufAllocator.DEFAULT));
    }
    return request;
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpPostRequestEncoder(io.netty.handler.codec.http.multipart.HttpPostRequestEncoder)

Example 10 with HttpPostRequestEncoder

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

the class FrontendIntegrationTest method createEncoder.

/**
 * 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.
 * @param usermetadata the {@link ByteBuffer} that represents user metadata
 * @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, ByteBuffer usermetadata) 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);
    fileUpload = new MemoryFileUpload(RestUtils.MultipartPost.USER_METADATA_PART, RestUtils.MultipartPost.USER_METADATA_PART, "application/octet-stream", "", Charset.forName("UTF-8"), usermetadata.remaining());
    fileUpload.setContent(Unpooled.wrappedBuffer(usermetadata));
    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)

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