Search in sources :

Example 1 with UploadContext

use of org.apache.tomcat.util.http.fileupload.UploadContext in project tomcat by apache.

the class FileItemIteratorImpl method init.

protected void init(final FileUploadBase fileUploadBase, @SuppressWarnings("unused") final RequestContext pRequestContext) throws FileUploadException, IOException {
    final String contentType = ctx.getContentType();
    if ((null == contentType) || (!contentType.toLowerCase(Locale.ENGLISH).startsWith(FileUploadBase.MULTIPART))) {
        throw new InvalidContentTypeException(String.format("the request doesn't contain a %s or %s stream, content type header is %s", FileUploadBase.MULTIPART_FORM_DATA, FileUploadBase.MULTIPART_MIXED, contentType));
    }
    final long requestSize = ((UploadContext) ctx).contentLength();
    // N.B. this is eventually closed in MultipartStream processing
    final InputStream input;
    if (sizeMax >= 0) {
        if (requestSize != -1 && requestSize > sizeMax) {
            throw new SizeLimitExceededException(String.format("the request was rejected because its size (%s) exceeds the configured maximum (%s)", Long.valueOf(requestSize), Long.valueOf(sizeMax)), requestSize, sizeMax);
        }
        // N.B. this is eventually closed in MultipartStream processing
        input = new LimitedInputStream(ctx.getInputStream(), sizeMax) {

            @Override
            protected void raiseError(final long pSizeMax, final long pCount) throws IOException {
                final FileUploadException ex = new SizeLimitExceededException(String.format("the request was rejected because its size (%s) exceeds the configured maximum (%s)", Long.valueOf(pCount), Long.valueOf(pSizeMax)), pCount, pSizeMax);
                throw new FileUploadIOException(ex);
            }
        };
    } else {
        input = ctx.getInputStream();
    }
    String charEncoding = fileUploadBase.getHeaderEncoding();
    if (charEncoding == null) {
        charEncoding = ctx.getCharacterEncoding();
    }
    multiPartBoundary = fileUploadBase.getBoundary(contentType);
    if (multiPartBoundary == null) {
        // avoid possible resource leak
        IOUtils.closeQuietly(input);
        throw new FileUploadException("the request was rejected because no multipart boundary was found");
    }
    progressNotifier = new MultipartStream.ProgressNotifier(fileUploadBase.getProgressListener(), requestSize);
    try {
        multiPartStream = new MultipartStream(input, multiPartBoundary, progressNotifier);
    } catch (final IllegalArgumentException iae) {
        // avoid possible resource leak
        IOUtils.closeQuietly(input);
        throw new InvalidContentTypeException(String.format("The boundary specified in the %s header is too long", FileUploadBase.CONTENT_TYPE), iae);
    }
    multiPartStream.setHeaderEncoding(charEncoding);
}
Also used : LimitedInputStream(org.apache.tomcat.util.http.fileupload.util.LimitedInputStream) InputStream(java.io.InputStream) LimitedInputStream(org.apache.tomcat.util.http.fileupload.util.LimitedInputStream) IOException(java.io.IOException) UploadContext(org.apache.tomcat.util.http.fileupload.UploadContext) MultipartStream(org.apache.tomcat.util.http.fileupload.MultipartStream) FileUploadException(org.apache.tomcat.util.http.fileupload.FileUploadException)

Aggregations

IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 FileUploadException (org.apache.tomcat.util.http.fileupload.FileUploadException)1 MultipartStream (org.apache.tomcat.util.http.fileupload.MultipartStream)1 UploadContext (org.apache.tomcat.util.http.fileupload.UploadContext)1 LimitedInputStream (org.apache.tomcat.util.http.fileupload.util.LimitedInputStream)1