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
}
}
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;
}
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;
}
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);
}
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());
}
Aggregations