Search in sources :

Example 1 with LineDelimitedJsonProgressMonitor

use of org.openntf.nsfodp.commons.LineDelimitedJsonProgressMonitor in project org.openntf.nsfodp by OpenNTF.

the class ODPCompilerServlet method doPost.

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    Principal user = req.getUserPrincipal();
    resp.setBufferSize(0);
    resp.setStatus(HttpServletResponse.SC_OK);
    ServletOutputStream os = resp.getOutputStream();
    Set<Path> cleanup = new HashSet<>();
    try {
        if (!ALLOW_ANONYMOUS && "Anonymous".equalsIgnoreCase(user.getName())) {
            // $NON-NLS-1$
            resp.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            // $NON-NLS-1$
            resp.setContentType("text/plain");
            os.println(Messages.ODPCompilerServlet_anonymousDisallowed);
            return;
        }
        // Developer's note: multipart/form-data with files broken out would be nice,
        // but Domino as of 9.0.1FP10 behaves poorly with them; for now, it's safer
        // to use a combined ZIP and pass options in headers
        String contentType = req.getContentType();
        if (!"application/zip".equals(contentType)) {
            // $NON-NLS-1$
            throw new IllegalArgumentException(Messages.ODPCompilerServlet_contentMustBeZip);
        }
        // $NON-NLS-1$ //$NON-NLS-2$
        Path packageFile = Files.createTempFile(NSFODPUtil.getTempDirectory(), "package", ".zip");
        cleanup.add(packageFile);
        try (InputStream reqInputStream = req.getInputStream()) {
            Files.copy(reqInputStream, packageFile, StandardCopyOption.REPLACE_EXISTING);
        }
        // Look for an ODP item
        Path odpZip = null;
        List<Path> siteZips = new ArrayList<>();
        List<Path> classPathJars = new ArrayList<>();
        try (ZipFile packageZip = new ZipFile(packageFile.toFile(), StandardCharsets.UTF_8)) {
            // $NON-NLS-1$
            ZipEntry odpEntry = packageZip.getEntry("odp.zip");
            if (odpEntry == null) {
                // Then the package is itself the ODP
                odpZip = packageFile;
            } else {
                // Then extract the ODP
                // $NON-NLS-1$ //$NON-NLS-2$
                odpZip = Files.createTempFile(NSFODPUtil.getTempDirectory(), "odp", ".zip");
                cleanup.add(odpZip);
                try (InputStream odpIs = packageZip.getInputStream(odpEntry)) {
                    Files.copy(odpIs, odpZip, StandardCopyOption.REPLACE_EXISTING);
                }
                // Look for any embedded update sites and classpath entries
                packageZip.stream().forEach(entry -> {
                    try {
                        if (SITE_ZIP_PATTERN.matcher(entry.getName()).matches()) {
                            // Then add it as an update site
                            // $NON-NLS-1$ //$NON-NLS-2$
                            Path siteZip = Files.createTempFile(NSFODPUtil.getTempDirectory(), "site", ".zip");
                            cleanup.add(siteZip);
                            try (InputStream siteIs = packageZip.getInputStream(entry)) {
                                Files.copy(siteIs, siteZip, StandardCopyOption.REPLACE_EXISTING);
                            }
                            siteZips.add(siteZip);
                        } else if (entry.getName().startsWith("classpath/")) {
                            // $NON-NLS-1$
                            // Then add it as an individual JAR
                            // $NON-NLS-1$ //$NON-NLS-2$
                            Path cpJar = Files.createTempFile(NSFODPUtil.getTempDirectory(), "classpathJar", ".jar");
                            cleanup.add(cpJar);
                            try (InputStream jarIs = packageZip.getInputStream(entry)) {
                                Files.copy(jarIs, cpJar, StandardCopyOption.REPLACE_EXISTING);
                            }
                            classPathJars.add(cpJar);
                        }
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                });
            }
        }
        IProgressMonitor mon = new LineDelimitedJsonProgressMonitor(os);
        Path nsf;
        try (FileSystem fs = NSFODPUtil.openZipPath(odpZip)) {
            // $NON-NLS-1$
            Path odpFile = fs.getPath("/");
            OnDiskProject odp = new OnDiskProject(odpFile);
            ODPCompiler compiler = new ODPCompiler(ODPCompilerActivator.instance.getBundle().getBundleContext(), odp, mon);
            // See if the client requested a specific compiler level
            String compilerLevel = req.getHeader(NSFODPConstants.HEADER_COMPILER_LEVEL);
            if (StringUtil.isNotEmpty(compilerLevel)) {
                compiler.setCompilerLevel(compilerLevel);
            }
            String appendTimestamp = req.getHeader(NSFODPConstants.HEADER_APPEND_TIMESTAMP);
            if ("true".equals(appendTimestamp)) {
                // $NON-NLS-1$
                compiler.setAppendTimestampToTitle(true);
            }
            String templateName = req.getHeader(NSFODPConstants.HEADER_TEMPLATE_NAME);
            if (StringUtil.isNotEmpty(templateName)) {
                compiler.setTemplateName(templateName);
                String templateVersion = req.getHeader(NSFODPConstants.HEADER_TEMPLATE_VERSION);
                if (StringUtil.isNotEmpty(templateVersion)) {
                    compiler.setTemplateVersion(templateVersion);
                }
            }
            String setXspOptions = req.getHeader(NSFODPConstants.HEADER_SET_PRODUCTION_XSP);
            if ("true".equals(setXspOptions)) {
                // $NON-NLS-1$
                compiler.setSetProductionXspOptions(true);
            }
            String odsRelease = req.getHeader(NSFODPConstants.HEADER_ODS_RELEASE);
            if (StringUtil.isNotEmpty(odsRelease)) {
                compiler.setOdsRelease(odsRelease);
            }
            String compileBasicLs = req.getHeader(NSFODPConstants.HEADER_COMPILE_BASICLS);
            if ("true".equals(compileBasicLs)) {
                // $NON-NLS-1$
                compiler.setCompileBasicElementLotusScript(true);
            }
            if (siteZips != null && !siteZips.isEmpty()) {
                for (Path siteZip : siteZips) {
                    Path siteFile = NSFODPUtil.expandZip(siteZip);
                    cleanup.add(siteFile);
                    UpdateSite updateSite = new FilesystemUpdateSite(siteFile);
                    compiler.addUpdateSite(updateSite);
                }
            }
            classPathJars.forEach(compiler::addClassPathEntry);
            nsf = this.exec.submit(() -> {
                Path result = compiler.compile();
                mon.done();
                return result;
            }).get();
        }
        // Now stream the NSF
        cleanup.add(nsf);
        try (OutputStream gzos = new GZIPOutputStream(os)) {
            Files.copy(nsf, gzos);
        }
        resp.flushBuffer();
        // Delete the NSF via the Notes API
        try (NotesAPI api = NotesAPI.get()) {
            api.deleteDatabase(nsf.toString());
        }
    } catch (Throwable e) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PrintWriter out = new PrintWriter(baos);
        e.printStackTrace(out);
        out.flush();
        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 : ServletOutputStream(javax.servlet.ServletOutputStream) ZipEntry(java.util.zip.ZipEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServletOutputStream(javax.servlet.ServletOutputStream) OutputStream(java.io.OutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) ArrayList(java.util.ArrayList) FilesystemUpdateSite(org.openntf.nsfodp.compiler.update.FilesystemUpdateSite) ODPCompiler(org.openntf.nsfodp.compiler.ODPCompiler) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileSystem(java.nio.file.FileSystem) HashSet(java.util.HashSet) NotesAPI(org.openntf.nsfodp.commons.odp.notesapi.NotesAPI) PrintWriter(java.io.PrintWriter) Path(java.nio.file.Path) InputStream(java.io.InputStream) LineDelimitedJsonProgressMonitor(org.openntf.nsfodp.commons.LineDelimitedJsonProgressMonitor) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ZipFile(java.util.zip.ZipFile) OnDiskProject(org.openntf.nsfodp.commons.odp.OnDiskProject) FilesystemUpdateSite(org.openntf.nsfodp.compiler.update.FilesystemUpdateSite) UpdateSite(org.openntf.nsfodp.compiler.update.UpdateSite) Principal(java.security.Principal)

Example 2 with LineDelimitedJsonProgressMonitor

use of org.openntf.nsfodp.commons.LineDelimitedJsonProgressMonitor 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)

Example 3 with LineDelimitedJsonProgressMonitor

use of org.openntf.nsfodp.commons.LineDelimitedJsonProgressMonitor in project org.openntf.nsfodp by OpenNTF.

the class ODPExporterServlet method handle.

protected void handle(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    // $NON-NLS-1$
    boolean post = "POST".equals(req.getMethod());
    Principal user = req.getUserPrincipal();
    resp.setBufferSize(0);
    resp.setStatus(HttpServletResponse.SC_OK);
    ServletOutputStream os = resp.getOutputStream();
    Set<Path> cleanup = new HashSet<>();
    try {
        if (!ALLOW_ANONYMOUS && "Anonymous".equalsIgnoreCase(user.getName())) {
            // $NON-NLS-1$
            resp.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            // $NON-NLS-1$
            resp.setContentType("text/plain");
            os.println(Messages.ODPExporterServlet_anonymousAccessDisallowed);
            return;
        }
        try (NotesAPI session = NotesAPI.get()) {
            NDatabase database;
            if (post) {
                // Then read the NSF from the body
                String contentType = req.getContentType();
                if (!"application/octet-stream".equals(contentType)) {
                    // $NON-NLS-1$
                    throw new IllegalArgumentException(MessageFormat.format(Messages.ODPExporterServlet_mismatchedContentType, NSFODPConstants.HEADER_DATABASE_PATH, contentType));
                }
                // $NON-NLS-1$
                Path nsfFile = Files.createTempFile(NSFODPUtil.getTempDirectory(), getClass().getName(), ".nsf");
                cleanup.add(nsfFile);
                try (InputStream reqInputStream = req.getInputStream()) {
                    Files.copy(reqInputStream, nsfFile, StandardCopyOption.REPLACE_EXISTING);
                }
                database = session.openDatabase(nsfFile.toString());
            } else {
                // Then look for an NSF path in the headers
                String databasePath = req.getHeader(NSFODPConstants.HEADER_DATABASE_PATH);
                if (StringUtil.isEmpty(databasePath)) {
                    throw new IllegalArgumentException(MessageFormat.format(Messages.ODPExporterServlet_dbPathMissing, NSFODPConstants.HEADER_DATABASE_PATH));
                }
                // Verify that the user can indeed export this DB
                try (NotesAPI userApi = NotesAPI.get(user.getName(), false, false)) {
                    try (NDatabase userDb = userApi.openDatabase(databasePath)) {
                        if (userDb.getCurrentAccessLevel() < 5) {
                            // Designer access
                            throw new UnsupportedOperationException(MessageFormat.format(Messages.ODPExporterServlet_insufficientAccess, user.getName(), databasePath));
                        }
                    }
                }
                database = session.openDatabase(databasePath);
            }
            try {
                IProgressMonitor mon = new LineDelimitedJsonProgressMonitor(os);
                ODPExporter exporter = new ODPExporter(database);
                String binaryDxl = req.getHeader(NSFODPConstants.HEADER_BINARY_DXL);
                if ("true".equals(binaryDxl)) {
                    // $NON-NLS-1$
                    exporter.setBinaryDxl(true);
                }
                String swiperFilter = req.getHeader(NSFODPConstants.HEADER_SWIPER_FILTER);
                if ("true".equals(swiperFilter)) {
                    // $NON-NLS-1$
                    exporter.setSwiperFilter(true);
                }
                String richTextAsItemData = req.getHeader(NSFODPConstants.HEADER_RICH_TEXT_AS_ITEM_DATA);
                if ("true".equals(richTextAsItemData)) {
                    // $NON-NLS-1$
                    exporter.setRichTextAsItemData(true);
                }
                exporter.setProjectName(req.getHeader(NSFODPConstants.HEADER_PROJECT_NAME));
                exporter.setOdpType(ODPType.ZIP);
                Path result = exporter.export();
                cleanup.add(result);
                mon.done();
                Files.copy(result, os);
            } finally {
                if (post) {
                    String filePath = database.getFilePath();
                    database.close();
                    session.deleteDatabase(filePath);
                }
            }
        }
    } catch (Throwable e) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PrintWriter out = new PrintWriter(baos);
        e.printStackTrace(out);
        out.flush();
        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) InputStream(java.io.InputStream) LineDelimitedJsonProgressMonitor(org.openntf.nsfodp.commons.LineDelimitedJsonProgressMonitor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) NDatabase(org.openntf.nsfodp.commons.odp.notesapi.NDatabase) ODPExporter(org.openntf.nsfodp.exporter.ODPExporter) Principal(java.security.Principal) HashSet(java.util.HashSet) NotesAPI(org.openntf.nsfodp.commons.odp.notesapi.NotesAPI) PrintWriter(java.io.PrintWriter)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 InputStream (java.io.InputStream)3 PrintWriter (java.io.PrintWriter)3 Path (java.nio.file.Path)3 Principal (java.security.Principal)3 HashSet (java.util.HashSet)3 ServletOutputStream (javax.servlet.ServletOutputStream)3 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)3 LineDelimitedJsonProgressMonitor (org.openntf.nsfodp.commons.LineDelimitedJsonProgressMonitor)3 ZipEntry (java.util.zip.ZipEntry)2 NotesAPI (org.openntf.nsfodp.commons.odp.notesapi.NotesAPI)2 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 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 FileSystem (java.nio.file.FileSystem)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1