Search in sources :

Example 1 with WODynamicURL

use of com.webobjects.appserver.WODynamicURL 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 2 with WODynamicURL

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

the class ERXStaticResourceRequestHandler method handleRequest.

@Override
public WOResponse handleRequest(WORequest request) {
    WOResponse response = null;
    InputStream is = null;
    long length = 0;
    String contentType = null;
    String uri = request.uri();
    if (uri.charAt(0) == '/') {
        WOResourceManager rm = application.resourceManager();
        String documentRoot = documentRoot();
        File file = null;
        StringBuilder sb = new StringBuilder(documentRoot.length() + uri.length());
        String wodataKey = request.stringFormValueForKey("wodata");
        if (uri.startsWith("/cgi-bin") && wodataKey != null) {
            uri = wodataKey;
            if (uri.startsWith("file:")) {
                // remove file:/
                uri = uri.substring(5);
            } else {
            }
        } else {
            int index = uri.indexOf("/wodata=");
            if (index >= 0) {
                uri = uri.substring(index + "/wodata=".length());
            } else {
                sb.append(documentRoot);
            }
        }
        if (_useRequestHandlerPath) {
            try {
                WODynamicURL dynamicURL = new WODynamicURL(uri);
                String requestHandlerPath = dynamicURL.requestHandlerPath();
                if (requestHandlerPath == null || requestHandlerPath.length() == 0) {
                    sb.append(uri);
                } else {
                    sb.append('/');
                    sb.append(requestHandlerPath);
                }
            } catch (Exception e) {
                throw new RuntimeException("Failed to parse URL '" + uri + "'.", e);
            }
        } else {
            sb.append(uri);
        }
        String path = sb.toString();
        try {
            path = path.replaceAll("\\?.*", "");
            if (request.userInfo() != null && !request.userInfo().containsKey("HttpServletRequest")) {
                /* PATH_INFO is already decoded by the servlet container */
                path = path.replace('+', ' ');
                path = URLDecoder.decode(path, StandardCharsets.UTF_8);
            }
            file = new File(path);
            if (path.startsWith("jar:")) {
                URLConnection uc = new URL(path).openConnection();
                if (uc instanceof JarURLConnection) {
                    length = (int) uc.getContentLengthLong();
                } else {
                    length = -1;
                }
                is = uc.getInputStream();
            } else {
                length = (int) file.length();
                is = new FileInputStream(file);
            }
            contentType = rm.contentTypeForResourceNamed(path);
            log.debug("Reading file '{}' for uri: {}", file, uri);
        } catch (IOException ex) {
            if (!uri.toLowerCase().endsWith("/favicon.ico")) {
                log.info("Unable to get contents of file '{}' for uri: {}", file, uri);
            }
        }
    } else {
        log.error("Can't fetch relative path: {}", uri);
    }
    response = _generateResponseForInputStream(is, length, contentType);
    NSNotificationCenter.defaultCenter().postNotification(WORequestHandler.DidHandleRequestNotification, response);
    response._finalizeInContext(null);
    return response;
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) JarURLConnection(java.net.JarURLConnection) WODynamicURL(com.webobjects.appserver.WODynamicURL) IOException(java.io.IOException) IOException(java.io.IOException) URLConnection(java.net.URLConnection) JarURLConnection(java.net.JarURLConnection) URL(java.net.URL) WODynamicURL(com.webobjects.appserver.WODynamicURL) FileInputStream(java.io.FileInputStream) WOResourceManager(com.webobjects.appserver.WOResourceManager) WOResponse(com.webobjects.appserver.WOResponse) File(java.io.File)

Aggregations

WODynamicURL (com.webobjects.appserver.WODynamicURL)2 WOResponse (com.webobjects.appserver.WOResponse)2 WOApplication (com.webobjects.appserver.WOApplication)1 WOContext (com.webobjects.appserver.WOContext)1 WOResourceManager (com.webobjects.appserver.WOResourceManager)1 WOSession (com.webobjects.appserver.WOSession)1 ERXMutableURL (er.extensions.foundation.ERXMutableURL)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 JarURLConnection (java.net.JarURLConnection)1 URL (java.net.URL)1 URLConnection (java.net.URLConnection)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 NoSuchElementException (java.util.NoSuchElementException)1 JSONRPCBridge (org.jabsorb.JSONRPCBridge)1 JSONRPCResult (org.jabsorb.JSONRPCResult)1 JSONException (org.json.JSONException)1