Search in sources :

Example 1 with TransferEncodingMechanism

use of io.netty.handler.codec.http.multipart.HttpPostBodyUtil.TransferEncodingMechanism in project netty by netty.

the class HttpPostMultipartRequestDecoder method getFileUpload.

/**
     * Get the FileUpload (new one or current one)
     *
     * @param delimiter
     *            the delimiter to use
     * @return the InterfaceHttpData if any
     * @throws ErrorDataDecoderException
     */
protected InterfaceHttpData getFileUpload(String delimiter) {
    // eventually restart from existing FileUpload
    // Now get value according to Content-Type and Charset
    Attribute encoding = currentFieldAttributes.get(HttpHeaderNames.CONTENT_TRANSFER_ENCODING);
    Charset localCharset = charset;
    // Default
    TransferEncodingMechanism mechanism = TransferEncodingMechanism.BIT7;
    if (encoding != null) {
        String code;
        try {
            code = encoding.getValue().toLowerCase();
        } catch (IOException e) {
            throw new ErrorDataDecoderException(e);
        }
        if (code.equals(HttpPostBodyUtil.TransferEncodingMechanism.BIT7.value())) {
            localCharset = CharsetUtil.US_ASCII;
        } else if (code.equals(HttpPostBodyUtil.TransferEncodingMechanism.BIT8.value())) {
            localCharset = CharsetUtil.ISO_8859_1;
            mechanism = TransferEncodingMechanism.BIT8;
        } else if (code.equals(HttpPostBodyUtil.TransferEncodingMechanism.BINARY.value())) {
            // no real charset, so let the default
            mechanism = TransferEncodingMechanism.BINARY;
        } else {
            throw new ErrorDataDecoderException("TransferEncoding Unknown: " + code);
        }
    }
    Attribute charsetAttribute = currentFieldAttributes.get(HttpHeaderValues.CHARSET);
    if (charsetAttribute != null) {
        try {
            localCharset = Charset.forName(charsetAttribute.getValue());
        } catch (IOException e) {
            throw new ErrorDataDecoderException(e);
        } catch (UnsupportedCharsetException e) {
            throw new ErrorDataDecoderException(e);
        }
    }
    if (currentFileUpload == null) {
        Attribute filenameAttribute = currentFieldAttributes.get(HttpHeaderValues.FILENAME);
        Attribute nameAttribute = currentFieldAttributes.get(HttpHeaderValues.NAME);
        Attribute contentTypeAttribute = currentFieldAttributes.get(HttpHeaderNames.CONTENT_TYPE);
        Attribute lengthAttribute = currentFieldAttributes.get(HttpHeaderNames.CONTENT_LENGTH);
        long size;
        try {
            size = lengthAttribute != null ? Long.parseLong(lengthAttribute.getValue()) : 0L;
        } catch (IOException e) {
            throw new ErrorDataDecoderException(e);
        } catch (NumberFormatException ignored) {
            size = 0;
        }
        try {
            String contentType;
            if (contentTypeAttribute != null) {
                contentType = contentTypeAttribute.getValue();
            } else {
                contentType = HttpPostBodyUtil.DEFAULT_BINARY_CONTENT_TYPE;
            }
            currentFileUpload = factory.createFileUpload(request, cleanString(nameAttribute.getValue()), cleanString(filenameAttribute.getValue()), contentType, mechanism.value(), localCharset, size);
        } catch (NullPointerException e) {
            throw new ErrorDataDecoderException(e);
        } catch (IllegalArgumentException e) {
            throw new ErrorDataDecoderException(e);
        } catch (IOException e) {
            throw new ErrorDataDecoderException(e);
        }
    }
    // load data as much as possible
    try {
        readFileUploadByteMultipart(delimiter);
    } catch (NotEnoughDataDecoderException e) {
        // So do not change the currentStatus
        return null;
    }
    if (currentFileUpload.isCompleted()) {
        // ready to load the next one
        if (currentStatus == MultiPartStatus.FILEUPLOAD) {
            currentStatus = MultiPartStatus.HEADERDELIMITER;
            currentFieldAttributes = null;
        } else {
            currentStatus = MultiPartStatus.MIXEDDELIMITER;
            cleanMixedAttributes();
        }
        FileUpload fileUpload = currentFileUpload;
        currentFileUpload = null;
        return fileUpload;
    }
    // So do not change the currentStatus
    return null;
}
Also used : TransferEncodingMechanism(io.netty.handler.codec.http.multipart.HttpPostBodyUtil.TransferEncodingMechanism) Charset(java.nio.charset.Charset) NotEnoughDataDecoderException(io.netty.handler.codec.http.multipart.HttpPostRequestDecoder.NotEnoughDataDecoderException) IOException(java.io.IOException) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) ErrorDataDecoderException(io.netty.handler.codec.http.multipart.HttpPostRequestDecoder.ErrorDataDecoderException)

Aggregations

TransferEncodingMechanism (io.netty.handler.codec.http.multipart.HttpPostBodyUtil.TransferEncodingMechanism)1 ErrorDataDecoderException (io.netty.handler.codec.http.multipart.HttpPostRequestDecoder.ErrorDataDecoderException)1 NotEnoughDataDecoderException (io.netty.handler.codec.http.multipart.HttpPostRequestDecoder.NotEnoughDataDecoderException)1 IOException (java.io.IOException)1 Charset (java.nio.charset.Charset)1 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)1