Class HttpMethodInterceptor
- All Implemented Interfaces:
Serializable,ConditionalInterceptor,Interceptor
badRequestResultName
will be returned or if action implements HttpMethodAware
and HttpMethodAware.getBadRequestResultName() returns non-null result name,
thus value will be used instead.
To limit allowed http methods, annotate action class with AllowedHttpMethod and specify,
which methods are allowed. You can also use shorter versions HttpGet, HttpPost,
HttpPut, HttpDelete and HttpGetOrPost
You can combine any of these annotations to achieve required allowed methods' filtering.
Method annotations apply to the invoked method, including the implicit execute() method.
Its annotation takes precedence; otherwise annotated declarations are looked up through the class
and interface hierarchy. Class annotations are looked up only when no method annotation is found.
Restrictions apply to HttpServletRequest.getMethod(), not a _method parameter.
The allowed methods can also be declared on the action in XML with the http-method attribute.
When both the attribute and annotations are present, the request method must satisfy both.
If no result can be resolved under the bad request result name, the response is
405 Method Not Allowed with an Allow header listing the accepted methods.
- Since:
- 6.2.0
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected StringdoIntercept(ActionInvocation invocation, AnnotatedElement element) protected StringgetBadRequestResultName(ActionInvocation invocation) intercept(ActionInvocation invocation) Override to handle interceptionprotected List<HttpMethod>readAllowedMethods(AnnotatedElement element) protected StringrejectRequest(ActionInvocation invocation, Collection<HttpMethod> allowedMethods) voidsetBadRequestResultName(String badRequestResultName) Methods inherited from class org.apache.struts2.interceptor.AbstractInterceptor
destroy, init, setDisabled, shouldIntercept
-
Constructor Details
-
HttpMethodInterceptor
public HttpMethodInterceptor()
-
-
Method Details
-
intercept
Description copied from class:AbstractInterceptorOverride to handle interception- Specified by:
interceptin interfaceInterceptor- Specified by:
interceptin classAbstractInterceptor- Parameters:
invocation- the action invocation- Returns:
- the return code, either returned from
ActionInvocation.invoke(), or from the interceptor itself. - Throws:
Exception- any system-level error, as defined inAction.execute().
-
doIntercept
protected String doIntercept(ActionInvocation invocation, AnnotatedElement element) throws Exception - Throws:
Exception
-
rejectRequest
protected String rejectRequest(ActionInvocation invocation, Collection<HttpMethod> allowedMethods) throws IOException - Returns:
- the bad request result name when the action maps that result, otherwise
Action.NONEafter sending405 Method Not Allowedwith anAllowheader - Throws:
IOException- Since:
- 7.5.0
-
readAllowedMethods
-
getBadRequestResultName
-
setBadRequestResultName
-