Search in sources :

Example 16 with WOResponse

use of com.webobjects.appserver.WOResponse in project wonder-slim by undur.

the class AjaxUtils method createResponse.

/**
 * Creates a response for the given context (which can be null), sets the charset to UTF-8, the connection to
 * keep-alive and flags it as a Ajax request by adding an AJAX_REQUEST_KEY header. You can check this header in the
 * session to decide if you want to save the request or not.
 *
 * @param request the current request
 * @param context the current context
 * @return a new Ajax response
 */
public static AjaxResponse createResponse(WORequest request, WOContext context) {
    AjaxResponse response = null;
    if (context != null && context.response() != null) {
        WOResponse existingResponse = context.response();
        if (existingResponse instanceof AjaxResponse) {
            response = (AjaxResponse) existingResponse;
        } else {
            response = new AjaxResponse(request, context);
            response.setHeaders(existingResponse.headers());
            response.setUserInfo(existingResponse.userInfo());
            response.appendContentString(existingResponse.contentString());
        }
    }
    if (response == null) {
        response = new AjaxResponse(request, context);
        response.setHeader("text/plain; charset=utf-8", "content-type");
    }
    if (context != null) {
        context._setResponse(response);
    }
    // Encode using UTF-8, although We are actually ASCII clean as all
    // unicode data is JSON escaped using backslash u. This is less data
    // efficient for foreign character sets but it is needed to support
    // naughty browsers such as Konqueror and Safari which do not honour the
    // charset set in the response
    response.setHeader("Connection", "keep-alive");
    response.setHeader(ERXAjaxSession.DONT_STORE_PAGE, ERXAjaxSession.DONT_STORE_PAGE);
    return response;
}
Also used : WOResponse(com.webobjects.appserver.WOResponse)

Example 17 with WOResponse

use of com.webobjects.appserver.WOResponse 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 18 with WOResponse

use of com.webobjects.appserver.WOResponse in project wonder-slim by undur.

the class AjaxSlider method handleRequest.

@Override
public WOActionResults handleRequest(WORequest worequest, WOContext wocontext) {
    WOResponse result = AjaxUtils.createResponse(worequest, wocontext);
    String mode = worequest.stringFormValueForKey("ajaxSlideTrigger");
    if (mode != null) {
        result.setHeader("text/javascript", "content-type");
        result.setContent((String) valueForBinding(mode + "Server", ""));
    }
    return result;
}
Also used : WOResponse(com.webobjects.appserver.WOResponse)

Example 19 with WOResponse

use of com.webobjects.appserver.WOResponse in project wonder-slim by undur.

the class AjaxModalContainer method handleRequest.

@Override
public WOActionResults handleRequest(WORequest request, WOContext context) {
    WOComponent component = context.component();
    WOResponse response = null;
    WOAssociation action = associations().objectForKey("action");
    if (action != null) {
        action.valueInComponent(component);
    }
    if (booleanValueForBinding("ajax", false, component) && hasChildrenElements()) {
        response = AjaxUtils.createResponse(request, context);
        AjaxUtils.setPageReplacementCacheKey(context, _containerID(context));
        appendChildrenToResponse(response, context);
    }
    return response;
}
Also used : WOAssociation(com.webobjects.appserver.WOAssociation) WOComponent(com.webobjects.appserver.WOComponent) WOResponse(com.webobjects.appserver.WOResponse)

Example 20 with WOResponse

use of com.webobjects.appserver.WOResponse in project wonder-slim by undur.

the class AjaxTabbedPanelTab method handleRequest.

/**
 * The pane content is rendered when an Ajax request is received.
 * @return the children rendered as HTML
 */
@Override
public WOActionResults handleRequest(WORequest request, WOContext context) {
    WOResponse response = null;
    String didSelect = request.stringFormValueForKey("didSelect");
    // This is not set when the tab is initially loaded, that is our cue to generate our content
    if (didSelect == null) {
        response = AjaxUtils.createResponse(request, context);
        AjaxUtils.setPageReplacementCacheKey(context, _containerID(context));
        if (content != null) {
            content.appendToResponse(response, context);
        }
    } else
        setIsSelected(context.component(), didSelect.equals("true"));
    return response;
}
Also used : WOResponse(com.webobjects.appserver.WOResponse)

Aggregations

WOResponse (com.webobjects.appserver.WOResponse)33 WOComponent (com.webobjects.appserver.WOComponent)10 WOApplication (com.webobjects.appserver.WOApplication)7 WOActionResults (com.webobjects.appserver.WOActionResults)6 WOContext (com.webobjects.appserver.WOContext)5 WORequest (com.webobjects.appserver.WORequest)4 WOSession (com.webobjects.appserver.WOSession)4 NSDictionary (com.webobjects.foundation.NSDictionary)4 WODynamicURL (com.webobjects.appserver.WODynamicURL)2 NSArray (com.webobjects.foundation.NSArray)2 File (java.io.File)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 Iterator (java.util.Iterator)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 NoSuchElementException (java.util.NoSuchElementException)2 JSONRPCBridge (org.jabsorb.JSONRPCBridge)2 JSONObject (org.json.JSONObject)2 WOAssociation (com.webobjects.appserver.WOAssociation)1