struts-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lukaszlen...@apache.org
Subject svn commit: r1207576 - in /struts/struts2/trunk/plugins/rest/src: main/java/org/apache/struts2/rest/RestActionInvocation.java test/java/org/apache/struts2/rest/RestActionInvocationTest.java
Date Mon, 28 Nov 2011 20:34:18 GMT
Author: lukaszlenart
Date: Mon Nov 28 20:34:18 2011
New Revision: 1207576

URL: http://svn.apache.org/viewvc?rev=1207576&view=rev
Log:
WW-3713 allows to return content not only for GET method

Modified:
    struts/struts2/trunk/plugins/rest/src/main/java/org/apache/struts2/rest/RestActionInvocation.java
    struts/struts2/trunk/plugins/rest/src/test/java/org/apache/struts2/rest/RestActionInvocationTest.java

Modified: struts/struts2/trunk/plugins/rest/src/main/java/org/apache/struts2/rest/RestActionInvocation.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/rest/src/main/java/org/apache/struts2/rest/RestActionInvocation.java?rev=1207576&r1=1207575&r2=1207576&view=diff
==============================================================================
--- struts/struts2/trunk/plugins/rest/src/main/java/org/apache/struts2/rest/RestActionInvocation.java
(original)
+++ struts/struts2/trunk/plugins/rest/src/main/java/org/apache/struts2/rest/RestActionInvocation.java
Mon Nov 28 20:34:18 2011
@@ -21,34 +21,30 @@
 
 package org.apache.struts2.rest;
 
+import com.opensymphony.xwork2.Action;
 import com.opensymphony.xwork2.ActionInvocation;
 import com.opensymphony.xwork2.DefaultActionInvocation;
-import com.opensymphony.xwork2.Result;
-import com.opensymphony.xwork2.Action;
 import com.opensymphony.xwork2.ModelDriven;
+import com.opensymphony.xwork2.Result;
 import com.opensymphony.xwork2.ValidationAware;
 import com.opensymphony.xwork2.config.ConfigurationException;
-import com.opensymphony.xwork2.config.entities.ResultConfig;
 import com.opensymphony.xwork2.config.entities.ActionConfig;
+import com.opensymphony.xwork2.config.entities.ResultConfig;
 import com.opensymphony.xwork2.inject.Inject;
 import com.opensymphony.xwork2.util.logging.Logger;
 import com.opensymphony.xwork2.util.logging.LoggerFactory;
 import com.opensymphony.xwork2.util.profiling.UtilTimerStack;
-
-import java.lang.reflect.Field;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.util.Map;
-import java.util.HashMap;
-
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-
 import org.apache.struts2.ServletActionContext;
 import org.apache.struts2.dispatcher.HttpHeaderResult;
 import org.apache.struts2.rest.handler.ContentTypeHandler;
 import org.apache.struts2.rest.handler.HtmlHandler;
 
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.lang.reflect.Field;
+import java.util.HashMap;
+import java.util.Map;
+
 
 /**
  * Extends the usual {@link ActionInvocation} to add support for processing the object returned
@@ -64,6 +60,7 @@ public class RestActionInvocation extend
     private ContentTypeHandlerManager handlerSelector;
     private boolean logger;
     private String defaultErrorResultName;
+    private boolean restrictToGet = true;
 
     protected HttpHeaders httpHeaders;
     protected Object target;
@@ -84,6 +81,17 @@ public class RestActionInvocation extend
         defaultErrorResultName = value;
     }
 
+    /**
+     * If set to true (be default) blocks returning content from any other methods than GET,
+     * if set to false, the content can be returned for any kind of method
+     * 
+     * @param value true or false
+     */
+    @Inject(value = "struts.rest.content.restrictToGET", required = false)
+    public void setRestrictToGet(String value) {
+        restrictToGet = "true".equalsIgnoreCase(value);
+    }
+
     @Inject
     public void setMimeTypeHandlerSelector(ContentTypeHandlerManager sel) {
         this.handlerSelector = sel;
@@ -345,12 +353,19 @@ public class RestActionInvocation extend
             target = action;
         }
 
-        // don't return any content for PUT, DELETE, and POST where there are no errors
-        if (!hasErrors && !"get".equalsIgnoreCase(ServletActionContext.getRequest().getMethod()))
{
+        if (shouldRestrictToGET()) {
             target = null;
         }
     }
 
+    // don't return any content for PUT, DELETE, and POST where there are no errors
+    // or backward compatible restrictToGET flag is set to true
+    private boolean shouldRestrictToGET() {
+        return !hasErrors
+                && !"get".equalsIgnoreCase(ServletActionContext.getRequest().getMethod())
+                && restrictToGet;
+    }
+
     private void disableCatching(HttpServletResponse response) {
         // No cache
         response.setHeader("Cache-Control", "no-cache");

Modified: struts/struts2/trunk/plugins/rest/src/test/java/org/apache/struts2/rest/RestActionInvocationTest.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/rest/src/test/java/org/apache/struts2/rest/RestActionInvocationTest.java?rev=1207576&r1=1207575&r2=1207576&view=diff
==============================================================================
--- struts/struts2/trunk/plugins/rest/src/test/java/org/apache/struts2/rest/RestActionInvocationTest.java
(original)
+++ struts/struts2/trunk/plugins/rest/src/test/java/org/apache/struts2/rest/RestActionInvocationTest.java
Mon Nov 28 20:34:18 2011
@@ -1,21 +1,5 @@
 package org.apache.struts2.rest;
 
-import static javax.servlet.http.HttpServletResponse.SC_NOT_MODIFIED;
-
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import javax.servlet.http.HttpServletResponse;
-
-import junit.framework.TestCase;
-
-import org.apache.struts2.ServletActionContext;
-import org.apache.struts2.dispatcher.HttpHeaderResult;
-import org.springframework.mock.web.MockHttpServletRequest;
-import org.springframework.mock.web.MockHttpServletResponse;
-
 import com.opensymphony.xwork2.ActionContext;
 import com.opensymphony.xwork2.DefaultUnknownHandlerManager;
 import com.opensymphony.xwork2.ModelDriven;
@@ -27,6 +11,19 @@ import com.opensymphony.xwork2.config.en
 import com.opensymphony.xwork2.mock.MockActionProxy;
 import com.opensymphony.xwork2.mock.MockInterceptor;
 import com.opensymphony.xwork2.util.XWorkTestCaseHelper;
+import junit.framework.TestCase;
+import org.apache.struts2.ServletActionContext;
+import org.apache.struts2.dispatcher.HttpHeaderResult;
+import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.mock.web.MockHttpServletResponse;
+
+import javax.servlet.http.HttpServletResponse;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static javax.servlet.http.HttpServletResponse.SC_NOT_MODIFIED;
 
 public class RestActionInvocationTest extends TestCase {
 
@@ -121,8 +118,18 @@ public class RestActionInvocationTest ex
 		request.setMethod("DELETE");
 		restActionInvocation.selectTarget();
 		assertEquals(null, restActionInvocation.target);
-		
-	}
+
+        // disable content restriction to GET only
+        model = new ArrayList<String>();
+        model.add("Item1");
+        restAction.model = model;
+
+        request.setMethod("POST");
+        restActionInvocation.setRestrictToGet("false");
+        restActionInvocation.selectTarget();
+        assertEquals(model, restActionInvocation.target);
+        assertEquals(model.get(0), "Item1");
+    }
 
 	/**
 	 * Test the not modified status code.
@@ -187,7 +194,7 @@ public class RestActionInvocationTest ex
 		restAction.model = model;
 		request.setMethod("GET");
 		restActionInvocation.setResultCode("index");
-		
+
 		try {
 			restActionInvocation.processResult();
 



Mime
View raw message