Search in sources :

Example 1 with FilesystemUpdateSite

use of org.openntf.nsfodp.compiler.update.FilesystemUpdateSite in project org.openntf.nsfodp by OpenNTF.

the class TranspilerApplication method start.

@Override
public Object start(IApplicationContext context) throws Exception {
    String notesIni = System.getenv(NSFODPConstants.PROP_NOTESINI);
    if (notesIni != null && !notesIni.isEmpty()) {
        // $NON-NLS-1$
        String execDir = System.getenv("Notes_ExecDirectory");
        // $NON-NLS-1$
        DominoAPI.get().NotesInitExtended(execDir, "=" + notesIni);
    }
    NotesThread.sinitThread();
    try {
        Path xspSourceRoot = toPath(System.getenv(NSFODPConstants.PROP_XSP_SOURCE_ROOT));
        Path ccSourceRoot = toPath(System.getenv(NSFODPConstants.PROP_CC_SOURCE_ROOT));
        List<Path> updateSites = toPaths(System.getenv(NSFODPConstants.PROP_UPDATESITE));
        Path outputDirectory = toPath(System.getenv(NSFODPConstants.PROP_OUTPUTFILE));
        IProgressMonitor mon = new PrintStreamProgressMonitor(System.out);
        XspTranspiler transpiler = new XspTranspiler(TranspilerActivator.instance.getBundle().getBundleContext(), xspSourceRoot, ccSourceRoot, mon);
        if (updateSites != null && !updateSites.isEmpty()) {
            updateSites.stream().map(FilesystemUpdateSite::new).forEach(transpiler::addUpdateSite);
        }
        exec.submit(() -> {
            try {
                Path javaSourceRoot = transpiler.transpile();
                Files.walk(javaSourceRoot, FileVisitOption.FOLLOW_LINKS).forEach(p -> {
                    Path relativePath = javaSourceRoot.relativize(p);
                    Path dest = outputDirectory.resolve(relativePath);
                    try {
                        if (Files.isDirectory(p)) {
                            Files.createDirectories(dest);
                        } else {
                            Files.copy(p, dest, StandardCopyOption.REPLACE_EXISTING);
                        }
                    } catch (IOException e) {
                        throw new UncheckedIOException(e);
                    }
                });
                mon.done();
            } catch (RuntimeException e) {
                throw e;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }).get();
        // $NON-NLS-1$
        System.out.println(getClass().getName() + "#end");
        exec.shutdownNow();
        exec.awaitTermination(30, TimeUnit.SECONDS);
        return EXIT_OK;
    } finally {
        NotesThread.stermThread();
    }
}
Also used : Path(java.nio.file.Path) TranspilerActivator(org.openntf.nsfodp.transpiler.TranspilerActivator) StandardCopyOption(java.nio.file.StandardCopyOption) JsonJavaFactory(com.ibm.commons.util.io.json.JsonJavaFactory) IApplication(org.eclipse.equinox.app.IApplication) Path(java.nio.file.Path) ExecutorService(java.util.concurrent.ExecutorService) Files(java.nio.file.Files) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) NSFODPConstants(org.openntf.nsfodp.commons.NSFODPConstants) Executors(java.util.concurrent.Executors) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) JsonParser(com.ibm.commons.util.io.json.JsonParser) UncheckedIOException(java.io.UncheckedIOException) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) DominoAPI(com.darwino.domino.napi.DominoAPI) JsonException(com.ibm.commons.util.io.json.JsonException) FilesystemUpdateSite(org.openntf.nsfodp.compiler.update.FilesystemUpdateSite) FileVisitOption(java.nio.file.FileVisitOption) Paths(java.nio.file.Paths) NotesThread(lotus.domino.NotesThread) XspTranspiler(org.openntf.nsfodp.transpiler.XspTranspiler) IApplicationContext(org.eclipse.equinox.app.IApplicationContext) PrintStreamProgressMonitor(org.openntf.nsfodp.commons.PrintStreamProgressMonitor) Collections(java.util.Collections) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) PrintStreamProgressMonitor(org.openntf.nsfodp.commons.PrintStreamProgressMonitor) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) XspTranspiler(org.openntf.nsfodp.transpiler.XspTranspiler) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) JsonException(com.ibm.commons.util.io.json.JsonException)

Example 2 with FilesystemUpdateSite

use of org.openntf.nsfodp.compiler.update.FilesystemUpdateSite 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)

Aggregations

IOException (java.io.IOException)2 Path (java.nio.file.Path)2 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)2 FilesystemUpdateSite (org.openntf.nsfodp.compiler.update.FilesystemUpdateSite)2 DominoAPI (com.darwino.domino.napi.DominoAPI)1 JsonException (com.ibm.commons.util.io.json.JsonException)1 JsonJavaFactory (com.ibm.commons.util.io.json.JsonJavaFactory)1 JsonParser (com.ibm.commons.util.io.json.JsonParser)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 PrintWriter (java.io.PrintWriter)1 UncheckedIOException (java.io.UncheckedIOException)1 FileSystem (java.nio.file.FileSystem)1 FileVisitOption (java.nio.file.FileVisitOption)1 Files (java.nio.file.Files)1 Paths (java.nio.file.Paths)1 StandardCopyOption (java.nio.file.StandardCopyOption)1 Principal (java.security.Principal)1 ArrayList (java.util.ArrayList)1