use of org.apache.commons.fileupload.servlet.ServletRequestContext in project fabric8 by jboss-fuse.
the class MavenUploadProxyServlet method doPut.
@Override
protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
String path = req.getPathInfo();
// Make sure path is valid
if (path == null) {
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
return;
}
if (path.startsWith("/")) {
path = path.substring(1);
}
UploadContext result = null;
// handle move
String location = req.getHeader(LOCATION_HEADER);
if (location != null) {
result = move(location, path);
} else {
// is it multipart data?
if (FileUploadBase.isMultipartContent(new ServletRequestContext(req))) {
List<FileItem> items = new ServletFileUpload(fileItemFactory).parseRequest(req);
// What to do with multiple paths?
if (items != null && items.size() > 0) {
FileItem item = items.get(0);
result = doUpload(item.getInputStream(), path);
item.delete();
}
} else {
result = doUpload(req.getInputStream(), path);
}
}
if (result != null && result.status()) {
handleDeploy(req, result);
addHeaders(resp, result.headers());
resp.setStatus(HttpServletResponse.SC_ACCEPTED);
} else {
resp.setStatus(HttpServletResponse.SC_NOT_ACCEPTABLE);
}
} catch (InvalidMavenArtifactRequest ex) {
// must response with status and flush as Jetty may report org.eclipse.jetty.server.Response Committed before 401 null
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
resp.setContentLength(0);
resp.flushBuffer();
} catch (Exception ex) {
// must response with status and flush as Jetty may report org.eclipse.jetty.server.Response Committed before 401 null
resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
resp.setContentLength(0);
resp.flushBuffer();
}
}
use of org.apache.commons.fileupload.servlet.ServletRequestContext in project wicket by apache.
the class MultipartServletWebRequestImpl method parseFileParts.
@Override
public void parseFileParts() throws FileUploadException {
HttpServletRequest request = getContainerRequest();
// The encoding that will be used to decode the string parameters
// It should NOT be null at this point, but it may be
// especially if the older Servlet API 2.2 is used
String encoding = request.getCharacterEncoding();
// the attribute 'accept-encoding' in wicket forms.
if (encoding == null) {
encoding = Application.get().getRequestCycleSettings().getResponseRequestEncoding();
}
FileUploadBase fileUpload = newFileUpload(encoding);
List<FileItem> items;
if (wantUploadProgressUpdates()) {
ServletRequestContext ctx = new ServletRequestContext(request) {
@Override
public InputStream getInputStream() throws IOException {
return new CountingInputStream(super.getInputStream());
}
};
totalBytes = request.getContentLength();
onUploadStarted(totalBytes);
try {
items = fileUpload.parseRequest(ctx);
} finally {
onUploadCompleted();
}
} else {
// try to parse the file uploads by using Apache Commons FileUpload APIs
// because they are feature richer (e.g. progress updates, cleaner)
items = fileUpload.parseRequest(new ServletRequestContext(request));
if (items.isEmpty()) {
// fallback to Servlet 3.0 APIs
items = readServlet3Parts(request);
}
}
// Loop through items
for (final FileItem item : items) {
// If item is a form field
if (item.isFormField()) {
// Set parameter value
final String value;
if (encoding != null) {
try {
value = item.getString(encoding);
} catch (UnsupportedEncodingException e) {
throw new WicketRuntimeException(e);
}
} else {
value = item.getString();
}
addParameter(item.getFieldName(), value);
} else {
List<FileItem> fileItems = files.get(item.getFieldName());
if (fileItems == null) {
fileItems = new ArrayList<>();
files.put(item.getFieldName(), fileItems);
}
// Add to file list
fileItems.add(item);
}
}
}
Aggregations