use of com.webobjects.appserver.WOResponse 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();
}
}
use of com.webobjects.appserver.WOResponse in project wonder-slim by undur.
the class AjaxHyperlink method handleRequest.
@Override
public WOActionResults handleRequest(WORequest request, WOContext context) {
WOComponent component = context.component();
WOActionResults results = (WOActionResults) valueForBinding("action", component);
if (results == null) {
String script = (String) valueForBinding("onClickServer", component);
if (script != null) {
WOResponse response = AjaxUtils.createResponse(request, context);
AjaxUtils.appendScriptHeader(response);
response.appendContentString(script);
AjaxUtils.appendScriptFooter(response);
results = response;
}
}
return results;
}
use of com.webobjects.appserver.WOResponse in project wonder-slim by undur.
the class AjaxInPlaceEditor method handleRequest.
// Formatting/Parsing method "inspired by" WOTextField
@Override
public WOActionResults handleRequest(WORequest request, WOContext context) {
WOComponent component = context.component();
String strValue = request.stringFormValueForKey("value");
Object objValue = strValue;
if (strValue != null) {
Format format = null;
if (strValue.length() != 0) {
format = WOFormatterRepository.formatterForComponent(component, _dateFormat, _numberFormat, _formatter);
}
if (format != null) {
try {
Object parsedValue = format.parseObject(strValue);
String formattedValue = format.format(parsedValue);
objValue = format.parseObject(formattedValue);
} catch (ParseException parseexception) {
String valueKeyPath = _valueAssociation.keyPath();
ValidationException validationexception = new ValidationException(parseexception.getMessage(), strValue, valueKeyPath);
component.validationFailedWithException(validationexception, strValue, valueKeyPath);
return null;
}
if (objValue != null && _useDecimalNumber != null && _useDecimalNumber.booleanValueInComponent(component)) {
objValue = new BigDecimal(objValue.toString());
}
} else if (objValue.toString().length() == 0) {
objValue = null;
}
}
_valueAssociation.setValue(objValue, component);
// just executing action, ignoring result
valueForBinding("action", component);
WOResponse response = AjaxUtils.createResponse(request, context);
_appendValueAttributeToResponse(response, context);
return response;
}
use of com.webobjects.appserver.WOResponse in project wonder-slim by undur.
the class AjaxUpdateContainer method handleRequest.
@Override
public WOActionResults handleRequest(WORequest request, WOContext context) {
WOComponent component = context.component();
String id = _containerID(context);
if (associations().objectForKey("action") != null) {
@SuppressWarnings("unused") WOActionResults results = (WOActionResults) valueForBinding("action", component);
// ignore results
}
WOResponse response = AjaxUtils.createResponse(request, context);
AjaxUtils.setPageReplacementCacheKey(context, id);
if (hasChildrenElements()) {
appendChildrenToResponse(response, context);
}
String onRefreshComplete = (String) valueForBinding("onRefreshComplete", component);
if (onRefreshComplete != null) {
AjaxUtils.appendScriptHeader(response);
response.appendContentString(onRefreshComplete);
AjaxUtils.appendScriptFooter(response);
}
if (AjaxModalDialog.isInDialog(context)) {
AjaxUtils.appendScriptHeader(response);
response.appendContentString("AMD.contentUpdated();");
AjaxUtils.appendScriptFooter(response);
}
return null;
}
use of com.webobjects.appserver.WOResponse in project wonder-slim by undur.
the class AjaxSubmitButton method handleRequest.
@Override
public WOActionResults handleRequest(WORequest request, WOContext context) {
WOComponent component = context.component();
WOActionResults result = (WOActionResults) valueForBinding("action", component);
if (ERXAjaxApplication.isAjaxReplacement(request)) {
AjaxUtils.setPageReplacementCacheKey(context, (String) valueForBinding("replaceID", component));
} else if (result == null || booleanValueForBinding("ignoreActionResponse", false, component)) {
WOResponse response = AjaxUtils.createResponse(request, context);
String onClickServer = (String) valueForBinding("onClickServer", component);
if (onClickServer != null) {
AjaxUtils.appendScriptHeaderIfNecessary(request, response);
response.appendContentString(onClickServer);
AjaxUtils.appendScriptFooterIfNecessary(request, response);
}
result = response;
} else {
String updateContainerID = AjaxUpdateContainer.updateContainerID(this, component);
if (updateContainerID != null) {
AjaxUtils.setPageReplacementCacheKey(context, updateContainerID);
}
}
return result;
}
Aggregations