Example usage for io.netty.handler.codec.http.multipart HttpPostMultipartRequestDecoder HttpPostMultipartRequestDecoder

List of usage examples for io.netty.handler.codec.http.multipart HttpPostMultipartRequestDecoder HttpPostMultipartRequestDecoder

Introduction

In this page you can find the example usage for io.netty.handler.codec.http.multipart HttpPostMultipartRequestDecoder HttpPostMultipartRequestDecoder.

Prototype

public HttpPostMultipartRequestDecoder(HttpDataFactory factory, HttpRequest request) 

Source Link

Usage

From source file:com.github.ambry.rest.NettyMultipartRequest.java

License:Open Source License

/**
 * {@inheritDoc}/*from   ww  w.java  2  s .  com*/
 * <p/>
 * Prepares the request for reading by decoding all the content added via {@link #addContent(HttpContent)}.
 * @throws RestServiceException if request channel is closed or if the request could not be decoded/prepared.
 */
@Override
public void prepare() throws RestServiceException {
    if (!isOpen()) {
        nettyMetrics.multipartRequestAlreadyClosedError.inc();
        throw new RestServiceException("Request is closed", RestServiceErrorCode.RequestChannelClosed);
    } else if (!readyForRead) {
        // make sure data is held in memory.
        HttpDataFactory httpDataFactory = new DefaultHttpDataFactory(false);
        HttpPostMultipartRequestDecoder postRequestDecoder = new HttpPostMultipartRequestDecoder(
                httpDataFactory, request);
        try {
            HttpContent httpContent = rawRequestContents.poll();
            while (httpContent != null) {
                try {
                    // if the request is also an instance of HttpContent, the HttpPostMultipartRequestDecoder does the offer
                    // automatically at the time of construction. We should not add it again.
                    if (httpContent != request) {
                        postRequestDecoder.offer(httpContent);
                    }
                } finally {
                    ReferenceCountUtil.release(httpContent);
                }
                httpContent = rawRequestContents.poll();
            }
            for (InterfaceHttpData part : postRequestDecoder.getBodyHttpDatas()) {
                processPart(part);
            }
            allArgsReadOnly = Collections.unmodifiableMap(allArgs);
            requestContents.add(LastHttpContent.EMPTY_LAST_CONTENT);
            readyForRead = true;
        } catch (HttpPostRequestDecoder.ErrorDataDecoderException e) {
            nettyMetrics.multipartRequestDecodeError.inc();
            throw new RestServiceException("There was an error decoding the request", e,
                    RestServiceErrorCode.MalformedRequest);
        } finally {
            postRequestDecoder.destroy();
        }
    }
}

From source file:org.robotbrains.support.web.server.netty.NettyWebServerHandler.java

License:Apache License

/**
 * Attempt to handle an HTTP PUT request.
 *
 * @param context//from w w w.  j  a v a2 s. c  om
 *          the context for the request
 * @param request
 *          the request
 * @param authResponse
 *          the authentication response
 *
 * @return {@code true} if the request was handled
 *
 * @throws IOException
 *           an IO exception happened
 */
private boolean handleWebPostRequest(ChannelHandlerContext context, HttpRequest request,
        HttpAuthResponse authResponse) throws IOException {
    if (request.getMethod() != POST) {
        return false;
    }

    NettyHttpPostRequestHandler postRequestHandler = webServer.locatePostRequestHandler(request);
    if (postRequestHandler == null) {
        return false;
    }

    if (!canUserAccessResource(authResponse, request.getUri())) {
        return false;
    }

    Set<HttpCookie> cookies = null;
    if (authResponse != null) {
        cookies = authResponse.getCookies();
    }

    try {
        NettyHttpFileUpload fileUpload = new NettyHttpFileUpload(request,
                new HttpPostMultipartRequestDecoder(HTTP_DATA_FACTORY, request), postRequestHandler, this,
                cookies);

        if (request instanceof HttpChunkedInput) {
            // Chunked data so more coming.
            fileUploadHandlers.put(((ChannelWithId) context.channel()).getId(), fileUpload);
        } else {
            fileUpload.completeNonChunked();

            fileUpload.fileUploadComplete(context);
        }
    } catch (Throwable e) {
        webServer.getLog().error("Could not start file upload", e);

        sendError(context, HttpResponseStatus.INTERNAL_SERVER_ERROR);
    }

    return true;
}