use of org.apache.commons.fileupload.FileItem in project felix by apache.
the class WebConsolePlugin method doPost.
/**
* @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// get the uploaded data
final String action = WebConsoleUtil.getParameter(req, Util.PARAM_ACTION);
if (ACTION_DEPLOY.equals(action)) {
Map params = (Map) req.getAttribute(AbstractWebConsolePlugin.ATTR_FILEUPLOAD);
if (params != null) {
final FileItem pck = getFileItem(params, PARAMETER_PCK_FILE, false);
final DeploymentAdmin admin = (DeploymentAdmin) adminTracker.getService();
if (admin != null) {
try {
admin.installDeploymentPackage(pck.getInputStream());
final String uri = req.getRequestURI();
resp.sendRedirect(uri);
return;
} catch (/*Deployment*/
Exception e) {
throw new ServletException("Unable to deploy package.", e);
}
}
}
throw new ServletException("Upload file or deployment admin missing.");
} else if (ACTION_UNINSTALL.equals(action)) {
final String pckId = req.getPathInfo().substring(req.getPathInfo().lastIndexOf('/') + 1);
if (pckId != null && pckId.length() > 0) {
final DeploymentAdmin admin = (DeploymentAdmin) adminTracker.getService();
if (admin != null) {
try {
final DeploymentPackage pck = admin.getDeploymentPackage(pckId);
if (pck != null) {
pck.uninstall();
}
} catch (/*Deployment*/
Exception e) {
throw new ServletException("Unable to undeploy package.", e);
}
}
}
final PrintWriter pw = resp.getWriter();
pw.println("{ \"reload\":true }");
return;
}
throw new ServletException("Unknown action: " + action);
}
use of org.apache.commons.fileupload.FileItem in project fess by codelibs.
the class FessMultipartRequestHandler method mappingParameter.
protected void mappingParameter(final HttpServletRequest request, final List<FileItem> items) {
showFieldLoggingTitle();
final Iterator<FileItem> iter = items.iterator();
while (iter.hasNext()) {
final FileItem item = iter.next();
if (item.isFormField()) {
showFormFieldParameter(item);
addTextParameter(request, item);
} else {
showFileFieldParameter(item);
final String itemName = item.getName();
if (itemName != null && !itemName.isEmpty()) {
addFileParameter(item);
}
}
}
}
use of org.apache.commons.fileupload.FileItem in project fess by codelibs.
the class FessMultipartRequestHandler method handleRequest.
// ===================================================================================
// Handle Request
// ==============
@Override
public void handleRequest(final HttpServletRequest request) throws ServletException {
// /- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// copied from super's method and extends it
// basically for JVN#14876762
// thought not all problems are resolved however the main case is safety
// - - - - - - - - - -/
final ServletFileUpload upload = createServletFileUpload(request);
prepareElementsHash();
try {
final List<FileItem> items = parseRequest(request, upload);
mappingParameter(request, items);
} catch (final SizeLimitExceededException e) {
handleSizeLimitExceededException(request, e);
} catch (final FileUploadException e) {
handleFileUploadException(e);
}
}
use of org.apache.commons.fileupload.FileItem in project sling by apache.
the class InstallServlet method installBasedOnUploadedJar.
private void installBasedOnUploadedJar(HttpServletRequest req, HttpServletResponse resp) throws IOException {
InstallationResult result = null;
try {
DiskFileItemFactory factory = new DiskFileItemFactory();
// try to hold even largish bundles in memory to potentially improve performance
factory.setSizeThreshold(UPLOAD_IN_MEMORY_SIZE_THRESHOLD);
ServletFileUpload upload = new ServletFileUpload();
upload.setFileItemFactory(factory);
@SuppressWarnings("unchecked") List<FileItem> items = upload.parseRequest(req);
if (items.size() != 1) {
logAndWriteError("Found " + items.size() + " items to process, but only updating 1 bundle is supported", resp);
return;
}
FileItem item = items.get(0);
JarInputStream jar = null;
InputStream rawInput = null;
try {
jar = new JarInputStream(item.getInputStream());
Manifest manifest = jar.getManifest();
if (manifest == null) {
logAndWriteError("Uploaded jar file does not contain a manifest", resp);
return;
}
final String symbolicName = manifest.getMainAttributes().getValue(Constants.BUNDLE_SYMBOLICNAME);
if (symbolicName == null) {
logAndWriteError("Manifest does not have a " + Constants.BUNDLE_SYMBOLICNAME, resp);
return;
}
final String version = manifest.getMainAttributes().getValue(Constants.BUNDLE_VERSION);
// the JarInputStream is used only for validation, we need a fresh input stream for updating
rawInput = item.getInputStream();
Bundle found = getBundle(symbolicName);
try {
installOrUpdateBundle(found, rawInput, "inputstream:" + symbolicName + "-" + version + ".jar");
result = new InstallationResult(true, null);
resp.setStatus(200);
result.render(resp.getWriter());
return;
} catch (BundleException e) {
logAndWriteError("Unable to install/update bundle " + symbolicName, e, resp);
return;
}
} finally {
IOUtils.closeQuietly(jar);
IOUtils.closeQuietly(rawInput);
}
} catch (FileUploadException e) {
logAndWriteError("Failed parsing uploaded bundle", e, resp);
return;
}
}
use of org.apache.commons.fileupload.FileItem in project libresonic by Libresonic.
the class ImportPlaylistController method handlePost.
@RequestMapping(method = RequestMethod.POST)
protected String handlePost(RedirectAttributes redirectAttributes, HttpServletRequest request) throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
try {
if (ServletFileUpload.isMultipartContent(request)) {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<?> items = upload.parseRequest(request);
for (Object o : items) {
FileItem item = (FileItem) o;
if ("file".equals(item.getFieldName()) && !StringUtils.isBlank(item.getName())) {
if (item.getSize() > MAX_PLAYLIST_SIZE_MB * 1024L * 1024L) {
throw new Exception("The playlist file is too large. Max file size is " + MAX_PLAYLIST_SIZE_MB + " MB.");
}
String playlistName = FilenameUtils.getBaseName(item.getName());
String fileName = FilenameUtils.getName(item.getName());
String format = StringUtils.lowerCase(FilenameUtils.getExtension(item.getName()));
String username = securityService.getCurrentUsername(request);
Playlist playlist = playlistService.importPlaylist(username, playlistName, fileName, format, item.getInputStream(), null);
map.put("playlist", playlist);
}
}
}
} catch (Exception e) {
map.put("error", e.getMessage());
}
redirectAttributes.addFlashAttribute("model", map);
return "redirect:importPlaylist";
}
Aggregations