Search in sources :

Example 6 with WOApplication

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();
    }
}
Also used : ERXMutableURL(er.extensions.foundation.ERXMutableURL) WODynamicURL(com.webobjects.appserver.WODynamicURL) JSONRPCBridge(org.jabsorb.JSONRPCBridge) JSONException(org.json.JSONException) JSONRPCResult(org.jabsorb.JSONRPCResult) JSONObject(org.json.JSONObject) WOContext(com.webobjects.appserver.WOContext) WOSession(com.webobjects.appserver.WOSession) JSONObject(org.json.JSONObject) WOResponse(com.webobjects.appserver.WOResponse) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) WOApplication(com.webobjects.appserver.WOApplication) NoSuchElementException(java.util.NoSuchElementException)

Example 7 with WOApplication

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");
}
Also used : ERXAjaxApplication(er.extensions.appserver.ajax.ERXAjaxApplication) WOApplication(com.webobjects.appserver.WOApplication)

Example 8 with WOApplication

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;
}
Also used : WOResponse(com.webobjects.appserver.WOResponse) WOApplication(com.webobjects.appserver.WOApplication)

Example 9 with WOApplication

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;
}
Also used : WOResponse(com.webobjects.appserver.WOResponse) WOApplication(com.webobjects.appserver.WOApplication)

Example 10 with WOApplication

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;
}
Also used : WORequest(com.webobjects.appserver.WORequest) WOActionResults(com.webobjects.appserver.WOActionResults) WOComponent(com.webobjects.appserver.WOComponent) WOResponse(com.webobjects.appserver.WOResponse) WOApplication(com.webobjects.appserver.WOApplication)

Aggregations

WOApplication (com.webobjects.appserver.WOApplication)11 WOResponse (com.webobjects.appserver.WOResponse)7 WOComponent (com.webobjects.appserver.WOComponent)4 WOContext (com.webobjects.appserver.WOContext)4 WOSession (com.webobjects.appserver.WOSession)3 WORequest (com.webobjects.appserver.WORequest)2 NSBundle (com.webobjects.foundation.NSBundle)2 NSDictionary (com.webobjects.foundation.NSDictionary)2 File (java.io.File)2 IOException (java.io.IOException)2 WOActionResults (com.webobjects.appserver.WOActionResults)1 WODynamicURL (com.webobjects.appserver.WODynamicURL)1 WOMultipartIterator (com.webobjects.appserver.WOMultipartIterator)1 WOStatisticsStore (com.webobjects.appserver.WOStatisticsStore)1 NSArray (com.webobjects.foundation.NSArray)1 ERXAjaxApplication (er.extensions.appserver.ajax.ERXAjaxApplication)1 ERXUnitAwareDecimalFormat (er.extensions.formatters.ERXUnitAwareDecimalFormat)1 ERXMutableURL (er.extensions.foundation.ERXMutableURL)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1