Search in sources :

Example 1 with Location

use of com.opensymphony.xwork2.util.location.Location in project tis by qlangtech.

the class TisResultMapBuilder method build.

@Override
public Map<String, ResultConfig> build(Class<?> actionClass, Action annotation, String actionName, PackageConfig packageConfig) {
    ResultConfig.Builder build = null;
    final String resultName = actionClass.getSimpleName();
    Map<String, ResultConfig> resultsConfig = new HashMap<String, ResultConfig>();
    resultsConfig.put(BasicModule.key_FORWARD, ACTION_RESULT_CONFIG);
    Matcher matcher = TisPackageBasedActionConfigBuilder.NAMESPACE_PATTERN.matcher(actionClass.getName());
    if (// || (matcher = TisPackageBasedActionConfigBuilder.NAMESPACE_TIS_PATTERN.matcher(actionClass.getName())).matches()
    matcher.matches()) {
        if ("action".equals(matcher.group(2))) {
            // process ajax
            String resultCode = resultName + "_ajax";
            build = new ResultConfig.Builder(resultCode, AjaxValve.class.getName());
            resultsConfig.put(resultCode, build.build());
            // process action submit
            resultCode = resultName + "_action";
            // final String resultCode = resultName.toString() + "_ajax";
            build = new ResultConfig.Builder(resultCode, ActionChainResult.class.getName());
            build.addParam("actionName", TisActionMapper.addUnderline(resultName).toString());
            build.addParam("namespace", "/" + matcher.group(1) + StringUtils.replace(matcher.group(3), ".", "/") + "#screen");
            resultsConfig.put(resultCode, build.build());
        } else {
        // build = new ResultConfig.Builder(resultName, TerminatorVelocityResult.class.getName());
        // build.addParam("location", "/" + matcher.group(1) + "/templates/"
        // + matcher.group(2) + StringUtils.replace(matcher.group(3), ".", "/") + '/' + getViewName(resultName) + ".vm");
        // resultsConfig.put(resultName, build.build());
        }
    } else {
        throw new IllegalStateException("class name :" + actionClass.getName() + " is illegal");
    }
    return resultsConfig;
}
Also used : ResultConfig(com.opensymphony.xwork2.config.entities.ResultConfig) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher)

Example 2 with Location

use of com.opensymphony.xwork2.util.location.Location in project struts by apache.

the class XmlConfigurationProvider method addAction.

protected void addAction(Element actionElement, PackageConfig.Builder packageContext) throws ConfigurationException {
    String name = actionElement.getAttribute("name");
    String className = actionElement.getAttribute("class");
    // methodName should be null if it's not set
    String methodName = StringUtils.trimToNull(actionElement.getAttribute("method"));
    Location location = DomHelper.getLocationObject(actionElement);
    if (location == null) {
        LOG.warn("Location null for {}", className);
    }
    // use the default-class-ref from the <package/>
    if (StringUtils.isEmpty(className)) {
    // if there is a package default-class-ref use that, otherwise use action support
    /* if (StringUtils.isNotEmpty(packageContext.getDefaultClassRef())) {
                className = packageContext.getDefaultClassRef();
            } else {
                className = ActionSupport.class.getName();
            }*/
    } else {
        if (!verifyAction(className, name, location)) {
            LOG.error("Unable to verify action [{}] with class [{}], from [{}]", name, className, location);
            return;
        }
    }
    Map<String, ResultConfig> results;
    try {
        results = buildResults(actionElement, packageContext);
    } catch (ConfigurationException e) {
        throw new ConfigurationException("Error building results for action " + name + " in namespace " + packageContext.getNamespace(), e, actionElement);
    }
    List<InterceptorMapping> interceptorList = buildInterceptorList(actionElement, packageContext);
    List<ExceptionMappingConfig> exceptionMappings = buildExceptionMappings(actionElement, packageContext);
    Set<String> allowedMethods = buildAllowedMethods(actionElement, packageContext);
    ActionConfig actionConfig = new ActionConfig.Builder(packageContext.getName(), name, className).methodName(methodName).addResultConfigs(results).addInterceptors(interceptorList).addExceptionMappings(exceptionMappings).addParams(XmlHelper.getParams(actionElement)).setStrictMethodInvocation(packageContext.isStrictMethodInvocation()).addAllowedMethod(allowedMethods).location(location).build();
    packageContext.addActionConfig(name, actionConfig);
    LOG.debug("Loaded {}{} in '{}' package: {}", StringUtils.isNotEmpty(packageContext.getNamespace()) ? (packageContext.getNamespace() + "/") : "", name, packageContext.getName(), actionConfig);
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) ResultConfig(com.opensymphony.xwork2.config.entities.ResultConfig) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) InterceptorMapping(com.opensymphony.xwork2.config.entities.InterceptorMapping) ExceptionMappingConfig(com.opensymphony.xwork2.config.entities.ExceptionMappingConfig) Location(com.opensymphony.xwork2.util.location.Location)

Example 3 with Location

use of com.opensymphony.xwork2.util.location.Location in project struts by apache.

the class XmlConfigurationProvider method buildExceptionMappings.

/**
 * Build a list of exception mapping objects from below a given XML element.
 *
 * @param element the given XML element
 * @param packageContext the package context
 *
 * @return list of exception mapping config objects
 */
protected List<ExceptionMappingConfig> buildExceptionMappings(Element element, PackageConfig.Builder packageContext) {
    NodeList exceptionMappingEls = element.getElementsByTagName("exception-mapping");
    List<ExceptionMappingConfig> exceptionMappings = new ArrayList<>();
    for (int i = 0; i < exceptionMappingEls.getLength(); i++) {
        Element ehElement = (Element) exceptionMappingEls.item(i);
        if (ehElement.getParentNode().equals(element) || ehElement.getParentNode().getNodeName().equals(element.getNodeName())) {
            String emName = ehElement.getAttribute("name");
            String exceptionClassName = ehElement.getAttribute("exception");
            String exceptionResult = ehElement.getAttribute("result");
            Map<String, String> params = XmlHelper.getParams(ehElement);
            if (StringUtils.isEmpty(emName)) {
                emName = exceptionResult;
            }
            ExceptionMappingConfig ehConfig = new ExceptionMappingConfig.Builder(emName, exceptionClassName, exceptionResult).addParams(params).location(DomHelper.getLocationObject(ehElement)).build();
            exceptionMappings.add(ehConfig);
        }
    }
    return exceptionMappings;
}
Also used : NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) ExceptionMappingConfig(com.opensymphony.xwork2.config.entities.ExceptionMappingConfig)

Example 4 with Location

use of com.opensymphony.xwork2.util.location.Location in project struts by apache.

the class XmlConfigurationProvider method buildPackageContext.

/**
 * <p>
 * This method builds a package context by looking for the parents of this new package.
 * </p>
 *
 * <p>
 * If no parents are found, it will return a root package.
 * </p>
 *
 * @param packageElement the package element
 *
 * @return the package config builder
 */
protected PackageConfig.Builder buildPackageContext(Element packageElement) {
    String parent = packageElement.getAttribute("extends");
    String abstractVal = packageElement.getAttribute("abstract");
    boolean isAbstract = Boolean.parseBoolean(abstractVal);
    String name = StringUtils.defaultString(packageElement.getAttribute("name"));
    String namespace = StringUtils.defaultString(packageElement.getAttribute("namespace"));
    // Strict DMI is enabled by default, it can disabled by user
    boolean strictDMI = true;
    if (packageElement.hasAttribute("strict-method-invocation")) {
        strictDMI = Boolean.parseBoolean(packageElement.getAttribute("strict-method-invocation"));
    }
    PackageConfig.Builder cfg = new PackageConfig.Builder(name).namespace(namespace).isAbstract(isAbstract).strictMethodInvocation(strictDMI).location(DomHelper.getLocationObject(packageElement));
    if (StringUtils.isNotEmpty(StringUtils.defaultString(parent))) {
        // has parents, let's look it up
        List<PackageConfig> parents = new ArrayList<>();
        for (String parentPackageName : ConfigurationUtil.buildParentListFromString(parent)) {
            if (configuration.getPackageConfigNames().contains(parentPackageName)) {
                parents.add(configuration.getPackageConfig(parentPackageName));
            } else if (declaredPackages.containsKey(parentPackageName)) {
                if (configuration.getPackageConfig(parentPackageName) == null) {
                    addPackage(declaredPackages.get(parentPackageName));
                }
                parents.add(configuration.getPackageConfig(parentPackageName));
            } else {
                throw new ConfigurationException("Parent package is not defined: " + parentPackageName);
            }
        }
        if (parents.size() <= 0) {
            cfg.needsRefresh(true);
        } else {
            cfg.addParents(parents);
        }
    }
    return cfg;
}
Also used : ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) ContainerBuilder(com.opensymphony.xwork2.inject.ContainerBuilder) ArrayList(java.util.ArrayList) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig)

Example 5 with Location

use of com.opensymphony.xwork2.util.location.Location in project struts by apache.

the class XmlConfigurationProvider method loadInterceptorStack.

protected InterceptorStackConfig loadInterceptorStack(Element element, PackageConfig.Builder context) throws ConfigurationException {
    String name = element.getAttribute("name");
    InterceptorStackConfig.Builder config = new InterceptorStackConfig.Builder(name).location(DomHelper.getLocationObject(element));
    NodeList interceptorRefList = element.getElementsByTagName("interceptor-ref");
    for (int j = 0; j < interceptorRefList.getLength(); j++) {
        Element interceptorRefElement = (Element) interceptorRefList.item(j);
        List<InterceptorMapping> interceptors = lookupInterceptorReference(context, interceptorRefElement);
        config.addInterceptors(interceptors);
    }
    return config.build();
}
Also used : InterceptorStackConfig(com.opensymphony.xwork2.config.entities.InterceptorStackConfig) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) InterceptorMapping(com.opensymphony.xwork2.config.entities.InterceptorMapping)

Aggregations

ResultConfig (com.opensymphony.xwork2.config.entities.ResultConfig)32 ServletContext (javax.servlet.ServletContext)22 PackageConfig (com.opensymphony.xwork2.config.entities.PackageConfig)21 HashSet (java.util.HashSet)15 ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)11 ActionContext (com.opensymphony.xwork2.ActionContext)10 ValueStack (com.opensymphony.xwork2.util.ValueStack)10 HashMap (java.util.HashMap)9 ActionInvocation (com.opensymphony.xwork2.ActionInvocation)8 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)8 ServletActionContext (org.apache.struts2.ServletActionContext)8 ResultTypeConfig (com.opensymphony.xwork2.config.entities.ResultTypeConfig)7 Location (com.opensymphony.xwork2.util.location.Location)7 ArrayList (java.util.ArrayList)7 NoAnnotationAction (org.apache.struts2.convention.actions.NoAnnotationAction)7 ClassLevelResultPathAction (org.apache.struts2.convention.actions.resultpath.ClassLevelResultPathAction)6 Action (org.apache.struts2.convention.annotation.Action)6 NodeList (org.w3c.dom.NodeList)6 ActionProxy (com.opensymphony.xwork2.ActionProxy)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)5