Example usage for io.netty.handler.codec.http.multipart HttpPostRequestDecoder isMultipart

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

Introduction

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

Prototype

public static boolean isMultipart(HttpRequest request) 

Source Link

Document

Check if the given request is a multipart request

Usage

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

License:Open Source License

/**
 * Handles a {@link HttpRequest}.//from www.  j a  v  a 2 s  .c om
 * <p/>
 * Does some state maintenance for all HTTP methods by creating a {@link RestRequest} wrapping this
 * {@link HttpRequest}
 * <p/>
 * In case of POST, delegates handling of {@link RestRequest} to the {@link RestRequestHandler}.
 * @param httpRequest the {@link HttpRequest} that needs to be handled.
 * @throws RestServiceException if there is an error handling the current {@link HttpRequest}.
 */
private void handleRequest(HttpRequest httpRequest) throws RestServiceException {
    if (responseChannel == null || responseChannel.isResponseComplete()) {
        long processingStartTime = System.currentTimeMillis();
        try {
            resetState();
            nettyMetrics.requestArrivalRate.mark();
            if (!httpRequest.getDecoderResult().isSuccess()) {
                logger.warn("Decoder failed because of malformed request on channel {}", ctx.channel());
                nettyMetrics.malformedRequestError.inc();
                throw new RestServiceException("Decoder failed because of malformed request",
                        RestServiceErrorCode.MalformedRequest);
            }
            // We need to maintain state about the request itself for the subsequent parts (if any) that come in. We will
            // attach content to the request as the content arrives.
            if (HttpPostRequestDecoder.isMultipart(httpRequest)) {
                nettyMetrics.multipartPostRequestRate.mark();
                request = new NettyMultipartRequest(httpRequest, nettyMetrics);
            } else {
                request = new NettyRequest(httpRequest, nettyMetrics);
            }
            responseChannel.setRequest(request);
            logger.trace("Channel {} now handling request {}", ctx.channel(), request.getUri());
            // We send POST that is not multipart for handling immediately since we expect valid content with it that will
            // be streamed in. In the case of POST that is multipart, all the content has to be received for Netty's
            // decoder and NettyMultipartRequest to work. So it is scheduled for handling when LastHttpContent is received.
            // With any other method that we support, we do not expect any valid content. LastHttpContent is a Netty thing.
            // So we wait for LastHttpContent (throw an error if we don't receive it or receive something else) and then
            // schedule the other methods for handling in handleContent().
            if (request.getRestMethod().equals(RestMethod.POST)
                    && !HttpPostRequestDecoder.isMultipart(httpRequest)) {
                requestHandler.handleRequest(request, responseChannel);
            }
        } finally {
            request.getMetricsTracker().nioMetricsTracker
                    .addToRequestProcessingTime(System.currentTimeMillis() - processingStartTime);
        }
    } else {
        // We have received a request when we were not expecting one. This shouldn't happen and there is no good way to
        // deal with it. So just update a metric and log an error.
        logger.warn(
                "Discarding unexpected request on channel {}. Request under processing: {}. Unexpected request: {}",
                ctx.channel(), request.getUri(), httpRequest.getUri());
        nettyMetrics.duplicateRequestError.inc();
    }
}

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

License:Open Source License

/**
 * Provides info on whether this request is multipart or not.
 * @return {@code true} if multipart. {@code false} otherwise.
 *//*from w  w w.ja va  2s .c om*/
protected boolean isMultipart() {
    return HttpPostRequestDecoder.isMultipart(request);
}