This is an automated email from the ASF dual-hosted git repository. lukaszlenart pushed a commit to branch http-interceptor in repository https://gitbox.apache.org/repos/asf/struts.git
commit b24c407125cfdfcd46d0cde92909c642f0b29bbd Author: Lukasz Lenart <[email protected]> AuthorDate: Sun Sep 21 22:04:53 2014 +0200 Extends interceptor to allow use combination of different Http methods --- .../httpmethod/HttpMethodInterceptor.java | 17 ++++-- .../org/apache/struts2/HttpMethodsTestAction.java | 12 ++++ .../httpmethod/HttpMethodInterceptorTest.java | 69 ++++++++++++++++++++++ 3 files changed, 93 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/org/apache/struts2/interceptor/httpmethod/HttpMethodInterceptor.java b/core/src/main/java/org/apache/struts2/interceptor/httpmethod/HttpMethodInterceptor.java index 668f39ce0..300e203f2 100644 --- a/core/src/main/java/org/apache/struts2/interceptor/httpmethod/HttpMethodInterceptor.java +++ b/core/src/main/java/org/apache/struts2/interceptor/httpmethod/HttpMethodInterceptor.java @@ -10,6 +10,7 @@ import org.apache.struts2.ServletActionContext; import javax.servlet.http.HttpServletRequest; import java.lang.reflect.AnnotatedElement; import java.lang.reflect.Method; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -94,18 +95,24 @@ public class HttpMethodInterceptor extends AbstractInterceptor { } protected List<HttpMethod> readAllowedMethods(AnnotatedElement element) { - List<HttpMethod> allowedMethods = Collections.emptyList(); + List<HttpMethod> allowedMethods = new ArrayList<HttpMethod>(); if (AnnotationUtils.isAnnotatedBy(element, AllowedHttpMethod.class)) { - allowedMethods = Arrays.asList(element.getAnnotation(AllowedHttpMethod.class).value()); + allowedMethods.addAll(Arrays.asList(element.getAnnotation(AllowedHttpMethod.class).value())); } if (AnnotationUtils.isAnnotatedBy(element, HttpGet.class)) { - allowedMethods = Arrays.asList(element.getAnnotation(HttpGet.class).value()); + allowedMethods.addAll(Arrays.asList(element.getAnnotation(HttpGet.class).value())); } if (AnnotationUtils.isAnnotatedBy(element, HttpPost.class)) { - allowedMethods = Arrays.asList(element.getAnnotation(HttpPost.class).value()); + allowedMethods.addAll(Arrays.asList(element.getAnnotation(HttpPost.class).value())); + } + if (AnnotationUtils.isAnnotatedBy(element, HttpPut.class)) { + allowedMethods.addAll(Arrays.asList(element.getAnnotation(HttpPut.class).value())); + } + if (AnnotationUtils.isAnnotatedBy(element, HttpDelete.class)) { + allowedMethods.addAll(Arrays.asList(element.getAnnotation(HttpDelete.class).value())); } if (AnnotationUtils.isAnnotatedBy(element, HttpGetOrPost.class)) { - allowedMethods = Arrays.asList(element.getAnnotation(HttpGetOrPost.class).value()); + allowedMethods.addAll(Arrays.asList(element.getAnnotation(HttpGetOrPost.class).value())); } return Collections.unmodifiableList(allowedMethods); } diff --git a/core/src/test/java/org/apache/struts2/HttpMethodsTestAction.java b/core/src/test/java/org/apache/struts2/HttpMethodsTestAction.java index ba93d7df3..067213a0c 100644 --- a/core/src/test/java/org/apache/struts2/HttpMethodsTestAction.java +++ b/core/src/test/java/org/apache/struts2/HttpMethodsTestAction.java @@ -2,11 +2,13 @@ package org.apache.struts2; import com.opensymphony.xwork2.ActionSupport; import org.apache.struts2.interceptor.httpmethod.AllowedHttpMethod; +import org.apache.struts2.interceptor.httpmethod.HttpDelete; import org.apache.struts2.interceptor.httpmethod.HttpGet; import org.apache.struts2.interceptor.httpmethod.HttpGetOrPost; import org.apache.struts2.interceptor.httpmethod.HttpMethod; import org.apache.struts2.interceptor.httpmethod.HttpMethodAware; import org.apache.struts2.interceptor.httpmethod.HttpPost; +import org.apache.struts2.interceptor.httpmethod.HttpPut; import static org.apache.struts2.interceptor.httpmethod.HttpMethod.POST; @@ -37,6 +39,16 @@ public class HttpMethodsTestAction extends ActionSupport implements HttpMethodAw return "onGetPostOnly"; } + @HttpPut @HttpPost + public String onPutOrPost() { + return "onPutOrPost"; + } + + @HttpDelete + public String onDelete() { + return "onDelete"; + } + public void setMethod(HttpMethod httpMethod) { } diff --git a/core/src/test/java/org/apache/struts2/interceptor/httpmethod/HttpMethodInterceptorTest.java b/core/src/test/java/org/apache/struts2/interceptor/httpmethod/HttpMethodInterceptorTest.java index 34303ea9b..3445c4de4 100644 --- a/core/src/test/java/org/apache/struts2/interceptor/httpmethod/HttpMethodInterceptorTest.java +++ b/core/src/test/java/org/apache/struts2/interceptor/httpmethod/HttpMethodInterceptorTest.java @@ -183,4 +183,73 @@ public class HttpMethodInterceptorTest extends StrutsInternalTestCase { assertEquals("onGetPostOnly", resultName); } + public void testDeleteOnMethod() throws Exception { + // given + HttpMethodsTestAction action = new HttpMethodsTestAction(); + HttpMethodInterceptor interceptor = new HttpMethodInterceptor(); + MockActionInvocation invocation = new MockActionInvocation(); + invocation.setAction(action); + MockActionProxy proxy = new MockActionProxy(); + proxy.setMethod("onDelete"); + proxy.setMethodSpecified(true); + invocation.setProxy(proxy); + + invocation.setResultCode("onDelete"); + + MockHttpServletRequest request = new MockHttpServletRequest("DELETE", "/action"); + ServletActionContext.setRequest(request); + + // when + String resultName = interceptor.intercept(invocation); + + // then + assertEquals("onDelete", resultName); + } + + public void testPutOnPutOrPostMethod() throws Exception { + // given + HttpMethodsTestAction action = new HttpMethodsTestAction(); + HttpMethodInterceptor interceptor = new HttpMethodInterceptor(); + MockActionInvocation invocation = new MockActionInvocation(); + invocation.setAction(action); + MockActionProxy proxy = new MockActionProxy(); + proxy.setMethod("onPutOrPost"); + proxy.setMethodSpecified(true); + invocation.setProxy(proxy); + + invocation.setResultCode("onPutOrPost"); + + MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/action"); + ServletActionContext.setRequest(request); + + // when + String resultName = interceptor.intercept(invocation); + + // then + assertEquals("onPutOrPost", resultName); + } + + public void testPostOnPutOrPostMethod() throws Exception { + // given + HttpMethodsTestAction action = new HttpMethodsTestAction(); + HttpMethodInterceptor interceptor = new HttpMethodInterceptor(); + MockActionInvocation invocation = new MockActionInvocation(); + invocation.setAction(action); + MockActionProxy proxy = new MockActionProxy(); + proxy.setMethod("onPutOrPost"); + proxy.setMethodSpecified(true); + invocation.setProxy(proxy); + + invocation.setResultCode("onPutOrPost"); + + MockHttpServletRequest request = new MockHttpServletRequest("POST", "/action"); + ServletActionContext.setRequest(request); + + // when + String resultName = interceptor.intercept(invocation); + + // then + assertEquals("onPutOrPost", resultName); + } + }
