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.");
}
}
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);
}
}
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) {
}
}
}
}
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.");
}
}
Aggregations