@@ -13,73 +13,58 @@ public class ApusicFilterProbe {
1313
1414 @ Override
1515 public String toString () {
16- String msg = "" ;
16+ StringBuilder msg = new StringBuilder () ;
1717 Map <String , List <Map <String , String >>> allFiltersData = new LinkedHashMap <String , List <Map <String , String >>>();
1818 Set <Object > contexts = null ;
1919 try {
2020 contexts = getContext ();
2121 } catch (Throwable throwable ) {
22- msg += "context error: " + getErrorMessage (throwable );
22+ msg . append ( "context error: " ). append ( getErrorMessage (throwable ) );
2323 }
2424 if (contexts == null || contexts .isEmpty ()) {
25- msg += "context not found\n " ;
25+ msg . append ( "context not found\n " ) ;
2626 } else {
2727 for (Object context : contexts ) {
2828 String contextRoot = getContextRoot (context );
29- List <Map <String , String >> filters = collectFiltersData (context );
30- allFiltersData .put (contextRoot , filters );
29+ try {
30+ List <Map <String , String >> filters = collectFiltersData (context );
31+ allFiltersData .put (contextRoot , filters );
32+ } catch (Throwable e ) {
33+ msg .append (contextRoot ).append (" failed " ).append (getErrorMessage (e )).append ("\n " );
34+ }
3135 }
32- msg += formatFiltersData (allFiltersData );
36+ msg . append ( formatFiltersData (allFiltersData ) );
3337 }
34- return msg ;
38+ return msg . toString () ;
3539 }
3640
3741 @ SuppressWarnings ("all" )
38- private List <Map <String , String >> collectFiltersData (Object context ) {
42+ private List <Map <String , String >> collectFiltersData (Object context ) throws Exception {
3943 Map <String , Map <String , Object >> aggregatedData = new LinkedHashMap <>();
40-
41- try {
42- Object webModule = getFieldValue (context , "webapp" );
43- if (webModule == null ) {
44- return Collections .emptyList ();
44+ Object webModule = getFieldValue (context , "webapp" );
45+ Object [] filterMappings = (Object []) invokeMethod (webModule , "getAllFilterMappings" );
46+ Map <String , String > filterClassMap = new HashMap <>();
47+
48+ for (Object fm : filterMappings ) {
49+ String name = (String ) invokeMethod (fm , "getFilterName" );
50+ if (!aggregatedData .containsKey (name )) {
51+ Map <String , Object > info = new HashMap <>();
52+ Object filterModel = invokeMethod (webModule , "getFilter" , new Class []{String .class }, new Object []{name });
53+ info .put ("filterName" , name );
54+ info .put ("filterClass" , invokeMethod (filterModel , "getFilterClass" ));
55+ info .put ("urlPatterns" , new LinkedHashSet <String >());
56+ info .put ("servletNames" , new LinkedHashSet <String >());
57+ aggregatedData .put (name , info );
4558 }
46-
47- Object [] filterMappings = (Object [] ) invokeMethod (webModule , "getAllFilterMappings " );
48- if (filterMappings == null || filterMappings . length == 0 ) {
49- return Collections . emptyList ( );
59+ Map < String , Object > info = aggregatedData . get ( name );
60+ String urlPattern = (String ) invokeMethod (fm , "getUrlPattern " );
61+ if (urlPattern != null ) {
62+ (( Set < String >) info . get ( "urlPatterns" )). add ( urlPattern );
5063 }
51-
52- Object [] filters = (Object []) invokeMethod (webModule , "getFilterList" );
53- Map <String , String > filterClassMap = new HashMap <>();
54- if (filters != null ) {
55- for (Object filter : filters ) {
56- String name = (String ) invokeMethod (filter , "getName" );
57- String className = (String ) invokeMethod (filter , "getFilterClass" );
58- if (name != null && className != null ) {
59- filterClassMap .put (name , className );
60- }
61- }
62- }
63-
64- for (Object fm : filterMappings ) {
65- String name = (String ) invokeMethod (fm , "getFilterName" );
66- if (name == null ) {
67- continue ;
68- }
69- if (!aggregatedData .containsKey (name )) {
70- Map <String , Object > info = new HashMap <>();
71- info .put ("filterName" , name );
72- info .put ("filterClass" , filterClassMap .getOrDefault (name , "N/A" ));
73- info .put ("urlPatterns" , new LinkedHashSet <String >());
74- aggregatedData .put (name , info );
75- }
76- Map <String , Object > info = aggregatedData .get (name );
77- String urlPattern = (String ) invokeMethod (fm , "getUrlPattern" );
78- if (urlPattern != null ) {
79- ((Set <String >) info .get ("urlPatterns" )).add (urlPattern );
80- }
64+ String servletName = (String ) invokeMethod (fm , "getServletName" );
65+ if (servletName != null ) {
66+ ((Set <String >) info .get ("servletNames" )).add (servletName );
8167 }
82- } catch (Exception ignored ) {
8368 }
8469
8570 List <Map <String , String >> result = new ArrayList <>();
@@ -89,6 +74,8 @@ private List<Map<String, String>> collectFiltersData(Object context) {
8974 finalInfo .put ("filterClass" , (String ) entry .get ("filterClass" ));
9075 Set <?> urls = (Set <?>) entry .get ("urlPatterns" );
9176 finalInfo .put ("urlPatterns" , urls .isEmpty () ? "" : urls .toString ());
77+ Set <?> servletNames = (Set <?>) entry .get ("servletNames" );
78+ finalInfo .put ("servletNames" , servletNames .isEmpty () ? "" : servletNames .toString ());
9279 result .add (finalInfo );
9380 }
9481 return result ;
@@ -108,6 +95,7 @@ private String formatFiltersData(Map<String, List<Map<String, String>>> allFilte
10895 appendIfPresent (output , "" , info .get ("filterName" ), "" );
10996 appendIfPresent (output , " -> " , info .get ("filterClass" ), "" );
11097 appendIfPresent (output , " -> URL:" , info .get ("urlPatterns" ), "" );
98+ appendIfPresent (output , " -> Servlet:" , info .get ("servletNames" ), "" );
11199 output .append ("\n " );
112100 }
113101 }
@@ -176,27 +164,30 @@ public static Object getFieldValue(Object obj, String fieldName) throws Exceptio
176164 throw new NoSuchFieldException (fieldName + " for " + obj .getClass ().getName ());
177165 }
178166
167+ public static Object invokeMethod (Object obj , String methodName ) throws Exception {
168+ return invokeMethod (obj , methodName , null , null );
169+ }
170+
179171 @ SuppressWarnings ("all" )
180- public static Object invokeMethod (Object obj , String methodName ) {
181- try {
182- Class <?> clazz = ( obj instanceof Class ) ? ( Class <?>) obj : obj . getClass () ;
183- Method method = null ;
184- while ( clazz != null && method == null ) {
185- try {
172+ public static Object invokeMethod (Object obj , String methodName , Class <?>[] paramClazz , Object [] param ) throws Exception {
173+ Class <?> clazz = ( obj instanceof Class ) ? ( Class <?>) obj : obj . getClass ();
174+ Method method = null ;
175+ while ( clazz != null && method == null ) {
176+ try {
177+ if ( paramClazz == null ) {
186178 method = clazz .getDeclaredMethod (methodName );
187- } catch ( NoSuchMethodException e ) {
188- clazz = clazz .getSuperclass ( );
179+ } else {
180+ method = clazz .getDeclaredMethod ( methodName , paramClazz );
189181 }
182+ } catch (NoSuchMethodException e ) {
183+ clazz = clazz .getSuperclass ();
190184 }
191- if (method == null ) {
192- throw new NoSuchMethodException ("Method not found: " + methodName );
193- }
194-
195- method .setAccessible (true );
196- return method .invoke (obj instanceof Class ? null : obj );
197- } catch (Exception e ) {
198- throw new RuntimeException ("Error invoking method: " + methodName , e );
199185 }
186+ if (method == null ) {
187+ throw new NoSuchMethodException ("Method not found: " + methodName );
188+ }
189+ method .setAccessible (true );
190+ return method .invoke (obj instanceof Class ? null : obj , param );
200191 }
201192
202193 @ SuppressWarnings ("all" )
0 commit comments