Search in sources :

Example 1 with JSONRPCBridge

use of org.jabsorb.JSONRPCBridge in project wonder-slim by undur.

the class JSONRequestHandler method createBridgeForComponent.

protected JSONRPCBridge createBridgeForComponent(JSONComponent component, String componentName, String componentInstance, Map<String, JSONRPCBridge> componentBridges) throws Exception {
    JSONRPCBridge jsonBridge = JSONBridge.createBridge();
    jsonBridge.registerCallableReference(JSONComponent.class);
    jsonBridge.registerObject("component", component);
    String componentNameAndInstance = JSONRequestHandler.componentNameAndInstance(componentName, componentInstance);
    componentBridges.put(componentNameAndInstance, jsonBridge);
    return jsonBridge;
}
Also used : JSONRPCBridge(org.jabsorb.JSONRPCBridge)

Example 2 with JSONRPCBridge

use of org.jabsorb.JSONRPCBridge 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 3 with JSONRPCBridge

use of org.jabsorb.JSONRPCBridge in project wonder-slim by undur.

the class AjaxProxy method handleRequest.

/**
 * Ask the an JSONRPCBridge object to handle the json request.
 */
@Override
public WOActionResults handleRequest(WORequest request, WOContext context) {
    WOResponse response = AjaxUtils.createResponse(request, context);
    response.setHeader("application/json", "content-type");
    String inputString = request.contentString();
    log.debug("AjaxProxy.handleRequest: input = {}", inputString);
    // Process the request
    JSONObject input = null;
    Object output = null;
    try {
        input = new JSONObject(inputString);
        Object proxy;
        if (canGetValueForBinding("proxy")) {
            proxy = valueForBinding("proxy");
        } else {
            proxy = parent();
            log.warn("No proxy binding given, so using parent component. This is probably a very bad idea.");
        }
        String proxyName = (String) valueForBinding("proxyName");
        JSONRPCBridge bridge = getBridgeBinding();
        if (bridge == null) {
            bridge = JSONBridge.createBridge();
            setBridgeBinding(bridge);
        }
        bridge.registerObject(proxyName, proxy);
        output = bridge.call(new Object[] { request, context, response, proxy }, input);
    } catch (NoSuchElementException e) {
        log.error("No method in request");
        output = JSONRPCResult.MSG_ERR_NOMETHOD;
    } catch (Exception e) {
        log.error("Exception", e);
        output = JSONRPCResult.MSG_ERR_NOMETHOD;
    }
    // Write the response
    log.debug("AjaxProxy.handleRequest: output = {}", output);
    response.appendContentString(output.toString());
    return response;
}
Also used : JSONObject(org.json.JSONObject) JSONRPCBridge(org.jabsorb.JSONRPCBridge) JSONObject(org.json.JSONObject) WOResponse(com.webobjects.appserver.WOResponse) NoSuchElementException(java.util.NoSuchElementException) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

JSONRPCBridge (org.jabsorb.JSONRPCBridge)3 WOResponse (com.webobjects.appserver.WOResponse)2 NoSuchElementException (java.util.NoSuchElementException)2 JSONObject (org.json.JSONObject)2 WOApplication (com.webobjects.appserver.WOApplication)1 WOContext (com.webobjects.appserver.WOContext)1 WODynamicURL (com.webobjects.appserver.WODynamicURL)1 WOSession (com.webobjects.appserver.WOSession)1 ERXMutableURL (er.extensions.foundation.ERXMutableURL)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 JSONRPCResult (org.jabsorb.JSONRPCResult)1 JSONException (org.json.JSONException)1