Search in sources :

Example 16 with HttpPostRequestEncoder

use of 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 17 with HttpPostRequestEncoder

use of 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 18 with HttpPostRequestEncoder

use of 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 19 with HttpPostRequestEncoder

use of 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)18 HttpRequest (io.netty.handler.codec.http.HttpRequest)11 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)8 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)8 DefaultHttpDataFactory (io.netty.handler.codec.http.multipart.DefaultHttpDataFactory)5 HttpDataFactory (io.netty.handler.codec.http.multipart.HttpDataFactory)5 HttpContent (io.netty.handler.codec.http.HttpContent)4 MemoryFileUpload (io.netty.handler.codec.http.multipart.MemoryFileUpload)4 DefaultHttpContent (io.netty.handler.codec.http.DefaultHttpContent)3 FileUpload (io.netty.handler.codec.http.multipart.FileUpload)3 Test (org.junit.Test)3 ResponseParts (com.github.ambry.rest.NettyClient.ResponseParts)2 Channel (io.netty.channel.Channel)2 ChannelFuture (io.netty.channel.ChannelFuture)2 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)2 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)2 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)2 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)2 InterfaceHttpData (io.netty.handler.codec.http.multipart.InterfaceHttpData)2 IOException (java.io.IOException)2