Search in sources :

Example 11 with HttpPostRequestEncoder

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.HttpPostRequestEncoder 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 12 with HttpPostRequestEncoder

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

the class DefaultSignalClient method assembleOutgoing.

private Outgoing assembleOutgoing(final HttpRequest request, final BodyForm body, final Attachment[] attachments) throws Exception {
    if (0 == attachments.length) {
        final LastHttpContent lastContent = buildLastContent(request, body);
        return new Outgoing(Observable.<Object>just(request, lastContent), lastContent.content().readableBytes(), new Action0() {

            @Override
            public void call() {
                ReferenceCountUtil.release(request);
                ReferenceCountUtil.release(lastContent);
            }
        });
    } else {
        // Use the PostBody encoder
        final HttpPostRequestEncoder postRequestEncoder = new HttpPostRequestEncoder(_DATA_FACTORY, request, true, CharsetUtil.UTF_8, // true => multipart
        EncoderMode.HTML5);
        final long signalSize = addSignalToMultipart(postRequestEncoder, body);
        final long attachmentSize = addAttachmentsToMultipart(postRequestEncoder, attachments);
        final long total = signalSize + attachmentSize;
        // finalize request
        final HttpRequest request4send = postRequestEncoder.finalizeRequest();
        final Action0 toRelease = new Action0() {

            @Override
            public void call() {
                ReferenceCountUtil.release(request4send);
                RxNettys.releaseObjects(postRequestEncoder.getBodyListAttributes());
            }
        };
        return postRequestEncoder.isChunked() ? new Outgoing(Observable.<Object>just(request4send, postRequestEncoder), total, toRelease) : new Outgoing(Observable.<Object>just(request4send), total, toRelease);
    }
}
Also used : DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) Action0(rx.functions.Action0) HttpPostRequestEncoder(io.netty.handler.codec.http.multipart.HttpPostRequestEncoder) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent)

Example 13 with HttpPostRequestEncoder

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

the class Util method prepareRequestWithMultiparts.

/**
 * Prepare carbon request message with multiparts.
 *
 * @param outboundRequest Represent outbound carbon request
 * @param requestStruct   Ballerina request struct which contains multipart data
 */
private static void prepareRequestWithMultiparts(HTTPCarbonMessage outboundRequest, BStruct requestStruct) {
    BStruct entityStruct = requestStruct.getNativeData(MESSAGE_ENTITY) != null ? (BStruct) requestStruct.getNativeData(MESSAGE_ENTITY) : null;
    if (entityStruct != null) {
        BRefValueArray bodyParts = entityStruct.getNativeData(BODY_PARTS) != null ? (BRefValueArray) entityStruct.getNativeData(BODY_PARTS) : null;
        if (bodyParts != null) {
            HttpDataFactory dataFactory = new DefaultHttpDataFactory(DefaultHttpDataFactory.MINSIZE);
            setDataFactory(dataFactory);
            try {
                HttpPostRequestEncoder nettyEncoder = new HttpPostRequestEncoder(dataFactory, outboundRequest.getNettyHttpRequest(), true);
                for (int i = 0; i < bodyParts.size(); i++) {
                    BStruct bodyPart = (BStruct) bodyParts.get(i);
                    encodeBodyPart(nettyEncoder, outboundRequest.getNettyHttpRequest(), bodyPart);
                }
                nettyEncoder.finalizeRequest();
                requestStruct.addNativeData(MULTIPART_ENCODER, nettyEncoder);
            } catch (HttpPostRequestEncoder.ErrorDataEncoderException e) {
                log.error("Error occurred while creating netty request encoder for multipart data binding", e.getMessage());
            }
        }
    }
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) HttpPostRequestEncoder(io.netty.handler.codec.http.multipart.HttpPostRequestEncoder) DefaultHttpDataFactory(io.netty.handler.codec.http.multipart.DefaultHttpDataFactory) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) HttpDataFactory(io.netty.handler.codec.http.multipart.HttpDataFactory) DefaultHttpDataFactory(io.netty.handler.codec.http.multipart.DefaultHttpDataFactory)

Example 14 with HttpPostRequestEncoder

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

the class HttpUploadClient method formpostmultipart.

/**
 * Multipart example
 */
private static void formpostmultipart(Bootstrap bootstrap, String host, int port, URI uriFile, HttpDataFactory factory, Iterable<Entry<String, String>> headers, List<InterfaceHttpData> bodylist) throws Exception {
    // XXX /formpostmultipart
    // Start the connection attempt.
    ChannelFuture future = bootstrap.connect(SocketUtils.socketAddress(host, port));
    // Wait until the connection attempt succeeds or fails.
    Channel channel = future.sync().channel();
    // Prepare the HTTP request.
    HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, uriFile.toASCIIString());
    // Use the PostBody encoder
    HttpPostRequestEncoder bodyRequestEncoder = // true => multipart
    new HttpPostRequestEncoder(factory, request, true);
    // it is legal to add directly header or cookie into the request until finalize
    for (Entry<String, String> entry : headers) {
        request.headers().set(entry.getKey(), entry.getValue());
    }
    // add Form attribute from previous request in formpost()
    bodyRequestEncoder.setBodyHttpDatas(bodylist);
    // finalize request
    bodyRequestEncoder.finalizeRequest();
    // send request
    channel.write(request);
    // test if request was chunked and if so, finish the write
    if (bodyRequestEncoder.isChunked()) {
        channel.write(bodyRequestEncoder);
    }
    channel.flush();
    // Now no more use of file representation (and list of HttpData)
    bodyRequestEncoder.cleanFiles();
    // Wait for the server to close the connection.
    channel.closeFuture().sync();
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Channel(io.netty.channel.Channel) HttpPostRequestEncoder(io.netty.handler.codec.http.multipart.HttpPostRequestEncoder)

Example 15 with HttpPostRequestEncoder

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

the class HttpUploadClient method formpost.

/**
 * Standard post without multipart but already support on Factory (memory management)
 *
 * @return the list of HttpData object (attribute and file) to be reused on next post
 */
private static List<InterfaceHttpData> formpost(Bootstrap bootstrap, String host, int port, URI uriSimple, File file, HttpDataFactory factory, List<Entry<String, String>> headers) throws Exception {
    // XXX /formpost
    // Start the connection attempt.
    ChannelFuture future = bootstrap.connect(SocketUtils.socketAddress(host, port));
    // Wait until the connection attempt succeeds or fails.
    Channel channel = future.sync().channel();
    // Prepare the HTTP request.
    HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, uriSimple.toASCIIString());
    // Use the PostBody encoder
    HttpPostRequestEncoder bodyRequestEncoder = // false => not multipart
    new HttpPostRequestEncoder(factory, request, false);
    // it is legal to add directly header or cookie into the request until finalize
    for (Entry<String, String> entry : headers) {
        request.headers().set(entry.getKey(), entry.getValue());
    }
    // add Form attribute
    bodyRequestEncoder.addBodyAttribute("getform", "POST");
    bodyRequestEncoder.addBodyAttribute("info", "first value");
    bodyRequestEncoder.addBodyAttribute("secondinfo", "secondvalue ���&");
    bodyRequestEncoder.addBodyAttribute("thirdinfo", textArea);
    bodyRequestEncoder.addBodyAttribute("fourthinfo", textAreaLong);
    bodyRequestEncoder.addBodyFileUpload("myfile", file, "application/x-zip-compressed", false);
    // finalize request
    request = bodyRequestEncoder.finalizeRequest();
    // Create the bodylist to be reused on the last version with Multipart support
    List<InterfaceHttpData> bodylist = bodyRequestEncoder.getBodyListAttributes();
    // send request
    channel.write(request);
    // test if request was chunked and if so, finish the write
    if (bodyRequestEncoder.isChunked()) {
        // could do either request.isChunked()
        // either do it through ChunkedWriteHandler
        channel.write(bodyRequestEncoder);
    }
    channel.flush();
    // Do not clear here since we will reuse the InterfaceHttpData on the next request
    // for the example (limit action on client side). Take this as a broadcast of the same
    // request on both Post actions.
    // 
    // On standard program, it is clearly recommended to clean all files after each request
    // bodyRequestEncoder.cleanFiles();
    // Wait for the server to close the connection.
    channel.closeFuture().sync();
    return bodylist;
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) InterfaceHttpData(io.netty.handler.codec.http.multipart.InterfaceHttpData) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Channel(io.netty.channel.Channel) HttpPostRequestEncoder(io.netty.handler.codec.http.multipart.HttpPostRequestEncoder)

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