Search in sources :

Example 16 with FileItem

use of org.apache.commons.fileupload.FileItem in project felix by apache.

the class WebConsolePlugin method doPost.

/**
 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
 */
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    // get the uploaded data
    final String action = WebConsoleUtil.getParameter(req, Util.PARAM_ACTION);
    if (ACTION_DEPLOY.equals(action)) {
        Map params = (Map) req.getAttribute(AbstractWebConsolePlugin.ATTR_FILEUPLOAD);
        if (params != null) {
            final FileItem pck = getFileItem(params, PARAMETER_PCK_FILE, false);
            final DeploymentAdmin admin = (DeploymentAdmin) adminTracker.getService();
            if (admin != null) {
                try {
                    admin.installDeploymentPackage(pck.getInputStream());
                    final String uri = req.getRequestURI();
                    resp.sendRedirect(uri);
                    return;
                } catch (/*Deployment*/
                Exception e) {
                    throw new ServletException("Unable to deploy package.", e);
                }
            }
        }
        throw new ServletException("Upload file or deployment admin missing.");
    } else if (ACTION_UNINSTALL.equals(action)) {
        final String pckId = req.getPathInfo().substring(req.getPathInfo().lastIndexOf('/') + 1);
        if (pckId != null && pckId.length() > 0) {
            final DeploymentAdmin admin = (DeploymentAdmin) adminTracker.getService();
            if (admin != null) {
                try {
                    final DeploymentPackage pck = admin.getDeploymentPackage(pckId);
                    if (pck != null) {
                        pck.uninstall();
                    }
                } catch (/*Deployment*/
                Exception e) {
                    throw new ServletException("Unable to undeploy package.", e);
                }
            }
        }
        final PrintWriter pw = resp.getWriter();
        pw.println("{ \"reload\":true }");
        return;
    }
    throw new ServletException("Unknown action: " + action);
}
Also used : DeploymentAdmin(org.osgi.service.deploymentadmin.DeploymentAdmin) ServletException(javax.servlet.ServletException) FileItem(org.apache.commons.fileupload.FileItem) DeploymentPackage(org.osgi.service.deploymentadmin.DeploymentPackage) Map(java.util.Map) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter)

Example 17 with FileItem

use of org.apache.commons.fileupload.FileItem in project fess by codelibs.

the class FessMultipartRequestHandler method mappingParameter.

protected void mappingParameter(final HttpServletRequest request, final List<FileItem> items) {
    showFieldLoggingTitle();
    final Iterator<FileItem> iter = items.iterator();
    while (iter.hasNext()) {
        final FileItem item = iter.next();
        if (item.isFormField()) {
            showFormFieldParameter(item);
            addTextParameter(request, item);
        } else {
            showFileFieldParameter(item);
            final String itemName = item.getName();
            if (itemName != null && !itemName.isEmpty()) {
                addFileParameter(item);
            }
        }
    }
}
Also used : FileItem(org.apache.commons.fileupload.FileItem)

Example 18 with FileItem

use of org.apache.commons.fileupload.FileItem in project fess by codelibs.

the class FessMultipartRequestHandler method handleRequest.

// ===================================================================================
//                                                                      Handle Request
//                                                                      ==============
@Override
public void handleRequest(final HttpServletRequest request) throws ServletException {
    // /- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    // copied from super's method and extends it
    // basically for JVN#14876762
    // thought not all problems are resolved however the main case is safety
    // - - - - - - - - - -/
    final ServletFileUpload upload = createServletFileUpload(request);
    prepareElementsHash();
    try {
        final List<FileItem> items = parseRequest(request, upload);
        mappingParameter(request, items);
    } catch (final SizeLimitExceededException e) {
        handleSizeLimitExceededException(request, e);
    } catch (final FileUploadException e) {
        handleFileUploadException(e);
    }
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) SizeLimitExceededException(org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Example 19 with FileItem

use of org.apache.commons.fileupload.FileItem in project sling by apache.

the class InstallServlet method installBasedOnUploadedJar.

private void installBasedOnUploadedJar(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    InstallationResult result = null;
    try {
        DiskFileItemFactory factory = new DiskFileItemFactory();
        // try to hold even largish bundles in memory to potentially improve performance
        factory.setSizeThreshold(UPLOAD_IN_MEMORY_SIZE_THRESHOLD);
        ServletFileUpload upload = new ServletFileUpload();
        upload.setFileItemFactory(factory);
        @SuppressWarnings("unchecked") List<FileItem> items = upload.parseRequest(req);
        if (items.size() != 1) {
            logAndWriteError("Found " + items.size() + " items to process, but only updating 1 bundle is supported", resp);
            return;
        }
        FileItem item = items.get(0);
        JarInputStream jar = null;
        InputStream rawInput = null;
        try {
            jar = new JarInputStream(item.getInputStream());
            Manifest manifest = jar.getManifest();
            if (manifest == null) {
                logAndWriteError("Uploaded jar file does not contain a manifest", resp);
                return;
            }
            final String symbolicName = manifest.getMainAttributes().getValue(Constants.BUNDLE_SYMBOLICNAME);
            if (symbolicName == null) {
                logAndWriteError("Manifest does not have a " + Constants.BUNDLE_SYMBOLICNAME, resp);
                return;
            }
            final String version = manifest.getMainAttributes().getValue(Constants.BUNDLE_VERSION);
            // the JarInputStream is used only for validation, we need a fresh input stream for updating
            rawInput = item.getInputStream();
            Bundle found = getBundle(symbolicName);
            try {
                installOrUpdateBundle(found, rawInput, "inputstream:" + symbolicName + "-" + version + ".jar");
                result = new InstallationResult(true, null);
                resp.setStatus(200);
                result.render(resp.getWriter());
                return;
            } catch (BundleException e) {
                logAndWriteError("Unable to install/update bundle " + symbolicName, e, resp);
                return;
            }
        } finally {
            IOUtils.closeQuietly(jar);
            IOUtils.closeQuietly(rawInput);
        }
    } catch (FileUploadException e) {
        logAndWriteError("Failed parsing uploaded bundle", e, resp);
        return;
    }
}
Also used : JarInputStream(java.util.jar.JarInputStream) JarInputStream(java.util.jar.JarInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Bundle(org.osgi.framework.Bundle) Manifest(java.util.jar.Manifest) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) FileItem(org.apache.commons.fileupload.FileItem) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) BundleException(org.osgi.framework.BundleException) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Example 20 with FileItem

use of org.apache.commons.fileupload.FileItem in project libresonic by Libresonic.

the class ImportPlaylistController method handlePost.

@RequestMapping(method = RequestMethod.POST)
protected String handlePost(RedirectAttributes redirectAttributes, HttpServletRequest request) throws Exception {
    Map<String, Object> map = new HashMap<String, Object>();
    try {
        if (ServletFileUpload.isMultipartContent(request)) {
            FileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload upload = new ServletFileUpload(factory);
            List<?> items = upload.parseRequest(request);
            for (Object o : items) {
                FileItem item = (FileItem) o;
                if ("file".equals(item.getFieldName()) && !StringUtils.isBlank(item.getName())) {
                    if (item.getSize() > MAX_PLAYLIST_SIZE_MB * 1024L * 1024L) {
                        throw new Exception("The playlist file is too large. Max file size is " + MAX_PLAYLIST_SIZE_MB + " MB.");
                    }
                    String playlistName = FilenameUtils.getBaseName(item.getName());
                    String fileName = FilenameUtils.getName(item.getName());
                    String format = StringUtils.lowerCase(FilenameUtils.getExtension(item.getName()));
                    String username = securityService.getCurrentUsername(request);
                    Playlist playlist = playlistService.importPlaylist(username, playlistName, fileName, format, item.getInputStream(), null);
                    map.put("playlist", playlist);
                }
            }
        }
    } catch (Exception e) {
        map.put("error", e.getMessage());
    }
    redirectAttributes.addFlashAttribute("model", map);
    return "redirect:importPlaylist";
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) Playlist(org.libresonic.player.domain.Playlist) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) HashMap(java.util.HashMap) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) FileItemFactory(org.apache.commons.fileupload.FileItemFactory) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

FileItem (org.apache.commons.fileupload.FileItem)165 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)78 DiskFileItemFactory (org.apache.commons.fileupload.disk.DiskFileItemFactory)72 FileUploadException (org.apache.commons.fileupload.FileUploadException)59 File (java.io.File)55 IOException (java.io.IOException)51 ArrayList (java.util.ArrayList)40 HashMap (java.util.HashMap)32 ServletException (javax.servlet.ServletException)30 List (java.util.List)27 InputStream (java.io.InputStream)24 FileItemFactory (org.apache.commons.fileupload.FileItemFactory)23 DiskFileItem (org.apache.commons.fileupload.disk.DiskFileItem)16 Map (java.util.Map)15 UnsupportedEncodingException (java.io.UnsupportedEncodingException)12 ServletRequestContext (org.apache.commons.fileupload.servlet.ServletRequestContext)10 Test (org.junit.Test)10 Iterator (java.util.Iterator)9 FileItemWrap (com.github.bordertech.wcomponents.file.FileItemWrap)8 Locale (java.util.Locale)8