Search in sources :

Example 76 with Result

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

the class RestActionInvocationTest method testSaveResult.

/**
 * Test the correct action results: null, String, HttpHeaders, Result
 * @throws Exception
 */
public void testSaveResult() throws Exception {
    Object methodResult = "index";
    ActionConfig actionConfig = restActionInvocation.getProxy().getConfig();
    assertEquals("index", restActionInvocation.saveResult(actionConfig, methodResult));
    setUp();
    methodResult = new DefaultHttpHeaders("show");
    assertEquals("show", restActionInvocation.saveResult(actionConfig, methodResult));
    assertEquals(methodResult, restActionInvocation.httpHeaders);
    setUp();
    methodResult = new HttpHeaderResult(HttpServletResponse.SC_ACCEPTED);
    assertEquals(null, restActionInvocation.saveResult(actionConfig, methodResult));
    assertEquals(methodResult, restActionInvocation.createResult());
    setUp();
    try {
        methodResult = new Object();
        restActionInvocation.saveResult(actionConfig, methodResult);
        // ko
        assertFalse(true);
    } catch (ConfigurationException c) {
    // ok, object not allowed
    }
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) HttpHeaderResult(org.apache.struts2.result.HttpHeaderResult)

Example 77 with Result

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

the class DefaultContentTypeHandlerManager method handleResult.

/**
 * Handles the result using handlers to generate content type-specific content
 *
 * @param invocation The action invocation for the current request
 * @param methodResult The object returned from the action method
 * @param target The object to return, usually the action object
 * @return The new result code to process
 * @throws IOException If unable to write to the response
 */
public String handleResult(ActionInvocation invocation, Object methodResult, Object target) throws IOException {
    String resultCode = readResultCode(methodResult);
    Integer statusCode = readStatusCode(methodResult);
    HttpServletRequest req = ServletActionContext.getRequest();
    HttpServletResponse res = ServletActionContext.getResponse();
    ActionConfig actionConfig = invocation.getProxy().getConfig();
    if (statusCode != null) {
        res.setStatus(statusCode);
    }
    ContentTypeHandler handler = getHandlerForResponse(req, res);
    if (handler != null) {
        String extCode = resultCode + "." + handler.getExtension();
        if (actionConfig.getResults().get(extCode) != null) {
            resultCode = extCode;
        } else {
            StringWriter writer = new StringWriter();
            resultCode = handler.fromObject(invocation, target, resultCode, writer);
            String text = writer.toString();
            if (text.length() > 0) {
                byte[] data = text.getBytes("UTF-8");
                res.setContentLength(data.length);
                res.setContentType(handler.getContentType());
                res.getOutputStream().write(data);
                res.getOutputStream().flush();
            }
        }
    }
    return resultCode;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ContentTypeHandler(org.apache.struts2.rest.handler.ContentTypeHandler) ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) StringWriter(java.io.StringWriter) HttpServletResponse(javax.servlet.http.HttpServletResponse)

Example 78 with Result

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

the class RestActionInvocation method saveResult.

/**
 * Save the result to be used later.
 * @param actionConfig current ActionConfig
 * @param methodResult the result of the action.
 * @return the result code to process.
 *
 * @throws ConfigurationException If it is an incorrect result.
 */
@Override
protected String saveResult(ActionConfig actionConfig, Object methodResult) {
    if (methodResult instanceof Result) {
        explicitResult = (Result) methodResult;
        // Wire the result automatically
        container.inject(explicitResult);
    } else if (methodResult instanceof HttpHeaders) {
        httpHeaders = (HttpHeaders) methodResult;
        resultCode = httpHeaders.getResultCode();
    } else if (methodResult instanceof String) {
        resultCode = (String) methodResult;
    } else if (methodResult != null) {
        throw new ConfigurationException("The result type " + methodResult.getClass() + " is not allowed. Use the type String, HttpHeaders or Result.");
    }
    return resultCode;
}
Also used : ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) HttpHeaderResult(org.apache.struts2.result.HttpHeaderResult)

Example 79 with Result

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

the class OgnlUtilTest method testAllowCallingMethodsOnObjectClassInDevModeTrue.

public void testAllowCallingMethodsOnObjectClassInDevModeTrue() {
    Exception expected = null;
    try {
        ognlUtil.setExcludedClasses(Foo.class.getName());
        ognlUtil.setDevModeExcludedClasses("");
        ognlUtil.setDevMode(Boolean.TRUE.toString());
        Foo foo = new Foo();
        String result = (String) ognlUtil.getValue("toString", ognlUtil.createDefaultContext(foo), foo, String.class);
        assertEquals("Foo", result);
    } catch (OgnlException e) {
        expected = e;
    }
    assertNull(expected);
}
Also used : OgnlException(ognl.OgnlException) Foo(com.opensymphony.xwork2.util.Foo) NoSuchPropertyException(ognl.NoSuchPropertyException) MethodFailedException(ognl.MethodFailedException) InappropriateExpressionException(ognl.InappropriateExpressionException) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) IntrospectionException(java.beans.IntrospectionException) StrutsException(org.apache.struts2.StrutsException) OgnlException(ognl.OgnlException)

Example 80 with Result

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

the class OgnlUtilTest method testAllowCallingMethodsOnObjectClassInDevModeFalse.

public void testAllowCallingMethodsOnObjectClassInDevModeFalse() {
    Exception expected = null;
    try {
        ognlUtil.setExcludedClasses(Foo.class.getName());
        ognlUtil.setDevModeExcludedClasses("");
        ognlUtil.setDevMode(Boolean.FALSE.toString());
        Foo foo = new Foo();
        String result = (String) ognlUtil.getValue("toString", ognlUtil.createDefaultContext(foo), foo, String.class);
        assertEquals("Foo", result);
    } catch (OgnlException e) {
        expected = e;
    }
    assertNotNull(expected);
    assertSame(NoSuchPropertyException.class, expected.getClass());
    assertEquals("com.opensymphony.xwork2.util.Foo.toString", expected.getMessage());
}
Also used : OgnlException(ognl.OgnlException) Foo(com.opensymphony.xwork2.util.Foo) NoSuchPropertyException(ognl.NoSuchPropertyException) MethodFailedException(ognl.MethodFailedException) InappropriateExpressionException(ognl.InappropriateExpressionException) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) IntrospectionException(java.beans.IntrospectionException) StrutsException(org.apache.struts2.StrutsException) OgnlException(ognl.OgnlException)

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