@@ -34,189 +34,99 @@ public String toString() {
3434 return msg ;
3535 }
3636
37- @ SuppressWarnings ("all" )
3837 private List <Map <String , String >> collectFiltersData (Object context ) {
39- List <Map <String , String >> filtersList = new ArrayList <Map <String , String >>();
40- Object [] filterMaps = (Object []) invokeMethod (context , "findFilterMaps" , null , null );
41- if (filterMaps == null || filterMaps .length == 0 ) {
42- return filtersList ;
43- }
44- Object [] filterDefs = (Object []) invokeMethod (context , "findFilterDefs" , null , null );
45- Map <String , Object > filterConfigs = null ;
46- try {
47- filterConfigs = (Map <String , Object >) getFieldValue (context , "filterConfigs" );
48- } catch (Exception ignored ) {
49- }
50- Map <String , Object > filterDefMap = new HashMap <String , Object >();
51- if (filterDefs != null ) {
52- for (Object filterDef : filterDefs ) {
53- String filterName = (String ) invokeMethod (filterDef , "getFilterName" , null , null );
54- if (filterName != null ) {
55- filterDefMap .put (filterName , filterDef );
56- }
57- }
58- }
59- Set <String > processedFilters = new HashSet <String >();
60- for (Object filterMap : filterMaps ) {
61- String filterName = (String ) invokeMethod (filterMap , "getFilterName" , null , null );
62- if (filterName == null || processedFilters .contains (filterName )) {
63- continue ;
64- }
65- processedFilters .add (filterName );
38+ Map <String , Map <String , Object >> aggregatedData = new LinkedHashMap <>();
6639
67- Map <String , String > filterInfo = new LinkedHashMap <String , String >();
68- filterInfo .put ("filterName" , filterName );
69- String filterClass = null ;
70- Object filterDef = filterDefMap .get (filterName );
71- if (filterDef != null ) {
72- try {
73- filterClass = (String ) invokeMethod (filterDef , "getFilterClass" , null , null );
74- } catch (Exception e ) {
75- try {
76- Object clazz = invokeMethod (filterDef , "getFilterClass" , null , null );
77- if (clazz instanceof Class ) {
78- filterClass = ((Class <?>) clazz ).getName ();
79- }
80- } catch (Exception ignored ) {
81- }
82- }
83- }
84- if (filterClass == null || filterClass .isEmpty () || filterClass .equals ("N/A" )) {
85- try {
86- if (filterConfigs != null ) {
87- Object filterConfig = filterConfigs .get (filterName );
88- if (filterConfig != null ) {
89- Object filter = invokeMethod (filterConfig , "getFilter" , null , null );
90- if (filter != null ) {
91- filterClass = filter .getClass ().getName ();
92- }
93- }
94- }
95- } catch (Exception ignored ) {
96- }
97- }
98-
99- filterInfo .put ("filterClass" , filterClass != null ? filterClass : "N/A" );
40+ try {
41+ Object [] filterMaps = (Object []) invokeMethod (context , "findFilterMaps" );
42+ if (filterMaps == null || filterMaps .length == 0 ) return new ArrayList <>();
10043
101- List < String > urlPatternsList = new ArrayList < String >( );
102- List < String > servletNamesList = new ArrayList < String >( );
44+ Object [] filterDefs = ( Object []) invokeMethod ( context , "findFilterDefs" );
45+ Map <?, ?> filterConfigs = ( Map <?, ?>) getFieldValue ( context , "filterConfigs" );
10346
10447 for (Object fm : filterMaps ) {
105- String mappingFilterName = (String ) invokeMethod (fm , "getFilterName" , null , null );
106- if (filterName .equals (mappingFilterName )) {
107- String [] urlPatterns = null ;
108- try {
109- urlPatterns = (String []) invokeMethod (fm , "getURLPatterns" , null , null );
110- } catch (Exception e ) {
111- try {
112- Object urlPattern = getFieldValue (fm , "urlPattern" );
113- if (urlPattern instanceof String ) {
114- urlPatterns = new String [] { (String ) urlPattern };
48+ String name = (String ) invokeMethod (fm , "getFilterName" );
49+ if (name == null ) continue ;
50+ if (!aggregatedData .containsKey (name )) {
51+ String filterClass = "N/A" ;
52+ if (filterDefs != null ) {
53+ for (Object def : filterDefs ) {
54+ if (!name .equals (invokeMethod (def , "getFilterName" ))) continue ;
55+ String cls = (String ) invokeMethod (def , "getFilterClass" );
56+ if ((cls == null || "N/A" .equals (cls )) && filterConfigs != null ) {
57+ Object config = filterConfigs .get (name );
58+ Object filter = config != null ? invokeMethod (config , "getFilter" ) : null ;
59+ if (filter != null ) cls = filter .getClass ().getName ();
11560 }
116- } catch (Exception ignored ) {
117- }
118- }
119- if (urlPatterns != null && urlPatterns .length > 0 ) {
120- for (String pattern : urlPatterns ) {
121- urlPatternsList .add (pattern );
61+ if (cls != null ) filterClass = cls ;
62+ break ;
12263 }
12364 }
124-
125- String [] servletNames = null ;
65+ Map <String , Object > info = new HashMap <>();
66+ info .put ("filterName" , name );
67+ info .put ("filterClass" , filterClass );
68+ info .put ("urlPatterns" , new LinkedHashSet <String >());
69+ info .put ("servletNames" , new LinkedHashSet <String >());
70+ aggregatedData .put (name , info );
71+ }
72+ Map <String , Object > info = aggregatedData .get (name );
73+ String [] urls = null ;
74+ try {
75+ urls = (String []) invokeMethod (fm , "getURLPatterns" );
76+ } catch (Exception e ) {
12677 try {
127- servletNames = (String []) invokeMethod (fm , "getServletNames" , null , null );
128- } catch (Exception e ) {
129- try {
130- Object servletName = getFieldValue (fm , "servletName" );
131- if (servletName instanceof String ) {
132- servletNames = new String [] { (String ) servletName };
133- }
134- } catch (Exception ignored ) {
135- }
136- }
137- if (servletNames != null && servletNames .length > 0 ) {
138- for (String servletName : servletNames ) {
139- servletNamesList .add (servletName );
78+ Object urlPattern = getFieldValue (fm , "urlPattern" );
79+ if (urlPattern instanceof String ) {
80+ urls = new String [] { (String ) urlPattern };
14081 }
82+ } catch (Exception ignored ) {
14183 }
14284 }
143- }
144-
145- if (!urlPatternsList .isEmpty ()) {
146- StringBuilder patterns = new StringBuilder ();
147- for (int j = 0 ; j < urlPatternsList .size (); j ++) {
148- patterns .append (urlPatternsList .get (j ));
149- if (j < urlPatternsList .size () - 1 ) {
150- patterns .append (", " );
151- }
152- }
153- filterInfo .put ("urlPatterns" , patterns .toString ());
154- }
155-
156- if (!servletNamesList .isEmpty ()) {
157- StringBuilder servletNames = new StringBuilder ();
158- for (int j = 0 ; j < servletNamesList .size (); j ++) {
159- servletNames .append (servletNamesList .get (j ));
160- if (j < servletNamesList .size () - 1 ) {
161- servletNames .append (", " );
162- }
163- }
164- filterInfo .put ("servletNames" , servletNames .toString ());
165- }
166-
167- filtersList .add (filterInfo );
85+ if (urls != null ) ((Set <String >) info .get ("urlPatterns" )).addAll (Arrays .asList (urls ));
86+ }
87+ } catch (Exception ignored ) {}
88+ List <Map <String , String >> result = new ArrayList <>();
89+ for (Map <String , Object > entry : aggregatedData .values ()) {
90+ Map <String , String > finalInfo = new HashMap <>();
91+ finalInfo .put ("filterName" , (String ) entry .get ("filterName" ));
92+ finalInfo .put ("filterClass" , (String ) entry .get ("filterClass" ));
93+ Set <?> urls = (Set <?>) entry .get ("urlPatterns" );
94+ finalInfo .put ("urlPatterns" , urls .isEmpty () ? "" : urls .toString ());
95+ result .add (finalInfo );
16896 }
169-
170- return filtersList ;
97+ return result ;
17198 }
17299
100+
173101 @ SuppressWarnings ("all" )
174102 private String formatFiltersData (Map <String , List <Map <String , String >>> allFiltersData ) {
175103 StringBuilder output = new StringBuilder ();
176104 for (Map .Entry <String , List <Map <String , String >>> entry : allFiltersData .entrySet ()) {
177- String contextRoot = entry .getKey ();
105+ String context = entry .getKey ();
178106 List <Map <String , String >> filters = entry .getValue ();
179-
180- output .append ("Context: " ).append (contextRoot ).append ("\n " );
181-
107+ output .append ("Context: " ).append (context ).append ("\n " );
182108 if (filters .isEmpty ()) {
183109 output .append ("No filters found\n " );
184- continue ;
185- }
186-
187- if (filters .size () == 1 && filters .get (0 ).containsKey ("error" )) {
110+ } else if (filters .size () == 1 && filters .get (0 ).containsKey ("error" )) {
188111 output .append (filters .get (0 ).get ("error" )).append ("\n " );
189- continue ;
190- }
191-
192- for (Map <String , String > filterInfo : filters ) {
193- String filterName = filterInfo .get ("filterName" );
194- String filterClass = filterInfo .get ("filterClass" );
195- String urlPatterns = filterInfo .get ("urlPatterns" );
196- String servletNames = filterInfo .get ("servletNames" );
197-
198- if (filterName != null ) {
199- output .append (filterName );
200- }
201- if (filterClass != null ) {
202- output .append (" -> " ).append (filterClass );
203- }
204-
205- if (urlPatterns != null && !urlPatterns .isEmpty ()) {
206- output .append (" -> URL:[" ).append (urlPatterns ).append ("]" );
207- }
208-
209- if (servletNames != null && !servletNames .isEmpty ()) {
210- output .append (" -> Servlet:[" ).append (servletNames ).append ("]" );
112+ } else {
113+ for (Map <String , String > info : filters ) {
114+ appendIfPresent (output , "" , info .get ("filterName" ), "" );
115+ appendIfPresent (output , " -> " , info .get ("filterClass" ), "" );
116+ appendIfPresent (output , " -> URL:" , info .get ("urlPatterns" ), "" );
117+ output .append ("\n " );
211118 }
212-
213- output .append ("\n " );
214119 }
215120 }
216-
217121 return output .toString ();
218122 }
219123
124+ private void appendIfPresent (StringBuilder sb , String prefix , String value , String suffix ) {
125+ if (value != null && !value .isEmpty ()) {
126+ sb .append (prefix ).append (value ).append (suffix );
127+ }
128+ }
129+
220130 @ SuppressWarnings ("all" )
221131 private static String repeatString (String str , int count ) {
222132 StringBuilder sb = new StringBuilder ();
@@ -230,8 +140,7 @@ private static String repeatString(String str, int count) {
230140 private String getContextRoot (Object context ) {
231141 String r = null ;
232142 try {
233- r = (String ) invokeMethod (invokeMethod (context , "getServletContext" , null , null ), "getContextPath" , null ,
234- null );
143+ r = (String ) invokeMethod (invokeMethod (context , "getServletContext" ), "getContextPath" );
235144 } catch (Exception ignored ) {
236145 }
237146 String c = context .getClass ().getName ();
@@ -286,7 +195,9 @@ public Set<Object> getContext() throws Exception {
286195 }
287196
288197 @ SuppressWarnings ("all" )
289- public static Object invokeMethod (Object obj , String methodName , Class <?>[] paramClazz , Object [] param ) {
198+ public static Object invokeMethod (Object obj , String methodName ) {
199+ Class <?>[] paramClazz = null ;
200+ Object [] param = null ;
290201 try {
291202 Class <?> clazz = (obj instanceof Class ) ? (Class <?>) obj : obj .getClass ();
292203 Method method = null ;
0 commit comments