Example usage for org.apache.commons.fileupload.servlet ServletFileUpload setFileItemFactory

List of usage examples for org.apache.commons.fileupload.servlet ServletFileUpload setFileItemFactory

Introduction

In this page you can find the example usage for org.apache.commons.fileupload.servlet ServletFileUpload setFileItemFactory.

Prototype

public void setFileItemFactory(FileItemFactory factory) 

Source Link

Document

Sets the factory class to use when creating file items.

Usage

From source file:org.geoserver.ows.Dispatcher.java

Request init(Request request) throws ServiceException, IOException {
    HttpServletRequest httpRequest = request.getHttpRequest();

    String reqContentType = httpRequest.getContentType();
    //figure out method
    request.setGet("GET".equalsIgnoreCase(httpRequest.getMethod()) || isForm(reqContentType));

    //create the kvp map
    parseKVP(request);//from  w  w w  . j  a  v a 2 s  . co  m

    if (!request.isGet()) { // && httpRequest.getInputStream().available() > 0) {
        //check for a SOAP request, if so we need to unwrap the SOAP stuff
        if (httpRequest.getContentType() != null && httpRequest.getContentType().startsWith(SOAP_MIME)) {
            request.setSOAP(true);
            request.setInput(soapReader(httpRequest));
        } else if (reqContentType != null && ServletFileUpload.isMultipartContent(httpRequest)) {
            // multipart form upload
            ServletFileUpload up = new ServletFileUpload();
            up.setFileItemFactory(new DiskFileItemFactory());

            // treat regular form fields as additional kvp parameters
            Map<String, FileItem> kvpFileItems = new CaseInsensitiveMap(new LinkedHashMap());
            try {
                for (FileItem item : (List<FileItem>) up.parseRequest(httpRequest)) {
                    if (item.isFormField()) {
                        kvpFileItems.put(item.getFieldName(), item);
                    } else {
                        request.setInput(fileItemReader(item));
                    }
                }
            } catch (Exception e) {
                throw new ServiceException("Error handling multipart/form-data content", e);
            }

            // if no file fields were found, look for one named "body"
            if (request.getInput() == null) {
                FileItem body = kvpFileItems.get("body");
                if (body != null) {
                    request.setInput(fileItemReader(body));
                    kvpFileItems.remove("body");
                }
            }

            Map<String, String> kvpItems = new LinkedHashMap();
            for (Map.Entry<String, FileItem> e : kvpFileItems.entrySet()) {
                kvpItems.put(e.getKey(), e.getValue().toString());
            }

            request.setOrAppendKvp(parseKVP(request, kvpFileItems));
        } else {
            //regular XML POST
            //wrap the input stream in a buffered input stream
            request.setInput(reader(httpRequest));
        }

        char[] req = new char[xmlPostRequestLogBufferSize];
        int read = request.getInput().read(req, 0, xmlPostRequestLogBufferSize);

        if (logger.isLoggable(Level.FINE)) {
            if (read == -1) {
                request.setInput(null);
            } else if (read < xmlPostRequestLogBufferSize) {
                logger.fine("Raw XML request: " + new String(req));
            } else if (xmlPostRequestLogBufferSize == 0) {
                // logging disabled --> do nothing
            } else {
                logger.fine("Raw XML request starts with: " + new String(req) + "...");
            }
        }
        if (read == -1)
            request.setInput(null);
        else
            request.getInput().reset();
    }
    // parse the request path into two components. (1) the 'path' which
    // is the string after the last '/', and the 'context' which is the 
    // string before the last '/'
    String ctxPath = request.httpRequest.getContextPath();
    String reqPath = request.httpRequest.getRequestURI();
    reqPath = reqPath.substring(ctxPath.length());

    //strip off leading and trailing slashes
    if (reqPath.startsWith("/")) {
        reqPath = reqPath.substring(1, reqPath.length());
    }

    if (reqPath.endsWith("/")) {
        reqPath = reqPath.substring(0, reqPath.length() - 1);
    }

    String context = reqPath;
    String path = null;
    int index = context.lastIndexOf('/');
    if (index != -1) {
        path = context.substring(index + 1);
        context = context.substring(0, index);
    } else {
        path = reqPath;
        context = null;
    }

    request.setContext(context);
    request.setPath(path);

    return fireInitCallback(request);
}

From source file:org.openfaces.util.FileUploadRequestWrapper.java

public FileUploadRequestWrapper(HttpServletRequest request, String tempDirPath, final long maxSizeOfFile,
        String uniqueFileId) {//from  w  w  w  .  j  av a2s .  c o  m
    super(request);
    final String contentLength = request.getHeader("content-length");
    if (contentLength == null)
        return;

    try {
        ServletFileUpload upload = new ServletFileUpload();
        upload.setFileItemFactory(new ProgressMonitorFileItemFactory(request, maxSizeOfFile, uniqueFileId));
        List<FileItem> fileItems = upload.parseRequest(request);
        for (FileItem fileItem : fileItems) {
            if (!fileItem.isFormField()) {
                if (fileItem.getSize() != 0) {
                    if (request.getSession()
                            .getAttribute(uniqueFileId + AbstractFileUploadRenderer.TERMINATED_TEXT) == null) {
                        String correctFileName = getCorrectFileName(fileItem.getName());
                        File f = writeFile(fileItem, tempDirPath, correctFileName);
                        int index = fileItem.getFieldName().indexOf(FIELD_NAME);
                        String genericNameForFile = fileItem.getFieldName().substring(0,
                                index + FIELD_NAME.length());
                        request.setAttribute(genericNameForFile,
                                new FileUploadItem(correctFileName, f, FileUploadStatus.SUCCESSFUL));
                        request.setAttribute("FILE_ID", uniqueFileId);
                    } else {
                        request.getSession()
                                .removeAttribute(uniqueFileId + AbstractFileUploadRenderer.TERMINATED_TEXT);
                    }
                } else {
                    throw new RuntimeException("File size is equal 0 bytes");
                }
            }
        }

    } catch (FileUploadException fe) {
        /*this exception can happened in case if something wrong with file or we stopped manually*/
        request.getSession().setAttribute(uniqueFileId + AbstractFileUploadRenderer.TERMINATED_TEXT, true);
    } catch (IOException ne) {
        /*this exception can happened in case if problem in writing file*/
        request.getSession().setAttribute(uniqueFileId + AbstractFileUploadRenderer.TERMINATED_TEXT, true);
    } catch (Exception e) {
        /*this exception can happened if on server some problem*/
        request.getSession().setAttribute(uniqueFileId + AbstractFileUploadRenderer.TERMINATED_TEXT, true);
    }
}