You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by mr...@apache.org on 2007/10/25 12:54:38 UTC

svn commit: r588196 - in /struts/sandbox/trunk/struts2-rest-plugin: showcase/src/main/java/org/apache/struts2/rest/example/ src/main/java/org/apache/struts2/rest/

Author: mrdon
Date: Thu Oct 25 03:54:30 2007
New Revision: 588196

URL: http://svn.apache.org/viewvc?rev=588196&view=rev
Log:
* Changing zero conf to look for classes ending in 'Controller' rather than 'Resource'
* Making the method name for different rest operations configurable
* Renaming RestInfo to HttpHeaders as it makes more sense

Added:
    struts/sandbox/trunk/struts2-rest-plugin/showcase/src/main/java/org/apache/struts2/rest/example/OrdersController.java
    struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/ControllerClasspathPackageProvider.java
      - copied, changed from r588193, struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/ResourceClasspathPackageProvider.java
    struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/DefaultHttpHeaders.java
      - copied, changed from r588193, struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/DefaultRestInfo.java
    struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/HttpHeaders.java
      - copied, changed from r588193, struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/RestInfo.java
Removed:
    struts/sandbox/trunk/struts2-rest-plugin/showcase/src/main/java/org/apache/struts2/rest/example/OrdersResource.java
    struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/DefaultRestInfo.java
    struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/ResourceClasspathPackageProvider.java
    struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/RestInfo.java
Modified:
    struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/ContentTypeHandlerManager.java
    struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/RestActionInvocation.java
    struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/RestActionMapper.java
    struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/RestWorkflowInterceptor.java

Added: struts/sandbox/trunk/struts2-rest-plugin/showcase/src/main/java/org/apache/struts2/rest/example/OrdersController.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-rest-plugin/showcase/src/main/java/org/apache/struts2/rest/example/OrdersController.java?rev=588196&view=auto
==============================================================================
--- struts/sandbox/trunk/struts2-rest-plugin/showcase/src/main/java/org/apache/struts2/rest/example/OrdersController.java (added)
+++ struts/sandbox/trunk/struts2-rest-plugin/showcase/src/main/java/org/apache/struts2/rest/example/OrdersController.java Thu Oct 25 03:54:30 2007
@@ -0,0 +1,84 @@
+package org.apache.struts2.rest.example;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.struts2.interceptor.ParameterAware;
+import org.apache.struts2.rest.DefaultHttpHeaders;
+import org.apache.struts2.rest.HttpHeaders;
+
+import com.opensymphony.xwork2.ModelDriven;
+import com.opensymphony.xwork2.Validateable;
+import com.opensymphony.xwork2.ValidationAwareSupport;
+
+public class OrdersController extends ValidationAwareSupport implements ModelDriven<Object>, ParameterAware, Validateable{
+    
+    private Order model = new Order();
+    private static Map<String,Order> orders = new HashMap<String,Order>();
+    
+    static {
+        orders.put("3", new Order("3", "Bob", 33));
+        orders.put("4", new Order("4", "Sarah", 44));
+        orders.put("5", new Order("5", "Jim", 66));
+    }
+    private Collection<Order> list;
+    
+    public void validate() {
+        if (model.getId() == null || model.getId().length() ==0) {
+            addFieldError("id", "ID is wrong");
+        }
+    }
+    
+    public String show() {
+        return "show";
+    }
+    
+    public String edit() {
+        return "edit";
+    }
+    
+    public String editNew() {
+        return "editNew";
+    }
+    
+    public String destroy() {
+        orders.remove(model.getId());
+        return "success";
+    }
+    
+    public HttpHeaders create() {
+        orders.put(model.getId(), model);
+        return new DefaultHttpHeaders()
+            .setLocationId(model.getId())
+            .renderResult("success");
+    }
+    
+    public String update() {
+        orders.put(model.getId(), model);
+        return "success";
+    }
+    
+    public HttpHeaders index() {
+        list = new ArrayList(orders.values());
+        
+        return new DefaultHttpHeaders()
+            .renderResult("index")
+            .withETag("2323");
+    }
+    
+    public Object getModel() {
+        return (list != null ? list : model);
+    }
+
+    // Silly workaround since modeldriven doesn't work right in xwork 2.1.0
+    public void setParameters(Map<String,String[]> parameters) {
+        if (parameters.get("id") != null && orders.get(parameters.get("id")[0]) != null) {
+            orders.get(parameters.get("id")[0]).copyTo(model);
+        }
+    }
+    
+    
+}

Modified: struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/ContentTypeHandlerManager.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/ContentTypeHandlerManager.java?rev=588196&r1=588195&r2=588196&view=diff
==============================================================================
--- struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/ContentTypeHandlerManager.java (original)
+++ struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/ContentTypeHandlerManager.java Thu Oct 25 03:54:30 2007
@@ -93,8 +93,8 @@
         }
         
         boolean statusNotOk = false;
-        if (methodResult instanceof RestInfo) {
-            RestInfo info = (RestInfo) methodResult;
+        if (methodResult instanceof HttpHeaders) {
+            HttpHeaders info = (HttpHeaders) methodResult;
             resultCode = info.apply(req, res, target);
             if (info.getStatus() != SC_OK) {
                 statusNotOk = true;

Copied: struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/ControllerClasspathPackageProvider.java (from r588193, struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/ResourceClasspathPackageProvider.java)
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/ControllerClasspathPackageProvider.java?p2=struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/ControllerClasspathPackageProvider.java&p1=struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/ResourceClasspathPackageProvider.java&r1=588193&r2=588196&rev=588196&view=diff
==============================================================================
--- struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/ResourceClasspathPackageProvider.java (original)
+++ struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/ControllerClasspathPackageProvider.java Thu Oct 25 03:54:30 2007
@@ -25,23 +25,23 @@
 import com.opensymphony.xwork2.util.ResolverUtil.ClassTest;
 
 /**
- * Checks for actions ending in Resource indicating a Rest resource
+ * Checks for actions ending in Controller indicating a Rest controller
  */
-public class ResourceClasspathPackageProvider extends ClasspathPackageProvider {
+public class ControllerClasspathPackageProvider extends ClasspathPackageProvider {
     
     @Override
     protected ClassTest createActionClassTest() {
         return new ClassTest() {
-            // Match Action implementations and classes ending with "Resource"
+            // Match Action implementations and classes ending with "Controller"
             public boolean matches(Class type) {
-                return (type.getSimpleName().endsWith("Resource"));
+                return (type.getSimpleName().endsWith("Controller"));
             }
         };
     }
     
     @Override
     protected String getClassSuffix() {
-        return "Resource";
+        return "Controller";
     }
 
 }

Copied: struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/DefaultHttpHeaders.java (from r588193, struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/DefaultRestInfo.java)
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/DefaultHttpHeaders.java?p2=struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/DefaultHttpHeaders.java&p1=struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/DefaultRestInfo.java&r1=588193&r2=588196&rev=588196&view=diff
==============================================================================
--- struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/DefaultRestInfo.java (original)
+++ struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/DefaultHttpHeaders.java Thu Oct 25 03:54:30 2007
@@ -30,7 +30,7 @@
 /**
  * Default implementation of rest info that uses fluent-style construction
  */
-public class DefaultRestInfo implements RestInfo {
+public class DefaultHttpHeaders implements HttpHeaders {
     String resultCode;
     int status = SC_OK;
     Object etag;
@@ -39,43 +39,43 @@
     boolean disableCaching;
     Date lastModified;
     
-    public DefaultRestInfo renderResult(String code) {
+    public DefaultHttpHeaders renderResult(String code) {
         this.resultCode = code;
         return this;
     }
     
-    public DefaultRestInfo withStatus(int code) {
+    public DefaultHttpHeaders withStatus(int code) {
         this.status = code;
         return this;
     }
     
-    public DefaultRestInfo withETag(Object etag) {
+    public DefaultHttpHeaders withETag(Object etag) {
         this.etag = etag;
         return this;
     }
     
-    public DefaultRestInfo setLocationId(Object id) {
+    public DefaultHttpHeaders setLocationId(Object id) {
         this.locationId = id;
         return this;
     }
     
-    public DefaultRestInfo setLocation(String loc) {
+    public DefaultHttpHeaders setLocation(String loc) {
         this.location = loc;
         return this;
     }
     
-    public DefaultRestInfo lastModified(Date date) {
+    public DefaultHttpHeaders lastModified(Date date) {
         this.lastModified = date;
         return this;
     }
     
-    public DefaultRestInfo disableCaching() {
+    public DefaultHttpHeaders disableCaching() {
         this.disableCaching = true;
         return this;
     }
     
     /* (non-Javadoc)
-     * @see org.apache.struts2.rest.RestInfo#apply(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object)
+     * @see org.apache.struts2.rest.HttpHeaders#apply(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object)
      */
     public String apply(HttpServletRequest request, HttpServletResponse response, Object target) {
         response.setStatus(status);

Copied: struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/HttpHeaders.java (from r588193, struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/RestInfo.java)
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/HttpHeaders.java?p2=struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/HttpHeaders.java&p1=struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/RestInfo.java&r1=588193&r2=588196&rev=588196&view=diff
==============================================================================
--- struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/RestInfo.java (original)
+++ struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/HttpHeaders.java Thu Oct 25 03:54:30 2007
@@ -24,9 +24,9 @@
 import javax.servlet.http.HttpServletResponse;
 
 /**
- * Type-safe rest-related information to apply to a response
+ * Type-safe rest-related informtion to apply to a response
  */
-public interface RestInfo {
+public interface HttpHeaders {
 
     /**
      * Applies the configured information to the response

Modified: struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/RestActionInvocation.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/RestActionInvocation.java?rev=588196&r1=588195&r2=588196&view=diff
==============================================================================
--- struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/RestActionInvocation.java (original)
+++ struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/RestActionInvocation.java Thu Oct 25 03:54:30 2007
@@ -67,7 +67,7 @@
 
 /**
  * Extends the usual {@link ActionInvocation} to add support for processing the object returned
- * from the action execution.  This allows us to support methods that return {@link RestInfo}
+ * from the action execution.  This allows us to support methods that return {@link HttpHeaders}
  * as well as apply content type-specific operations to the result.
  */
 public class RestActionInvocation extends DefaultActionInvocation {

Modified: struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/RestActionMapper.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/RestActionMapper.java?rev=588196&r1=588195&r2=588196&view=diff
==============================================================================
--- struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/RestActionMapper.java (original)
+++ struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/RestActionMapper.java Thu Oct 25 03:54:30 2007
@@ -54,6 +54,27 @@
  *       from the action name.  Whether or not the method is specified, the mapper will 
  *       try to truncate the identifier from the url and store it as a parameter.
  *   </li>
+ *   <li><code>struts.mapper.indexMethodName</code> - The method name to call for a GET
+ *       request with no id parameter. Defaults to 'index'.
+ *   </li>
+ *   <li><code>struts.mapper.getMethodName</code> - The method name to call for a GET
+ *       request with an id parameter. Defaults to 'show'.
+ *   </li>
+ *   <li><code>struts.mapper.postMethodName</code> - The method name to call for a POST
+ *       request with no id parameter. Defaults to 'create'.
+ *   </li>
+ *   <li><code>struts.mapper.putMethodName</code> - The method name to call for a PUT
+ *       request with an id parameter. Defaults to 'update'.
+ *   </li>
+ *   <li><code>struts.mapper.deleteMethodName</code> - The method name to call for a DELETE
+ *       request with an id parameter. Defaults to 'destroy'.
+ *   </li>
+ *   <li><code>struts.mapper.editMethodName</code> - The method name to call for a GET
+ *       request with an id parameter and the 'edit' view specified. Defaults to 'edit'.
+ *   </li>
+ *   <li><code>struts.mapper.newMethodName</code> - The method name to call for a GET
+ *       request with no id parameter and the 'new' view specified. Defaults to 'editNew'.
+ *   </li>
  * </ul>
  * <p>
  * The following URL's will invoke its methods:
@@ -78,6 +99,13 @@
     protected static final Logger LOG = LoggerFactory.getLogger(RestActionMapper.class);
     public static final String HTTP_METHOD_PARAM = "_method";
     private String idParameterName = "id";
+    private String indexMethodName = "index";
+    private String getMethodName = "show";
+    private String postMethodName = "create";
+    private String editMethodName = "edit";
+    private String newMethodName = "editNew";
+    private String deleteMethodName = "destroy";
+    private String putMethodName = "update";
     
     public RestActionMapper() {
     }
@@ -90,7 +118,42 @@
     public void setIdParameterName(String idParameterName) {
         this.idParameterName = idParameterName;
     }
-    
+
+    @Inject(required=false,value="struts.mapper.indexMethodName")
+    public void setIndexMethodName(String indexMethodName) {
+        this.indexMethodName = indexMethodName;
+    }
+
+    @Inject(required=false,value="struts.mapper.getMethodName")
+    public void setGetMethodName(String getMethodName) {
+        this.getMethodName = getMethodName;
+    }
+
+    @Inject(required=false,value="struts.mapper.postMethodName")
+    public void setPostMethodName(String postMethodName) {
+        this.postMethodName = postMethodName;
+    }
+
+    @Inject(required=false,value="struts.mapper.editMethodName")
+    public void setEditMethodName(String editMethodName) {
+        this.editMethodName = editMethodName;
+    }
+
+    @Inject(required=false,value="struts.mapper.newMethodName")
+    public void setNewMethodName(String newMethodName) {
+        this.newMethodName = newMethodName;
+    }
+
+    @Inject(required=false,value="struts.mapper.deleteMethodName")
+    public void setDeleteMethodName(String deleteMethodName) {
+        this.deleteMethodName = deleteMethodName;
+    }
+
+    @Inject(required=false,value="struts.mapper.putMethodName")
+    public void setPutMethodName(String putMethodName) {
+        this.putMethodName = putMethodName;
+    }
+
     public ActionMapping getMapping(HttpServletRequest request,
             ConfigurationManager configManager) {
         ActionMapping mapping = new ActionMapping();
@@ -134,11 +197,11 @@
 
                     // Index e.g. foo
                     if (isGet(request)) {
-                        mapping.setMethod("index");
+                        mapping.setMethod(indexMethodName);
                         
                     // Creating a new entry on POST e.g. foo
                     } else if (isPost(request)) {
-                        mapping.setMethod("create");
+                        mapping.setMethod(postMethodName);
                     }
 
                 // Handle uris with an id at the end
@@ -147,23 +210,23 @@
                     // Viewing the form to edit an item e.g. foo/1;edit
                     if (isGet(request) && id.endsWith(";edit")) {
                         id = id.substring(0, id.length() - ";edit".length());
-                        mapping.setMethod("edit");
+                        mapping.setMethod(editMethodName);
                         
                     // Viewing the form to create a new item e.g. foo/new
                     } else if (isGet(request) && "new".equals(id)) {
-                        mapping.setMethod("editNew");
+                        mapping.setMethod(newMethodName);
 
                     // Removing an item e.g. foo/1
                     } else if (isDelete(request)) {
-                        mapping.setMethod("destroy");
+                        mapping.setMethod(deleteMethodName);
                         
                     // Viewing an item e.g. foo/1
                     } else if (isGet(request)) {
-                        mapping.setMethod("show");
+                        mapping.setMethod(getMethodName);
                     
                     // Updating an item e.g. foo/1    
                     }  else if (isPut(request)) {
-                        mapping.setMethod("update");
+                        mapping.setMethod(putMethodName);
                     }
                 }
             }

Modified: struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/RestWorkflowInterceptor.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/RestWorkflowInterceptor.java?rev=588196&r1=588195&r2=588196&view=diff
==============================================================================
--- struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/RestWorkflowInterceptor.java (original)
+++ struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/RestWorkflowInterceptor.java Thu Oct 25 03:54:30 2007
@@ -181,7 +181,7 @@
                 }
                 
                 
-            	RestInfo info = new DefaultRestInfo()
+            	HttpHeaders info = new DefaultHttpHeaders()
             	    .disableCaching()
             	    .renderResult(method)
             	    .withStatus(SC_BAD_REQUEST);