Search in sources :

Example 46 with StrutsException

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

the class ActionComponent method executeAction.

/**
 * Execute the requested action.  If no namespace is provided, we'll
 * attempt to derive a namespace using buildNamespace().  The ActionProxy
 * and the namespace will be saved into the instance variables proxy and
 * namespace respectively.
 *
 * @see org.apache.struts2.views.jsp.TagUtils#buildNamespace
 */
protected void executeAction() {
    String actualName = findString(name, "name", "Action name is required. Example: updatePerson");
    if (actualName == null) {
        throw new StrutsException("Unable to find value for name " + name);
    }
    // handle "name!method" convention.
    final String actionName;
    final String methodName;
    ActionMapping mapping = actionMapper.getMappingFromActionName(actualName);
    actionName = mapping.getName();
    methodName = mapping.getMethod();
    String namespace;
    if (this.namespace == null) {
        namespace = TagUtils.buildNamespace(actionMapper, getStack(), req);
    } else {
        namespace = findString(this.namespace);
    }
    // get the old value stack from the request
    ValueStack stack = getStack();
    // execute at this point, after params have been set
    ActionInvocation inv = ActionContext.getContext().getActionInvocation();
    try {
        proxy = actionProxyFactory.createActionProxy(namespace, actionName, methodName, createExtraContext(), executeResult, true);
        // set the new stack into the request for the taglib to use
        req.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, proxy.getInvocation().getStack());
        req.setAttribute(StrutsStatics.STRUTS_ACTION_TAG_INVOCATION, Boolean.TRUE);
        proxy.execute();
    } catch (Exception e) {
        String message = "Could not execute action: " + namespace + "/" + actualName;
        LOG.error(message, e);
        if (rethrowException) {
            throw new StrutsException(message, e);
        }
    } finally {
        req.removeAttribute(StrutsStatics.STRUTS_ACTION_TAG_INVOCATION);
        // set the old stack back on the request
        req.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack);
        if (inv != null) {
            ActionContext.getContext().withActionInvocation(inv);
        }
    }
    if ((getVar() != null) && (proxy != null)) {
        putInContext(proxy.getAction());
    }
}
Also used : StrutsException(org.apache.struts2.StrutsException) ActionMapping(org.apache.struts2.dispatcher.mapper.ActionMapping) ValueStack(com.opensymphony.xwork2.util.ValueStack) ActionInvocation(com.opensymphony.xwork2.ActionInvocation) IOException(java.io.IOException) StrutsException(org.apache.struts2.StrutsException)

Example 47 with StrutsException

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

the class ComponentTest method testComponent_coverageTest.

/**
 * Attempt some code coverage tests for {@link Component} that can be achieved without
 * too much difficulty.
 */
public void testComponent_coverageTest() {
    HashMap<String, Object> propertyMap = new HashMap<>();
    Exception exception = new Exception("Generic exception");
    Property property = new Property(stack);
    ActionComponent actionComponent = new ActionComponent(stack, request, response);
    try {
        actionComponent.setName("componentName");
        // Simulate component attribute with a hyphen in its name.
        propertyMap.put("hyphen-keyname-for-coverage", "hyphen-keyname-for-coverage-value");
        propertyMap.put("someKeyName", "someKeyValue");
        actionComponent.copyParams(propertyMap);
        actionComponent.addAllParameters(propertyMap);
        try {
            actionComponent.findString(null, "fieldName", "errorMessage");
            fail("null expr parameter should cause a StrutsException");
        } catch (StrutsException se) {
        // expected
        }
        assertNull("completeExpression of a null expression returned non-null result ?", actionComponent.completeExpression(null));
        try {
            actionComponent.findValue(null, "fieldName", "errorMessage");
            fail("null expr parameter should cause a StrutsException");
        } catch (StrutsException se) {
        // expected
        }
        try {
            actionComponent.findValue("", "fieldName", "errorMessage");
            fail("empty expr parameter should cause a StrutsException due to finding a null value");
        } catch (StrutsException se) {
        // expected
        }
        assertNotNull("the toString() method with Exception parameter returned null result ?", actionComponent.toString(exception));
        assertFalse("Initial performClearTagStateForTagPoolingServers not false ?", actionComponent.getPerformClearTagStateForTagPoolingServers());
        actionComponent.setPerformClearTagStateForTagPoolingServers(true);
        assertTrue("performClearTagStateForTagPoolingServers false after setting to true ?", actionComponent.getPerformClearTagStateForTagPoolingServers());
    } finally {
        property.getComponentStack().pop();
    }
}
Also used : StrutsException(org.apache.struts2.StrutsException) HashMap(java.util.HashMap) StrutsException(org.apache.struts2.StrutsException)

Example 48 with StrutsException

use of org.apache.struts2.StrutsException in project beangle3 by beangle.

the class Component method findValue.

/**
 * Evaluates the OGNL stack to find an Object value.
 * <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 paramter.
 *
 * @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 : StrutsException(org.apache.struts2.StrutsException) IOException(java.io.IOException)

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