1
- /*
2
- * Copyright (c) Mirth Corporation. All rights reserved.
3
- *
4
- * http://www.mirthcorp.com
5
- *
6
- * The software in this package is published under the terms of the MPL license a copy of which has
7
- * been included with this distribution in the LICENSE.txt file.
8
- */
9
-
10
1
package com .mirth .connect .server .api .providers ;
11
2
12
3
import java .io .IOException ;
13
-
14
- import javax .servlet .Filter ;
15
- import javax .servlet .FilterChain ;
16
- import javax .servlet .FilterConfig ;
17
- import javax .servlet .ServletException ;
18
- import javax .servlet .ServletRequest ;
19
- import javax .servlet .ServletResponse ;
20
- import javax .servlet .http .HttpServletRequest ;
21
- import javax .servlet .http .HttpServletResponse ;
4
+ import java .lang .reflect .Method ;
5
+ import java .util .List ;
6
+
7
+ import javax .annotation .Priority ;
8
+ import javax .ws .rs .Priorities ;
9
+ import javax .ws .rs .container .ContainerRequestContext ;
10
+ import javax .ws .rs .container .ContainerRequestFilter ;
11
+ import javax .ws .rs .container .ResourceInfo ;
12
+ import javax .ws .rs .core .Context ;
13
+ import javax .ws .rs .core .Response ;
22
14
import javax .ws .rs .ext .Provider ;
23
15
24
16
import org .apache .commons .configuration2 .PropertiesConfiguration ;
25
17
import org .apache .commons .lang3 .StringUtils ;
26
18
19
+ import com .mirth .connect .server .api .DontRequireRequestedWith ;
20
+
27
21
@ Provider
28
- public class RequestedWithFilter implements Filter {
22
+ @ Priority (Priorities .AUTHENTICATION + 100 )
23
+ public class RequestedWithFilter implements ContainerRequestFilter {
29
24
30
- private boolean isRequestedWithHeaderRequired = true ;
25
+ @ Context
26
+ private ResourceInfo resourceInfo ;
31
27
28
+ private static boolean isRequestedWithHeaderRequired = true ;
32
29
33
- public RequestedWithFilter (PropertiesConfiguration mirthProperties ) {
34
-
30
+ // Jax requires a no-arg constructor to instantiate providers via classpath scanning.
31
+ public RequestedWithFilter () {
32
+ }
33
+
34
+ public static void configure (PropertiesConfiguration mirthProperties ) {
35
35
isRequestedWithHeaderRequired = mirthProperties .getBoolean ("server.api.require-requested-with" , true );
36
36
}
37
37
38
38
@ Override
39
- public void init (FilterConfig filterConfig ) throws ServletException {}
39
+ public void filter (ContainerRequestContext requestContext ) throws IOException {
40
+ if (!isRequestedWithHeaderRequired ) {
41
+ return ;
42
+ }
40
43
41
- @ Override
42
- public void doFilter (ServletRequest request , ServletResponse response , FilterChain chain ) throws IOException , ServletException {
43
- HttpServletResponse res = (HttpServletResponse ) response ;
44
+ // If the resource method or class is annotated with DontRequireRequestedWith, skip the check
45
+ if (resourceInfo != null ) {
46
+ Method method = resourceInfo .getResourceMethod ();
47
+ if (method != null && method .getAnnotation (DontRequireRequestedWith .class ) != null ) {
48
+ return ;
49
+ }
50
+ Class <?> resourceClass = resourceInfo .getResourceClass ();
51
+ if (resourceClass != null && resourceClass .getAnnotation (DontRequireRequestedWith .class ) != null ) {
52
+ return ;
53
+ }
54
+ }
44
55
45
- HttpServletRequest servletRequest = (HttpServletRequest )request ;
46
- String requestedWithHeader = (String ) servletRequest .getHeader ("X-Requested-With" );
56
+ List <String > header = requestContext .getHeaders ().get ("X-Requested-With" );
47
57
48
58
//if header is required and not present, send an error
49
- if ( isRequestedWithHeaderRequired && StringUtils .isBlank (requestedWithHeader )) {
50
- res . sendError ( 400 , "All requests must have 'X-Requested-With' header" );
59
+ if ( header == null || header . isEmpty () || StringUtils .isBlank (header . get ( 0 ) )) {
60
+ requestContext . abortWith ( Response . status ( 400 ). entity ( "All requests must have 'X-Requested-With' header" ). build () );
51
61
}
52
- else {
53
- chain .doFilter (request , response );
54
- }
55
-
56
62
}
57
-
58
- public boolean isRequestedWithHeaderRequired () {
59
- return isRequestedWithHeaderRequired ;
60
- }
61
-
62
- @ Override
63
- public void destroy () {}
64
- }
63
+ }
0 commit comments