All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.elopteryx.upload.rs.internal.RestUploadParser Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
package com.github.elopteryx.upload.rs.internal;

import static java.nio.charset.StandardCharsets.ISO_8859_1;

import com.github.elopteryx.upload.errors.RequestSizeException;
import com.github.elopteryx.upload.internal.BlockingUploadParser;
import com.github.elopteryx.upload.internal.MultipartParser;
import com.github.elopteryx.upload.internal.Headers;
import com.github.elopteryx.upload.internal.PartStreamImpl;
import com.github.elopteryx.upload.internal.UploadContextImpl;
import com.github.elopteryx.upload.rs.Part;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.List;
import java.util.stream.Collectors;

/**
 * A subclass of the blocking parser. It doesn't have a dependency
 * on the servlet request and can be initialized from the header values.
 * This makes it ideal for a Jax-Rs environment, to be used in a
 * message body reader.
 */
public class RestUploadParser extends BlockingUploadParser {

    /**
     * Public constructor.
     */
    public RestUploadParser() {
        super(null);
    }

    /**
     * Initializes the parser from the given parameters and performs
     * a blocking parse.
     * @param contentLength The length of the request
     * @param mimeType The content type of the request
     * @param encoding The character encoding of the request
     * @param stream The request stream
     * @return The multipart object, representing the request
     * @throws IOException If an error occurred with the I/O
     */
    public MultiPartImpl doBlockingParse(long contentLength, String mimeType, String encoding, InputStream stream)
    throws IOException {
        if (maxRequestSize > -1) {
            if (contentLength > maxRequestSize) {
                throw new RequestSizeException("The size of the request ("
                        + contentLength
                        + ") is greater than the allowed size (" + maxRequestSize + ")!",
                        contentLength, maxRequestSize);
            }
        }

        checkBuffer = ByteBuffer.allocate(sizeThreshold);
        context = new UploadContextImpl(null, null);
        dataBuffer = ByteBuffer.allocate(maxBytesUsed / 2);

        String boundary;
        if (mimeType != null && mimeType.startsWith(MULTIPART_FORM_DATA)) {
            boundary = Headers.extractBoundaryFromHeader(mimeType);
            if (boundary == null) {
                throw new IllegalArgumentException("Could not find boundary in multipart request with ContentType: "
                        + mimeType
                        + ", multipart data will not be available");
            }
            Charset charset = encoding != null ? Charset.forName(encoding) : ISO_8859_1;
            parseState = MultipartParser.beginParse(this, boundary.getBytes(), maxBytesUsed, charset);

            inputStream = stream;
        }
        blockingRead();
        List parts = context.getPartStreams()
                .stream()
                .map(PartStreamImpl.class::cast)
                .map(PartImpl::new)
                .collect(Collectors.toList());
        return new MultiPartImpl(parts, requestSize);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy