Search in sources :

Example 1 with FreemarkerResult

use of org.apache.struts2.views.freemarker.FreemarkerResult in project struts by apache.

the class ExecuteAndWaitInterceptor method doIntercept.

/* (non-Javadoc)
     * @see com.opensymphony.xwork2.interceptor.MethodFilterInterceptor#doIntercept(com.opensymphony.xwork2.ActionInvocation)
     */
protected String doIntercept(ActionInvocation actionInvocation) throws Exception {
    ActionProxy proxy = actionInvocation.getProxy();
    String name = getBackgroundProcessName(proxy);
    ActionContext context = actionInvocation.getInvocationContext();
    Map session = context.getSession();
    HttpSession httpSession = ServletActionContext.getRequest().getSession(true);
    Boolean secondTime = true;
    if (executeAfterValidationPass) {
        secondTime = (Boolean) context.get(KEY);
        if (secondTime == null) {
            context.put(KEY, true);
            secondTime = false;
        } else {
            secondTime = true;
            context.put(KEY, null);
        }
    }
    // on every request
    synchronized (httpSession) {
        BackgroundProcess bp = (BackgroundProcess) session.get(KEY + name);
        // WW-4900 Checks if from a de-serialized session? so background thread missed, let's start a new one.
        if (bp != null && bp.getInvocation() == null) {
            session.remove(KEY + name);
            bp = null;
        }
        if ((!executeAfterValidationPass || secondTime) && bp == null) {
            bp = getNewBackgroundProcess(name, actionInvocation, threadPriority);
            session.put(KEY + name, bp);
            // first time let some time pass before showing wait page
            performInitialDelay(bp);
            secondTime = false;
        }
        if ((!executeAfterValidationPass || !secondTime) && bp != null && !bp.isDone()) {
            actionInvocation.getStack().push(bp.getAction());
            final String token = TokenHelper.getToken();
            if (token != null) {
                TokenHelper.setSessionToken(TokenHelper.getTokenName(), token);
            }
            Map results = proxy.getConfig().getResults();
            if (!results.containsKey(WAIT)) {
                LOG.warn("ExecuteAndWait interceptor has detected that no result named 'wait' is available. " + "Defaulting to a plain built-in wait page. It is highly recommend you " + "provide an action-specific or global result named '{}'.", WAIT);
                // no wait result? hmm -- let's try to do dynamically put it in for you!
                // we used to add a fake "wait" result here, since the configuration is unmodifiable, that is no longer
                // an option, see WW-3068
                FreemarkerResult waitResult = new FreemarkerResult();
                container.inject(waitResult);
                waitResult.setLocation("/org/apache/struts2/interceptor/wait.ftl");
                waitResult.execute(actionInvocation);
                return Action.NONE;
            }
            return WAIT;
        } else if ((!executeAfterValidationPass || !secondTime) && bp != null && bp.isDone()) {
            session.remove(KEY + name);
            actionInvocation.getStack().push(bp.getAction());
            // if an exception occured during action execution, throw it here
            if (bp.getException() != null) {
                throw bp.getException();
            }
            return bp.getResult();
        } else {
            // this interceptor
            return actionInvocation.invoke();
        }
    }
}
Also used : FreemarkerResult(org.apache.struts2.views.freemarker.FreemarkerResult) ActionProxy(com.opensymphony.xwork2.ActionProxy) HttpSession(javax.servlet.http.HttpSession) ServletActionContext(org.apache.struts2.ServletActionContext) ActionContext(com.opensymphony.xwork2.ActionContext) Map(java.util.Map)

Example 2 with FreemarkerResult

use of org.apache.struts2.views.freemarker.FreemarkerResult in project struts by apache.

the class StrutsFreeMarkerAttributeRenderer method render.

@Override
public void render(String path, Request request) throws IOException {
    if (path != null) {
        LOG.trace("Rendering freemarker tile [{}]", path);
        ActionContext ctx = readActionContext(request);
        registerTilesBeanModel(ctx);
        FreemarkerResult result = new FreemarkerResult(path);
        result.setWriter(request.getWriter());
        Container container = ctx.getContainer();
        container.inject(result);
        try {
            ActionInvocation invocation = ctx.getActionInvocation();
            result.doExecute(path, invocation);
        } catch (TemplateException e) {
            LOG.error("Exception was thrown during rendering value {}: {}", path, e.getMessage());
            throw new InvalidTemplateException(e);
        }
    } else {
        LOG.error("Path is null, cannot render template!");
        throw new InvalidTemplateException("Cannot render a null template");
    }
}
Also used : FreemarkerResult(org.apache.struts2.views.freemarker.FreemarkerResult) Container(com.opensymphony.xwork2.inject.Container) TemplateException(freemarker.template.TemplateException) InvalidTemplateException(org.apache.tiles.impl.InvalidTemplateException) ActionInvocation(com.opensymphony.xwork2.ActionInvocation) ServletActionContext(org.apache.struts2.ServletActionContext) ActionContext(com.opensymphony.xwork2.ActionContext) InvalidTemplateException(org.apache.tiles.impl.InvalidTemplateException)

Example 3 with FreemarkerResult

use of org.apache.struts2.views.freemarker.FreemarkerResult in project struts by apache.

the class DebuggingInterceptor method intercept.

/*
     * (non-Javadoc)
     *
     * @see com.opensymphony.xwork2.interceptor.Interceptor#invoke(com.opensymphony.xwork2.ActionInvocation)
     */
public String intercept(ActionInvocation inv) throws Exception {
    boolean actionOnly = false;
    boolean cont = true;
    Boolean devModeOverride = PrepareOperations.getDevModeOverride();
    boolean devMode = devModeOverride != null ? devModeOverride : this.devMode;
    if (devMode) {
        final ActionContext ctx = ActionContext.getContext();
        String type = getParameter(DEBUG_PARAM);
        ctx.getParameters().remove(DEBUG_PARAM);
        if (XML_MODE.equals(type)) {
            inv.addPreResultListener(new PreResultListener() {

                public void beforeResult(ActionInvocation inv, String result) {
                    printContext();
                }
            });
        } else if (CONSOLE_MODE.equals(type)) {
            consoleEnabled = true;
            inv.addPreResultListener(new PreResultListener() {

                public void beforeResult(ActionInvocation inv, String actionResult) {
                    String xml = "";
                    if (enableXmlWithConsole) {
                        StringWriter writer = new StringWriter();
                        printContext(new PrettyPrintWriter(writer));
                        xml = writer.toString();
                        xml = xml.replaceAll("&", "&");
                        xml = xml.replaceAll(">", ">");
                        xml = xml.replaceAll("<", "&lt;");
                    }
                    ActionContext.getContext().put("debugXML", xml);
                    FreemarkerResult result = new FreemarkerResult();
                    result.setFreemarkerManager(freemarkerManager);
                    result.setContentType("text/html");
                    result.setLocation("/org/apache/struts2/interceptor/debugging/console.ftl");
                    result.setParse(false);
                    try {
                        result.execute(inv);
                    } catch (Exception ex) {
                        LOG.error("Unable to create debugging console", ex);
                    }
                }
            });
        } else if (COMMAND_MODE.equals(type)) {
            ValueStack stack = (ValueStack) ctx.getSession().get(SESSION_KEY);
            if (stack == null) {
                // allows it to be embedded on another page
                stack = ctx.getValueStack();
                ctx.getSession().put(SESSION_KEY, stack);
            }
            String cmd = getParameter(EXPRESSION_PARAM);
            ServletActionContext.getRequest().setAttribute("decorator", "none");
            HttpServletResponse res = ServletActionContext.getResponse();
            res.setContentType("text/plain");
            try (PrintWriter writer = ServletActionContext.getResponse().getWriter()) {
                writer.print(stack.findValue(cmd));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            cont = false;
        } else if (BROWSER_MODE.equals(type)) {
            actionOnly = true;
            inv.addPreResultListener(new PreResultListener() {

                public void beforeResult(ActionInvocation inv, String actionResult) {
                    String rootObjectExpression = getParameter(OBJECT_PARAM);
                    if (rootObjectExpression == null) {
                        rootObjectExpression = "action";
                    }
                    String decorate = getParameter(DECORATE_PARAM);
                    ValueStack stack = ctx.getValueStack();
                    Object rootObject = stack.findValue(rootObjectExpression);
                    try (StringWriter writer = new StringWriter()) {
                        ObjectToHTMLWriter htmlWriter = new ObjectToHTMLWriter(writer);
                        htmlWriter.write(reflectionProvider, rootObject, rootObjectExpression);
                        String html = writer.toString();
                        writer.close();
                        stack.set("debugHtml", html);
                        // but we need plain text on the other ones
                        if ("false".equals(decorate))
                            ServletActionContext.getRequest().setAttribute("decorator", "none");
                        FreemarkerResult result = new FreemarkerResult();
                        result.setFreemarkerManager(freemarkerManager);
                        result.setContentType("text/html");
                        result.setLocation("/org/apache/struts2/interceptor/debugging/browser.ftl");
                        result.execute(inv);
                    } catch (Exception ex) {
                        LOG.error("Unable to create debugging console", ex);
                    }
                }
            });
        }
    }
    if (cont) {
        try {
            if (actionOnly) {
                inv.invokeActionOnly();
                return null;
            } else {
                return inv.invoke();
            }
        } finally {
            if (devMode && consoleEnabled) {
                final ActionContext ctx = ActionContext.getContext();
                ctx.getSession().put(SESSION_KEY, ctx.getValueStack());
            }
        }
    } else {
        return null;
    }
}
Also used : ValueStack(com.opensymphony.xwork2.util.ValueStack) ActionInvocation(com.opensymphony.xwork2.ActionInvocation) HttpServletResponse(javax.servlet.http.HttpServletResponse) PreResultListener(com.opensymphony.xwork2.interceptor.PreResultListener) IOException(java.io.IOException) ActionContext(com.opensymphony.xwork2.ActionContext) ServletActionContext(org.apache.struts2.ServletActionContext) IOException(java.io.IOException) FreemarkerResult(org.apache.struts2.views.freemarker.FreemarkerResult) StringWriter(java.io.StringWriter) PrintWriter(java.io.PrintWriter)

Aggregations

ActionContext (com.opensymphony.xwork2.ActionContext)3 ServletActionContext (org.apache.struts2.ServletActionContext)3 FreemarkerResult (org.apache.struts2.views.freemarker.FreemarkerResult)3 ActionInvocation (com.opensymphony.xwork2.ActionInvocation)2 ActionProxy (com.opensymphony.xwork2.ActionProxy)1 Container (com.opensymphony.xwork2.inject.Container)1 PreResultListener (com.opensymphony.xwork2.interceptor.PreResultListener)1 ValueStack (com.opensymphony.xwork2.util.ValueStack)1 TemplateException (freemarker.template.TemplateException)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 Map (java.util.Map)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 HttpSession (javax.servlet.http.HttpSession)1 InvalidTemplateException (org.apache.tiles.impl.InvalidTemplateException)1