@@ -46,41 +46,51 @@ protected String generateFilter(Map<String, Object> filterMap, Options options)
4646 DispatcherFilter filter = new DispatcherFilter (filterMap );
4747
4848 StringBuilder sb = new StringBuilder ()
49- .append ("{ " )
50- .append ("/type \" " ).append (filter .getType ()).append ("\" " );
49+ .append ("{ " );
5150
52- if (StringUtils .isNotEmpty (filter .getMethod ())) {
53- sb .append ("/method '" ).append (filter .getMethod ()).append ("' " );
54- }
55- if (StringUtils .isNotEmpty (filter .getUrl ())) {
56- sb .append ("/url '" ).append (filter .getUrl ()).append ("' " );
57- }
58- if (StringUtils .isNotEmpty (filter .getQuery ())) {
59- sb .append ("/query '" ).append (filter .getQuery ()).append ("' " );
60- }
61- if (StringUtils .isNotEmpty (filter .getProtocol ())) {
62- sb .append ("/protocol '" ).append (filter .getProtocol ()).append ("' " );
63- }
51+ applySimpleValue (sb , "type" , filter .getType ().toString ());
6452
65- if (StringUtils .isNotEmpty (filter .getPath ())) {
66- sb .append ("/path '" ).append (filter .getPath ()).append ("' " );
67- }
68- if (StringUtils .isNotEmpty (filter .getSelectors ())) {
69- sb .append ("/selectors '" ).append (filter .getSelectors ()).append ("' " );
70- }
71- if (StringUtils .isNotEmpty (filter .getExtension ())) {
72- sb .append ("/extension '" ).append (filter .getExtension ()).append ("' " );
73- }
74- if (StringUtils .isNotEmpty (filter .getSuffix ())) {
75- sb .append ("/suffix '" ).append (filter .getSuffix ()).append ("' " );
76- }
53+ applyRegexValue (sb , "method" , filter .getMethod ());
54+ applyRegexValue (sb , "url" , filter .getUrl ());
55+ applyRegexValue (sb , "query" , filter .getQuery ());
56+ applyRegexValue (sb , "protocol" , filter .getProtocol ());
7757
78- if (StringUtils .isNotEmpty (filter .getGlob ())) {
79- sb .append ("/glob '" ).append (filter .getGlob ()).append ("' " );
80- }
58+ applyRegexValue (sb , "path" , filter .getPath ());
59+ applyRegexValueEmptyStringAllowed (sb , "selectors" , filter .getSelectors ());
60+ applyRegexValueEmptyStringAllowed (sb , "extension" , filter .getExtension ());
61+ applyRegexValueEmptyStringAllowed (sb , "suffix" , filter .getSuffix ());
62+
63+ applyRegexValue (sb , "glob" , filter .getGlob ());
8164
8265 sb .append ("}" );
8366 return sb .toString ();
8467 }
8568
69+ /**
70+ * Append filter parameter as simple fixed string (enclosed in "").
71+ */
72+ private void applySimpleValue (StringBuilder sb , String key , String value ) {
73+ if (StringUtils .isNotEmpty (value )) {
74+ sb .append ("/" ).append (key ).append (" \" " ).append (value ).append ("\" " );
75+ }
76+ }
77+
78+ /**
79+ * Append filter parameter as string that may be a regex (enclosed in ''). Empty string are ignored.
80+ */
81+ private void applyRegexValue (StringBuilder sb , String key , String value ) {
82+ if (StringUtils .isNotEmpty (value )) {
83+ sb .append ("/" ).append (key ).append (" '" ).append (value ).append ("' " );
84+ }
85+ }
86+
87+ /**
88+ * Append filter parameter as string that may be a regex (enclosed in ''). Empty strings are left as-is.
89+ */
90+ private void applyRegexValueEmptyStringAllowed (StringBuilder sb , String key , String value ) {
91+ if (value != null ) {
92+ sb .append ("/" ).append (key ).append (" '" ).append (value ).append ("' " );
93+ }
94+ }
95+
8696}
0 commit comments