Search in sources :

Example 6 with DefaultHttpDataFactory

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

the class FormsRequestExtractor method doExtract.

@Override
protected Optional<ImmutableMap<String, String>> doExtract(final HttpRequest request) {
    HttpPostRequestDecoder decoder = null;
    try {
        FullHttpRequest targetRequest = ((DefaultHttpRequest) request).toFullHttpRequest();
        Charset charset = HttpUtil.getCharset(targetRequest);
        HttpDataFactory factory = new DefaultHttpDataFactory(DefaultHttpDataFactory.MINSIZE, charset);
        decoder = new HttpPostRequestDecoder(factory, targetRequest, charset);
        return of(doExtractForms(decoder));
    } catch (HttpPostRequestDecoder.ErrorDataDecoderException idde) {
        return Optional.empty();
    } finally {
        if (decoder != null) {
            decoder.destroy();
        }
    }
}
Also used : FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultHttpRequest(com.github.dreamhead.moco.model.DefaultHttpRequest) DefaultHttpDataFactory(io.netty.handler.codec.http.multipart.DefaultHttpDataFactory) Charset(java.nio.charset.Charset) HttpPostRequestDecoder(io.netty.handler.codec.http.multipart.HttpPostRequestDecoder) DefaultHttpDataFactory(io.netty.handler.codec.http.multipart.DefaultHttpDataFactory) HttpDataFactory(io.netty.handler.codec.http.multipart.HttpDataFactory)

Example 7 with DefaultHttpDataFactory

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

Example 8 with DefaultHttpDataFactory

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

the class NettyMultipartRequestTest method createEncoder.

/**
 * Creates a {@link HttpPostRequestEncoder} that encodes the given {@code request} and {@code parts}.
 * @param request the {@link HttpRequest} containing headers and other metadata about the request.
 * @param parts the {@link InMemoryFile}s that will form the parts of the request.
 * @return a {@link HttpPostRequestEncoder} that can encode the {@code request} and {@code parts}.
 * @throws HttpPostRequestEncoder.ErrorDataEncoderException
 * @throws IOException
 */
private HttpPostRequestEncoder createEncoder(HttpRequest request, InMemoryFile[] parts) throws HttpPostRequestEncoder.ErrorDataEncoderException, IOException {
    HttpDataFactory httpDataFactory = new DefaultHttpDataFactory(false);
    HttpPostRequestEncoder encoder = new HttpPostRequestEncoder(httpDataFactory, request, true);
    if (parts != null) {
        for (InMemoryFile part : parts) {
            FileUpload fileUpload = new MemoryFileUpload(part.name, part.name, "application/octet-stream", "", Charset.forName("UTF-8"), part.content.remaining());
            fileUpload.setContent(Unpooled.wrappedBuffer(part.content));
            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) DefaultHttpDataFactory(io.netty.handler.codec.http.multipart.DefaultHttpDataFactory) HttpDataFactory(io.netty.handler.codec.http.multipart.HttpDataFactory)

Example 9 with DefaultHttpDataFactory

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

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

Aggregations

DefaultHttpDataFactory (io.netty.handler.codec.http.multipart.DefaultHttpDataFactory)10 HttpDataFactory (io.netty.handler.codec.http.multipart.HttpDataFactory)8 HttpPostRequestEncoder (io.netty.handler.codec.http.multipart.HttpPostRequestEncoder)6 MemoryFileUpload (io.netty.handler.codec.http.multipart.MemoryFileUpload)5 FileUpload (io.netty.handler.codec.http.multipart.FileUpload)4 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)2 HttpRequest (io.netty.handler.codec.http.HttpRequest)2 InterfaceHttpData (io.netty.handler.codec.http.multipart.InterfaceHttpData)2 File (java.io.File)2 DefaultHttpRequest (com.github.dreamhead.moco.model.DefaultHttpRequest)1 Bootstrap (io.netty.bootstrap.Bootstrap)1 EventLoopGroup (io.netty.channel.EventLoopGroup)1 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)1 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)1 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)1 HttpMethod (io.netty.handler.codec.http.HttpMethod)1 HttpVersion (io.netty.handler.codec.http.HttpVersion)1 DiskFileUpload (io.netty.handler.codec.http.multipart.DiskFileUpload)1 HttpPostMultipartRequestDecoder (io.netty.handler.codec.http.multipart.HttpPostMultipartRequestDecoder)1