Search in sources :

Example 11 with HttpPostRequestDecoder

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

the class DefaultSignalClientTestCase method testSignalClientWithAttachmentSuccess.

@Test
public void testSignalClientWithAttachmentSuccess() throws Exception {
    final TestResponse respToSendback = new TestResponse("0", "OK");
    final AtomicReference<HttpMethod> reqMethodReceivedRef = new AtomicReference<>();
    final AtomicReference<String> reqpathReceivedRef = new AtomicReference<>();
    final AtomicReference<TestRequestByPost> reqbeanReceivedRef = new AtomicReference<>();
    final List<FileUpload> uploads = new ArrayList<>();
    final Action2<FullHttpRequest, HttpTrade> requestAndTradeAwareWhenCompleted = new Action2<FullHttpRequest, HttpTrade>() {

        @Override
        public void call(final FullHttpRequest req, final HttpTrade trade) {
            reqMethodReceivedRef.set(req.method());
            reqpathReceivedRef.set(req.uri());
            HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(HTTP_DATA_FACTORY, req);
            // first is signal
            boolean isfirst = true;
            while (decoder.hasNext()) {
                final InterfaceHttpData data = decoder.next();
                if (!isfirst) {
                    if (data instanceof FileUpload) {
                        uploads.add((FileUpload) data);
                    }
                } else {
                    isfirst = false;
                    try {
                        reqbeanReceivedRef.set((TestRequestByPost) JSON.parseObject(Nettys.dumpByteBufAsBytes(((FileUpload) data).content()), TestRequestByPost.class));
                    } catch (Exception e) {
                        LOG.warn("exception when JSON.parseObject, detail: {}", ExceptionUtils.exception2detail(e));
                    }
                }
            }
            trade.outbound(buildResponse(respToSendback, trade.onTerminate()));
        }
    };
    // launch test server for attachment send
    final String testAddr = UUID.randomUUID().toString();
    final Subscription server = TestHttpUtil.createTestServerWith(testAddr, requestAndTradeAwareWhenCompleted, Feature.ENABLE_LOGGING, Feature.ENABLE_COMPRESSOR);
    try {
        final TestChannelCreator creator = new TestChannelCreator();
        final TestChannelPool pool = new TestChannelPool(1);
        final DefaultHttpClient httpclient = new DefaultHttpClient(creator, pool);
        final DefaultSignalClient signalClient = new DefaultSignalClient(new URI("http://test"), httpclient, new AttachmentBuilder4InMemory());
        signalClient.registerRequestType(TestRequestByPost.class, TestResponse.class, null, buildUri2Addr(testAddr), Feature.ENABLE_LOGGING);
        final AttachmentInMemory[] attachsToSend = new AttachmentInMemory[] { new AttachmentInMemory("1", "text/plain", "11111111111111".getBytes(Charsets.UTF_8)), new AttachmentInMemory("2", "text/plain", "22222222222222222".getBytes(Charsets.UTF_8)), new AttachmentInMemory("3", "text/plain", "333333333333333".getBytes(Charsets.UTF_8)) };
        final TestRequestByPost reqToSend = new TestRequestByPost("1", null);
        final TestResponse respReceived = ((SignalClient) signalClient).interaction().request(reqToSend).feature(attachsToSend).<TestResponse>build().timeout(1, TimeUnit.SECONDS).toBlocking().single();
        assertEquals(HttpMethod.POST, reqMethodReceivedRef.get());
        assertEquals("/test/simpleRequest", reqpathReceivedRef.get());
        assertEquals(reqToSend, reqbeanReceivedRef.get());
        assertEquals(respToSendback, respReceived);
        final FileUpload[] attachsReceived = uploads.toArray(new FileUpload[0]);
        assertEquals(attachsToSend.length, attachsReceived.length);
        for (int idx = 0; idx < attachsToSend.length; idx++) {
            final AttachmentInMemory inmemory = attachsToSend[idx];
            final FileUpload upload = attachsReceived[idx];
            assertEquals(inmemory.filename, upload.getName());
            assertEquals(inmemory.contentType, upload.getContentType());
            assertTrue(Arrays.equals(inmemory.content(), upload.get()));
        }
        pool.awaitRecycleChannels();
    } finally {
        server.unsubscribe();
    }
}
Also used : FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) ArrayList(java.util.ArrayList) URI(java.net.URI) DefaultHttpClient(org.jocean.http.client.impl.DefaultHttpClient) AttachmentInMemory(org.jocean.http.rosa.impl.AttachmentInMemory) HttpTrade(org.jocean.http.server.HttpServerBuilder.HttpTrade) DefaultSignalClient(org.jocean.http.rosa.impl.DefaultSignalClient) InterfaceHttpData(io.netty.handler.codec.http.multipart.InterfaceHttpData) AttachmentBuilder4InMemory(org.jocean.http.rosa.impl.AttachmentBuilder4InMemory) Subscription(rx.Subscription) FileUpload(io.netty.handler.codec.http.multipart.FileUpload) Action2(rx.functions.Action2) AtomicReference(java.util.concurrent.atomic.AtomicReference) HttpPostRequestDecoder(io.netty.handler.codec.http.multipart.HttpPostRequestDecoder) SSLException(javax.net.ssl.SSLException) IOException(java.io.IOException) TestChannelPool(org.jocean.http.client.impl.TestChannelPool) SignalClient(org.jocean.http.rosa.SignalClient) DefaultSignalClient(org.jocean.http.rosa.impl.DefaultSignalClient) HttpMethod(io.netty.handler.codec.http.HttpMethod) TestChannelCreator(org.jocean.http.client.impl.TestChannelCreator) Test(org.junit.Test)

Example 12 with HttpPostRequestDecoder

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

the class FormDecoder method parseForm.

@SuppressWarnings("deprecation")
public static Form parseForm(Context context, TypedData body, MultiValueMap<String, String> base) throws RuntimeException {
    Request request = context.getRequest();
    HttpMethod method = io.netty.handler.codec.http.HttpMethod.valueOf(request.getMethod().getName());
    HttpRequest nettyRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, method, request.getUri());
    nettyRequest.headers().add(HttpHeaderNames.CONTENT_TYPE, body.getContentType().toString());
    HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(nettyRequest);
    HttpContent content = new DefaultHttpContent(body.getBuffer());
    decoder.offer(content);
    decoder.offer(LastHttpContent.EMPTY_LAST_CONTENT);
    Map<String, List<String>> attributes = new LinkedHashMap<>(base.getAll());
    Map<String, List<UploadedFile>> files = new LinkedHashMap<>();
    try {
        InterfaceHttpData data = decoder.next();
        while (data != null) {
            if (data.getHttpDataType().equals(InterfaceHttpData.HttpDataType.Attribute)) {
                List<String> values = attributes.get(data.getName());
                if (values == null) {
                    values = new ArrayList<>(1);
                    attributes.put(data.getName(), values);
                }
                try {
                    values.add(((Attribute) data).getValue());
                } catch (IOException e) {
                    throw uncheck(e);
                }
            } else if (data.getHttpDataType().equals(InterfaceHttpData.HttpDataType.FileUpload)) {
                List<UploadedFile> values = files.computeIfAbsent(data.getName(), k -> new ArrayList<>(1));
                try {
                    FileUpload nettyFileUpload = (FileUpload) data;
                    final ByteBuf byteBuf = nettyFileUpload.getByteBuf();
                    byteBuf.retain();
                    context.onClose(ro -> byteBuf.release());
                    MediaType contentType;
                    String rawContentType = nettyFileUpload.getContentType();
                    if (rawContentType == null) {
                        contentType = null;
                    } else {
                        Charset charset = nettyFileUpload.getCharset();
                        if (charset == null) {
                            contentType = DefaultMediaType.get(rawContentType);
                        } else {
                            contentType = DefaultMediaType.get(rawContentType + ";charset=" + charset);
                        }
                    }
                    UploadedFile fileUpload = new DefaultUploadedFile(new ByteBufBackedTypedData(byteBuf, contentType), nettyFileUpload.getFilename());
                    values.add(fileUpload);
                } catch (IOException e) {
                    throw uncheck(e);
                }
            }
            data = decoder.next();
        }
    } catch (HttpPostRequestDecoder.EndOfDataDecoderException ignore) {
    // ignore
    } finally {
        decoder.destroy();
    }
    return new DefaultForm(new ImmutableDelegatingMultiValueMap<>(attributes), new ImmutableDelegatingMultiValueMap<>(files));
}
Also used : Context(ratpack.handling.Context) FileUpload(io.netty.handler.codec.http.multipart.FileUpload) MultiValueMap(ratpack.util.MultiValueMap) Form(ratpack.form.Form) ImmutableDelegatingMultiValueMap(ratpack.util.internal.ImmutableDelegatingMultiValueMap) Exceptions.uncheck(ratpack.util.Exceptions.uncheck) DefaultMediaType(ratpack.http.internal.DefaultMediaType) IOException(java.io.IOException) InterfaceHttpData(io.netty.handler.codec.http.multipart.InterfaceHttpData) UploadedFile(ratpack.form.UploadedFile) ArrayList(java.util.ArrayList) TypedData(ratpack.http.TypedData) LinkedHashMap(java.util.LinkedHashMap) Attribute(io.netty.handler.codec.http.multipart.Attribute) io.netty.handler.codec.http(io.netty.handler.codec.http) Request(ratpack.http.Request) List(java.util.List) ByteBuf(io.netty.buffer.ByteBuf) Charset(java.nio.charset.Charset) MediaType(ratpack.http.MediaType) ByteBufBackedTypedData(ratpack.http.internal.ByteBufBackedTypedData) Map(java.util.Map) HttpPostRequestDecoder(io.netty.handler.codec.http.multipart.HttpPostRequestDecoder) ArrayList(java.util.ArrayList) ByteBuf(io.netty.buffer.ByteBuf) LinkedHashMap(java.util.LinkedHashMap) InterfaceHttpData(io.netty.handler.codec.http.multipart.InterfaceHttpData) DefaultMediaType(ratpack.http.internal.DefaultMediaType) MediaType(ratpack.http.MediaType) ArrayList(java.util.ArrayList) List(java.util.List) ByteBufBackedTypedData(ratpack.http.internal.ByteBufBackedTypedData) FileUpload(io.netty.handler.codec.http.multipart.FileUpload) Request(ratpack.http.Request) Charset(java.nio.charset.Charset) IOException(java.io.IOException) HttpPostRequestDecoder(io.netty.handler.codec.http.multipart.HttpPostRequestDecoder) UploadedFile(ratpack.form.UploadedFile)

Example 13 with HttpPostRequestDecoder

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

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.HttpPostRequestDecoder in project vert.x by eclipse.

the class Http2ServerRequest method setExpectMultipart.

@Override
public HttpServerRequest setExpectMultipart(boolean expect) {
    synchronized (conn) {
        checkEnded();
        if (expect) {
            if (postRequestDecoder == null) {
                String contentType = headersMap.get(HttpHeaderNames.CONTENT_TYPE);
                if (contentType == null) {
                    throw new IllegalStateException("Request must have a content-type header to decode a multipart request");
                }
                if (!HttpUtils.isValidMultipartContentType(contentType)) {
                    throw new IllegalStateException("Request must have a valid content-type header to decode a multipart request");
                }
                if (!HttpUtils.isValidMultipartMethod(method.toNetty())) {
                    throw new IllegalStateException("Request method must be one of POST, PUT, PATCH or DELETE to decode a multipart request");
                }
                HttpRequest req = new DefaultHttpRequest(io.netty.handler.codec.http.HttpVersion.HTTP_1_1, method.toNetty(), uri);
                req.headers().add(HttpHeaderNames.CONTENT_TYPE, contentType);
                NettyFileUploadDataFactory factory = new NettyFileUploadDataFactory(context, this, () -> uploadHandler);
                factory.setMaxLimit(conn.options.getMaxFormAttributeSize());
                postRequestDecoder = new HttpPostRequestDecoder(factory, req);
            }
        } else {
            postRequestDecoder = null;
        }
    }
    return this;
}
Also used : DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpPostRequestDecoder(io.netty.handler.codec.http.multipart.HttpPostRequestDecoder)

Example 15 with HttpPostRequestDecoder

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

the class HttpUploadServerHandler method channelRead0.

@Override
public void channelRead0(final ChannelHandlerContext ctx, final HttpObject msg) throws Exception {
    if (msg instanceof HttpRequest) {
        final HttpRequest request = this.request = (HttpRequest) msg;
        responseContent.setLength(0);
        if (request.getMethod().equals(HttpMethod.POST)) {
            final URI uri = new URI(request.getUri());
            String signature = null;
            String expires = null;
            String metadata = null;
            String hostname = null;
            long contentLength = 0;
            for (final Entry<String, String> entry : request.headers()) {
                switch(entry.getKey()) {
                    case HEADER_SIGNATURE:
                        signature = entry.getValue();
                        break;
                    case HEADER_METADATA:
                        metadata = entry.getValue();
                        break;
                    case HEADER_EXPIRES:
                        expires = entry.getValue();
                        break;
                    case HEADER_HOST:
                        hostname = entry.getValue();
                        break;
                    case HttpHeaders.Names.CONTENT_LENGTH:
                        contentLength = Long.parseLong(entry.getValue());
                        break;
                }
            }
            logger.info("HEADER: signature=" + signature);
            logger.info("HEADER: metadata=" + metadata);
            logger.info("HEADER: expires=" + expires);
            logger.info("HEADER: hostname=" + hostname);
            logger.info("HEADER: Content-Length=" + contentLength);
            final QueryStringDecoder decoderQuery = new QueryStringDecoder(uri);
            final Map<String, List<String>> uriAttributes = decoderQuery.parameters();
            uuid = uriAttributes.get("uuid").get(0);
            logger.info("URI: uuid=" + uuid);
            UploadEntity uploadEntity = null;
            try {
                // Validate the request here
                storageResource.validatePostUploadRequest(signature, metadata, expires, hostname, contentLength, uuid);
                // create an upload entity. This will fail if entity already exists.
                uploadEntity = storageResource.createUploadEntity(uuid, metadata, contentLength);
            } catch (final InvalidParameterValueException ex) {
                logger.error("post request validation failed", ex);
                responseContent.append(ex.getMessage());
                writeResponse(ctx.channel(), HttpResponseStatus.BAD_REQUEST);
                requestProcessed = true;
                return;
            }
            if (uploadEntity == null) {
                logger.error("Unable to create upload entity. An exception occurred.");
                responseContent.append("Internal Server Error");
                writeResponse(ctx.channel(), HttpResponseStatus.INTERNAL_SERVER_ERROR);
                requestProcessed = true;
                return;
            }
            // set the base directory to download the file
            DiskFileUpload.baseDirectory = uploadEntity.getInstallPathPrefix();
            logger.info("base directory: " + DiskFileUpload.baseDirectory);
            try {
                // initialize the decoder
                decoder = new HttpPostRequestDecoder(factory, request);
            } catch (ErrorDataDecoderException | IncompatibleDataDecoderException e) {
                logger.error("exception while initialising the decoder", e);
                responseContent.append(e.getMessage());
                writeResponse(ctx.channel(), HttpResponseStatus.INTERNAL_SERVER_ERROR);
                requestProcessed = true;
                return;
            }
        } else {
            logger.warn("received a get request");
            responseContent.append("only post requests are allowed");
            writeResponse(ctx.channel(), HttpResponseStatus.BAD_REQUEST);
            requestProcessed = true;
            return;
        }
    }
    // check if the decoder was constructed before
    if (decoder != null) {
        if (msg instanceof HttpContent) {
            // New chunk is received
            final HttpContent chunk = (HttpContent) msg;
            try {
                decoder.offer(chunk);
            } catch (final ErrorDataDecoderException e) {
                logger.error("data decoding exception", e);
                responseContent.append(e.getMessage());
                writeResponse(ctx.channel(), HttpResponseStatus.INTERNAL_SERVER_ERROR);
                requestProcessed = true;
                return;
            }
            if (chunk instanceof LastHttpContent) {
                writeResponse(ctx.channel(), readFileUploadData());
                reset();
            }
        }
    }
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) IncompatibleDataDecoderException(io.netty.handler.codec.http.multipart.HttpPostRequestDecoder.IncompatibleDataDecoderException) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) URI(java.net.URI) HttpPostRequestDecoder(io.netty.handler.codec.http.multipart.HttpPostRequestDecoder) QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) UploadEntity(com.cloud.storage.template.UploadEntity) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) List(java.util.List) ErrorDataDecoderException(io.netty.handler.codec.http.multipart.HttpPostRequestDecoder.ErrorDataDecoderException) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent)

Aggregations

HttpPostRequestDecoder (io.netty.handler.codec.http.multipart.HttpPostRequestDecoder)16 IOException (java.io.IOException)9 InterfaceHttpData (io.netty.handler.codec.http.multipart.InterfaceHttpData)7 List (java.util.List)7 QueryStringDecoder (io.netty.handler.codec.http.QueryStringDecoder)6 Attribute (io.netty.handler.codec.http.multipart.Attribute)6 ArrayList (java.util.ArrayList)6 HttpRequest (io.netty.handler.codec.http.HttpRequest)5 HttpContent (io.netty.handler.codec.http.HttpContent)4 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)4 URI (java.net.URI)4 ErrorDataDecoderException (io.netty.handler.codec.http.multipart.HttpPostRequestDecoder.ErrorDataDecoderException)3 ByteBuf (io.netty.buffer.ByteBuf)2 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)2 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)2 HttpMethod (io.netty.handler.codec.http.HttpMethod)2 FileUpload (io.netty.handler.codec.http.multipart.FileUpload)2 IncompatibleDataDecoderException (io.netty.handler.codec.http.multipart.HttpPostRequestDecoder.IncompatibleDataDecoderException)2 File (java.io.File)2 Charset (java.nio.charset.Charset)2