Search in sources :

Example 6 with MemoryFileUpload

use of io.netty.handler.codec.http.multipart.MemoryFileUpload 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)

Example 7 with MemoryFileUpload

use of io.netty.handler.codec.http.multipart.MemoryFileUpload in project jocean-http by isdom.

the class HttpPostRequestEncoderTestCase method testGetBodyListAttributes.

@Test
public final void testGetBodyListAttributes() throws Exception {
    final HttpDataFactory factory = new DefaultHttpDataFactory(false);
    final HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
    // Use the PostBody encoder
    final HttpPostRequestEncoder postRequestEncoder = // true => multipart
    new HttpPostRequestEncoder(factory, request, true);
    final MemoryFileUpload f1 = new MemoryFileUpload("m1", "m1", "application/json", null, null, 100);
    final DiskFileUpload f2 = new DiskFileUpload("d1", "d1", "application/json", null, null, 100);
    final DiskFileUpload f3 = new DiskFileUpload("d2", "d2", "application/json", null, null, 100);
    postRequestEncoder.addBodyHttpData(f1);
    postRequestEncoder.addBodyHttpData(f2);
    postRequestEncoder.addBodyHttpData(f3);
    final List<InterfaceHttpData> attrs = postRequestEncoder.getBodyListAttributes();
    final InterfaceHttpData[] datas = new InterfaceHttpData[] { f1, f2, f3 };
    for (int idx = 0; idx < datas.length; idx++) {
        assertEquals(datas[idx], attrs.toArray(new InterfaceHttpData[0])[idx]);
    }
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) DiskFileUpload(io.netty.handler.codec.http.multipart.DiskFileUpload) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) InterfaceHttpData(io.netty.handler.codec.http.multipart.InterfaceHttpData) HttpPostRequestEncoder(io.netty.handler.codec.http.multipart.HttpPostRequestEncoder) DefaultHttpDataFactory(io.netty.handler.codec.http.multipart.DefaultHttpDataFactory) MemoryFileUpload(io.netty.handler.codec.http.multipart.MemoryFileUpload) DefaultHttpDataFactory(io.netty.handler.codec.http.multipart.DefaultHttpDataFactory) HttpDataFactory(io.netty.handler.codec.http.multipart.HttpDataFactory) Test(org.junit.Test)

Example 8 with MemoryFileUpload

use of io.netty.handler.codec.http.multipart.MemoryFileUpload in project jocean-http by isdom.

the class DefaultSignalClient method addSignalToMultipart.

private long addSignalToMultipart(final HttpPostRequestEncoder postRequestEncoder, final BodyForm body) throws IOException, ErrorDataEncoderException {
    if (null != body) {
        final MemoryFileUpload signalPayload = new MemoryFileUpload("__signal", "__signal", body.contentType(), null, null, body.length()) {

            @Override
            public Charset getCharset() {
                return null;
            }
        };
        signalPayload.setContent(body.content().retain());
        postRequestEncoder.addBodyHttpData(signalPayload);
        return body.length();
    } else {
        // TODO ?
        return 0;
    }
}
Also used : MemoryFileUpload(io.netty.handler.codec.http.multipart.MemoryFileUpload)

Example 9 with MemoryFileUpload

use of io.netty.handler.codec.http.multipart.MemoryFileUpload in project jocean-http by isdom.

the class AttachmentBuilder4InMemory method call.

@Override
public HttpData call(final Attachment attachment) {
    if (attachment instanceof AttachmentInMemory) {
        final AttachmentInMemory inmemory = (AttachmentInMemory) attachment;
        final MemoryFileUpload fileupload = new MemoryFileUpload(inmemory.filename, inmemory.filename, inmemory.contentType, null, null, inmemory.content().length) {

            @Override
            public Charset getCharset() {
                return null;
            }
        };
        try {
            fileupload.setContent(Unpooled.wrappedBuffer(inmemory.content()));
        } catch (IOException e) {
            LOG.warn("exception when fileupload.setContent, detail: {}", ExceptionUtils.exception2detail(e));
        }
        return fileupload;
    } else {
        throw new RuntimeException("attachment " + attachment + " is not AttachmentInMemory instance.");
    }
}
Also used : MemoryFileUpload(io.netty.handler.codec.http.multipart.MemoryFileUpload) IOException(java.io.IOException)

Example 10 with MemoryFileUpload

use of io.netty.handler.codec.http.multipart.MemoryFileUpload in project ambry by linkedin.

the class FrontendIntegrationTestBase 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
 */
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", "", StandardCharsets.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", "", StandardCharsets.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

MemoryFileUpload (io.netty.handler.codec.http.multipart.MemoryFileUpload)12 DefaultHttpDataFactory (io.netty.handler.codec.http.multipart.DefaultHttpDataFactory)5 HttpDataFactory (io.netty.handler.codec.http.multipart.HttpDataFactory)5 HttpPostRequestEncoder (io.netty.handler.codec.http.multipart.HttpPostRequestEncoder)5 IOException (java.io.IOException)5 FileUpload (io.netty.handler.codec.http.multipart.FileUpload)4 Test (org.junit.Test)2 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)1 HttpRequest (io.netty.handler.codec.http.HttpRequest)1 DiskFileUpload (io.netty.handler.codec.http.multipart.DiskFileUpload)1 InterfaceHttpData (io.netty.handler.codec.http.multipart.InterfaceHttpData)1