You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by st...@apache.org on 2011/08/11 00:11:54 UTC

svn commit: r1156388 - in /cocoon/cocoon3/trunk/cocoon-rest/src: main/java/org/apache/cocoon/rest/controller/MethodDelegator.java test/java/org/apache/cocoon/rest/controller/MethodDelegatorTest.java

Author: stevendolg
Date: Wed Aug 10 22:11:54 2011
New Revision: 1156388

URL: http://svn.apache.org/viewvc?rev=1156388&view=rev
Log:
reworked class to fix warnings

Added:
    cocoon/cocoon3/trunk/cocoon-rest/src/test/java/org/apache/cocoon/rest/controller/MethodDelegatorTest.java   (with props)
Modified:
    cocoon/cocoon3/trunk/cocoon-rest/src/main/java/org/apache/cocoon/rest/controller/MethodDelegator.java

Modified: cocoon/cocoon3/trunk/cocoon-rest/src/main/java/org/apache/cocoon/rest/controller/MethodDelegator.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-rest/src/main/java/org/apache/cocoon/rest/controller/MethodDelegator.java?rev=1156388&r1=1156387&r2=1156388&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-rest/src/main/java/org/apache/cocoon/rest/controller/MethodDelegator.java (original)
+++ cocoon/cocoon3/trunk/cocoon-rest/src/main/java/org/apache/cocoon/rest/controller/MethodDelegator.java Wed Aug 10 22:11:54 2011
@@ -30,35 +30,35 @@ import org.apache.cocoon.rest.controller
 import org.apache.cocoon.rest.controller.method.Put;
 import org.apache.cocoon.rest.controller.response.RestResponse;
 import org.apache.cocoon.rest.controller.response.Status;
+import org.apache.cocoon.sitemap.util.ExceptionHandler;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public class MethodDelegator {
 
-    private static Map<String, MethodDelegate> delegates = new HashMap<String, MethodDelegate>();
+    private static final Map<String, MethodDelegate> DELEGATES = new HashMap<String, MethodDelegate>();
 
     /**
      * Logger.
      */
-    private static final Logger LOG =
-            LoggerFactory.getLogger(MethodDelegator.class);
-    
+    private static final Logger LOG = LoggerFactory.getLogger(MethodDelegator.class);
+
     static {
-        delegates.put("DELETE", new DeleteDelegate());
-        delegates.put("GET", new GetDelegate());
-        delegates.put("HEAD", new HeadDelegate());
-        delegates.put("OPTIONS", new OptionsDelegate());
-        delegates.put("POST", new PostDelegate());
-        delegates.put("PUT", new PutDelegate());
+        DELEGATES.put("DELETE", new DeleteDelegate());
+        DELEGATES.put("GET", new GetDelegate());
+        DELEGATES.put("HEAD", new HeadDelegate());
+        DELEGATES.put("OPTIONS", new OptionsDelegate());
+        DELEGATES.put("POST", new PostDelegate());
+        DELEGATES.put("PUT", new PutDelegate());
     }
 
     public MethodDelegator() {
         super();
     }
 
-    public RestResponse delegate(HttpServletRequest request, Object controller) throws Exception {
+    public RestResponse delegate(HttpServletRequest request, Object controller) {
         if (request != null && request.getMethod() != null) {
-            MethodDelegate methodDelegate = delegates.get(this.getMethod(request));
+            MethodDelegate methodDelegate = DELEGATES.get(this.getMethod(request));
             if (methodDelegate != null) {
                 return methodDelegate.execute(controller);
             }
@@ -69,20 +69,24 @@ public class MethodDelegator {
 
     private String getMethod(HttpServletRequest request) {
         String alternativeMethod = request.getParameter("_method");
-        if (alternativeMethod != null) {
-            alternativeMethod = alternativeMethod.toUpperCase();
-            if (delegates.keySet().contains(alternativeMethod)) {
-                if (LOG.isDebugEnabled()) {
-                    LOG.debug("Using alternative request method '" + alternativeMethod
-                            + "' as provided by the request parameter '_method'");
-                }
+        if (alternativeMethod == null) {
+            return request.getMethod().toUpperCase();
+        }
 
-                return alternativeMethod;
-            }
+        alternativeMethod = alternativeMethod.toUpperCase();
 
-            if (LOG.isWarnEnabled()) {
-                LOG.warn("The request parameter '_request' refers to an unsupported request method: _method='" + alternativeMethod + "'");
+        if (DELEGATES.containsKey(alternativeMethod)) {
+            if (LOG.isDebugEnabled()) {
+                LOG.debug("Using alternative request method '" + alternativeMethod
+                        + "' as provided by the request parameter '_method'");
             }
+
+            return alternativeMethod;
+        }
+
+        if (LOG.isWarnEnabled()) {
+            LOG.warn("The request parameter '_request' refers to an unsupported request method: _method='"
+                    + alternativeMethod + "'");
         }
 
         return request.getMethod().toUpperCase();
@@ -95,11 +99,17 @@ public class MethodDelegator {
         }
 
         @Override
-        public RestResponse execute(Object controller) throws Exception {
+        public RestResponse execute(Object controller) {
             if (controller instanceof Delete) {
                 Delete delete = (Delete) controller;
-                return delete.doDelete();
+
+                try {
+                    return delete.doDelete();
+                } catch (Exception e) {
+                    throw ExceptionHandler.getInvocationException(e);
+                }
             }
+
             return super.execute(controller);
         }
     }
@@ -111,11 +121,16 @@ public class MethodDelegator {
         }
 
         @Override
-        public RestResponse execute(Object controller) throws Exception {
+        public RestResponse execute(Object controller) {
             if (controller instanceof Get) {
                 Get get = (Get) controller;
-                return get.doGet();
+                try {
+                    return get.doGet();
+                } catch (Exception e) {
+                    throw ExceptionHandler.getInvocationException(e);
+                }
             }
+
             return super.execute(controller);
         }
     }
@@ -127,10 +142,14 @@ public class MethodDelegator {
         }
 
         @Override
-        public RestResponse execute(Object controller) throws Exception {
+        public RestResponse execute(Object controller) {
             if (controller instanceof Head) {
                 Head head = (Head) controller;
-                return head.doHead();
+                try {
+                    return head.doHead();
+                } catch (Exception e) {
+                    throw ExceptionHandler.getInvocationException(e);
+                }
             }
 
             // According to http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html a
@@ -140,13 +159,13 @@ public class MethodDelegator {
         }
     }
 
-    private static abstract class MethodDelegate {
+    private abstract static class MethodDelegate {
 
         public MethodDelegate() {
             super();
         }
 
-        public RestResponse execute(Object controller) throws Exception {
+        public RestResponse execute(@SuppressWarnings("unused") Object controller) {
             return new Status(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
         }
     }
@@ -158,10 +177,14 @@ public class MethodDelegator {
         }
 
         @Override
-        public RestResponse execute(Object controller) throws Exception {
+        public RestResponse execute(Object controller) {
             if (controller instanceof Options) {
                 Options options = (Options) controller;
-                return options.doOptions();
+                try {
+                    return options.doOptions();
+                } catch (Exception e) {
+                    throw ExceptionHandler.getInvocationException(e);
+                }
             }
 
             return super.execute(controller);
@@ -175,10 +198,14 @@ public class MethodDelegator {
         }
 
         @Override
-        public RestResponse execute(Object controller) throws Exception {
+        public RestResponse execute(Object controller) {
             if (controller instanceof Post) {
                 Post post = (Post) controller;
-                return post.doPost();
+                try {
+                    return post.doPost();
+                } catch (Exception e) {
+                    throw ExceptionHandler.getInvocationException(e);
+                }
             }
 
             return super.execute(controller);
@@ -192,13 +219,17 @@ public class MethodDelegator {
         }
 
         @Override
-        public RestResponse execute(Object controller) throws Exception {
+        public RestResponse execute(Object controller) {
             if (controller instanceof Put) {
                 Put put = (Put) controller;
-                return put.doPut();
+                try {
+                    return put.doPut();
+                } catch (Exception e) {
+                    throw ExceptionHandler.getInvocationException(e);
+                }
             }
 
             return super.execute(controller);
         }
     }
-}
+}
\ No newline at end of file

Added: cocoon/cocoon3/trunk/cocoon-rest/src/test/java/org/apache/cocoon/rest/controller/MethodDelegatorTest.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-rest/src/test/java/org/apache/cocoon/rest/controller/MethodDelegatorTest.java?rev=1156388&view=auto
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-rest/src/test/java/org/apache/cocoon/rest/controller/MethodDelegatorTest.java (added)
+++ cocoon/cocoon3/trunk/cocoon-rest/src/test/java/org/apache/cocoon/rest/controller/MethodDelegatorTest.java Wed Aug 10 22:11:54 2011
@@ -0,0 +1,217 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.rest.controller;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.cocoon.rest.controller.method.Delete;
+import org.apache.cocoon.rest.controller.method.Get;
+import org.apache.cocoon.rest.controller.method.Head;
+import org.apache.cocoon.rest.controller.method.Options;
+import org.apache.cocoon.rest.controller.method.Post;
+import org.apache.cocoon.rest.controller.method.Put;
+import org.apache.cocoon.rest.controller.response.RestResponse;
+import org.apache.cocoon.rest.controller.response.Status;
+import org.junit.Test;
+import org.springframework.mock.web.MockHttpServletRequest;
+
+public class MethodDelegatorTest {
+
+    private MethodDelegator methodDelegator = new MethodDelegator();
+
+    @Test
+    public void missingMethod() {
+        MockHttpServletRequest request = new MockHttpServletRequest(null, "request-uri");
+        RestResponse response = this.methodDelegator.delegate(request, null);
+
+        assertTrue(response instanceof Status);
+        assertEquals(HttpServletResponse.SC_METHOD_NOT_ALLOWED, ((Status) response).getStatus());
+    }
+
+    @Test
+    public void unknownMethod() {
+        MockHttpServletRequest request = new MockHttpServletRequest("unknown-method", "request-uri");
+        RestResponse response = this.methodDelegator.delegate(request, null);
+
+        assertTrue(response instanceof Status);
+        assertEquals(HttpServletResponse.SC_METHOD_NOT_ALLOWED, ((Status) response).getStatus());
+    }
+
+    @Test
+    public void unsupportedMethod() {
+        MockHttpServletRequest request = new MockHttpServletRequest("get", "request-uri");
+        RestResponse response = this.methodDelegator.delegate(request, new Object());
+
+        assertTrue(response instanceof Status);
+        assertEquals(HttpServletResponse.SC_METHOD_NOT_ALLOWED, ((Status) response).getStatus());
+    }
+
+    @Test
+    public void getMethod() {
+        TestController controller = new TestController();
+        MockHttpServletRequest request = new MockHttpServletRequest("get", "request-uri");
+        RestResponse response = this.methodDelegator.delegate(request, controller);
+
+        assertTrue(response instanceof Status);
+        assertEquals(HttpServletResponse.SC_OK, ((Status) response).getStatus());
+        assertTrue(controller.invokedGet());
+    }
+
+    @Test
+    public void putMethod() {
+        TestController controller = new TestController();
+        MockHttpServletRequest request = new MockHttpServletRequest("PUT", "request-uri");
+        RestResponse response = this.methodDelegator.delegate(request, controller);
+
+        assertTrue(response instanceof Status);
+        assertEquals(HttpServletResponse.SC_OK, ((Status) response).getStatus());
+        assertTrue(controller.invokedPut());
+    }
+
+    @Test
+    public void postMethod() {
+        TestController controller = new TestController();
+        MockHttpServletRequest request = new MockHttpServletRequest("post", "request-uri");
+        RestResponse response = this.methodDelegator.delegate(request, controller);
+
+        assertTrue(response instanceof Status);
+        assertEquals(HttpServletResponse.SC_OK, ((Status) response).getStatus());
+        assertTrue(controller.invokedPost());
+    }
+
+    @Test
+    public void headMethod() {
+        TestController controller = new TestController();
+        MockHttpServletRequest request = new MockHttpServletRequest("HEAD", "request-uri");
+        RestResponse response = this.methodDelegator.delegate(request, controller);
+
+        assertTrue(response instanceof Status);
+        assertEquals(HttpServletResponse.SC_OK, ((Status) response).getStatus());
+        assertTrue(controller.invokedHead());
+    }
+
+    @Test
+    public void optionsMethod() {
+        TestController controller = new TestController();
+        MockHttpServletRequest request = new MockHttpServletRequest("options", "request-uri");
+        RestResponse response = this.methodDelegator.delegate(request, controller);
+
+        assertTrue(response instanceof Status);
+        assertEquals(HttpServletResponse.SC_OK, ((Status) response).getStatus());
+        assertTrue(controller.invokedOptions());
+    }
+
+    @Test
+    public void deleteMethod() {
+        TestController controller = new TestController();
+        MockHttpServletRequest request = new MockHttpServletRequest("DELETE", "request-uri");
+        RestResponse response = this.methodDelegator.delegate(request, controller);
+
+        assertTrue(response instanceof Status);
+        assertEquals(HttpServletResponse.SC_OK, ((Status) response).getStatus());
+        assertTrue(controller.invokedDelete());
+    }
+
+    @Test
+    public void alternativeDeleteMethod() {
+        TestController controller = new TestController();
+        MockHttpServletRequest request = new MockHttpServletRequest("POST", "request-uri");
+        request.addParameter("_method", "DELETE");
+        RestResponse response = this.methodDelegator.delegate(request, controller);
+
+        assertTrue(response instanceof Status);
+        assertEquals(HttpServletResponse.SC_OK, ((Status) response).getStatus());
+        assertTrue(controller.invokedDelete());
+    }
+
+    @Test
+    public void alternativeUnknownMethod() {
+        MockHttpServletRequest request = new MockHttpServletRequest("POST", "request-uri");
+        request.addParameter("_method", "unknown-method");
+        RestResponse response = this.methodDelegator.delegate(request, null);
+
+        assertTrue(response instanceof Status);
+        assertEquals(HttpServletResponse.SC_METHOD_NOT_ALLOWED, ((Status) response).getStatus());
+    }
+
+    protected static class TestController implements Get, Post, Head, Put, Delete, Options {
+
+        private boolean invokedOptions;
+        private boolean invokedDelete;
+        private boolean invokedPut;
+        private boolean invokedHead;
+        private boolean invokedPost;
+        private boolean invokedGet;
+
+        public RestResponse doOptions() throws Exception {
+            this.invokedOptions = true;
+            return new Status(HttpServletResponse.SC_OK);
+        }
+
+        public RestResponse doDelete() throws Exception {
+            this.invokedDelete = true;
+            return new Status(HttpServletResponse.SC_OK);
+        }
+
+        public RestResponse doPut() throws Exception {
+            this.invokedPut = true;
+            return new Status(HttpServletResponse.SC_OK);
+        }
+
+        public RestResponse doHead() throws Exception {
+            this.invokedHead = true;
+            return new Status(HttpServletResponse.SC_OK);
+        }
+
+        public RestResponse doPost() throws Exception {
+            this.invokedPost = true;
+            return new Status(HttpServletResponse.SC_OK);
+        }
+
+        public boolean invokedOptions() {
+            return this.invokedOptions;
+        }
+
+        public boolean invokedDelete() {
+            return this.invokedDelete;
+        }
+
+        public boolean invokedPut() {
+            return this.invokedPut;
+        }
+
+        public boolean invokedHead() {
+            return this.invokedHead;
+        }
+
+        public boolean invokedPost() {
+            return this.invokedPost;
+        }
+
+        public boolean invokedGet() {
+            return this.invokedGet;
+        }
+
+        public RestResponse doGet() throws Exception {
+            this.invokedGet = true;
+            return new Status(HttpServletResponse.SC_OK);
+        }
+    }
+}

Propchange: cocoon/cocoon3/trunk/cocoon-rest/src/test/java/org/apache/cocoon/rest/controller/MethodDelegatorTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cocoon/cocoon3/trunk/cocoon-rest/src/test/java/org/apache/cocoon/rest/controller/MethodDelegatorTest.java
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: cocoon/cocoon3/trunk/cocoon-rest/src/test/java/org/apache/cocoon/rest/controller/MethodDelegatorTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain