Search in sources :

Example 1 with IFileItemHeaders

use of com.helger.web.fileupload.IFileItemHeaders in project ph-web by phax.

the class AbstractFileUploadBase method getParsedHeaders.

/**
 * <p>
 * Parses the <code>header-part</code> and returns as key/value pairs.
 * <p>
 * If there are multiple headers of the same names, the name will map to a
 * comma-separated list containing the values.
 *
 * @param sHeaderPart
 *        The <code>header-part</code> of the current
 *        <code>encapsulation</code>.
 * @return A <code>Map</code> containing the parsed HTTP request headers.
 */
@Nonnull
protected IFileItemHeaders getParsedHeaders(@Nonnull final String sHeaderPart) {
    final int nLen = sHeaderPart.length();
    final FileItemHeaders aHeaders = createFileItemHeaders();
    int nStart = 0;
    for (; ; ) {
        int nEnd = _parseEndOfLine(sHeaderPart, nStart);
        if (nStart == nEnd) {
            break;
        }
        final StringBuilder aHeader = new StringBuilder(sHeaderPart.substring(nStart, nEnd));
        nStart = nEnd + 2;
        while (nStart < nLen) {
            int nNonWs = nStart;
            while (nNonWs < nLen) {
                final char c = sHeaderPart.charAt(nNonWs);
                if (c != ' ' && c != '\t')
                    break;
                ++nNonWs;
            }
            if (nNonWs == nStart)
                break;
            // Continuation line found
            nEnd = _parseEndOfLine(sHeaderPart, nNonWs);
            aHeader.append(' ').append(sHeaderPart.substring(nNonWs, nEnd));
            nStart = nEnd + 2;
        }
        _parseHeaderLine(aHeaders, aHeader.toString());
    }
    return aHeaders;
}
Also used : IFileItemHeaders(com.helger.web.fileupload.IFileItemHeaders) Nonnull(javax.annotation.Nonnull)

Example 2 with IFileItemHeaders

use of com.helger.web.fileupload.IFileItemHeaders in project ph-web by phax.

the class AbstractFileUploadBase method parseRequest.

/**
 * Processes an <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>
 * compliant <code>multipart/form-data</code> stream.
 *
 * @param aCtx
 *        The context for the request to be parsed.
 * @return A list of <code>FileItem</code> instances parsed from the request,
 *         in the order that they were transmitted.
 * @throws FileUploadException
 *         if there are problems reading/parsing the request or storing files.
 */
@Nonnull
@ReturnsMutableCopy
public ICommonsList<IFileItem> parseRequest(@Nonnull final IRequestContext aCtx) throws FileUploadException {
    final ICommonsList<IFileItem> aItems = new CommonsArrayList<>();
    boolean bSuccessful = false;
    try {
        final IFileItemIterator aItemIter = getItemIterator(aCtx);
        final IFileItemFactory aFileItemFactory = getFileItemFactory();
        if (aFileItemFactory == null)
            throw new IllegalStateException("No FileItemFactory has been set.");
        while (aItemIter.hasNext()) {
            final IFileItemStream aFileItemStream = aItemIter.next();
            // Don't use getName() here to prevent an InvalidFileNameException.
            final IFileItem aFileItem = aFileItemFactory.createItem(aFileItemStream.getFieldName(), aFileItemStream.getContentType(), aFileItemStream.isFormField(), aFileItemStream.getNameUnchecked());
            aItems.add(aFileItem);
            try (final InputStream aIS = aFileItemStream.openStream();
                final OutputStream aOS = aFileItem.getOutputStream()) {
                final byte[] aBuffer = new byte[8192];
                int nBytesRead;
                // potentially blocking read
                while ((nBytesRead = aIS.read(aBuffer, 0, aBuffer.length)) > -1) {
                    aOS.write(aBuffer, 0, nBytesRead);
                }
            } catch (final FileUploadIOException ex) {
                throw (FileUploadException) ex.getCause();
            } catch (final IOException ex) {
                throw new IOFileUploadException("Processing of " + RequestHelper.MULTIPART_FORM_DATA + " request failed. " + ex.getMessage(), ex);
            }
            if (aFileItem instanceof IFileItemHeadersSupport) {
                final IFileItemHeaders aFileItemHeaders = aFileItemStream.getHeaders();
                ((IFileItemHeadersSupport) aFileItem).setHeaders(aFileItemHeaders);
            }
        }
        bSuccessful = true;
        return aItems;
    } catch (final FileUploadIOException ex) {
        throw (FileUploadException) ex.getCause();
    } catch (final IOException ex) {
        throw new FileUploadException(ex.getMessage(), ex);
    } finally {
        if (!bSuccessful) {
            // Delete all file items
            for (final IFileItem aFileItem : aItems) {
                try {
                    aFileItem.delete();
                } catch (final Exception ex) {
                    // ignore it
                    if (LOGGER.isErrorEnabled())
                        LOGGER.error("Failed to delete fileItem " + aFileItem, ex);
                }
            }
        }
    }
}
Also used : IFileItemHeaders(com.helger.web.fileupload.IFileItemHeaders) AbstractLimitedInputStream(com.helger.web.fileupload.io.AbstractLimitedInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileUploadIOException(com.helger.web.fileupload.exception.FileUploadIOException) IOException(java.io.IOException) SizeLimitExceededException(com.helger.web.fileupload.exception.SizeLimitExceededException) InvalidContentTypeException(com.helger.web.fileupload.exception.InvalidContentTypeException) FileUploadException(com.helger.web.fileupload.exception.FileUploadException) FileUploadIOException(com.helger.web.fileupload.exception.FileUploadIOException) NoSuchElementException(java.util.NoSuchElementException) IOException(java.io.IOException) IOFileUploadException(com.helger.web.fileupload.exception.IOFileUploadException) FileUploadIOException(com.helger.web.fileupload.exception.FileUploadIOException) IFileItemStream(com.helger.web.fileupload.IFileItemStream) IFileItemIterator(com.helger.web.fileupload.IFileItemIterator) IOFileUploadException(com.helger.web.fileupload.exception.IOFileUploadException) IFileItemHeadersSupport(com.helger.web.fileupload.IFileItemHeadersSupport) IFileItem(com.helger.web.fileupload.IFileItem) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) FileUploadException(com.helger.web.fileupload.exception.FileUploadException) IOFileUploadException(com.helger.web.fileupload.exception.IOFileUploadException) IFileItemFactory(com.helger.web.fileupload.IFileItemFactory) ReturnsMutableCopy(com.helger.commons.annotation.ReturnsMutableCopy) Nonnull(javax.annotation.Nonnull)

Aggregations

IFileItemHeaders (com.helger.web.fileupload.IFileItemHeaders)2 Nonnull (javax.annotation.Nonnull)2 ReturnsMutableCopy (com.helger.commons.annotation.ReturnsMutableCopy)1 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)1 IFileItem (com.helger.web.fileupload.IFileItem)1 IFileItemFactory (com.helger.web.fileupload.IFileItemFactory)1 IFileItemHeadersSupport (com.helger.web.fileupload.IFileItemHeadersSupport)1 IFileItemIterator (com.helger.web.fileupload.IFileItemIterator)1 IFileItemStream (com.helger.web.fileupload.IFileItemStream)1 FileUploadException (com.helger.web.fileupload.exception.FileUploadException)1 FileUploadIOException (com.helger.web.fileupload.exception.FileUploadIOException)1 IOFileUploadException (com.helger.web.fileupload.exception.IOFileUploadException)1 InvalidContentTypeException (com.helger.web.fileupload.exception.InvalidContentTypeException)1 SizeLimitExceededException (com.helger.web.fileupload.exception.SizeLimitExceededException)1 AbstractLimitedInputStream (com.helger.web.fileupload.io.AbstractLimitedInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 NoSuchElementException (java.util.NoSuchElementException)1