Search in sources :

Example 11 with WebContext

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

the class DefaultWebContextBuilder method set.

/* (non-Javadoc)
     * @see org.directwebremoting.WebContextBuilder#set(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, javax.servlet.ServletConfig, javax.servlet.ServletContext, org.directwebremoting.Container)
     */
public void set(HttpServletRequest request, HttpServletResponse response, ServletConfig config, ServletContext context, Container container) {
    try {
        WebContext ec = new DefaultWebContext(request, response, config, context, container);
        user.set(ec);
    } catch (Exception ex) {
        log.fatal("Failed to create an ExecutionContext", ex);
    }
}
Also used : WebContext(org.directwebremoting.WebContext)

Example 12 with WebContext

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

the class DefaultCreatorManager method addCreator.

/* (non-Javadoc)
     * @see org.directwebremoting.CreatorManager#addCreator(java.lang.String, org.directwebremoting.Creator)
     */
public void addCreator(String scriptName, Creator creator) throws IllegalArgumentException {
    // Check that we don't have this one already
    Creator other = (Creator) creators.get(scriptName);
    if (other != null) {
        throw new IllegalArgumentException(Messages.getString("DefaultCreatorManager.DuplicateName", scriptName, other.getType().getName(), creator));
    }
    // Check that it can at least tell us what type of thing we will be getting
    try {
        Class test = creator.getType();
        if (test == null) {
            log.error("Creator: '" + creator + "' for " + scriptName + ".js is returning null for type queries.");
        } else {
            log.debug("- adding creator: " + LocalUtil.getShortClassName(creator.getClass()) + " for " + scriptName);
            creators.put(scriptName, creator);
        }
    } catch (NoClassDefFoundError ex) {
        log.error("Missing class for creator '" + creator + "'. Cause: " + ex.getMessage());
    } catch (Exception ex) {
        log.error("Error loading class for creator '" + creator + "'.", ex);
    }
    // DefaultRemoter.execute(Call call)
    if (initApplicationScopeCreatorsAtStartup && creator.getScope().equals(Creator.APPLICATION)) {
        try {
            WebContext webcx = WebContextFactory.get();
            Object object = creator.getInstance();
            webcx.getServletContext().setAttribute(creator.getJavascript(), object);
            log.debug("Created new " + creator.getJavascript() + ", stored in application.");
        } catch (InstantiationException ex) {
            log.warn("Failed to create " + creator.getJavascript(), ex);
            log.debug("Maybe it will succeed when the application is in flight. If so you should probably set initApplicationScopeCreatorsAtStartup=false in web.xml");
        }
    }
}
Also used : WebContext(org.directwebremoting.WebContext) Creator(org.directwebremoting.extend.Creator)

Example 13 with WebContext

use of org.directwebremoting.WebContext 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)

Example 14 with WebContext

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

the class LocalizableMessageConverter method convertOutbound.

@Override
public OutboundVariable convertOutbound(Object data, OutboundContext outctx) throws MarshallException {
    WebContext webctx = WebContextFactory.get();
    LocalizableMessage lm = (LocalizableMessage) data;
    String s = lm.getLocalizedMessage(I18NUtils.getBundle(webctx.getHttpServletRequest()));
    return super.convertOutbound(s, outctx);
}
Also used : WebContext(org.directwebremoting.WebContext) LocalizableMessage(com.serotonin.web.i18n.LocalizableMessage)

Example 15 with WebContext

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

the class LocalizationFilter method doFilter.

public Object doFilter(Object obj, Method method, Object[] params, AjaxFilterChain chain) throws Exception {
    WebContext webContext = WebContextFactory.get();
    if (resourceBundleDirectory != null && resourceBundleLoader == null)
        resourceBundleLoader = new ResourceBundleLoader(webContext.getServletContext().getRealPath(resourceBundleDirectory));
    I18NUtils.prepareRequest(webContext.getHttpServletRequest(), localeResolverName, bundleBaseName, resourceBundleLoader);
    return chain.doFilter(obj, method, params);
}
Also used : WebContext(org.directwebremoting.WebContext) ResourceBundleLoader(com.serotonin.web.i18n.ResourceBundleLoader)

Aggregations

WebContext (org.directwebremoting.WebContext)18 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 DwrPermission (com.serotonin.m2m2.web.dwr.util.DwrPermission)4 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)3 Iterator (java.util.Iterator)3 Creator (org.directwebremoting.extend.Creator)3 ReportVO (com.serotonin.m2m2.reports.vo.ReportVO)2 Method (java.lang.reflect.Method)2 MarshallException (org.directwebremoting.extend.MarshallException)2 InvalidArgumentException (com.serotonin.InvalidArgumentException)1 IMangoLifecycle (com.serotonin.m2m2.IMangoLifecycle)1 CompoundChild (com.serotonin.m2m2.gviews.component.CompoundChild)1 CompoundComponent (com.serotonin.m2m2.gviews.component.CompoundComponent)1 ImageChartComponent (com.serotonin.m2m2.gviews.component.ImageChartComponent)1 PointComponent (com.serotonin.m2m2.gviews.component.PointComponent)1 SimpleCompoundComponent (com.serotonin.m2m2.gviews.component.SimpleCompoundComponent)1 SimplePointComponent (com.serotonin.m2m2.gviews.component.SimplePointComponent)1 ViewComponent (com.serotonin.m2m2.gviews.component.ViewComponent)1 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)1 ReportDao (com.serotonin.m2m2.reports.ReportDao)1