Search in sources :

Example 1 with FileItemProvider

use of ninja.uploads.FileItemProvider in project ninja by ninjaframework.

the class NinjaServletContext method processFormFields.

private void processFormFields() {
    if (formFieldsProcessed)
        return;
    formFieldsProcessed = true;
    // return if not multipart
    if (!ServletFileUpload.isMultipartContent(httpServletRequest))
        return;
    // get fileProvider from route method/class, or defaults to an injected one
    // if none injected, then we do not process form fields this way and let the user
    // call classic getFileItemIterator() by himself
    FileProvider fileProvider = null;
    if (route != null) {
        if (fileProvider == null) {
            fileProvider = route.getControllerMethod().getAnnotation(FileProvider.class);
        }
        if (fileProvider == null) {
            fileProvider = route.getControllerClass().getAnnotation(FileProvider.class);
        }
    }
    // get file item provider from file provider or default one
    FileItemProvider fileItemProvider = null;
    if (fileProvider == null) {
        fileItemProvider = injector.getInstance(FileItemProvider.class);
    } else {
        fileItemProvider = injector.getInstance(fileProvider.value());
    }
    if (fileItemProvider instanceof NoFileItemProvider)
        return;
    // Initialize maps and other constants
    ArrayListMultimap<String, String> formMap = ArrayListMultimap.create();
    ArrayListMultimap<String, FileItem> fileMap = ArrayListMultimap.create();
    // This is the iterator we can use to iterate over the contents of the request.
    try {
        FileItemIterator fileItemIterator = getFileItemIterator();
        while (fileItemIterator.hasNext()) {
            FileItemStream item = fileItemIterator.next();
            if (item.isFormField()) {
                String charset = NinjaConstant.UTF_8;
                String contentType = item.getContentType();
                if (contentType != null) {
                    charset = HttpHeaderUtils.getCharsetOfContentTypeOrUtf8(contentType);
                }
                // save the form field for later use from getParameter
                String value = Streams.asString(item.openStream(), charset);
                formMap.put(item.getFieldName(), value);
            } else {
                // process file as input stream and save for later use in getParameterAsFile or getParameterAsInputStream
                FileItem fileItem = fileItemProvider.create(item);
                fileMap.put(item.getFieldName(), fileItem);
            }
        }
    } catch (FileUploadException | IOException e) {
        throw new RuntimeException("Failed to parse multipart request data", e);
    }
    // convert both multimap<K,V> to map<K,List<V>>
    formFieldsMap = toUnmodifiableMap(formMap);
    fileFieldsMap = toUnmodifiableMap(fileMap);
}
Also used : NoFileItemProvider(ninja.uploads.NoFileItemProvider) FileItemProvider(ninja.uploads.FileItemProvider) NoFileItemProvider(ninja.uploads.NoFileItemProvider) IOException(java.io.IOException) FileItem(ninja.uploads.FileItem) FileItemStream(org.apache.commons.fileupload.FileItemStream) FileProvider(ninja.uploads.FileProvider) FileItemIterator(org.apache.commons.fileupload.FileItemIterator) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Aggregations

IOException (java.io.IOException)1 FileItem (ninja.uploads.FileItem)1 FileItemProvider (ninja.uploads.FileItemProvider)1 FileProvider (ninja.uploads.FileProvider)1 NoFileItemProvider (ninja.uploads.NoFileItemProvider)1 FileItemIterator (org.apache.commons.fileupload.FileItemIterator)1 FileItemStream (org.apache.commons.fileupload.FileItemStream)1 FileUploadException (org.apache.commons.fileupload.FileUploadException)1