struts-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lukaszlen...@apache.org
Subject svn commit: r1397694 - in /struts/struts2/trunk/plugins/rest/src: main/java/org/apache/struts2/rest/DefaultContentTypeHandlerManager.java test/java/org/apache/struts2/rest/ContentTypeHandlerManagerTest.java
Date Fri, 12 Oct 2012 19:12:17 GMT
Author: lukaszlenart
Date: Fri Oct 12 19:12:17 2012
New Revision: 1397694

URL: http://svn.apache.org/viewvc?rev=1397694&view=rev
Log:
WW-3896 Solves problem with reading result code from HttpHeaders

Modified:
    struts/struts2/trunk/plugins/rest/src/main/java/org/apache/struts2/rest/DefaultContentTypeHandlerManager.java
    struts/struts2/trunk/plugins/rest/src/test/java/org/apache/struts2/rest/ContentTypeHandlerManagerTest.java

Modified: struts/struts2/trunk/plugins/rest/src/main/java/org/apache/struts2/rest/DefaultContentTypeHandlerManager.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/rest/src/main/java/org/apache/struts2/rest/DefaultContentTypeHandlerManager.java?rev=1397694&r1=1397693&r2=1397694&view=diff
==============================================================================
--- struts/struts2/trunk/plugins/rest/src/main/java/org/apache/struts2/rest/DefaultContentTypeHandlerManager.java
(original)
+++ struts/struts2/trunk/plugins/rest/src/main/java/org/apache/struts2/rest/DefaultContentTypeHandlerManager.java
Fri Oct 12 19:12:17 2012
@@ -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 DefaultContentTypeHandlerMa
     /** 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 DefaultContentTypeHandlerMa
      * @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 DefaultContentTypeHandlerMa
             }
         }
         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
      * 

Modified: struts/struts2/trunk/plugins/rest/src/test/java/org/apache/struts2/rest/ContentTypeHandlerManagerTest.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/rest/src/test/java/org/apache/struts2/rest/ContentTypeHandlerManagerTest.java?rev=1397694&r1=1397693&r2=1397694&view=diff
==============================================================================
--- struts/struts2/trunk/plugins/rest/src/test/java/org/apache/struts2/rest/ContentTypeHandlerManagerTest.java
(original)
+++ struts/struts2/trunk/plugins/rest/src/test/java/org/apache/struts2/rest/ContentTypeHandlerManagerTest.java
Fri Oct 12 19:12:17 2012
@@ -33,8 +33,6 @@ import org.apache.struts2.rest.handler.F
 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 ContentTypeHandlerManagerTe
             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 ContentTypeHandlerManagerTe
         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 ContentTypeHandlerManagerTe
     }
 
     /** 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 ContentTypeHandlerManagerTe
 
         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());
     }
 }



Mime
View raw message