use of org.apache.commons.fileupload.servlet.ServletFileUpload in project zm-mailbox by Zimbra.
the class FileUploadServlet method handlePlainUpload.
/**
* This is used when handling a POST request generated by {@link ZMailbox#uploadContentAsStream}
*
* @param req
* @param resp
* @param fmt
* @param acct
* @param limitByFileUploadMaxSize
* @return
* @throws IOException
* @throws ServiceException
*/
List<Upload> handlePlainUpload(HttpServletRequest req, HttpServletResponse resp, String fmt, Account acct, boolean limitByFileUploadMaxSize) throws IOException, ServiceException {
// metadata is encoded in the response's HTTP headers
ContentType ctype = new ContentType(req.getContentType());
String contentType = ctype.getContentType(), filename = ctype.getParameter("name");
if (filename == null) {
filename = new ContentDisposition(req.getHeader("Content-Disposition")).getParameter("filename");
}
if (filename == null || filename.trim().equals("")) {
mLog.info("Rejecting upload with no name.");
drainRequestStream(req);
sendResponse(resp, HttpServletResponse.SC_NO_CONTENT, fmt, null, null, null);
return Collections.emptyList();
}
// Unescape the filename so it actually displays correctly
filename = StringEscapeUtils.unescapeHtml(filename);
// store the fetched file as a normal upload
ServletFileUpload upload = getUploader2(limitByFileUploadMaxSize);
FileItem fi = upload.getFileItemFactory().createItem("upload", contentType, false, filename);
try {
// write the upload to disk, but make sure not to exceed the permitted max upload size
long size = ByteUtil.copy(req.getInputStream(), false, fi.getOutputStream(), true, upload.getSizeMax() * 3);
if ((upload.getSizeMax() >= 0) && (size > upload.getSizeMax())) {
mLog.debug("handlePlainUpload(): deleting %s", fi);
fi.delete();
mLog.info("Exceeded maximum upload size of " + upload.getSizeMax() + " bytes: " + acct.getId());
drainRequestStream(req);
sendResponse(resp, HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE, fmt, null, null, null);
return Collections.emptyList();
}
} catch (IOException ioe) {
mLog.warn("Unable to store upload. Deleting %s", fi, ioe);
fi.delete();
drainRequestStream(req);
sendResponse(resp, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, fmt, null, null, null);
return Collections.emptyList();
}
List<FileItem> items = new ArrayList<FileItem>(1);
items.add(fi);
Upload up = new Upload(acct.getId(), fi, filename);
mLog.info("Received plain: %s", up);
synchronized (mPending) {
mPending.put(up.uuid, up);
}
List<Upload> uploads = Arrays.asList(up);
sendResponse(resp, HttpServletResponse.SC_OK, fmt, null, uploads, items);
return uploads;
}
use of org.apache.commons.fileupload.servlet.ServletFileUpload in project jena by apache.
the class Upload method fileUploadWorker.
/** Process an HTTP upload of RDF files (triples or quads)
* Stream straight into a graph or dataset -- unlike SPARQL_Upload the destination
* is known at the start of the multipart file body
*/
public static UploadDetails fileUploadWorker(HttpAction action, StreamRDF dest) {
String base = ActionLib.wholeRequestURL(action.request);
ServletFileUpload upload = new ServletFileUpload();
//log.info(format("[%d] Upload: Field=%s ignored", action.id, fieldName)) ;
// Overall counting.
StreamRDFCounting countingDest = StreamRDFLib.count(dest);
try {
FileItemIterator iter = upload.getItemIterator(action.request);
while (iter.hasNext()) {
FileItemStream fileStream = iter.next();
if (fileStream.isFormField()) {
// Ignore?
String fieldName = fileStream.getFieldName();
InputStream stream = fileStream.openStream();
String value = Streams.asString(stream, "UTF-8");
ServletOps.errorBadRequest(format("Only files accepted in multipart file upload (got %s=%s)", fieldName, value));
}
//Ignore the field name.
//String fieldName = fileStream.getFieldName();
InputStream stream = fileStream.openStream();
// Process the input stream
String contentTypeHeader = fileStream.getContentType();
ContentType ct = ContentType.create(contentTypeHeader);
Lang lang = null;
if (!matchContentType(ctTextPlain, ct))
lang = RDFLanguages.contentTypeToLang(ct.getContentType());
if (lang == null) {
String name = fileStream.getName();
if (name == null || name.equals(""))
ServletOps.errorBadRequest("No name for content - can't determine RDF syntax");
lang = RDFLanguages.filenameToLang(name);
if (name.endsWith(".gz"))
stream = new GZIPInputStream(stream);
}
if (lang == null)
// Desperate.
lang = RDFLanguages.RDFXML;
String printfilename = fileStream.getName();
if (printfilename == null || printfilename.equals(""))
printfilename = "<none>";
// Before
// action.log.info(format("[%d] Filename: %s, Content-Type=%s, Charset=%s => %s",
// action.id, printfilename, ct.getContentType(), ct.getCharset(), lang.getName())) ;
// count just this step
StreamRDFCounting countingDest2 = StreamRDFLib.count(countingDest);
try {
ActionSPARQL.parse(action, countingDest2, stream, lang, base);
UploadDetails details1 = new UploadDetails(countingDest2.count(), countingDest2.countTriples(), countingDest2.countQuads());
action.log.info(format("[%d] Filename: %s, Content-Type=%s, Charset=%s => %s : %s", action.id, printfilename, ct.getContentType(), ct.getCharset(), lang.getName(), details1.detailsStr()));
} catch (RiotParseException ex) {
action.log.info(format("[%d] Filename: %s, Content-Type=%s, Charset=%s => %s : %s", action.id, printfilename, ct.getContentType(), ct.getCharset(), lang.getName(), ex.getMessage()));
throw ex;
}
}
} catch (ActionErrorException ex) {
throw ex;
} catch (Exception ex) {
ServletOps.errorOccurred(ex.getMessage());
}
// Overall results.
UploadDetails details = new UploadDetails(countingDest.count(), countingDest.countTriples(), countingDest.countQuads());
return details;
}
use of org.apache.commons.fileupload.servlet.ServletFileUpload in project sling by apache.
the class ParameterSupport method parseMultiPartPost.
private void parseMultiPartPost(ParameterMap parameters) {
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload();
upload.setSizeMax(ParameterSupport.maxRequestSize);
upload.setFileSizeMax(ParameterSupport.maxFileSize);
upload.setFileItemFactory(new DiskFileItemFactory(ParameterSupport.fileSizeThreshold, ParameterSupport.location));
RequestContext rc = new ServletRequestContext(this.getServletRequest()) {
@Override
public String getCharacterEncoding() {
String enc = super.getCharacterEncoding();
return (enc != null) ? enc : Util.ENCODING_DIRECT;
}
};
// Parse the request
List<?> /* FileItem */
items = null;
try {
items = upload.parseRequest(rc);
} catch (FileUploadException fue) {
this.log.error("parseMultiPartPost: Error parsing request", fue);
}
if (items != null && items.size() > 0) {
for (Iterator<?> ii = items.iterator(); ii.hasNext(); ) {
FileItem fileItem = (FileItem) ii.next();
RequestParameter pp = new MultipartRequestParameter(fileItem);
parameters.addParameter(pp, false);
}
}
}
Aggregations