mirror of
https://github.com/apache/struts.git
synced 2026-08-11 01:27:14 +00:00
WW-3896 Solves problem with reading result code from HttpHeaders
git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@1397694 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
+24
-21
@@ -21,26 +21,20 @@
|
||||
|
||||
package org.apache.struts2.rest;
|
||||
|
||||
import static javax.servlet.http.HttpServletResponse.SC_NOT_MODIFIED;
|
||||
import static javax.servlet.http.HttpServletResponse.SC_OK;
|
||||
import com.opensymphony.xwork2.config.entities.ActionConfig;
|
||||
import com.opensymphony.xwork2.inject.Container;
|
||||
import com.opensymphony.xwork2.inject.Inject;
|
||||
import org.apache.struts2.ServletActionContext;
|
||||
import org.apache.struts2.rest.handler.ContentTypeHandler;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.apache.struts2.ServletActionContext;
|
||||
import org.apache.struts2.rest.handler.ContentTypeHandler;
|
||||
|
||||
import com.opensymphony.xwork2.ModelDriven;
|
||||
import com.opensymphony.xwork2.config.entities.ActionConfig;
|
||||
import com.opensymphony.xwork2.inject.Container;
|
||||
import com.opensymphony.xwork2.inject.Inject;
|
||||
|
||||
/**
|
||||
* Manages {@link ContentTypeHandler} instances and uses them to
|
||||
* process results
|
||||
@@ -52,7 +46,7 @@ public class DefaultContentTypeHandlerManager implements ContentTypeHandlerManag
|
||||
/** ContentTypeHandlers keyed by the content-type */
|
||||
Map<String,ContentTypeHandler> handlersByContentType = new HashMap<String,ContentTypeHandler>();
|
||||
|
||||
String defaultExtension;
|
||||
private String defaultExtension;
|
||||
|
||||
@Inject("struts.rest.defaultExtension")
|
||||
public void setDefaultExtension(String name) {
|
||||
@@ -126,15 +120,14 @@ public class DefaultContentTypeHandlerManager implements ContentTypeHandlerManag
|
||||
* @return The new result code to process
|
||||
* @throws IOException If unable to write to the response
|
||||
*/
|
||||
public String handleResult(ActionConfig actionConfig, Object methodResult, Object target)
|
||||
throws IOException {
|
||||
String resultCode = null;
|
||||
public String handleResult(ActionConfig actionConfig, Object methodResult, Object target) throws IOException {
|
||||
String resultCode = readResultCode(methodResult);
|
||||
HttpServletRequest req = ServletActionContext.getRequest();
|
||||
HttpServletResponse res = ServletActionContext.getResponse();
|
||||
|
||||
|
||||
ContentTypeHandler handler = getHandlerForResponse(req, res);
|
||||
if (handler != null) {
|
||||
String extCode = resultCode+"-"+handler.getExtension();
|
||||
String extCode = resultCode + "." + handler.getExtension();
|
||||
if (actionConfig.getResults().get(extCode) != null) {
|
||||
resultCode = extCode;
|
||||
} else {
|
||||
@@ -151,9 +144,19 @@ public class DefaultContentTypeHandlerManager implements ContentTypeHandlerManag
|
||||
}
|
||||
}
|
||||
return resultCode;
|
||||
|
||||
}
|
||||
|
||||
|
||||
protected String readResultCode(Object methodResult) {
|
||||
if (methodResult == null) {
|
||||
return null;
|
||||
}
|
||||
if (methodResult instanceof HttpHeaders) {
|
||||
return ((HttpHeaders) methodResult).getResultCode();
|
||||
} else {
|
||||
return methodResult.toString();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds the extension in the url
|
||||
*
|
||||
|
||||
+14
-5
@@ -33,8 +33,6 @@ import org.apache.struts2.rest.handler.FormUrlEncodedHandler;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
|
||||
import static javax.servlet.http.HttpServletResponse.SC_NOT_MODIFIED;
|
||||
import static javax.servlet.http.HttpServletResponse.SC_OK;
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.io.Writer;
|
||||
@@ -43,6 +41,9 @@ import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
|
||||
import static javax.servlet.http.HttpServletResponse.SC_NOT_MODIFIED;
|
||||
import static javax.servlet.http.HttpServletResponse.SC_OK;
|
||||
|
||||
public class ContentTypeHandlerManagerTest extends TestCase {
|
||||
|
||||
private DefaultContentTypeHandlerManager mgr;
|
||||
@@ -80,7 +81,7 @@ public class ContentTypeHandlerManagerTest extends TestCase {
|
||||
public String getExtension() { return "foo"; }
|
||||
};
|
||||
mgr.handlersByExtension.put("xml", handler);
|
||||
mgr.defaultExtension = "xml";
|
||||
mgr.setDefaultExtension("xml");
|
||||
mgr.handleResult(new ActionConfig.Builder("", "", "").build(), new DefaultHttpHeaders().withStatus(SC_OK), obj);
|
||||
|
||||
assertEquals(obj.getBytes().length, mockResponse.getContentLength());
|
||||
@@ -96,6 +97,11 @@ public class ContentTypeHandlerManagerTest extends TestCase {
|
||||
assertEquals(0, mockResponse.getContentLength());
|
||||
}
|
||||
|
||||
public void testHandleValidationError() throws Exception {
|
||||
mockRequest.setMethod("PUT");
|
||||
|
||||
}
|
||||
|
||||
public void testHandlerOverride() {
|
||||
Mock mockHandlerXml = new Mock(ContentTypeHandler.class);
|
||||
mockHandlerXml.matchAndReturn("getExtension", "xml");
|
||||
@@ -132,7 +138,7 @@ public class ContentTypeHandlerManagerTest extends TestCase {
|
||||
}
|
||||
|
||||
/** Assert that the request content-type and differ from the response content type */
|
||||
public void HandleRequestContentType() throws IOException {
|
||||
public void testHandleRequestContentType() throws IOException {
|
||||
|
||||
Mock mockHandlerForm = new Mock(ContentTypeHandler.class);
|
||||
mockHandlerForm.matchAndReturn("getExtension", null);
|
||||
@@ -146,12 +152,15 @@ public class ContentTypeHandlerManagerTest extends TestCase {
|
||||
|
||||
Mock mockContainer = new Mock(Container.class);
|
||||
mockContainer.matchAndReturn("getInstance", C.args(C.eq(ContentTypeHandler.class), C.eq("x-www-form-urlencoded")), mockHandlerForm.proxy());
|
||||
mockContainer.matchAndReturn("getInstance", C.args(C.eq(ContentTypeHandler.class), C.eq("json")), mockHandlerJson.proxy());
|
||||
mockContainer.matchAndReturn("getInstance", C.args(C.eq(String.class), C.eq("struts.rest.handlerOverride.json")), null);
|
||||
mockContainer.expectAndReturn("getInstanceNames", C.args(C.eq(ContentTypeHandler.class)), new HashSet(Arrays.asList("x-www-form-urlencoded", "json")));
|
||||
|
||||
mockRequest.setContentType(FormUrlEncodedHandler.CONTENT_TYPE);
|
||||
mockRequest.setContent("a=1&b=2".getBytes("UTF-8"));
|
||||
mgr.setContainer((Container) mockContainer.proxy());
|
||||
ContentTypeHandler handler = mgr.getHandlerForRequest(mockRequest);
|
||||
|
||||
assertEquals("x-www-form-urlencoded", toString());
|
||||
assertEquals("application/x-www-form-urlencoded", handler.getContentType());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user