use of com.webobjects.appserver.WOMultipartIterator in project wonder-slim by undur.
the class AjaxFileUploadRequestHandler method handleRequest.
@Override
public WOResponse handleRequest(WORequest request) {
WOApplication application = WOApplication.application();
application.awake();
try {
WOContext context = application.createContextForRequest(request);
WOResponse response = application.createResponseInContext(context);
String uploadIdentifier = null;
String uploadFileName = null;
InputStream uploadInputStream = null;
long streamLength = -1L;
try {
String sessionIdKey = WOApplication.application().sessionIdKey();
String sessionId = request.cookieValueForKey(sessionIdKey);
WOMultipartIterator multipartIterator = request.multipartIterator();
if (multipartIterator == null) {
response.appendContentString("Already Consumed!");
} else {
WOMultipartIterator.WOFormData formData = null;
while ((formData = multipartIterator.nextFormData()) != null) {
String name = formData.name();
if (sessionIdKey.equals(name)) {
sessionId = formData.formValue();
} else if ("id".equals(name)) {
uploadIdentifier = formData.formValue();
} else if (formData.isFileUpload()) {
uploadFileName = request.stringFormValueForKey(name + ".filename");
streamLength = multipartIterator.contentLengthRemaining();
uploadInputStream = formData.formDataInputStream();
break;
}
}
context._setRequestSessionID(sessionId);
WOSession session = null;
if (context._requestSessionID() != null) {
session = WOApplication.application().restoreSessionWithID(sessionId, context);
}
if (session == null) {
throw new Exception("No valid session!");
}
File tempFile = File.createTempFile("AjaxFileUpload", ".tmp", _tempFileFolder);
tempFile.deleteOnExit();
AjaxUploadProgress progress = new AjaxUploadProgress(uploadIdentifier, tempFile, uploadFileName, streamLength);
try {
AjaxProgressBar.registerProgress(session, progress);
} finally {
if (context._requestSessionID() != null) {
WOApplication.application().saveSessionForContext(context);
}
}
if (formData != null) {
NSArray<String> contentType = (NSArray<String>) formData.headers().valueForKey("content-type");
if (contentType != null) {
progress.setContentType(contentType.objectAtIndex(0));
}
}
try {
if (_maxUploadSize >= 0L && streamLength > _maxUploadSize) {
IOException e = new IOException("You attempted to upload a file larger than the maximum allowed size of " + new ERXUnitAwareDecimalFormat(ERXUnitAwareDecimalFormat.BYTE).format(_maxUploadSize) + ".");
progress.setFailure(e);
progress.dispose();
throw e;
}
try (FileOutputStream fos = new FileOutputStream(progress.tempFile())) {
progress.copyAndTrack(uploadInputStream, fos, _maxUploadSize);
}
if (!progress.isCanceled() && !progress.shouldReset()) {
downloadFinished(progress);
}
} finally {
progress.setDone(true);
}
}
} catch (Throwable t) {
log.error("Upload failed", t);
response.appendContentString("Failed: " + t.getMessage());
} finally {
if (uploadInputStream != null) {
try {
uploadInputStream.close();
} catch (IOException e) {
// ignore
}
}
}
return response;
} finally {
application.sleep();
}
}
Aggregations