Search in sources :

Example 1 with AjaxFilterChain

use of org.directwebremoting.AjaxFilterChain in project ma-core-public by infiniteautomation.

the class DefaultRemoter method execute.

/**
 * Execute a single call object
 * @param call The call to execute
 * @return A Reply to the Call
 */
public Reply execute(Call call) {
    try {
        Method method = call.getMethod();
        if (method == null || call.getException() != null) {
            return new Reply(call.getCallId(), null, call.getException());
        }
        // Get a list of the available matching methods with the coerced
        // parameters that we will use to call it if we choose to use that
        // method.
        Creator creator = creatorManager.getCreator(call.getScriptName());
        // We don't need to check accessControl.getReasonToNotExecute()
        // because the checks are made by the doExec method, but we do check
        // if we can display it
        accessControl.assertExecutionIsPossible(creator, call.getScriptName(), method);
        // Get ourselves an object to execute a method on unless the
        // method is static
        Object object = null;
        String scope = creator.getScope();
        boolean create = false;
        if (!Modifier.isStatic(method.getModifiers())) {
            WebContext webcx = WebContextFactory.get();
            // Check the various scopes to see if it is there
            if (scope.equals(Creator.APPLICATION)) {
                object = webcx.getServletContext().getAttribute(call.getScriptName());
            } else if (scope.equals(Creator.SESSION)) {
                object = webcx.getSession().getAttribute(call.getScriptName());
            } else if (scope.equals(Creator.SCRIPT)) {
                object = webcx.getScriptSession().getAttribute(call.getScriptName());
            } else if (scope.equals(Creator.REQUEST)) {
                object = webcx.getHttpServletRequest().getAttribute(call.getScriptName());
            }
            // If we don't have an object the call the creator
            if (object == null) {
                create = true;
                object = creator.getInstance();
            }
            // Remember it for next time
            if (create) {
                if (scope.equals(Creator.APPLICATION)) {
                    // This might also be done at application startup by
                    // DefaultCreatorManager.addCreator(String, Creator)
                    webcx.getServletContext().setAttribute(call.getScriptName(), object);
                } else if (scope.equals(Creator.SESSION)) {
                    webcx.getSession().setAttribute(call.getScriptName(), object);
                } else if (scope.equals(Creator.SCRIPT)) {
                    webcx.getScriptSession().setAttribute(call.getScriptName(), object);
                } else if (scope.equals(Creator.REQUEST)) {
                    webcx.getHttpServletRequest().setAttribute(call.getScriptName(), object);
                }
            // Creator.PAGE scope means we create one every time anyway
            }
        }
        // Some debug
        log.info("Exec: " + call.getScriptName() + "." + call.getMethodName() + "()");
        if (log.isDebugEnabled()) {
            StringBuffer buffer = new StringBuffer();
            if (create) {
                buffer.append("--Object created, ");
                if (!scope.equals(Creator.PAGE)) {
                    buffer.append(" stored in ");
                    buffer.append(scope);
                } else {
                    buffer.append(" not stored");
                }
            } else {
                buffer.append("--Object found in ");
                buffer.append(scope);
            }
            buffer.append(". ");
            // It would be good to debug the params but it's not easy
            // buffer.append("Call params (");
            // for (int j = 0; j < inctx.getParameterCount(callNum); j++)
            // {
            // if (j != 0)
            // {
            // buffer.append(", ");
            // }
            // InboundVariable param = inctx.getParameter(callNum, j);
            // buffer.append(param.toString());
            // }
            // buffer.append(") ");
            buffer.append("id=");
            buffer.append(call.getCallId());
            log.debug(buffer.toString());
        }
        // Execute the filter chain method.toString()
        final Iterator it = ajaxFilterManager.getAjaxFilters(call.getScriptName());
        AjaxFilterChain chain = new AjaxFilterChain() {

            public Object doFilter(Object obj, Method meth, Object[] p) throws Exception {
                AjaxFilter next = (AjaxFilter) it.next();
                return next.doFilter(obj, meth, p, this);
            }
        };
        Object reply = chain.doFilter(object, method, call.getParameters());
        return new Reply(call.getCallId(), reply);
    } catch (InvocationTargetException ex) {
        // Allow Jetty RequestRetry exception to propogate to container
        Continuation.rethrowIfContinuation(ex);
        log.warn("Method execution failed: ", ex.getTargetException());
        return new Reply(call.getCallId(), null, ex.getTargetException());
    } catch (Exception ex) {
        // Allow Jetty RequestRetry exception to propogate to container
        Continuation.rethrowIfContinuation(ex);
        log.warn("Method execution failed: ", ex);
        return new Reply(call.getCallId(), null, ex);
    }
}
Also used : WebContext(org.directwebremoting.WebContext) Method(java.lang.reflect.Method) Creator(org.directwebremoting.extend.Creator) AjaxFilterChain(org.directwebremoting.AjaxFilterChain) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Iterator(java.util.Iterator) Reply(org.directwebremoting.extend.Reply) AjaxFilter(org.directwebremoting.AjaxFilter)

Aggregations

InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 Iterator (java.util.Iterator)1 AjaxFilter (org.directwebremoting.AjaxFilter)1 AjaxFilterChain (org.directwebremoting.AjaxFilterChain)1 WebContext (org.directwebremoting.WebContext)1 Creator (org.directwebremoting.extend.Creator)1 Reply (org.directwebremoting.extend.Reply)1