Search in sources :

Example 1 with DeployCommand

use of org.structr.web.maintenance.DeployCommand in project structr by structr.

the class ExportConsoleCommand method run.

@Override
public void run(final SecurityContext securityContext, final List<String> parameters, final Writable writable) throws FrameworkException, IOException {
    final Principal user = securityContext.getUser(false);
    if (user != null && user.isAdmin()) {
        final DeployCommand cmd = StructrApp.getInstance(securityContext).command(DeployCommand.class);
        cmd.setLogBuffer(writable);
        cmd.execute(toMap("mode", "export", "target", getParameter(parameters, 1)));
    } else {
        writable.println("You must be admin user to use this command.");
    }
}
Also used : DeployCommand(org.structr.web.maintenance.DeployCommand) Principal(org.structr.core.entity.Principal)

Example 2 with DeployCommand

use of org.structr.web.maintenance.DeployCommand in project structr by structr.

the class DeploymentServlet method doPost.

@Override
protected void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException {
    try (final Tx tx = StructrApp.getInstance().tx()) {
        if (!ServletFileUpload.isMultipartContent(request)) {
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            response.getOutputStream().write("ERROR (400): Request does not contain multipart content.\n".getBytes("UTF-8"));
            return;
        }
        final SecurityContext securityContext;
        try {
            securityContext = getConfig().getAuthenticator().initializeAndExamineRequest(request, response);
        } catch (AuthenticationException ae) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            response.getOutputStream().write("ERROR (401): Invalid user or password.\n".getBytes("UTF-8"));
            return;
        }
        if (securityContext.getUser(false) == null && !Settings.DeploymentAllowAnonymousUploads.getValue()) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            response.getOutputStream().write("ERROR (401): Anonymous uploads forbidden.\n".getBytes("UTF-8"));
            return;
        }
        // Ensure access mode is frontend
        securityContext.setAccessMode(AccessMode.Frontend);
        request.setCharacterEncoding("UTF-8");
        // Important: Set character encoding before calling response.getWriter() !!, see Servlet Spec 5.4
        response.setCharacterEncoding("UTF-8");
        // don't continue on redirects
        if (response.getStatus() == 302) {
            return;
        }
        final String pathInfo = request.getPathInfo();
        String type = null;
        if (StringUtils.isNotBlank(pathInfo)) {
            type = SchemaHelper.normalizeEntityName(StringUtils.stripStart(pathInfo.trim(), "/"));
        }
        uploader.setFileSizeMax(MEGABYTE * Settings.DeploymentMaxFileSize.getValue());
        uploader.setSizeMax(MEGABYTE * Settings.DeploymentMaxRequestSize.getValue());
        response.setContentType("text/html");
        final List<FileItem> fileItemsList = uploader.parseRequest(request);
        final Iterator<FileItem> fileItemsIterator = fileItemsList.iterator();
        final Map<String, Object> params = new HashMap<>();
        while (fileItemsIterator.hasNext()) {
            final FileItem item = fileItemsIterator.next();
            try {
                final String directoryPath = "/tmp/" + UUID.randomUUID();
                final String filePath = directoryPath + ".zip";
                File file = new File(filePath);
                Files.write(IOUtils.toByteArray(item.getInputStream()), file);
                unzip(file, directoryPath);
                DeployCommand deployCommand = StructrApp.getInstance(securityContext).command(DeployCommand.class);
                final Map<String, Object> attributes = new HashMap<>();
                attributes.put("source", directoryPath + "/" + StringUtils.substringBeforeLast(item.getName(), "."));
                deployCommand.execute(attributes);
                file.deleteOnExit();
                File dir = new File(directoryPath);
                dir.deleteOnExit();
            } catch (IOException ex) {
                logger.warn("Could not upload file", ex);
            }
        }
        tx.success();
    } catch (FrameworkException | IOException | FileUploadException t) {
        logger.error("Exception while processing request", t);
        UiAuthenticator.writeInternalServerError(response);
    }
}
Also used : DeployCommand(org.structr.web.maintenance.DeployCommand) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) AuthenticationException(org.structr.core.auth.exception.AuthenticationException) HashMap(java.util.HashMap) IOException(java.io.IOException) FileItem(org.apache.commons.fileupload.FileItem) SecurityContext(org.structr.common.SecurityContext) ZipFile(java.util.zip.ZipFile) File(java.io.File) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Example 3 with DeployCommand

use of org.structr.web.maintenance.DeployCommand in project structr by structr.

the class DeploymentTest method doImportExportRoundtrip.

private void doImportExportRoundtrip(final boolean deleteTestDirectory, final boolean cleanDatabase, final Function callback) {
    final DeployCommand cmd = app.command(DeployCommand.class);
    final Path tmp = Paths.get("/tmp/structr-deployment-test" + System.currentTimeMillis() + System.nanoTime());
    try {
        if (tmp != null) {
            // export to temp directory
            final Map<String, Object> firstExportParams = new HashMap<>();
            firstExportParams.put("mode", "export");
            firstExportParams.put("target", tmp.toString());
            // execute deploy command
            cmd.execute(firstExportParams);
            if (cleanDatabase) {
                cleanDatabase();
            }
            // apply callback if present
            if (callback != null) {
                callback.apply(null);
            }
            // import from exported source
            final Map<String, Object> firstImportParams = new HashMap<>();
            firstImportParams.put("source", tmp.toString());
            // execute deploy command
            cmd.execute(firstImportParams);
        } else {
            fail("Unable to create temporary directory.");
        }
    } catch (Throwable t) {
        t.printStackTrace();
    } finally {
        if (deleteTestDirectory) {
            try {
                // clean directories
                Files.walkFileTree(tmp, new DeletingFileVisitor());
                Files.delete(tmp);
            } catch (IOException ioex) {
            }
        }
    }
}
Also used : Path(java.nio.file.Path) DeployCommand(org.structr.web.maintenance.DeployCommand) HashMap(java.util.HashMap) GraphObject(org.structr.core.GraphObject) IOException(java.io.IOException)

Example 4 with DeployCommand

use of org.structr.web.maintenance.DeployCommand in project structr by structr.

the class ImportConsoleCommand method run.

@Override
public void run(final SecurityContext securityContext, final List<String> parameters, final Writable writable) throws FrameworkException, IOException {
    final Principal user = securityContext.getUser(false);
    if (user != null && user.isAdmin()) {
        final DeployCommand cmd = StructrApp.getInstance(securityContext).command(DeployCommand.class);
        cmd.setLogBuffer(writable);
        cmd.execute(toMap("mode", "import", "source", getParameter(parameters, 1)));
    } else {
        writable.println("You must be admin user to use this command.");
    }
}
Also used : DeployCommand(org.structr.web.maintenance.DeployCommand) Principal(org.structr.core.entity.Principal)

Aggregations

DeployCommand (org.structr.web.maintenance.DeployCommand)4 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 Principal (org.structr.core.entity.Principal)2 File (java.io.File)1 Path (java.nio.file.Path)1 ZipFile (java.util.zip.ZipFile)1 FileItem (org.apache.commons.fileupload.FileItem)1 FileUploadException (org.apache.commons.fileupload.FileUploadException)1 SecurityContext (org.structr.common.SecurityContext)1 FrameworkException (org.structr.common.error.FrameworkException)1 GraphObject (org.structr.core.GraphObject)1 AuthenticationException (org.structr.core.auth.exception.AuthenticationException)1 Tx (org.structr.core.graph.Tx)1