You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by lu...@apache.org on 2014/04/22 08:47:49 UTC
[01/11] git commit: Extends trace() to accept Object... as params
Repository: struts
Updated Branches:
refs/heads/feature/http-interceptor [created] b10096b36
Extends trace() to accept Object... as params
Project: http://git-wip-us.apache.org/repos/asf/struts/repo
Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/9519cd12
Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/9519cd12
Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/9519cd12
Branch: refs/heads/feature/http-interceptor
Commit: 9519cd12e206ad85fc76bd34db838163cf482daf
Parents: 54c9efc
Author: Lukasz Lenart <lu...@apache.org>
Authored: Sat Apr 19 17:45:05 2014 +0200
Committer: Lukasz Lenart <lu...@apache.org>
Committed: Sat Apr 19 17:45:05 2014 +0200
----------------------------------------------------------------------
.../main/java/com/opensymphony/xwork2/util/logging/Logger.java | 4 ++++
.../opensymphony/xwork2/util/logging/commons/CommonsLogger.java | 4 ++++
.../java/com/opensymphony/xwork2/util/logging/jdk/JdkLogger.java | 4 ++++
.../com/opensymphony/xwork2/util/logging/slf4j/Slf4jLogger.java | 4 ++++
4 files changed, 16 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/struts/blob/9519cd12/xwork-core/src/main/java/com/opensymphony/xwork2/util/logging/Logger.java
----------------------------------------------------------------------
diff --git a/xwork-core/src/main/java/com/opensymphony/xwork2/util/logging/Logger.java b/xwork-core/src/main/java/com/opensymphony/xwork2/util/logging/Logger.java
index 52270af..f6fb355 100644
--- a/xwork-core/src/main/java/com/opensymphony/xwork2/util/logging/Logger.java
+++ b/xwork-core/src/main/java/com/opensymphony/xwork2/util/logging/Logger.java
@@ -19,8 +19,11 @@ package com.opensymphony.xwork2.util.logging;
* Main logger interface for logging things
*/
public interface Logger {
+
void trace(String msg, String... args);
+ void trace(String msg, Object... args);
+
void trace(String msg, Throwable ex, String... args);
boolean isTraceEnabled();
@@ -58,4 +61,5 @@ public interface Logger {
void fatal(String msg, Throwable ex, String... args);
boolean isFatalEnabled();
+
}
http://git-wip-us.apache.org/repos/asf/struts/blob/9519cd12/xwork-core/src/main/java/com/opensymphony/xwork2/util/logging/commons/CommonsLogger.java
----------------------------------------------------------------------
diff --git a/xwork-core/src/main/java/com/opensymphony/xwork2/util/logging/commons/CommonsLogger.java b/xwork-core/src/main/java/com/opensymphony/xwork2/util/logging/commons/CommonsLogger.java
index c32e9dd..bf00fa9 100644
--- a/xwork-core/src/main/java/com/opensymphony/xwork2/util/logging/commons/CommonsLogger.java
+++ b/xwork-core/src/main/java/com/opensymphony/xwork2/util/logging/commons/CommonsLogger.java
@@ -88,6 +88,10 @@ public class CommonsLogger implements Logger {
log.trace(LoggerUtils.format(msg, args));
}
+ public void trace(String msg, Object... args) {
+ log.trace(LoggerUtils.format(msg, args));
+ }
+
public void trace(String msg, Throwable ex, String... args) {
log.trace(LoggerUtils.format(msg, args), ex);
}
http://git-wip-us.apache.org/repos/asf/struts/blob/9519cd12/xwork-core/src/main/java/com/opensymphony/xwork2/util/logging/jdk/JdkLogger.java
----------------------------------------------------------------------
diff --git a/xwork-core/src/main/java/com/opensymphony/xwork2/util/logging/jdk/JdkLogger.java b/xwork-core/src/main/java/com/opensymphony/xwork2/util/logging/jdk/JdkLogger.java
index 6a95d44..5d29213 100644
--- a/xwork-core/src/main/java/com/opensymphony/xwork2/util/logging/jdk/JdkLogger.java
+++ b/xwork-core/src/main/java/com/opensymphony/xwork2/util/logging/jdk/JdkLogger.java
@@ -95,6 +95,10 @@ public class JdkLogger implements Logger {
log.log(Level.FINEST, LoggerUtils.format(msg, args));
}
+ public void trace(String msg, Object... args) {
+ log.log(Level.FINEST, LoggerUtils.format(msg, args));
+ }
+
public void trace(String msg, Throwable ex, String... args) {
log.log(Level.FINEST, LoggerUtils.format(msg, args), ex);
}
http://git-wip-us.apache.org/repos/asf/struts/blob/9519cd12/xwork-core/src/main/java/com/opensymphony/xwork2/util/logging/slf4j/Slf4jLogger.java
----------------------------------------------------------------------
diff --git a/xwork-core/src/main/java/com/opensymphony/xwork2/util/logging/slf4j/Slf4jLogger.java b/xwork-core/src/main/java/com/opensymphony/xwork2/util/logging/slf4j/Slf4jLogger.java
index 5164c4e..fae4d2b 100644
--- a/xwork-core/src/main/java/com/opensymphony/xwork2/util/logging/slf4j/Slf4jLogger.java
+++ b/xwork-core/src/main/java/com/opensymphony/xwork2/util/logging/slf4j/Slf4jLogger.java
@@ -85,6 +85,10 @@ public class Slf4jLogger implements Logger {
log.trace(LoggerUtils.format(msg, args));
}
+ public void trace(String msg, Object... args) {
+ log.trace(LoggerUtils.format(msg, args));
+ }
+
public void trace(String msg, Throwable ex, String... args) {
log.trace(LoggerUtils.format(msg, args), ex);
}
[06/11] git commit: Adds JavaDoc
Posted by lu...@apache.org.
Adds JavaDoc
Project: http://git-wip-us.apache.org/repos/asf/struts/repo
Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/5664284a
Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/5664284a
Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/5664284a
Branch: refs/heads/feature/http-interceptor
Commit: 5664284a685bf6dd510ee94711dd6d9139c93a13
Parents: 9930ceb
Author: Lukasz Lenart <lu...@apache.org>
Authored: Sat Apr 19 21:12:49 2014 +0200
Committer: Lukasz Lenart <lu...@apache.org>
Committed: Sat Apr 19 21:12:49 2014 +0200
----------------------------------------------------------------------
.../httpmethod/HttpMethodInterceptor.java | 20 ++++++++++++++++++++
1 file changed, 20 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/struts/blob/5664284a/core/src/main/java/org/apache/struts2/interceptor/httpmethod/HttpMethodInterceptor.java
----------------------------------------------------------------------
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 36efb52..e22e6a7 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
@@ -12,6 +12,26 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.List;
+/**
+ * Interceptor is used to control with what http methods action can be called,
+ * if request with not allowed method was performed, {@link #badRequestResultName}
+ * will be returned or if action implements {@link HttpMethodAware}
+ * and {@link HttpMethodAware#getBadRequestResultName()} returns non-null result name,
+ * thus value will be used instead.
+ *
+ * To limit allowed http methods, annotate action class with {@link AllowedMethod} and specify
+ * which methods are allowed. You can also use shorter versions {@link GetOnly}, {@link PostOnly}
+ * and {@link GetPostOnly}
+ *
+ * @see HttpMethodAware
+ * @see HttpMethod
+ * @see AllowedMethod
+ * @see GetOnly
+ * @see PostOnly
+ * @see GetPostOnly
+ *
+ * @since 2.3.18
+ */
public class HttpMethodInterceptor extends AbstractInterceptor {
public static final Class[] HTTP_METHOD_ANNOTATIONS = { AllowedMethod.class, PostOnly.class, GetOnly.class, GetPostOnly.class };
[03/11] git commit: Defines annotations to control access based on
http method Defines main annotation AllowedMethod and helper annotations used
in most common situation, eg. GetOnly, PostOnly, etc.
Posted by lu...@apache.org.
Defines annotations to control access based on http method
Defines main annotation AllowedMethod and helper annotations used in
most common situation, eg. GetOnly, PostOnly, etc.
Project: http://git-wip-us.apache.org/repos/asf/struts/repo
Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/2ae020ce
Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/2ae020ce
Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/2ae020ce
Branch: refs/heads/feature/http-interceptor
Commit: 2ae020ce8c8d94a1e8beb070f38d7d08e587f922
Parents: 507e338
Author: Lukasz Lenart <lu...@apache.org>
Authored: Sat Apr 19 17:49:50 2014 +0200
Committer: Lukasz Lenart <lu...@apache.org>
Committed: Sat Apr 19 17:49:50 2014 +0200
----------------------------------------------------------------------
.../struts2/interceptor/httpmethod/AllowedMethod.java | 14 ++++++++++++++
.../struts2/interceptor/httpmethod/GetOnly.java | 14 ++++++++++++++
.../struts2/interceptor/httpmethod/GetPostOnly.java | 14 ++++++++++++++
.../struts2/interceptor/httpmethod/PostOnly.java | 14 ++++++++++++++
4 files changed, 56 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/struts/blob/2ae020ce/core/src/main/java/org/apache/struts2/interceptor/httpmethod/AllowedMethod.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/struts2/interceptor/httpmethod/AllowedMethod.java b/core/src/main/java/org/apache/struts2/interceptor/httpmethod/AllowedMethod.java
new file mode 100644
index 0000000..05d3795
--- /dev/null
+++ b/core/src/main/java/org/apache/struts2/interceptor/httpmethod/AllowedMethod.java
@@ -0,0 +1,14 @@
+package org.apache.struts2.interceptor.httpmethod;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Target({ElementType.METHOD, ElementType.TYPE})
+@Retention(RetentionPolicy.RUNTIME)
+public @interface AllowedMethod {
+
+ HttpMethod[] value() default {};
+
+}
http://git-wip-us.apache.org/repos/asf/struts/blob/2ae020ce/core/src/main/java/org/apache/struts2/interceptor/httpmethod/GetOnly.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/struts2/interceptor/httpmethod/GetOnly.java b/core/src/main/java/org/apache/struts2/interceptor/httpmethod/GetOnly.java
new file mode 100644
index 0000000..2c782bd
--- /dev/null
+++ b/core/src/main/java/org/apache/struts2/interceptor/httpmethod/GetOnly.java
@@ -0,0 +1,14 @@
+package org.apache.struts2.interceptor.httpmethod;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Target({ElementType.METHOD, ElementType.TYPE})
+@Retention(RetentionPolicy.RUNTIME)
+public @interface GetOnly {
+
+ HttpMethod[] value() default { HttpMethod.GET };
+
+}
http://git-wip-us.apache.org/repos/asf/struts/blob/2ae020ce/core/src/main/java/org/apache/struts2/interceptor/httpmethod/GetPostOnly.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/struts2/interceptor/httpmethod/GetPostOnly.java b/core/src/main/java/org/apache/struts2/interceptor/httpmethod/GetPostOnly.java
new file mode 100644
index 0000000..9f8df02
--- /dev/null
+++ b/core/src/main/java/org/apache/struts2/interceptor/httpmethod/GetPostOnly.java
@@ -0,0 +1,14 @@
+package org.apache.struts2.interceptor.httpmethod;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Target({ElementType.METHOD, ElementType.TYPE})
+@Retention(RetentionPolicy.RUNTIME)
+public @interface GetPostOnly {
+
+ HttpMethod[] value() default { HttpMethod.GET, HttpMethod.POST };
+
+}
http://git-wip-us.apache.org/repos/asf/struts/blob/2ae020ce/core/src/main/java/org/apache/struts2/interceptor/httpmethod/PostOnly.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/struts2/interceptor/httpmethod/PostOnly.java b/core/src/main/java/org/apache/struts2/interceptor/httpmethod/PostOnly.java
new file mode 100644
index 0000000..1163188
--- /dev/null
+++ b/core/src/main/java/org/apache/struts2/interceptor/httpmethod/PostOnly.java
@@ -0,0 +1,14 @@
+package org.apache.struts2.interceptor.httpmethod;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Target({ElementType.METHOD, ElementType.TYPE})
+@Retention(RetentionPolicy.RUNTIME)
+public @interface PostOnly {
+
+ HttpMethod[] value() default { HttpMethod.POST };
+
+}
[10/11] git commit: Renames basic annotation to better express its
meaning
Posted by lu...@apache.org.
Renames basic annotation to better express its meaning
Project: http://git-wip-us.apache.org/repos/asf/struts/repo
Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/8d35e613
Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/8d35e613
Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/8d35e613
Branch: refs/heads/feature/http-interceptor
Commit: 8d35e613c1e375f18d83b5ac962a716bfda680db
Parents: f888e67
Author: Lukasz Lenart <lu...@apache.org>
Authored: Sun Apr 20 22:35:01 2014 +0200
Committer: Lukasz Lenart <lu...@apache.org>
Committed: Sun Apr 20 22:35:01 2014 +0200
----------------------------------------------------------------------
.../httpmethod/AllowedHttpMethod.java | 20 ++++++++++++++++++++
.../interceptor/httpmethod/AllowedMethod.java | 20 --------------------
.../httpmethod/HttpMethodInterceptor.java | 14 +++++++-------
.../apache/struts2/HttpMethodsTestAction.java | 4 ++--
4 files changed, 29 insertions(+), 29 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/struts/blob/8d35e613/core/src/main/java/org/apache/struts2/interceptor/httpmethod/AllowedHttpMethod.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/struts2/interceptor/httpmethod/AllowedHttpMethod.java b/core/src/main/java/org/apache/struts2/interceptor/httpmethod/AllowedHttpMethod.java
new file mode 100644
index 0000000..7438c45
--- /dev/null
+++ b/core/src/main/java/org/apache/struts2/interceptor/httpmethod/AllowedHttpMethod.java
@@ -0,0 +1,20 @@
+package org.apache.struts2.interceptor.httpmethod;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Use this annotation to limit with what http method action or action's method can be called
+ *
+ * @see HttpMethodInterceptor
+ * @since 2.3.18
+ */
+@Target({ElementType.METHOD, ElementType.TYPE})
+@Retention(RetentionPolicy.RUNTIME)
+public @interface AllowedHttpMethod {
+
+ HttpMethod[] value() default {};
+
+}
http://git-wip-us.apache.org/repos/asf/struts/blob/8d35e613/core/src/main/java/org/apache/struts2/interceptor/httpmethod/AllowedMethod.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/struts2/interceptor/httpmethod/AllowedMethod.java b/core/src/main/java/org/apache/struts2/interceptor/httpmethod/AllowedMethod.java
deleted file mode 100644
index 435e60e..0000000
--- a/core/src/main/java/org/apache/struts2/interceptor/httpmethod/AllowedMethod.java
+++ /dev/null
@@ -1,20 +0,0 @@
-package org.apache.struts2.interceptor.httpmethod;
-
-import java.lang.annotation.ElementType;
-import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
-import java.lang.annotation.Target;
-
-/**
- * Use this annotation to limit with what http method action or action's method can be called
- *
- * @see HttpMethodInterceptor
- * @since 2.3.18
- */
-@Target({ElementType.METHOD, ElementType.TYPE})
-@Retention(RetentionPolicy.RUNTIME)
-public @interface AllowedMethod {
-
- HttpMethod[] value() default {};
-
-}
http://git-wip-us.apache.org/repos/asf/struts/blob/8d35e613/core/src/main/java/org/apache/struts2/interceptor/httpmethod/HttpMethodInterceptor.java
----------------------------------------------------------------------
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 1a57af4..ac64050 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
@@ -21,13 +21,13 @@ import java.util.List;
* and {@link HttpMethodAware#getBadRequestResultName()} returns non-null result name,
* thus value will be used instead.
* <p/>
- * To limit allowed http methods, annotate action class with {@link AllowedMethod} and specify
+ * To limit allowed http methods, annotate action class with {@link AllowedHttpMethod} and specify
* which methods are allowed. You can also use shorter versions {@link GetOnly}, {@link PostOnly}
* and {@link GetPostOnly}
*
* @see HttpMethodAware
* @see HttpMethod
- * @see AllowedMethod
+ * @see AllowedHttpMethod
* @see GetOnly
* @see PostOnly
* @see GetPostOnly
@@ -35,7 +35,7 @@ import java.util.List;
*/
public class HttpMethodInterceptor extends AbstractInterceptor {
- public static final Class[] HTTP_METHOD_ANNOTATIONS = {AllowedMethod.class, PostOnly.class, GetOnly.class, GetPostOnly.class};
+ public static final Class[] HTTP_METHOD_ANNOTATIONS = {AllowedHttpMethod.class, PostOnly.class, GetOnly.class, GetPostOnly.class};
private static final Logger LOG = LoggerFactory.getLogger(HttpMethodInterceptor.class);
@@ -57,14 +57,14 @@ public class HttpMethodInterceptor extends AbstractInterceptor {
if (AnnotationUtils.isAnnotatedBy(method, HTTP_METHOD_ANNOTATIONS)) {
if (LOG.isDebugEnabled()) {
LOG.debug("Action's method #0 annotated with #1, checking if request #2 meets allowed methods!",
- invocation.getProxy().getMethod(), AllowedMethod.class.getSimpleName(), request.getMethod());
+ invocation.getProxy().getMethod(), AllowedHttpMethod.class.getSimpleName(), request.getMethod());
}
return doIntercept(invocation, method);
}
} else if (AnnotationUtils.isAnnotatedBy(action.getClass(), HTTP_METHOD_ANNOTATIONS)) {
if (LOG.isDebugEnabled()) {
LOG.debug("Action #0 annotated with #1, checking if request #2 meets allowed methods!",
- action, AllowedMethod.class.getSimpleName(), request.getMethod());
+ action, AllowedHttpMethod.class.getSimpleName(), request.getMethod());
}
return doIntercept(invocation, action.getClass());
}
@@ -90,8 +90,8 @@ public class HttpMethodInterceptor extends AbstractInterceptor {
protected List<HttpMethod> readAllowedMethods(AnnotatedElement element) {
List<HttpMethod> allowedMethods = Collections.emptyList();
- if (AnnotationUtils.isAnnotatedBy(element, AllowedMethod.class)) {
- allowedMethods = Arrays.asList(element.getAnnotation(AllowedMethod.class).value());
+ if (AnnotationUtils.isAnnotatedBy(element, AllowedHttpMethod.class)) {
+ allowedMethods = Arrays.asList(element.getAnnotation(AllowedHttpMethod.class).value());
}
if (AnnotationUtils.isAnnotatedBy(element, GetOnly.class)) {
allowedMethods = Arrays.asList(element.getAnnotation(GetOnly.class).value());
http://git-wip-us.apache.org/repos/asf/struts/blob/8d35e613/core/src/test/java/org/apache/struts2/HttpMethodsTestAction.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/struts2/HttpMethodsTestAction.java b/core/src/test/java/org/apache/struts2/HttpMethodsTestAction.java
index 79dd16e..c67ec39 100644
--- a/core/src/test/java/org/apache/struts2/HttpMethodsTestAction.java
+++ b/core/src/test/java/org/apache/struts2/HttpMethodsTestAction.java
@@ -1,7 +1,7 @@
package org.apache.struts2;
import com.opensymphony.xwork2.ActionSupport;
-import org.apache.struts2.interceptor.httpmethod.AllowedMethod;
+import org.apache.struts2.interceptor.httpmethod.AllowedHttpMethod;
import org.apache.struts2.interceptor.httpmethod.GetOnly;
import org.apache.struts2.interceptor.httpmethod.GetPostOnly;
import org.apache.struts2.interceptor.httpmethod.HttpMethod;
@@ -10,7 +10,7 @@ import org.apache.struts2.interceptor.httpmethod.PostOnly;
import static org.apache.struts2.interceptor.httpmethod.HttpMethod.POST;
-@AllowedMethod(POST)
+@AllowedHttpMethod(POST)
public class HttpMethodsTestAction extends ActionSupport implements HttpMethodAware {
private String resultName = null;
[09/11] git commit: Updates JavaDocs in annotations
Posted by lu...@apache.org.
Updates JavaDocs in annotations
Project: http://git-wip-us.apache.org/repos/asf/struts/repo
Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/f888e677
Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/f888e677
Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/f888e677
Branch: refs/heads/feature/http-interceptor
Commit: f888e67751fceaac3a9fca8b991ddb4b4697d042
Parents: e798a9f
Author: Lukasz Lenart <lu...@apache.org>
Authored: Sun Apr 20 22:13:58 2014 +0200
Committer: Lukasz Lenart <lu...@apache.org>
Committed: Sun Apr 20 22:13:58 2014 +0200
----------------------------------------------------------------------
.../apache/struts2/interceptor/httpmethod/AllowedMethod.java | 6 ++++++
.../org/apache/struts2/interceptor/httpmethod/GetOnly.java | 6 ++++++
.../org/apache/struts2/interceptor/httpmethod/GetPostOnly.java | 6 ++++++
.../org/apache/struts2/interceptor/httpmethod/HttpMethod.java | 6 ++++++
.../apache/struts2/interceptor/httpmethod/HttpMethodAware.java | 1 +
.../org/apache/struts2/interceptor/httpmethod/PostOnly.java | 6 ++++++
6 files changed, 31 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/struts/blob/f888e677/core/src/main/java/org/apache/struts2/interceptor/httpmethod/AllowedMethod.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/struts2/interceptor/httpmethod/AllowedMethod.java b/core/src/main/java/org/apache/struts2/interceptor/httpmethod/AllowedMethod.java
index 05d3795..435e60e 100644
--- a/core/src/main/java/org/apache/struts2/interceptor/httpmethod/AllowedMethod.java
+++ b/core/src/main/java/org/apache/struts2/interceptor/httpmethod/AllowedMethod.java
@@ -5,6 +5,12 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
+/**
+ * Use this annotation to limit with what http method action or action's method can be called
+ *
+ * @see HttpMethodInterceptor
+ * @since 2.3.18
+ */
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface AllowedMethod {
http://git-wip-us.apache.org/repos/asf/struts/blob/f888e677/core/src/main/java/org/apache/struts2/interceptor/httpmethod/GetOnly.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/struts2/interceptor/httpmethod/GetOnly.java b/core/src/main/java/org/apache/struts2/interceptor/httpmethod/GetOnly.java
index 2c782bd..6cf2dc6 100644
--- a/core/src/main/java/org/apache/struts2/interceptor/httpmethod/GetOnly.java
+++ b/core/src/main/java/org/apache/struts2/interceptor/httpmethod/GetOnly.java
@@ -5,6 +5,12 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
+/**
+ * Use this annotation to allow call action or action's method via GET request only
+ *
+ * @see HttpMethodInterceptor
+ * @since 2.3.18
+ */
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface GetOnly {
http://git-wip-us.apache.org/repos/asf/struts/blob/f888e677/core/src/main/java/org/apache/struts2/interceptor/httpmethod/GetPostOnly.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/struts2/interceptor/httpmethod/GetPostOnly.java b/core/src/main/java/org/apache/struts2/interceptor/httpmethod/GetPostOnly.java
index 9f8df02..0668784 100644
--- a/core/src/main/java/org/apache/struts2/interceptor/httpmethod/GetPostOnly.java
+++ b/core/src/main/java/org/apache/struts2/interceptor/httpmethod/GetPostOnly.java
@@ -5,6 +5,12 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
+/**
+ * Use this annotation to allow call action or action's method via GET or POST request only
+ *
+ * @see HttpMethodInterceptor
+ * @since 2.3.18
+ */
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface GetPostOnly {
http://git-wip-us.apache.org/repos/asf/struts/blob/f888e677/core/src/main/java/org/apache/struts2/interceptor/httpmethod/HttpMethod.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/struts2/interceptor/httpmethod/HttpMethod.java b/core/src/main/java/org/apache/struts2/interceptor/httpmethod/HttpMethod.java
index 30f4ea3..e1437f4 100644
--- a/core/src/main/java/org/apache/struts2/interceptor/httpmethod/HttpMethod.java
+++ b/core/src/main/java/org/apache/struts2/interceptor/httpmethod/HttpMethod.java
@@ -1,5 +1,11 @@
package org.apache.struts2.interceptor.httpmethod;
+/**
+ * Enum represents possible http request types
+ *
+ * @see HttpMethodInterceptor
+ * @since 2.3.18
+ */
public enum HttpMethod {
GET,
http://git-wip-us.apache.org/repos/asf/struts/blob/f888e677/core/src/main/java/org/apache/struts2/interceptor/httpmethod/HttpMethodAware.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/struts2/interceptor/httpmethod/HttpMethodAware.java b/core/src/main/java/org/apache/struts2/interceptor/httpmethod/HttpMethodAware.java
index cfdc782..d6561f7 100644
--- a/core/src/main/java/org/apache/struts2/interceptor/httpmethod/HttpMethodAware.java
+++ b/core/src/main/java/org/apache/struts2/interceptor/httpmethod/HttpMethodAware.java
@@ -7,6 +7,7 @@ package org.apache.struts2.interceptor.httpmethod;
* Another function of this interface is to return result which should be returned when action
* was called with wrong http method
*
+ * @see HttpMethodInterceptor
* @since 2.3.18
*/
public interface HttpMethodAware {
http://git-wip-us.apache.org/repos/asf/struts/blob/f888e677/core/src/main/java/org/apache/struts2/interceptor/httpmethod/PostOnly.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/struts2/interceptor/httpmethod/PostOnly.java b/core/src/main/java/org/apache/struts2/interceptor/httpmethod/PostOnly.java
index 1163188..0f3bcd5 100644
--- a/core/src/main/java/org/apache/struts2/interceptor/httpmethod/PostOnly.java
+++ b/core/src/main/java/org/apache/struts2/interceptor/httpmethod/PostOnly.java
@@ -5,6 +5,12 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
+/**
+ * Use this annotation to allow call action or action's method via POST request only
+ *
+ * @see HttpMethodInterceptor
+ * @since 2.3.18
+ */
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface PostOnly {
[08/11] git commit: Adds test cases
Posted by lu...@apache.org.
Adds test cases
Project: http://git-wip-us.apache.org/repos/asf/struts/repo
Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/e798a9f6
Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/e798a9f6
Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/e798a9f6
Branch: refs/heads/feature/http-interceptor
Commit: e798a9f62214b727b8de5034f17104a326aa3336
Parents: b855351
Author: Lukasz Lenart <lu...@apache.org>
Authored: Sat Apr 19 21:51:10 2014 +0200
Committer: Lukasz Lenart <lu...@apache.org>
Committed: Sat Apr 19 21:51:10 2014 +0200
----------------------------------------------------------------------
.../apache/struts2/HttpMethodsTestAction.java | 47 +++++
.../httpmethod/HttpMethodInterceptorTest.java | 186 +++++++++++++++++++
.../interceptor/httpmethod/HttpMethodTest.java | 35 ++++
3 files changed, 268 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/struts/blob/e798a9f6/core/src/test/java/org/apache/struts2/HttpMethodsTestAction.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/struts2/HttpMethodsTestAction.java b/core/src/test/java/org/apache/struts2/HttpMethodsTestAction.java
new file mode 100644
index 0000000..79dd16e
--- /dev/null
+++ b/core/src/test/java/org/apache/struts2/HttpMethodsTestAction.java
@@ -0,0 +1,47 @@
+package org.apache.struts2;
+
+import com.opensymphony.xwork2.ActionSupport;
+import org.apache.struts2.interceptor.httpmethod.AllowedMethod;
+import org.apache.struts2.interceptor.httpmethod.GetOnly;
+import org.apache.struts2.interceptor.httpmethod.GetPostOnly;
+import org.apache.struts2.interceptor.httpmethod.HttpMethod;
+import org.apache.struts2.interceptor.httpmethod.HttpMethodAware;
+import org.apache.struts2.interceptor.httpmethod.PostOnly;
+
+import static org.apache.struts2.interceptor.httpmethod.HttpMethod.POST;
+
+@AllowedMethod(POST)
+public class HttpMethodsTestAction extends ActionSupport implements HttpMethodAware {
+
+ private String resultName = null;
+
+ public HttpMethodsTestAction() {
+ }
+
+ public HttpMethodsTestAction(String resultName) {
+ this.resultName = resultName;
+ }
+
+ @GetOnly
+ public String onGetOnly() {
+ return "onGetOnly";
+ }
+
+ @PostOnly
+ public String onPostOnly() {
+ return "onPostOnly";
+ }
+
+ @GetPostOnly
+ public String onGetPostOnly() {
+ return "onGetPostOnly";
+ }
+
+ public void setMethod(HttpMethod httpMethod) {
+
+ }
+
+ public String getBadRequestResultName() {
+ return resultName;
+ }
+}
http://git-wip-us.apache.org/repos/asf/struts/blob/e798a9f6/core/src/test/java/org/apache/struts2/interceptor/httpmethod/HttpMethodInterceptorTest.java
----------------------------------------------------------------------
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
new file mode 100644
index 0000000..34303ea
--- /dev/null
+++ b/core/src/test/java/org/apache/struts2/interceptor/httpmethod/HttpMethodInterceptorTest.java
@@ -0,0 +1,186 @@
+package org.apache.struts2.interceptor.httpmethod;
+
+import com.opensymphony.xwork2.mock.MockActionInvocation;
+import com.opensymphony.xwork2.mock.MockActionProxy;
+import org.apache.struts2.HttpMethodsTestAction;
+import org.apache.struts2.ServletActionContext;
+import org.apache.struts2.StrutsInternalTestCase;
+import org.apache.struts2.TestAction;
+import org.springframework.mock.web.MockHttpServletRequest;
+
+public class HttpMethodInterceptorTest extends StrutsInternalTestCase {
+
+ public void testNotAnnotatedAction() throws Exception {
+ // given
+ TestAction action = new TestAction();
+ HttpMethodInterceptor interceptor = new HttpMethodInterceptor();
+ MockActionInvocation invocation = new MockActionInvocation();
+ invocation.setAction(action);
+ invocation.setProxy(new MockActionProxy());
+
+ invocation.setResultCode("success");
+
+ MockHttpServletRequest request = new MockHttpServletRequest("post", "/action");
+ ServletActionContext.setRequest(request);
+
+ // when
+ String resultName = interceptor.intercept(invocation);
+
+ // then
+ assertEquals("success", resultName);
+ }
+
+ public void testActionWithPostAllowed() throws Exception {
+ // given
+ HttpMethodsTestAction action = new HttpMethodsTestAction();
+ HttpMethodInterceptor interceptor = new HttpMethodInterceptor();
+ MockActionInvocation invocation = new MockActionInvocation();
+ invocation.setAction(action);
+ invocation.setProxy(new MockActionProxy());
+
+ invocation.setResultCode("success");
+
+ MockHttpServletRequest request = new MockHttpServletRequest("post", "/action");
+ ServletActionContext.setRequest(request);
+
+ // when
+ String resultName = interceptor.intercept(invocation);
+
+ // then
+ assertEquals("success", resultName);
+ }
+
+ public void testGetIsNotAllowed() throws Exception {
+ // given
+ HttpMethodsTestAction action = new HttpMethodsTestAction();
+ HttpMethodInterceptor interceptor = new HttpMethodInterceptor();
+ MockActionInvocation invocation = new MockActionInvocation();
+ invocation.setAction(action);
+ invocation.setProxy(new MockActionProxy());
+
+ invocation.setResultCode("success");
+
+ MockHttpServletRequest request = new MockHttpServletRequest("get", "/action");
+ ServletActionContext.setRequest(request);
+
+ // when
+ String resultName = interceptor.intercept(invocation);
+
+ // then
+ assertEquals("bad-request", resultName);
+ }
+
+ public void testGetIsNotAllowedWithCustomResultName() throws Exception {
+ // given
+ HttpMethodsTestAction action = new HttpMethodsTestAction();
+ HttpMethodInterceptor interceptor = new HttpMethodInterceptor();
+ interceptor.setBadRequestResultName("custom-bad-request");
+
+ MockActionInvocation invocation = new MockActionInvocation();
+ invocation.setAction(action);
+ invocation.setProxy(new MockActionProxy());
+
+ invocation.setResultCode("success");
+
+ MockHttpServletRequest request = new MockHttpServletRequest("get", "/action");
+ ServletActionContext.setRequest(request);
+
+ // when
+ String resultName = interceptor.intercept(invocation);
+
+ // then
+ assertEquals("custom-bad-request", resultName);
+ }
+
+ public void testGetIsNotAllowedWithActionDefinedResultName() throws Exception {
+ // given
+ HttpMethodsTestAction action = new HttpMethodsTestAction("action-bad-request");
+ HttpMethodInterceptor interceptor = new HttpMethodInterceptor();
+ interceptor.setBadRequestResultName("custom-bad-request");
+
+ MockActionInvocation invocation = new MockActionInvocation();
+ invocation.setAction(action);
+ invocation.setProxy(new MockActionProxy());
+
+ invocation.setResultCode("success");
+
+ MockHttpServletRequest request = new MockHttpServletRequest("get", "/action");
+ ServletActionContext.setRequest(request);
+
+ // when
+ String resultName = interceptor.intercept(invocation);
+
+ // then
+ assertEquals("action-bad-request", resultName);
+ }
+
+ public void testGetOnlyOnMethod() throws Exception {
+ // given
+ HttpMethodsTestAction action = new HttpMethodsTestAction();
+ HttpMethodInterceptor interceptor = new HttpMethodInterceptor();
+ MockActionInvocation invocation = new MockActionInvocation();
+ invocation.setAction(action);
+ MockActionProxy proxy = new MockActionProxy();
+ proxy.setMethod("onGetOnly");
+ proxy.setMethodSpecified(true);
+ invocation.setProxy(proxy);
+
+ invocation.setResultCode("onGetOnly");
+
+ MockHttpServletRequest request = new MockHttpServletRequest("get", "/action");
+ ServletActionContext.setRequest(request);
+
+ // when
+ String resultName = interceptor.intercept(invocation);
+
+ // then
+ assertEquals("onGetOnly", resultName);
+ }
+
+ public void testPostOnlyOnMethod() throws Exception {
+ // given
+ HttpMethodsTestAction action = new HttpMethodsTestAction();
+ HttpMethodInterceptor interceptor = new HttpMethodInterceptor();
+ MockActionInvocation invocation = new MockActionInvocation();
+ invocation.setAction(action);
+ MockActionProxy proxy = new MockActionProxy();
+ proxy.setMethod("onPostOnly");
+ proxy.setMethodSpecified(true);
+ invocation.setProxy(proxy);
+
+ invocation.setResultCode("onPostOnly");
+
+ MockHttpServletRequest request = new MockHttpServletRequest("post", "/action");
+ ServletActionContext.setRequest(request);
+
+ // when
+ String resultName = interceptor.intercept(invocation);
+
+ // then
+ assertEquals("onPostOnly", resultName);
+ }
+
+ public void testGetPostOnlyOnMethod() throws Exception {
+ // given
+ HttpMethodsTestAction action = new HttpMethodsTestAction();
+ HttpMethodInterceptor interceptor = new HttpMethodInterceptor();
+ MockActionInvocation invocation = new MockActionInvocation();
+ invocation.setAction(action);
+ MockActionProxy proxy = new MockActionProxy();
+ proxy.setMethod("onGetPostOnly");
+ proxy.setMethodSpecified(true);
+ invocation.setProxy(proxy);
+
+ invocation.setResultCode("onGetPostOnly");
+
+ MockHttpServletRequest request = new MockHttpServletRequest("post", "/action");
+ ServletActionContext.setRequest(request);
+
+ // when
+ String resultName = interceptor.intercept(invocation);
+
+ // then
+ assertEquals("onGetPostOnly", resultName);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/struts/blob/e798a9f6/core/src/test/java/org/apache/struts2/interceptor/httpmethod/HttpMethodTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/struts2/interceptor/httpmethod/HttpMethodTest.java b/core/src/test/java/org/apache/struts2/interceptor/httpmethod/HttpMethodTest.java
new file mode 100644
index 0000000..4de4a40
--- /dev/null
+++ b/core/src/test/java/org/apache/struts2/interceptor/httpmethod/HttpMethodTest.java
@@ -0,0 +1,35 @@
+package org.apache.struts2.interceptor.httpmethod;
+
+import junit.framework.TestCase;
+
+public class HttpMethodTest extends TestCase {
+
+ public void testConvertHttpRequestMethod() throws Exception {
+ // given
+ String httpRequestMethod = "post";
+
+ // when
+ HttpMethod httpMethod = HttpMethod.parse(httpRequestMethod);
+
+ // then
+ assertEquals(HttpMethod.POST, httpMethod);
+ }
+
+ public void testValueOfThrowsException() throws Exception {
+ // given
+ String httpRequestMethod = "post";
+
+ // when
+ IllegalArgumentException expected = null;
+ try {
+ HttpMethod.valueOf(httpRequestMethod);
+ } catch (IllegalArgumentException e) {
+ expected = e;
+ }
+
+ // then
+ assertNotNull(expected);
+ assertEquals(expected.getClass(), IllegalArgumentException.class);
+ }
+
+}
[07/11] git commit: Adds support for annotations on methods
Posted by lu...@apache.org.
Adds support for annotations on methods
Project: http://git-wip-us.apache.org/repos/asf/struts/repo
Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/b855351e
Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/b855351e
Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/b855351e
Branch: refs/heads/feature/http-interceptor
Commit: b855351ecb13132033df7ab6d127d86987cf8ef1
Parents: 5664284
Author: Lukasz Lenart <lu...@apache.org>
Authored: Sat Apr 19 21:51:01 2014 +0200
Committer: Lukasz Lenart <lu...@apache.org>
Committed: Sat Apr 19 21:51:01 2014 +0200
----------------------------------------------------------------------
.../httpmethod/HttpMethodInterceptor.java | 50 ++++++++++++--------
1 file changed, 30 insertions(+), 20 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/struts/blob/b855351e/core/src/main/java/org/apache/struts2/interceptor/httpmethod/HttpMethodInterceptor.java
----------------------------------------------------------------------
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 e22e6a7..1a57af4 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
@@ -8,6 +8,8 @@ import com.opensymphony.xwork2.util.logging.LoggerFactory;
import org.apache.struts2.ServletActionContext;
import javax.servlet.http.HttpServletRequest;
+import java.lang.reflect.AnnotatedElement;
+import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@@ -18,7 +20,7 @@ import java.util.List;
* will be returned or if action implements {@link HttpMethodAware}
* and {@link HttpMethodAware#getBadRequestResultName()} returns non-null result name,
* thus value will be used instead.
- *
+ * <p/>
* To limit allowed http methods, annotate action class with {@link AllowedMethod} and specify
* which methods are allowed. You can also use shorter versions {@link GetOnly}, {@link PostOnly}
* and {@link GetPostOnly}
@@ -29,12 +31,11 @@ import java.util.List;
* @see GetOnly
* @see PostOnly
* @see GetPostOnly
- *
* @since 2.3.18
*/
public class HttpMethodInterceptor extends AbstractInterceptor {
- public static final Class[] HTTP_METHOD_ANNOTATIONS = { AllowedMethod.class, PostOnly.class, GetOnly.class, GetPostOnly.class };
+ public static final Class[] HTTP_METHOD_ANNOTATIONS = {AllowedMethod.class, PostOnly.class, GetOnly.class, GetPostOnly.class};
private static final Logger LOG = LoggerFactory.getLogger(HttpMethodInterceptor.class);
@@ -49,48 +50,57 @@ public class HttpMethodInterceptor extends AbstractInterceptor {
LOG.debug("Action #0 implements #1, setting request method #3",
action, HttpMethodAware.class.getSimpleName(), request.getMethod());
}
- ((HttpMethodAware) (action)).setMethod(HttpMethod.valueOf(request.getMethod()));
+ ((HttpMethodAware) (action)).setMethod(HttpMethod.parse(request.getMethod()));
}
- if (AnnotationUtils.isAnnotatedBy(action.getClass(), HTTP_METHOD_ANNOTATIONS)) {
+ if (invocation.getProxy().isMethodSpecified()) {
+ Method method = action.getClass().getMethod(invocation.getProxy().getMethod(), new Class[0]);
+ if (AnnotationUtils.isAnnotatedBy(method, HTTP_METHOD_ANNOTATIONS)) {
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Action's method #0 annotated with #1, checking if request #2 meets allowed methods!",
+ invocation.getProxy().getMethod(), AllowedMethod.class.getSimpleName(), request.getMethod());
+ }
+ return doIntercept(invocation, method);
+ }
+ } else if (AnnotationUtils.isAnnotatedBy(action.getClass(), HTTP_METHOD_ANNOTATIONS)) {
if (LOG.isDebugEnabled()) {
LOG.debug("Action #0 annotated with #1, checking if request #2 meets allowed methods!",
action, AllowedMethod.class.getSimpleName(), request.getMethod());
}
- return doIntercept(invocation);
+ return doIntercept(invocation, action.getClass());
}
return invocation.invoke();
}
- protected String doIntercept(ActionInvocation invocation) throws Exception {
- List<HttpMethod> allowedMethods = readAllowedMethods(invocation.getAction().getClass());
+ protected String doIntercept(ActionInvocation invocation, AnnotatedElement element) throws Exception {
+ List<HttpMethod> allowedMethods = readAllowedMethods(element);
HttpServletRequest request = ServletActionContext.getRequest();
- HttpMethod requestedMethod = HttpMethod.valueOf(request.getMethod());
+ HttpMethod requestedMethod = HttpMethod.parse(request.getMethod());
if (allowedMethods.contains(requestedMethod)) {
- if(LOG.isTraceEnabled()) {
+ if (LOG.isTraceEnabled()) {
LOG.trace("Request method #0 matches allowed methods #1, continuing invocation!", requestedMethod, allowedMethods);
}
return invocation.invoke();
} else {
- if(LOG.isTraceEnabled()) {
+ if (LOG.isTraceEnabled()) {
LOG.trace("Request method #0 doesn't match allowed methods #1, continuing invocation!", requestedMethod, allowedMethods);
}
return getBadRequestResultName(invocation);
}
}
- protected List<HttpMethod> readAllowedMethods(Class<? extends Object> klass) {
+ protected List<HttpMethod> readAllowedMethods(AnnotatedElement element) {
List<HttpMethod> allowedMethods = Collections.emptyList();
- if (AnnotationUtils.isAnnotatedBy(klass, AllowedMethod.class)) {
- allowedMethods = Arrays.asList(klass.getAnnotation(AllowedMethod.class).value());
+ if (AnnotationUtils.isAnnotatedBy(element, AllowedMethod.class)) {
+ allowedMethods = Arrays.asList(element.getAnnotation(AllowedMethod.class).value());
}
- if (AnnotationUtils.isAnnotatedBy(klass, GetOnly.class)) {
- allowedMethods = Arrays.asList(klass.getAnnotation(GetOnly.class).value());
+ if (AnnotationUtils.isAnnotatedBy(element, GetOnly.class)) {
+ allowedMethods = Arrays.asList(element.getAnnotation(GetOnly.class).value());
}
- if (AnnotationUtils.isAnnotatedBy(klass, PostOnly.class)) {
- allowedMethods = Arrays.asList(klass.getAnnotation(PostOnly.class).value());
+ if (AnnotationUtils.isAnnotatedBy(element, PostOnly.class)) {
+ allowedMethods = Arrays.asList(element.getAnnotation(PostOnly.class).value());
}
- if (AnnotationUtils.isAnnotatedBy(klass, GetPostOnly.class)) {
- allowedMethods = Arrays.asList(klass.getAnnotation(GetPostOnly.class).value());
+ if (AnnotationUtils.isAnnotatedBy(element, GetPostOnly.class)) {
+ allowedMethods = Arrays.asList(element.getAnnotation(GetPostOnly.class).value());
}
return Collections.unmodifiableList(allowedMethods);
}
[05/11] git commit: Adds new interface to allow action cooperate with
HttpMethodInterceptor
Posted by lu...@apache.org.
Adds new interface to allow action cooperate with HttpMethodInterceptor
Project: http://git-wip-us.apache.org/repos/asf/struts/repo
Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/9930ceb7
Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/9930ceb7
Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/9930ceb7
Branch: refs/heads/feature/http-interceptor
Commit: 9930ceb7fe3e203c3e33df6c12ce0f44adfc529d
Parents: 84a7cc3
Author: Lukasz Lenart <lu...@apache.org>
Authored: Sat Apr 19 18:05:58 2014 +0200
Committer: Lukasz Lenart <lu...@apache.org>
Committed: Sat Apr 19 18:05:58 2014 +0200
----------------------------------------------------------------------
.../interceptor/httpmethod/HttpMethodAware.java | 30 ++++++++++++++++++++
1 file changed, 30 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/struts/blob/9930ceb7/core/src/main/java/org/apache/struts2/interceptor/httpmethod/HttpMethodAware.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/struts2/interceptor/httpmethod/HttpMethodAware.java b/core/src/main/java/org/apache/struts2/interceptor/httpmethod/HttpMethodAware.java
new file mode 100644
index 0000000..cfdc782
--- /dev/null
+++ b/core/src/main/java/org/apache/struts2/interceptor/httpmethod/HttpMethodAware.java
@@ -0,0 +1,30 @@
+package org.apache.struts2.interceptor.httpmethod;
+
+/**
+ * Action when implements this interface is notified about what method was used to perform request,
+ * it works in connection with {@link org.apache.struts2.interceptor.httpmethod.HttpMethodInterceptor}
+ *
+ * Another function of this interface is to return result which should be returned when action
+ * was called with wrong http method
+ *
+ * @since 2.3.18
+ */
+public interface HttpMethodAware {
+
+ /**
+ * Notifies action about http method used to perform request
+ *
+ * @param httpMethod {@link javax.servlet.http.HttpServletRequest#getMethod()} translated to enum
+ */
+ public void setMethod(HttpMethod httpMethod);
+
+ /**
+ * Action name to use when action was requested with wrong http method
+ * can return null and then default result name will be used instead defined
+ * in {@link org.apache.struts2.interceptor.httpmethod.HttpMethodInterceptor}
+ *
+ * @return result name or null
+ */
+ public String getBadRequestResultName();
+
+}
[11/11] git commit: Adds httpInterceptor to stacks
Posted by lu...@apache.org.
Adds httpInterceptor to stacks
Project: http://git-wip-us.apache.org/repos/asf/struts/repo
Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/b10096b3
Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/b10096b3
Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/b10096b3
Branch: refs/heads/feature/http-interceptor
Commit: b10096b36f71918427052dcaf8e702f936862658
Parents: 8d35e61
Author: Lukasz Lenart <lu...@apache.org>
Authored: Sun Apr 20 22:35:18 2014 +0200
Committer: Lukasz Lenart <lu...@apache.org>
Committed: Sun Apr 20 22:35:18 2014 +0200
----------------------------------------------------------------------
core/src/main/resources/struts-default.xml | 4 ++++
1 file changed, 4 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/struts/blob/b10096b3/core/src/main/resources/struts-default.xml
----------------------------------------------------------------------
diff --git a/core/src/main/resources/struts-default.xml b/core/src/main/resources/struts-default.xml
index 87f1ff5..5dc70ef 100644
--- a/core/src/main/resources/struts-default.xml
+++ b/core/src/main/resources/struts-default.xml
@@ -192,11 +192,13 @@
<interceptor name="annotationWorkflow" class="com.opensymphony.xwork2.interceptor.annotations.AnnotationWorkflowInterceptor" />
<interceptor name="multiselect" class="org.apache.struts2.interceptor.MultiselectInterceptor" />
<interceptor name="deprecation" class="org.apache.struts2.interceptor.DeprecationInterceptor" />
+ <interceptor name="httpMethod" class="org.apache.struts2.interceptor.httpmethod.HttpMethodInterceptor" />
<!-- Basic stack -->
<interceptor-stack name="basicStack">
<interceptor-ref name="exception"/>
<interceptor-ref name="servletConfig"/>
+ <interceptor-ref name="httpMethod"/>
<interceptor-ref name="prepare"/>
<interceptor-ref name="checkbox"/>
<interceptor-ref name="datetime"/>
@@ -263,6 +265,7 @@
<param name="excludeParams">(.*\.|^)class\..*,^dojo\..*,^struts\..*,^session\..*,^request\..*,^application\..*,^servlet(Request|Response)\..*,^parameters\..*,^action:.*,^method:.*</param>
</interceptor-ref>
<interceptor-ref name="servletConfig"/>
+ <interceptor-ref name="httpMethod"/>
<interceptor-ref name="prepare"/>
<interceptor-ref name="chain"/>
<interceptor-ref name="modelDriven"/>
@@ -296,6 +299,7 @@
<interceptor-ref name="exception"/>
<interceptor-ref name="alias"/>
<interceptor-ref name="servletConfig"/>
+ <interceptor-ref name="httpMethod"/>
<interceptor-ref name="i18n"/>
<interceptor-ref name="prepare"/>
<interceptor-ref name="chain"/>
[04/11] git commit: Adds new http interceptor to allow block access
based on method type
Posted by lu...@apache.org.
Adds new http interceptor to allow block access based on method type
Project: http://git-wip-us.apache.org/repos/asf/struts/repo
Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/84a7cc3d
Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/84a7cc3d
Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/84a7cc3d
Branch: refs/heads/feature/http-interceptor
Commit: 84a7cc3da00cf84fdd67aef1cd2071840b687249
Parents: 2ae020c
Author: Lukasz Lenart <lu...@apache.org>
Authored: Sat Apr 19 17:50:32 2014 +0200
Committer: Lukasz Lenart <lu...@apache.org>
Committed: Sat Apr 19 17:50:32 2014 +0200
----------------------------------------------------------------------
.../httpmethod/HttpMethodInterceptor.java | 97 ++++++++++++++++++++
1 file changed, 97 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/struts/blob/84a7cc3d/core/src/main/java/org/apache/struts2/interceptor/httpmethod/HttpMethodInterceptor.java
----------------------------------------------------------------------
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
new file mode 100644
index 0000000..36efb52
--- /dev/null
+++ b/core/src/main/java/org/apache/struts2/interceptor/httpmethod/HttpMethodInterceptor.java
@@ -0,0 +1,97 @@
+package org.apache.struts2.interceptor.httpmethod;
+
+import com.opensymphony.xwork2.ActionInvocation;
+import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
+import com.opensymphony.xwork2.util.AnnotationUtils;
+import com.opensymphony.xwork2.util.logging.Logger;
+import com.opensymphony.xwork2.util.logging.LoggerFactory;
+import org.apache.struts2.ServletActionContext;
+
+import javax.servlet.http.HttpServletRequest;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+public class HttpMethodInterceptor extends AbstractInterceptor {
+
+ public static final Class[] HTTP_METHOD_ANNOTATIONS = { AllowedMethod.class, PostOnly.class, GetOnly.class, GetPostOnly.class };
+
+ private static final Logger LOG = LoggerFactory.getLogger(HttpMethodInterceptor.class);
+
+ private String badRequestResultName = "bad-request";
+
+ @Override
+ public String intercept(ActionInvocation invocation) throws Exception {
+ Object action = invocation.getAction();
+ HttpServletRequest request = ServletActionContext.getRequest();
+ if (action instanceof HttpMethodAware) {
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Action #0 implements #1, setting request method #3",
+ action, HttpMethodAware.class.getSimpleName(), request.getMethod());
+ }
+ ((HttpMethodAware) (action)).setMethod(HttpMethod.valueOf(request.getMethod()));
+ }
+ if (AnnotationUtils.isAnnotatedBy(action.getClass(), HTTP_METHOD_ANNOTATIONS)) {
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Action #0 annotated with #1, checking if request #2 meets allowed methods!",
+ action, AllowedMethod.class.getSimpleName(), request.getMethod());
+ }
+ return doIntercept(invocation);
+ }
+ return invocation.invoke();
+ }
+
+ protected String doIntercept(ActionInvocation invocation) throws Exception {
+ List<HttpMethod> allowedMethods = readAllowedMethods(invocation.getAction().getClass());
+ HttpServletRequest request = ServletActionContext.getRequest();
+ HttpMethod requestedMethod = HttpMethod.valueOf(request.getMethod());
+ if (allowedMethods.contains(requestedMethod)) {
+ if(LOG.isTraceEnabled()) {
+ LOG.trace("Request method #0 matches allowed methods #1, continuing invocation!", requestedMethod, allowedMethods);
+ }
+ return invocation.invoke();
+ } else {
+ if(LOG.isTraceEnabled()) {
+ LOG.trace("Request method #0 doesn't match allowed methods #1, continuing invocation!", requestedMethod, allowedMethods);
+ }
+ return getBadRequestResultName(invocation);
+ }
+ }
+
+ protected List<HttpMethod> readAllowedMethods(Class<? extends Object> klass) {
+ List<HttpMethod> allowedMethods = Collections.emptyList();
+ if (AnnotationUtils.isAnnotatedBy(klass, AllowedMethod.class)) {
+ allowedMethods = Arrays.asList(klass.getAnnotation(AllowedMethod.class).value());
+ }
+ if (AnnotationUtils.isAnnotatedBy(klass, GetOnly.class)) {
+ allowedMethods = Arrays.asList(klass.getAnnotation(GetOnly.class).value());
+ }
+ if (AnnotationUtils.isAnnotatedBy(klass, PostOnly.class)) {
+ allowedMethods = Arrays.asList(klass.getAnnotation(PostOnly.class).value());
+ }
+ if (AnnotationUtils.isAnnotatedBy(klass, GetPostOnly.class)) {
+ allowedMethods = Arrays.asList(klass.getAnnotation(GetPostOnly.class).value());
+ }
+ return Collections.unmodifiableList(allowedMethods);
+ }
+
+ protected String getBadRequestResultName(ActionInvocation invocation) {
+ Object action = invocation.getAction();
+ String resultName = badRequestResultName;
+ if (action instanceof HttpMethodAware) {
+ String actionResultName = ((HttpMethodAware) action).getBadRequestResultName();
+ if (actionResultName != null) {
+ resultName = actionResultName;
+ }
+ }
+ if (LOG.isTraceEnabled()) {
+ LOG.trace("Bad request result name is #0", resultName);
+ }
+ return resultName;
+ }
+
+ public void setBadRequestResultName(String badRequestResultName) {
+ this.badRequestResultName = badRequestResultName;
+ }
+
+}
[02/11] git commit: Defined enum with all http methods
Posted by lu...@apache.org.
Defined enum with all http methods
Project: http://git-wip-us.apache.org/repos/asf/struts/repo
Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/507e338a
Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/507e338a
Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/507e338a
Branch: refs/heads/feature/http-interceptor
Commit: 507e338a10b9c0b9f62233c3e9d24ca9436940be
Parents: 9519cd1
Author: Lukasz Lenart <lu...@apache.org>
Authored: Sat Apr 19 17:46:59 2014 +0200
Committer: Lukasz Lenart <lu...@apache.org>
Committed: Sat Apr 19 17:46:59 2014 +0200
----------------------------------------------------------------------
.../interceptor/httpmethod/HttpMethod.java | 19 +++++++++++++++++++
1 file changed, 19 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/struts/blob/507e338a/core/src/main/java/org/apache/struts2/interceptor/httpmethod/HttpMethod.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/struts2/interceptor/httpmethod/HttpMethod.java b/core/src/main/java/org/apache/struts2/interceptor/httpmethod/HttpMethod.java
new file mode 100644
index 0000000..30f4ea3
--- /dev/null
+++ b/core/src/main/java/org/apache/struts2/interceptor/httpmethod/HttpMethod.java
@@ -0,0 +1,19 @@
+package org.apache.struts2.interceptor.httpmethod;
+
+public enum HttpMethod {
+
+ GET,
+ HEAD,
+ POST,
+ PUT,
+ DELETE,
+ TRACE,
+ OPTIONS,
+ CONNECT,
+ PATCH;
+
+ public static HttpMethod parse(String httpRequestMethod) {
+ return valueOf(httpRequestMethod.toUpperCase());
+ }
+
+}