Search in sources :

Example 11 with ServletRequestContext

use of org.apache.commons.fileupload.servlet.ServletRequestContext in project fabric8 by jboss-fuse.

the class MavenUploadProxyServlet method doPut.

@Override
protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    try {
        String path = req.getPathInfo();
        // Make sure path is valid
        if (path == null) {
            resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            return;
        }
        if (path.startsWith("/")) {
            path = path.substring(1);
        }
        UploadContext result = null;
        // handle move
        String location = req.getHeader(LOCATION_HEADER);
        if (location != null) {
            result = move(location, path);
        } else {
            // is it multipart data?
            if (FileUploadBase.isMultipartContent(new ServletRequestContext(req))) {
                List<FileItem> items = new ServletFileUpload(fileItemFactory).parseRequest(req);
                // What to do with multiple paths?
                if (items != null && items.size() > 0) {
                    FileItem item = items.get(0);
                    result = doUpload(item.getInputStream(), path);
                    item.delete();
                }
            } else {
                result = doUpload(req.getInputStream(), path);
            }
        }
        if (result != null && result.status()) {
            handleDeploy(req, result);
            addHeaders(resp, result.headers());
            resp.setStatus(HttpServletResponse.SC_ACCEPTED);
        } else {
            resp.setStatus(HttpServletResponse.SC_NOT_ACCEPTABLE);
        }
    } catch (InvalidMavenArtifactRequest ex) {
        // must response with status and flush as Jetty may report org.eclipse.jetty.server.Response Committed before 401 null
        resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        resp.setContentLength(0);
        resp.flushBuffer();
    } catch (Exception ex) {
        // must response with status and flush as Jetty may report org.eclipse.jetty.server.Response Committed before 401 null
        resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        resp.setContentLength(0);
        resp.flushBuffer();
    }
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) ServletRequestContext(org.apache.commons.fileupload.servlet.ServletRequestContext) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 12 with ServletRequestContext

use of org.apache.commons.fileupload.servlet.ServletRequestContext in project wicket by apache.

the class MultipartServletWebRequestImpl method parseFileParts.

@Override
public void parseFileParts() throws FileUploadException {
    HttpServletRequest request = getContainerRequest();
    // The encoding that will be used to decode the string parameters
    // It should NOT be null at this point, but it may be
    // especially if the older Servlet API 2.2 is used
    String encoding = request.getCharacterEncoding();
    // the attribute 'accept-encoding' in wicket forms.
    if (encoding == null) {
        encoding = Application.get().getRequestCycleSettings().getResponseRequestEncoding();
    }
    FileUploadBase fileUpload = newFileUpload(encoding);
    List<FileItem> items;
    if (wantUploadProgressUpdates()) {
        ServletRequestContext ctx = new ServletRequestContext(request) {

            @Override
            public InputStream getInputStream() throws IOException {
                return new CountingInputStream(super.getInputStream());
            }
        };
        totalBytes = request.getContentLength();
        onUploadStarted(totalBytes);
        try {
            items = fileUpload.parseRequest(ctx);
        } finally {
            onUploadCompleted();
        }
    } else {
        // try to parse the file uploads by using Apache Commons FileUpload APIs
        // because they are feature richer (e.g. progress updates, cleaner)
        items = fileUpload.parseRequest(new ServletRequestContext(request));
        if (items.isEmpty()) {
            // fallback to Servlet 3.0 APIs
            items = readServlet3Parts(request);
        }
    }
    // Loop through items
    for (final FileItem item : items) {
        // If item is a form field
        if (item.isFormField()) {
            // Set parameter value
            final String value;
            if (encoding != null) {
                try {
                    value = item.getString(encoding);
                } catch (UnsupportedEncodingException e) {
                    throw new WicketRuntimeException(e);
                }
            } else {
                value = item.getString();
            }
            addParameter(item.getFieldName(), value);
        } else {
            List<FileItem> fileItems = files.get(item.getFieldName());
            if (fileItems == null) {
                fileItems = new ArrayList<>();
                files.put(item.getFieldName(), fileItems);
            }
            // Add to file list
            fileItems.add(item);
        }
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) FileItem(org.apache.commons.fileupload.FileItem) FileUploadBase(org.apache.commons.fileupload.FileUploadBase) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) ServletRequestContext(org.apache.commons.fileupload.servlet.ServletRequestContext) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

ServletRequestContext (org.apache.commons.fileupload.servlet.ServletRequestContext)12 FileItem (org.apache.commons.fileupload.FileItem)10 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)10 DiskFileItemFactory (org.apache.commons.fileupload.disk.DiskFileItemFactory)9 File (java.io.File)6 FileUploadException (org.apache.commons.fileupload.FileUploadException)6 RequestContext (org.apache.commons.fileupload.RequestContext)5 IOException (java.io.IOException)4 InputStream (java.io.InputStream)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 ServletException (javax.servlet.ServletException)4 ArrayList (java.util.ArrayList)3 FileItemFactory (org.apache.commons.fileupload.FileItemFactory)3 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2 List (java.util.List)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 Part (javax.servlet.http.Part)2 FileUploadBase (org.apache.commons.fileupload.FileUploadBase)2 DiskFileItem (org.apache.commons.fileupload.disk.DiskFileItem)2