Search in sources :

Example 96 with Result

use of com.opensymphony.xwork2.Result in project struts by apache.

the class StrutsResultFactory method buildResult.

public Result buildResult(ResultConfig resultConfig, Map<String, Object> extraContext) throws Exception {
    String resultClassName = resultConfig.getClassName();
    Result result = null;
    if (resultClassName != null) {
        result = (Result) objectFactory.buildBean(resultClassName, extraContext);
        Map<String, String> params = resultConfig.getParams();
        if (params != null) {
            setParameters(extraContext, result, params);
        }
    }
    return result;
}
Also used : Result(com.opensymphony.xwork2.Result) ParamNameAwareResult(com.opensymphony.xwork2.result.ParamNameAwareResult)

Example 97 with Result

use of com.opensymphony.xwork2.Result in project struts by apache.

the class TokenSessionStoreInterceptor method handleInvalidToken.

/**
 * Handles processing of invalid tokens.  If a previously stored invocation is
 * available, the method will attempt to return and render its result.  Otherwise
 * it will return INVALID_TOKEN_CODE.
 *
 * Note: Because a stored (previously completed) invocation's PageContext will be closed,
 *   this method must replace the stored PageContext with the current invocation's one (or a null).
 *   See {@link org.apache.struts2.util.InvocationSessionStore#loadInvocation(String key, String token)} for details.
 *
 * @param invocation
 *
 * @return
 * @throws Exception
 */
@Override
protected String handleInvalidToken(ActionInvocation invocation) throws Exception {
    ActionContext ac = invocation.getInvocationContext();
    HttpServletRequest request = ac.getServletRequest();
    HttpServletResponse response = ac.getServletResponse();
    String tokenName = TokenHelper.getTokenName();
    String token = TokenHelper.getToken(tokenName);
    if ((tokenName != null) && (token != null)) {
        HttpParameters params = ac.getParameters();
        params.remove(tokenName);
        params.remove(TokenHelper.TOKEN_NAME_FIELD);
        String sessionTokenName = TokenHelper.buildTokenSessionAttributeName(tokenName);
        ActionInvocation savedInvocation = InvocationSessionStore.loadInvocation(sessionTokenName, token);
        if (savedInvocation != null) {
            // set the savedInvocation's valuestack to the request scope
            ValueStack stack = savedInvocation.getStack();
            request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack);
            ActionContext savedContext = savedInvocation.getInvocationContext();
            savedContext.getContextMap().put(ServletActionContext.HTTP_REQUEST, request);
            savedContext.getContextMap().put(ServletActionContext.HTTP_RESPONSE, response);
            Result result = savedInvocation.getResult();
            if ((result != null) && (savedInvocation.getProxy().getExecuteResult())) {
                result.execute(savedInvocation);
            }
            // turn off execution of this invocations result
            invocation.getProxy().setExecuteResult(false);
            return savedInvocation.getResultCode();
        }
    }
    return INVALID_TOKEN_CODE;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpParameters(org.apache.struts2.dispatcher.HttpParameters) ValueStack(com.opensymphony.xwork2.util.ValueStack) ActionInvocation(com.opensymphony.xwork2.ActionInvocation) HttpServletResponse(javax.servlet.http.HttpServletResponse) ActionContext(com.opensymphony.xwork2.ActionContext) ServletActionContext(org.apache.struts2.ServletActionContext) Result(com.opensymphony.xwork2.Result)

Example 98 with Result

use of com.opensymphony.xwork2.Result in project struts by apache.

the class ScopeInterceptor method beforeResult.

/* (non-Javadoc)
     * @see com.opensymphony.xwork2.interceptor.PreResultListener#beforeResult(com.opensymphony.xwork2.ActionInvocation, java.lang.String)
     */
public void beforeResult(ActionInvocation invocation, String resultCode) {
    String key = getKey(invocation);
    Map<String, Object> application = ActionContext.getContext().getApplication();
    final ValueStack stack = ActionContext.getContext().getValueStack();
    if (this.application != null)
        for (String string : this.application) {
            Object value = stack.findValue(string);
            LOG.debug("Application scoped variable saved {} = {}", string, String.valueOf(value));
            // if( value != null)
            application.put(key + string, nullConvert(value));
        }
    boolean ends = "end".equals(type);
    Map<String, Object> session = ActionContext.getContext().getSession();
    if (session != null) {
        if (this.session != null) {
            for (String string : this.session) {
                if (ends) {
                    session.remove(key + string);
                } else {
                    Object value = stack.findValue(string);
                    LOG.debug("Session scoped variable saved {} = {}", string, String.valueOf(value));
                    // Null value should be scoped too
                    // if( value != null)
                    session.put(key + string, nullConvert(value));
                }
            }
        }
        unlock(session);
    } else {
        LOG.debug("No HttpSession created... Cannot save session scoped variables.");
    }
    LOG.debug("scope interceptor after (before result)");
}
Also used : ValueStack(com.opensymphony.xwork2.util.ValueStack)

Example 99 with Result

use of com.opensymphony.xwork2.Result in project struts by apache.

the class DefaultResultMapBuilder method createFromResources.

/**
 * Creates any result types from the resources available in the web application. This scans the
 * web application resources using the servlet context.
 *
 * @param   actionClass The action class the results are being built for.
 * @param   results The results map to put the result configs created into.
 * @param   resultPath The calculated path to the resources.
 * @param   resultPrefix The prefix for the result. This is usually <code>/resultPath/actionName</code>.
 * @param   actionName The action name which is used only for logging in this implementation.
 * @param   packageConfig The package configuration which is passed along in order to determine
 * @param   resultsByExtension The map of extensions to result type configuration instances.
 */
protected void createFromResources(Class<?> actionClass, Map<String, ResultConfig> results, final String resultPath, final String resultPrefix, final String actionName, PackageConfig packageConfig, Map<String, ResultTypeConfig> resultsByExtension) {
    if (LOG.isTraceEnabled()) {
        LOG.trace("Searching for results in the Servlet container at [{}]" + " with result prefix of [#1]", resultPath, resultPrefix);
    }
    // Build from web application using the ServletContext
    @SuppressWarnings("unchecked") Set<String> paths = servletContext.getResourcePaths(flatResultLayout ? resultPath : resultPrefix);
    if (paths != null) {
        for (String path : paths) {
            LOG.trace("Processing resource path [{}]", path);
            String fileName = StringUtils.substringAfterLast(path, "/");
            if (StringUtils.isBlank(fileName) || StringUtils.startsWith(fileName, ".")) {
                LOG.trace("Ignoring file without name [{}]", path);
                continue;
            } else if (fileName.lastIndexOf(".") > 0) {
                String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
                if (conventionsService.getResultTypesByExtension(packageConfig).get(suffix) == null) {
                    LOG.debug("No result type defined for file suffix : [{}]. Ignoring file {}", suffix, fileName);
                    continue;
                }
            }
            makeResults(actionClass, path, resultPrefix, results, packageConfig, resultsByExtension);
        }
    }
    // Building from the classpath
    String classPathLocation = resultPath.startsWith("/") ? resultPath.substring(1, resultPath.length()) : resultPath;
    if (LOG.isTraceEnabled()) {
        LOG.trace("Searching for results in the class path at [{}]" + " with a result prefix of [{}] and action name [{}]", classPathLocation, resultPrefix, actionName);
    }
    ResourceFinder finder = new ResourceFinder(classPathLocation, getClassLoaderInterface());
    try {
        Map<String, URL> matches = finder.getResourcesMap("");
        if (matches != null) {
            Test<URL> resourceTest = getResourceTest(resultPath, actionName);
            for (Map.Entry<String, URL> entry : matches.entrySet()) {
                if (resourceTest.test(entry.getValue())) {
                    LOG.trace("Processing URL [{}]", entry.getKey());
                    String urlStr = entry.getValue().toString();
                    int index = urlStr.lastIndexOf(resultPrefix);
                    String path = urlStr.substring(index);
                    makeResults(actionClass, path, resultPrefix, results, packageConfig, resultsByExtension);
                }
            }
        }
    } catch (IOException ex) {
        LOG.error("Unable to scan directory [{}] for results", ex, classPathLocation);
    }
}
Also used : ResourceFinder(com.opensymphony.xwork2.util.finder.ResourceFinder) IOException(java.io.IOException) URL(java.net.URL)

Example 100 with Result

use of com.opensymphony.xwork2.Result in project struts by apache.

the class DefaultResultMapBuilder method createResultConfig.

/**
 * Creates the result configuration for the single result annotation. This will use all the
 * information from the annotation and anything that isn't specified will be fetched from the
 * PackageConfig defaults (if they exist).
 *
 * @param   actionClass The action class the results are being built for.
 * @param   info The result info that is used to create the ResultConfig instance.
 * @param   packageConfig The PackageConfig to use to fetch defaults for result and parameters.
 * @param   result (Optional) The result annotation to pull additional information from.
 * @return  The ResultConfig or null if the Result annotation is given and the annotation is
 *          targeted to some other action than this one.
 */
@SuppressWarnings(value = { "unchecked" })
protected ResultConfig createResultConfig(Class<?> actionClass, ResultInfo info, PackageConfig packageConfig, Result result) {
    // Look up by the type that was determined from the annotation or by the extension in the
    // ResultInfo class
    ResultTypeConfig resultTypeConfig = packageConfig.getAllResultTypeConfigs().get(info.type);
    if (resultTypeConfig == null) {
        throw new ConfigurationException("The Result type [" + info.type + "] which is" + " defined in the Result annotation on the class [" + actionClass + "] or determined" + " by the file extension or is the default result type for the PackageConfig of the" + " action, could not be found as a result-type defined for the Struts/XWork package [" + packageConfig.getName() + "]");
    }
    // Add the default parameters for the result type config (if any)
    HashMap<String, String> params = new HashMap<>();
    if (resultTypeConfig.getParams() != null) {
        params.putAll(resultTypeConfig.getParams());
    }
    // Handle the annotation
    if (result != null) {
        params.putAll(StringTools.createParameterMap(result.params()));
    }
    // Map the location to the default param for the result or a param named location
    if (info.location != null) {
        String defaultParamName = resultTypeConfig.getDefaultResultParam();
        if (!params.containsKey(defaultParamName)) {
            params.put(defaultParamName, info.location);
        }
    }
    return new ResultConfig.Builder(info.name, resultTypeConfig.getClassName()).addParams(params).build();
}
Also used : ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) ResultTypeConfig(com.opensymphony.xwork2.config.entities.ResultTypeConfig)

Aggregations

ActionSupport (com.opensymphony.xwork2.ActionSupport)63 Test (org.junit.Test)52 ActionProxy (com.opensymphony.xwork2.ActionProxy)51 List (java.util.List)49 ValueStack (com.opensymphony.xwork2.util.ValueStack)38 Result (edu.stanford.CVC4.Result)35 ResultConfig (com.opensymphony.xwork2.config.entities.ResultConfig)34 ActionContext (com.opensymphony.xwork2.ActionContext)33 Expr (edu.stanford.CVC4.Expr)32 SExpr (edu.stanford.CVC4.SExpr)32 CVC4.vectorExpr (edu.stanford.CVC4.vectorExpr)31 ArrayList (java.util.ArrayList)31 ActionInvocation (com.opensymphony.xwork2.ActionInvocation)30 HashMap (java.util.HashMap)29 Rational (edu.stanford.CVC4.Rational)25 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)22 Map (java.util.Map)21 ServletActionContext (org.apache.struts2.ServletActionContext)21 Result (com.opensymphony.xwork2.Result)18 PackageConfig (com.opensymphony.xwork2.config.entities.PackageConfig)18