use of com.webobjects.appserver.WOApplication in project wonder-slim by undur.
the class JSONRequestHandler method handleRequest.
@SuppressWarnings("unchecked")
@Override
public WOResponse handleRequest(WORequest request) {
WOApplication application = WOApplication.application();
application.awake();
try {
WOContext context = application.createContextForRequest(request);
WOResponse response = application.createResponseInContext(context);
Object output;
try {
String inputString = request.contentString();
JSONObject input = new JSONObject(inputString);
String sessionIdKey = WOApplication.application().sessionIdKey();
String sessionId = request.cookieValueForKey(sessionIdKey);
if (sessionId == null) {
ERXMutableURL url = new ERXMutableURL();
url.setQueryParameters(request.queryString());
sessionId = url.queryParameter(sessionIdKey);
if (sessionId == null && input.has(sessionIdKey)) {
sessionId = input.getString(sessionIdKey);
}
}
context._setRequestSessionID(sessionId);
WOSession session = null;
if (context._requestSessionID() != null) {
session = WOApplication.application().restoreSessionWithID(sessionId, context);
}
if (session != null) {
session.awake();
}
try {
JSONComponentCallback componentCallback = null;
WODynamicURL url = request._uriDecomposed();
String requestHandlerPath = url.requestHandlerPath();
JSONRPCBridge jsonBridge;
if (requestHandlerPath != null && requestHandlerPath.length() > 0) {
String componentNameAndInstance = requestHandlerPath;
String componentInstance;
String componentName;
int slashIndex = componentNameAndInstance.indexOf('/');
if (slashIndex == -1) {
componentName = componentNameAndInstance;
componentInstance = null;
} else {
componentName = componentNameAndInstance.substring(0, slashIndex);
componentInstance = componentNameAndInstance.substring(slashIndex + 1);
}
if (session == null) {
session = context.session();
}
String bridgesKey = (componentInstance == null) ? "_JSONGlobalBridges" : "_JSONInstanceBridges";
Map<String, JSONRPCBridge> componentBridges = (Map<String, JSONRPCBridge>) session.objectForKey(bridgesKey);
if (componentBridges == null) {
int limit = ERXProperties.intForKeyWithDefault((componentInstance == null) ? "er.ajax.json.globalBacktrackCacheSize" : "er.ajax.json.backtrackCacheSize", WOApplication.application().pageCacheSize());
componentBridges = new LRUMap<>(limit);
session.setObjectForKey(componentBridges, bridgesKey);
}
jsonBridge = componentBridges.get(componentNameAndInstance);
if (jsonBridge == null) {
Class componentClass = _NSUtilities.classWithName(componentName);
JSONComponent component;
if (JSONComponent.class.isAssignableFrom(componentClass)) {
component = (JSONComponent) _NSUtilities.instantiateObject(componentClass, new Class[] { WOContext.class }, new Object[] { context }, true, false);
} else {
throw new SecurityException("There is no JSON component named '" + componentName + "'.");
}
jsonBridge = createBridgeForComponent(component, componentName, componentInstance, componentBridges);
}
componentCallback = new JSONComponentCallback(context);
jsonBridge.registerCallback(componentCallback, WOContext.class);
} else {
jsonBridge = _sharedBridge;
}
try {
output = jsonBridge.call(new Object[] { request, response, context }, input);
} finally {
if (componentCallback != null) {
jsonBridge.unregisterCallback(componentCallback, WOContext.class);
}
}
if (context._session() != null) {
WOSession contextSession = context._session();
// If this is a new session, then we have to force it to be a cookie session
if (sessionId == null) {
boolean storesIDsInCookies = contextSession.storesIDsInCookies();
try {
contextSession.setStoresIDsInCookies(true);
contextSession._appendCookieToResponse(response);
} finally {
contextSession.setStoresIDsInCookies(storesIDsInCookies);
}
} else {
contextSession._appendCookieToResponse(response);
}
}
response.appendContentString(output.toString());
response._finalizeInContext(context);
response.disableClientCaching();
} finally {
try {
if (session != null) {
session.sleep();
}
} finally {
if (context._session() != null) {
WOApplication.application().saveSessionForContext(context);
}
}
}
} catch (NoSuchElementException e) {
e.printStackTrace();
output = new JSONRPCResult(JSONRPCResult.CODE_ERR_NOMETHOD, null, JSONRPCResult.MSG_ERR_NOMETHOD);
} catch (JSONException e) {
e.printStackTrace();
output = new JSONRPCResult(JSONRPCResult.CODE_ERR_PARSE, null, JSONRPCResult.MSG_ERR_PARSE);
} catch (Throwable t) {
t.printStackTrace();
output = new JSONRPCResult(JSONRPCResult.CODE_ERR_PARSE, null, t.getMessage());
}
return response;
} finally {
application.sleep();
}
}
use of com.webobjects.appserver.WOApplication in project wonder-slim by undur.
the class Ajax method finishInitialization.
/**
* This is called directly only for when ERXApplication is sub-classed.
*/
@Override
public void finishInitialization() {
WOApplication application = WOApplication.application();
if (!AjaxRequestHandler.useAjaxRequestHandler()) {
application.registerRequestHandler(new AjaxRequestHandler(), AjaxRequestHandler.AjaxRequestHandlerKey);
log.debug("AjaxRequestHandler installed");
}
application.registerRequestHandler(new AjaxPushRequestHandler(), AjaxPushRequestHandler.AjaxCometRequestHandlerKey);
// to fix some weird border cases caused by structural page changes.
if (application instanceof ERXAjaxApplication) {
((ERXAjaxApplication) application).setResponseDelegate(new AjaxResponse.AjaxResponseDelegate());
}
log.debug("Ajax loaded");
}
use of com.webobjects.appserver.WOApplication in project wonder-slim by undur.
the class ERXDirectActionRequestHandler method handleRequest.
@Override
public WOResponse handleRequest(WORequest request) {
WOResponse response = null;
// ak: when addressed with a DA link with this instance's ID (and
// an expired session) and the app is refusing new sessions, the
// default implementation will create a session anyway, which will
// wreak havoc if the app is memory starved.
// Search engines are a nuisance in that regard
WOApplication app = WOApplication.application();
if (app.isRefusingNewSessions() && request.isUsingWebServer() && !isSystemRequest(request)) {
if (isSessionIDInRequest(request)) {
// yadda-yadda.
if (app.sessionStore().getClass() == WOServerSessionStore.class) {
if (app.sessionStore().restoreSessionWithID(request.sessionID(), request) == null) {
response = generateRequestRefusal(request);
// AK: should be a permanent redirect, as the session is gone for good.
// However, the adaptor checks explicitly on 302 so we return that...
// It shouldn't matter which instance we go to now.
response.setStatus(302);
}
}
} else {
// if no session was supplied, what are we doing here in the
// first place? The adaptor shouldn't have linked to us as
// we are refusing new sessions.
response = generateRequestRefusal(request);
}
}
if (response == null) {
response = super.handleRequest(request);
}
return response;
}
use of com.webobjects.appserver.WOApplication in project wonder-slim by undur.
the class ERXComponentRequestHandler method handleRequest.
@Override
public WOResponse handleRequest(WORequest aRequest) {
WOApplication anApplication = WOApplication.application();
Object globalLock = anApplication.requestHandlingLock();
WOResponse aResponse;
if (globalLock != null) {
synchronized (globalLock) {
aResponse = _handleRequest(aRequest);
}
} else {
aResponse = _handleRequest(aRequest);
}
return aResponse;
}
use of com.webobjects.appserver.WOApplication in project wonder-slim by undur.
the class ERXComponentRequestHandler method _dispatchWithPreparedPage.
private WOResponse _dispatchWithPreparedPage(WOComponent aPage, WOSession aSession, WOContext aContext, NSDictionary someElements) {
WORequest aRequest = aContext.request();
WOApplication anApplication = WOApplication.application();
WOResponse aResponse = anApplication.createResponseInContext(aContext);
String aSenderID = aContext.senderID();
String oldContextID = aSession._contextIDMatchingIDs(aContext);
aResponse.setHTTPVersion(aRequest.httpVersion());
aResponse.setHeader("text/html", "content-type");
aContext._setResponse(aResponse);
if (oldContextID == null) {
if (aSenderID != null) {
if (aRequest._hasFormValues()) {
anApplication.takeValuesFromRequest(aRequest, aContext);
}
}
aContext._setPageChanged(false);
if (aSenderID != null) {
WOActionResults anActionResults = anApplication.invokeAction(aRequest, aContext);
if ((anActionResults == null) || ((anActionResults instanceof WOComponent))) {
WOComponent aResultComponent = (WOComponent) anActionResults;
if ((aResultComponent != null) && (aResultComponent.context() != aContext)) {
aResultComponent._awakeInContext(aContext);
}
boolean didPageChange = false;
if ((aResultComponent != null) && (aResultComponent != aContext._pageElement())) {
didPageChange = true;
}
aContext._setPageChanged(didPageChange);
if (didPageChange) {
aContext._setPageElement(aResultComponent);
}
} else {
WOResponse theResponse = anActionResults.generateResponse();
return theResponse;
}
}
} else {
WOComponent responsePage = _restorePageForContextID(oldContextID, aSession);
aContext._setPageElement(responsePage);
}
anApplication.appendToResponse(aResponse, aContext);
return aResponse;
}
Aggregations