Search in sources :

Example 1 with FileItem

use of com.ibm.xsp.http.fileupload.FileItem in project org.openntf.nsfodp by OpenNTF.

the class NSFDeploymentServlet method doPost.

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    Principal user = req.getUserPrincipal();
    resp.setBufferSize(0);
    ServletOutputStream os = resp.getOutputStream();
    Set<Path> cleanup = new HashSet<>();
    try {
        if ("Anonymous".equalsIgnoreCase(user.getName())) {
            // $NON-NLS-1$
            resp.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            // $NON-NLS-1$
            resp.setContentType("text/plain");
            os.write("Anonymous access disallowed".getBytes());
            return;
        }
        if (!ServletFileUpload.isMultipartContent(req)) {
            throw new IllegalArgumentException("POST body must be a multipart upload");
        }
        ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
        Map<String, List<FileItem>> param = upload.parseParameterMap(req);
        if (!param.containsKey(PARAM_DEST_PATH)) {
            throw new IllegalArgumentException(MessageFormat.format("Content must include a {0} component", PARAM_DEST_PATH));
        }
        FileItem destFileItem = param.get(PARAM_DEST_PATH).get(0);
        if (!destFileItem.isFormField()) {
            throw new IllegalArgumentException(MessageFormat.format("{0} must not be a file", PARAM_DEST_PATH));
        }
        String destPath = destFileItem.getString();
        boolean replaceDesign = false;
        if (param.containsKey(PARAM_REPLACE_DESIGN)) {
            FileItem replaceDesignItem = param.get(PARAM_REPLACE_DESIGN).get(0);
            if (!replaceDesignItem.isFormField()) {
                throw new IllegalArgumentException(MessageFormat.format("{0} must not be a file", PARAM_REPLACE_DESIGN));
            }
            replaceDesign = Boolean.valueOf(replaceDesignItem.getString());
        }
        boolean signDatabase = true;
        String signDatabaseParam = req.getHeader(NSFODPConstants.HEADER_DEPLOY_SIGN);
        if (StringUtil.isNotEmpty(signDatabaseParam)) {
            signDatabase = Boolean.valueOf(signDatabaseParam);
        }
        if (!param.containsKey(PARAM_FILE)) {
            throw new IllegalArgumentException(MessageFormat.format("Content must include a {0} component", PARAM_FILE));
        }
        FileItem fileItem = param.get(PARAM_FILE).get(0);
        if (fileItem.isFormField()) {
            throw new IllegalArgumentException(MessageFormat.format("{0} part must be a file", PARAM_FILE));
        }
        // $NON-NLS-1$ //$NON-NLS-2$
        Path nsf = Files.createTempFile(NSFODPUtil.getTempDirectory(), "nsfdeployment", ".data");
        cleanup.add(nsf);
        try (InputStream reqInputStream = fileItem.getInputStream()) {
            Files.copy(reqInputStream, nsf, StandardCopyOption.REPLACE_EXISTING);
        }
        if (String.valueOf(fileItem.getContentType()).startsWith("application/zip")) {
            // $NON-NLS-1$
            // If it's a ZIP, expand it - otherwise, use the file content as-is
            // $NON-NLS-1$ //$NON-NLS-2$
            Path expanded = Files.createTempFile(NSFODPUtil.getTempDirectory(), "nsfdeployment", ".nsf");
            cleanup.add(expanded);
            try (InputStream is = NSFODPUtil.newInputStream(nsf)) {
                try (ZipInputStream zis = new ZipInputStream(is, StandardCharsets.UTF_8)) {
                    ZipEntry firstEntry = zis.getNextEntry();
                    if (firstEntry == null) {
                        throw new IllegalArgumentException("ZIP file must contain an entry");
                    }
                    Files.copy(is, expanded, StandardCopyOption.REPLACE_EXISTING);
                    nsf = expanded;
                }
            }
        }
        IProgressMonitor mon = new LineDelimitedJsonProgressMonitor(os);
        DeployNSFTask task = new DeployNSFTask(nsf, destPath, replaceDesign, signDatabase);
        task.run();
        mon.done();
    } catch (Throwable e) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try (PrintWriter out = new PrintWriter(baos)) {
            e.printStackTrace(out);
        }
        os.println(LineDelimitedJsonProgressMonitor.message(// $NON-NLS-1$ //$NON-NLS-2$
        "type", // $NON-NLS-1$ //$NON-NLS-2$
        "error", // $NON-NLS-1$
        "stackTrace", // $NON-NLS-1$
        baos.toString()));
    } finally {
        NSFODPUtil.deltree(cleanup);
    }
}
Also used : Path(java.nio.file.Path) ServletOutputStream(javax.servlet.ServletOutputStream) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) LineDelimitedJsonProgressMonitor(org.openntf.nsfodp.commons.LineDelimitedJsonProgressMonitor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DiskFileItemFactory(com.ibm.xsp.http.fileupload.disk.DiskFileItemFactory) DeployNSFTask(org.openntf.nsfodp.deployment.DeployNSFTask) FileItem(com.ibm.xsp.http.fileupload.FileItem) ZipInputStream(java.util.zip.ZipInputStream) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ServletFileUpload(com.ibm.xsp.http.fileupload.servlet.ServletFileUpload) List(java.util.List) Principal(java.security.Principal) HashSet(java.util.HashSet) PrintWriter(java.io.PrintWriter)

Aggregations

FileItem (com.ibm.xsp.http.fileupload.FileItem)1 DiskFileItemFactory (com.ibm.xsp.http.fileupload.disk.DiskFileItemFactory)1 ServletFileUpload (com.ibm.xsp.http.fileupload.servlet.ServletFileUpload)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 PrintWriter (java.io.PrintWriter)1 Path (java.nio.file.Path)1 Principal (java.security.Principal)1 HashSet (java.util.HashSet)1 List (java.util.List)1 ZipEntry (java.util.zip.ZipEntry)1 ZipInputStream (java.util.zip.ZipInputStream)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 LineDelimitedJsonProgressMonitor (org.openntf.nsfodp.commons.LineDelimitedJsonProgressMonitor)1 DeployNSFTask (org.openntf.nsfodp.deployment.DeployNSFTask)1