1010 * @author ReaJason
1111 */
1212public class GlassFishFilterProbe {
13+
1314 @ Override
1415 public String toString () {
15- String msg = "" ;
16+ StringBuilder msg = new StringBuilder () ;
1617 Map <String , List <Map <String , String >>> allFiltersData = new LinkedHashMap <String , List <Map <String , String >>>();
1718 Set <Object > contexts = null ;
1819 try {
1920 contexts = getContext ();
2021 } catch (Throwable throwable ) {
21- msg += "context error: " + getErrorMessage (throwable );
22+ msg . append ( "context error: " ). append ( getErrorMessage (throwable ) );
2223 }
2324 if (contexts == null || contexts .isEmpty ()) {
24- msg += "context not found\n " ;
25+ msg . append ( "context not found\n " ) ;
2526 } else {
2627 for (Object context : contexts ) {
2728 String contextRoot = getContextRoot (context );
28- List <Map <String , String >> filters = collectFiltersData (context );
29- 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+ }
3035 }
31- msg += formatFiltersData (allFiltersData );
36+ msg . append ( formatFiltersData (allFiltersData ) );
3237 }
33- return msg ;
38+ return msg . toString () ;
3439 }
3540
36- private List <Map <String , String >> collectFiltersData (Object context ) {
41+ @ SuppressWarnings ("unchecked" )
42+ private List <Map <String , String >> collectFiltersData (Object context ) throws Exception {
3743 Map <String , Map <String , Object >> aggregatedData = new LinkedHashMap <>();
3844
39- try {
40- List filterMaps = (List ) invokeMethod (context , "findFilterMaps" );
41- if (filterMaps == null || filterMaps .isEmpty ()) return Collections .emptyList ();
42-
43- Object [] filterDefs = (Object []) invokeMethod (context , "findFilterDefs" );
44- for (Object fm : filterMaps ) {
45- String name = (String ) invokeMethod (fm , "getFilterName" );
46- if (name == null ) continue ;
47- if (!aggregatedData .containsKey (name )) {
48- String filterClass = "N/A" ;
49- if (filterDefs != null ) {
50- for (Object def : filterDefs ) {
51- if (!name .equals (invokeMethod (def , "getFilterName" ))) continue ;
52- Class <?> cls = (Class <?>) invokeMethod (def , "getFilterClass" );
53- if (cls == null ) {
54- Object config = invokeMethod (context , "findFilterConfig" , new Class []{String .class }, new Object []{name });
55- Object filter = config != null ? invokeMethod (config , "getFilter" ) : null ;
56- if (filter != null ) filterClass = filter .getClass ().getName ();
57- }
58- if (cls != null ) filterClass = cls .getName ();
59- break ;
60- }
61- }
62- Map <String , Object > info = new HashMap <>();
63- info .put ("filterName" , name );
64- info .put ("filterClass" , filterClass );
65- info .put ("urlPatterns" , new LinkedHashSet <String >());
66- info .put ("servletNames" , new LinkedHashSet <String >());
67- aggregatedData .put (name , info );
68- }
69- Map <String , Object > info = aggregatedData .get (name );
70- String [] urls = null ;
71- try {
72- urls = (String []) invokeMethod (fm , "getURLPatterns" );
73- } catch (Exception e ) {
45+ List filterMaps = (List ) invokeMethod (context , "findFilterMaps" );
46+ if (filterMaps == null || filterMaps .isEmpty ()) return Collections .emptyList ();
47+
48+ Object [] filterDefs = (Object []) invokeMethod (context , "findFilterDefs" );
49+
50+ for (Object fm : filterMaps ) {
51+ String name = (String ) invokeMethod (fm , "getFilterName" );
52+ if (name == null ) continue ;
53+ if (!aggregatedData .containsKey (name )) {
54+ String filterClass = "N/A" ;
55+ if (filterDefs != null ) {
56+ Object filterDef = invokeMethod (context , "findFilterDef" , new Class []{String .class }, new Object []{name });
7457 try {
75- Object urlPattern = invokeMethod (fm , "getURLPattern" );
76- if (urlPattern instanceof String ) {
77- urls = new String [] { (String ) urlPattern };
78- }
79- } catch (Exception ignored ) {
58+ filterClass = (String ) invokeMethod (filterDef , "getFilterClass" );
59+ } catch (Throwable throwable ) {
60+ filterClass = (String ) invokeMethod (filterDef , "getFilterClassName" );
8061 }
62+ if (filterClass == null ) {
63+ Object filterConfig = invokeMethod (context , "findFilterConfig" , new Class []{String .class }, new Object []{name });
64+ Object filter = invokeMethod (filterConfig , "getFilter" );
65+ if (filter != null ) filterClass = filter .getClass ().getName ();
66+ }
67+ }
68+ Map <String , Object > info = new HashMap <>();
69+ info .put ("filterName" , name );
70+ info .put ("filterClass" , filterClass );
71+ info .put ("urlPatterns" , new LinkedHashSet <String >());
72+ info .put ("servletNames" , new LinkedHashSet <String >());
73+ aggregatedData .put (name , info );
74+ }
75+ Map <String , Object > info = aggregatedData .get (name );
76+ String [] urls = null ;
77+ try {
78+ urls = (String []) invokeMethod (fm , "getURLPatterns" );
79+ } catch (Exception e ) {
80+ // Tomcat 5
81+ String urlPattern = (String ) invokeMethod (fm , "getURLPattern" );
82+ if (urlPattern != null ) {
83+ urls = new String []{urlPattern };
8184 }
82- if (urls != null ) ((Set <String >) info .get ("urlPatterns" )).addAll (Arrays .asList (urls ));
8385 }
84- } catch (Exception ignored ) {}
86+ if (urls != null ) ((Set <String >) info .get ("urlPatterns" )).addAll (Arrays .asList (urls ));
87+ String [] servletNames = null ;
88+ try {
89+ servletNames = (String []) invokeMethod (fm , "getServletNames" );
90+ } catch (Exception e ) {
91+ // Tomcat 5
92+ String servletName = (String ) invokeMethod (fm , "getServletName" );
93+ if (servletName != null ) {
94+ servletNames = new String []{servletName };
95+ }
96+ }
97+ if (servletNames != null ) ((Set <String >) info .get ("servletNames" )).addAll (Arrays .asList (servletNames ));
98+ }
8599 List <Map <String , String >> result = new ArrayList <>();
86100 for (Map <String , Object > entry : aggregatedData .values ()) {
87101 Map <String , String > finalInfo = new HashMap <>();
88102 finalInfo .put ("filterName" , (String ) entry .get ("filterName" ));
89103 finalInfo .put ("filterClass" , (String ) entry .get ("filterClass" ));
90104 Set <?> urls = (Set <?>) entry .get ("urlPatterns" );
91105 finalInfo .put ("urlPatterns" , urls .isEmpty () ? "" : urls .toString ());
106+ Set <?> servletNames = (Set <?>) entry .get ("servletNames" );
107+ finalInfo .put ("servletNames" , servletNames .isEmpty () ? "" : servletNames .toString ());
92108 result .add (finalInfo );
93109 }
94110 return result ;
@@ -111,6 +127,7 @@ private String formatFiltersData(Map<String, List<Map<String, String>>> allFilte
111127 appendIfPresent (output , "" , info .get ("filterName" ), "" );
112128 appendIfPresent (output , " -> " , info .get ("filterClass" ), "" );
113129 appendIfPresent (output , " -> URL:" , info .get ("urlPatterns" ), "" );
130+ appendIfPresent (output , " -> Servlet:" , info .get ("servletNames" ), "" );
114131 output .append ("\n " );
115132 }
116133 }
0 commit comments