Search in sources :

Example 26 with StrutsException

use of org.apache.struts2.StrutsException in project struts by apache.

the class StrutsConversionPropertiesProcessor method loadConversionProperties.

public void loadConversionProperties(String propsName, boolean require) {
    try {
        Iterator<URL> resources = ClassLoaderUtil.getResources(propsName, getClass(), true);
        while (resources.hasNext()) {
            if (XWORK_CONVERSION_PROPERTIES.equals(propsName)) {
                LOG.warn("Instead of using deprecated {} please use the new file name {}", XWORK_CONVERSION_PROPERTIES, STRUTS_CONVERSION_PROPERTIES);
            }
            URL url = resources.next();
            Properties props = new Properties();
            props.load(url.openStream());
            LOG.debug("Processing conversion file [{}]", propsName);
            for (Object o : props.entrySet()) {
                Map.Entry entry = (Map.Entry) o;
                String key = (String) entry.getKey();
                try {
                    TypeConverter typeConverter = converterCreator.createTypeConverter((String) entry.getValue());
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("\t{}:{} [treated as TypeConverter {}]", key, entry.getValue(), typeConverter);
                    }
                    converterHolder.addDefaultMapping(key, typeConverter);
                } catch (Exception e) {
                    LOG.error("Conversion registration error", e);
                }
            }
        }
    } catch (IOException ex) {
        if (require) {
            throw new StrutsException("Cannot load conversion properties file: " + propsName, ex);
        } else {
            LOG.debug("Cannot load conversion properties file: {}", propsName, ex);
        }
    }
}
Also used : TypeConverter(com.opensymphony.xwork2.conversion.TypeConverter) StrutsException(org.apache.struts2.StrutsException) IOException(java.io.IOException) Properties(java.util.Properties) Map(java.util.Map) URL(java.net.URL) IOException(java.io.IOException) StrutsException(org.apache.struts2.StrutsException)

Example 27 with StrutsException

use of org.apache.struts2.StrutsException in project struts by apache.

the class Component method findValue.

/**
 * <p>
 * Evaluates the OGNL stack to find an Object value.
 * </p>
 *
 * <p>
 * Function just like <code>findValue(String)</code> except that if the
 * given expression is <tt>null</tt> a error is logged and
 * a <code>RuntimeException</code> is thrown constructed with a
 * messaged based on the given field and errorMsg parameter.
 * </p>
 *
 * @param expr     OGNL expression.
 * @param field    field name used when throwing <code>RuntimeException</code>.
 * @param errorMsg error message used when throwing <code>RuntimeException</code>.
 * @return the Object found, is never <tt>null</tt>.
 * @throws StrutsException is thrown in case of not found in the OGNL stack, or expression is <tt>null</tt>.
 */
protected Object findValue(String expr, String field, String errorMsg) {
    if (expr == null) {
        throw fieldError(field, errorMsg, null);
    } else {
        Object value = null;
        Exception problem = null;
        try {
            value = findValue(expr);
        } catch (Exception e) {
            problem = e;
        }
        if (value == null) {
            throw fieldError(field, errorMsg, problem);
        }
        return value;
    }
}
Also used : IOException(java.io.IOException) StrutsException(org.apache.struts2.StrutsException)

Example 28 with StrutsException

use of org.apache.struts2.StrutsException in project struts by apache.

the class PackageBasedActionConfigBuilder method buildConfiguration.

@SuppressWarnings("unchecked")
protected void buildConfiguration(Set<Class> classes) {
    Map<String, PackageConfig.Builder> packageConfigs = new HashMap<>();
    for (Class<?> actionClass : classes) {
        Actions actionsAnnotation = actionClass.getAnnotation(Actions.class);
        Action actionAnnotation = actionClass.getAnnotation(Action.class);
        // Skip classes that can't be instantiated
        if (cannotInstantiate(actionClass)) {
            LOG.trace("Class [{}] did not pass the instantiation test and will be ignored", actionClass.getName());
            continue;
        }
        if (eagerLoading) {
            // Tell the ObjectFactory about this class
            try {
                objectFactory.getClassInstance(actionClass.getName());
            } catch (ClassNotFoundException e) {
                LOG.error("Object Factory was unable to load class [{}]", actionClass.getName(), e);
                throw new StrutsException("Object Factory was unable to load class " + actionClass.getName(), e);
            }
        }
        // Determine the action package
        String actionPackage = actionClass.getPackage().getName();
        LOG.debug("Processing class [{}] in package [{}]", actionClass.getName(), actionPackage);
        Set<String> allowedMethods = getAllowedMethods(actionClass);
        // Determine the default namespace and action name
        List<String> namespaces = determineActionNamespace(actionClass);
        for (String namespace : namespaces) {
            String defaultActionName = determineActionName(actionClass);
            PackageConfig.Builder defaultPackageConfig = getPackageConfig(packageConfigs, namespace, actionPackage, actionClass, null);
            // Verify that the annotations have no errors and also determine if the default action
            // configuration should still be built or not.
            Map<String, List<Action>> map = getActionAnnotations(actionClass);
            Set<String> actionNames = new HashSet<>();
            boolean hasDefaultMethod = ReflectionTools.containsMethod(actionClass, DEFAULT_METHOD);
            if (!map.containsKey(DEFAULT_METHOD) && hasDefaultMethod && actionAnnotation == null && actionsAnnotation == null && (alwaysMapExecute || map.isEmpty())) {
                boolean found = false;
                for (List<Action> actions : map.values()) {
                    for (Action action : actions) {
                        // Check if there are duplicate action names in the annotations.
                        String actionName = action.value().equals(Action.DEFAULT_VALUE) ? defaultActionName : action.value();
                        if (actionNames.contains(actionName)) {
                            throw new ConfigurationException("The action class [" + actionClass + "] contains two methods with an action name annotation whose value " + "is the same (they both might be empty as well).");
                        } else {
                            actionNames.add(actionName);
                        }
                        // Check this annotation is the default action
                        if (action.value().equals(Action.DEFAULT_VALUE)) {
                            found = true;
                        }
                    }
                }
                // Build the default
                if (!found) {
                    createActionConfig(defaultPackageConfig, actionClass, defaultActionName, DEFAULT_METHOD, null, allowedMethods);
                }
            }
            // Build the actions for the annotations
            for (Map.Entry<String, List<Action>> entry : map.entrySet()) {
                String method = entry.getKey();
                List<Action> actions = entry.getValue();
                for (Action action : actions) {
                    PackageConfig.Builder pkgCfg = defaultPackageConfig;
                    if (action.value().contains("/") && !slashesInActionNames) {
                        pkgCfg = getPackageConfig(packageConfigs, namespace, actionPackage, actionClass, action);
                    }
                    createActionConfig(pkgCfg, actionClass, defaultActionName, method, action, allowedMethods);
                }
            }
            // where the action mapper is the one that finds the right method at runtime
            if (map.isEmpty() && mapAllMatches && actionAnnotation == null && actionsAnnotation == null) {
                createActionConfig(defaultPackageConfig, actionClass, defaultActionName, null, actionAnnotation, allowedMethods);
            }
            // if there are @Actions or @Action at the class level, create the mappings for them
            String methodName = hasDefaultMethod ? DEFAULT_METHOD : null;
            if (actionsAnnotation != null) {
                List<Action> actionAnnotations = checkActionsAnnotation(actionsAnnotation);
                for (Action actionAnnotation2 : actionAnnotations) createActionConfig(defaultPackageConfig, actionClass, defaultActionName, methodName, actionAnnotation2, allowedMethods);
            } else if (actionAnnotation != null)
                createActionConfig(defaultPackageConfig, actionClass, defaultActionName, methodName, actionAnnotation, allowedMethods);
        }
    }
    buildIndexActions(packageConfigs);
    // Add the new actions to the configuration
    Set<String> packageNames = packageConfigs.keySet();
    for (String packageName : packageNames) {
        configuration.addPackageConfig(packageName, packageConfigs.get(packageName).build());
    }
}
Also used : StrutsException(org.apache.struts2.StrutsException) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException)

Example 29 with StrutsException

use of org.apache.struts2.StrutsException in project struts by apache.

the class ConventionUnknownHandler method buildResult.

protected Result buildResult(String path, String resultCode, ResultTypeConfig config, ActionContext invocationContext) {
    String resultClass = config.getClassName();
    Map<String, String> params = new LinkedHashMap<>();
    if (config.getParams() != null) {
        params.putAll(config.getParams());
    }
    params.put(config.getDefaultResultParam(), path);
    ResultConfig resultConfig = new ResultConfig.Builder(resultCode, resultClass).addParams(params).build();
    try {
        return objectFactory.buildResult(resultConfig, invocationContext.getContextMap());
    } catch (Exception e) {
        throw new StrutsException("Unable to build convention result", e, resultConfig);
    }
}
Also used : StrutsException(org.apache.struts2.StrutsException) InterceptorBuilder(com.opensymphony.xwork2.config.providers.InterceptorBuilder) MalformedURLException(java.net.MalformedURLException) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) StrutsException(org.apache.struts2.StrutsException) LinkedHashMap(java.util.LinkedHashMap)

Example 30 with StrutsException

use of org.apache.struts2.StrutsException in project struts by apache.

the class AbstractAdapterNode method getChildBeforeOrAfter.

public Node getChildBeforeOrAfter(Node child, boolean before) {
    log.debug("getChildBeforeOrAfter: ");
    List adapters = getChildAdapters();
    if (log.isDebugEnabled()) {
        log.debug("childAdapters = {}", adapters);
        log.debug("child = {}", child);
    }
    int index = adapters.indexOf(child);
    if (index < 0)
        throw new StrutsException(child + " is no child of " + this);
    int siblingIndex = before ? index - 1 : index + 1;
    return ((0 < siblingIndex) && (siblingIndex < adapters.size())) ? ((Node) adapters.get(siblingIndex)) : null;
}
Also used : StrutsException(org.apache.struts2.StrutsException) List(java.util.List) LinkedList(java.util.LinkedList) ArrayList(java.util.ArrayList)

Aggregations

StrutsException (org.apache.struts2.StrutsException)46 IOException (java.io.IOException)17 ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)9 ArrayList (java.util.ArrayList)7 InputStream (java.io.InputStream)6 URL (java.net.URL)6 ValueStack (com.opensymphony.xwork2.util.ValueStack)5 List (java.util.List)4 ServletContext (javax.servlet.ServletContext)4 ActionContext (com.opensymphony.xwork2.ActionContext)3 IntrospectionException (java.beans.IntrospectionException)3 ActionInvocation (com.opensymphony.xwork2.ActionInvocation)2 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)2 CompoundRoot (com.opensymphony.xwork2.util.CompoundRoot)2 File (java.io.File)2 Method (java.lang.reflect.Method)2 Iterator (java.util.Iterator)2 Map (java.util.Map)2 Properties (java.util.Properties)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2