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-
101package com .mirth .connect .server .api .providers ;
112
123import java .io .IOException ;
4+ import java .lang .reflect .Method ;
5+ import java .util .List ;
136
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 ;
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 ;
2214import javax .ws .rs .ext .Provider ;
2315
2416import org .apache .commons .configuration2 .PropertiesConfiguration ;
2517import org .apache .commons .lang3 .StringUtils ;
2618
19+ import com .mirth .connect .server .api .DontRequireRequestedWith ;
20+
2721@ Provider
28- public class RequestedWithFilter implements Filter {
22+ @ Priority (Priorities .AUTHENTICATION + 100 )
23+ public class RequestedWithFilter implements ContainerRequestFilter {
2924
30- private boolean isRequestedWithHeaderRequired = true ;
25+ @ Context
26+ private ResourceInfo resourceInfo ;
3127
28+ private static boolean isRequestedWithHeaderRequired = true ;
3229
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 ) {
3535 isRequestedWithHeaderRequired = mirthProperties .getBoolean ("server.api.require-requested-with" , true );
3636 }
3737
38- @ Override
39- public void init (FilterConfig filterConfig ) throws ServletException {}
38+ public static boolean isRequestedWithHeaderRequired () {
39+ return isRequestedWithHeaderRequired ;
40+ }
4041
4142 @ Override
42- public void doFilter (ServletRequest request , ServletResponse response , FilterChain chain ) throws IOException , ServletException {
43- HttpServletResponse res = (HttpServletResponse ) response ;
43+ public void filter (ContainerRequestContext requestContext ) throws IOException {
44+ if (!isRequestedWithHeaderRequired ) {
45+ return ;
46+ }
47+
48+ // If the resource method or class is annotated with DontRequireRequestedWith, skip the check
49+ if (resourceInfo != null ) {
50+ Method method = resourceInfo .getResourceMethod ();
51+ if (method != null && method .getAnnotation (DontRequireRequestedWith .class ) != null ) {
52+ return ;
53+ }
54+ Class <?> resourceClass = resourceInfo .getResourceClass ();
55+ if (resourceClass != null && resourceClass .getAnnotation (DontRequireRequestedWith .class ) != null ) {
56+ return ;
57+ }
58+ }
4459
45- HttpServletRequest servletRequest = (HttpServletRequest )request ;
46- String requestedWithHeader = (String ) servletRequest .getHeader ("X-Requested-With" );
60+ List <String > header = requestContext .getHeaders ().get ("X-Requested-With" );
4761
4862 //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" );
63+ if ( header == null || header . isEmpty () || StringUtils .isBlank (header . get ( 0 ) )) {
64+ requestContext . abortWith ( Response . status ( 400 ). entity ( "All requests must have 'X-Requested-With' header" ). build () );
5165 }
52- else {
53- chain .doFilter (request , response );
54- }
55-
5666 }
57-
58- public boolean isRequestedWithHeaderRequired () {
59- return isRequestedWithHeaderRequired ;
60- }
61-
62- @ Override
63- public void destroy () {}
64- }
67+ }
0 commit comments