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