use of com.webobjects.appserver.WOContext in project wonder-slim by undur.
the class ERXComponentUtilities method componentTree.
/**
* Returns an array of the current component names.
*
* @return array of current component names
*/
public static NSArray<String> componentTree() {
WOContext context = ERXWOContext.currentContext();
NSMutableArray<String> result = new NSMutableArray<>();
if (context != null) {
WOComponent c = context.component();
while (c != null) {
result.addObject(c.name());
c = c.parent();
}
}
return result;
}
use of com.webobjects.appserver.WOContext 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.WOContext in project wonder-slim by undur.
the class AjaxUtils method redirectTo.
/**
* Creates (or modifies if already created) an AjaxResponse to redirect to the passed component with a component action.
* Anything previously written to the AjaxResponse is preserved.
*
* @param component full page WOComponent instance to redirect to
*/
public static void redirectTo(WOComponent component) {
WOContext context = component.context();
ERXRedirect redirect = (ERXRedirect) component.pageWithName(ERXRedirect.class.getName());
redirect.setComponent(component);
redirect.appendToResponse(AjaxUtils.createResponse(context.request(), context), context);
}
use of com.webobjects.appserver.WOContext in project wonder-slim by undur.
the class ERXAjaxSession method savePageInPermanentCache.
/**
* Saves a page in the permanent cache. Overridden to not save in the super implementation's iVars but in our own.
*/
// FIXME: ak: as we save the perm pages under a lot of context IDs, we should have a way to actually limit the size...
// not sure how, though
@Override
public void savePageInPermanentCache(WOComponent wocomponent) {
if (overridePrivateCache) {
WOContext wocontext = context();
String contextID = wocontext.contextID();
log.debug("Saving page for contextID: {}", contextID);
NSMutableDictionary permanentPageCache = _permanentPageCache();
for (int i = WOApplication.application().permanentPageCacheSize(); _permanentContextIDArray.count() >= i; _permanentContextIDArray.removeObjectAtIndex(0)) {
String s1 = (String) _permanentContextIDArray.objectAtIndex(0);
WOComponent page = (WOComponent) permanentPageCache.removeObjectForKey(s1);
if (storesPageInfo()) {
pageInfoDictionary().removeObjectForKey(page);
}
}
permanentPageCache.setObjectForKey(wocomponent, contextID);
_permanentContextIDArray.addObject(contextID);
} else {
super.savePageInPermanentCache(wocomponent);
}
}
use of com.webobjects.appserver.WOContext in project wonder-slim by undur.
the class ERXAjaxSession method restorePageForContextID.
/**
* Extension of restorePageForContextID that implements the other side of
* Page Replacement Cache.
*/
@Override
public WOComponent restorePageForContextID(String contextID) {
log.debug("Restoring page for contextID: {}", contextID);
LinkedHashMap pageReplacementCache = (LinkedHashMap) objectForKey(ERXAjaxSession.PAGE_REPLACEMENT_CACHE_KEY);
WOComponent page = null;
if (pageReplacementCache != null) {
TransactionRecord pageRecord = (TransactionRecord) pageReplacementCache.get(contextID);
if (pageRecord != null) {
log.debug("Restoring page for contextID: {} pageRecord = {}", contextID, pageRecord);
page = pageRecord.page();
} else {
log.debug("No page in pageReplacementCache for contextID: {}", contextID);
// If we got the page out of the replacement cache above, then we're obviously still
// using Ajax, and it's likely our cache will be cleaned out in an Ajax update. If the
// requested page was not in the cache, though, then we might be done with Ajax,
// so give the cache a quick run-through for expired pages.
cleanPageReplacementCacheIfNecessary();
}
}
// AK: this will get handled last in the super implementation, so we do it here
if (page == null && overridePrivateCache) {
page = _permanentPageWithContextID(contextID);
if (page != null)
page._awakeInContext(context());
}
if (page == null) {
page = super.restorePageForContextID(contextID);
}
if (page != null) {
WOContext context = page.context();
if (context == null) {
page._awakeInContext(context());
context = page.context();
}
WORequest request = context.request();
// enough information at this point to know whether to do it or not, unfortunately.
if (request != null) {
request.setHeader(contextID, ERXAjaxSession.ORIGINAL_CONTEXT_ID_KEY);
}
}
return page;
}
Aggregations