View Javadoc
1   package cn.home1.oss.boot.autoconfigure;
2   
3   import static org.apache.commons.lang3.StringUtils.isNotBlank;
4   
5   import org.springframework.util.AntPathMatcher;
6   
7   import java.util.function.Predicate;
8   
9   /**
10   * Created by zhanghaolun on 16/11/3.
11   */
12  public abstract class PathUtils {
13  
14    private PathUtils() {
15    }
16  
17    /**
18     * Any path satisfies this condition.
19     *
20     * @return predicate that is always true
21     */
22    public static Predicate<String> any() {
23      return t -> true;
24    }
25  
26    /**
27     * No path satisfies this condition.
28     *
29     * @return predicate that is always false
30     */
31    public static Predicate<String> none() {
32      return t -> false;
33    }
34  
35    /**
36     * Predicate that evaluates the supplied regular expression.
37     *
38     * @param pathRegex - regex
39     * @return predicate that matches a particular regex
40     */
41    public static Predicate<String> regex(final String pathRegex) {
42      return input -> input.matches(pathRegex);
43    }
44  
45    /**
46     * Predicate that evaluates the supplied ant pattern.
47     *
48     * @param antPattern - ant Pattern
49     * @return predicate that matches a particular ant pattern
50     */
51    public static Predicate<String> ant(final String antPattern) {
52      return input -> {
53        AntPathMatcher matcher = new AntPathMatcher();
54        return matcher.match(antPattern, input);
55      };
56    }
57  
58    public static Predicate<String> managementPaths(final String managementContextPath) {
59      final Predicate<String> result;
60      if (isNotBlank(managementContextPath)) {
61        result = regex(managementContextPath + "/.*") //
62          .or(ant(managementContextPath)) //
63          .or(ant(managementContextPath + "" + ".json")) //
64          .or(regex("/env(\\..+|/.*)?")) // environment-manager-mvc-endpoint@org.springframework.cloud.context
65          .or(regex("/restart(\\..+|/.*)?")) // restart-mvc-endpoint@org.springframework.cloud.context
66        ;
67      } else {
68        result = regex("/archaius(\\..+|/.*)?") // endpoint-mvc-adapter
69          .or(regex("/autoconfig(\\..+|/.*)?")) //
70          .or(regex("/beans(\\..+|/.*)?"))
71          .or(regex("/configprops(\\..+|/.*)?"))
72          .or(regex("/dump(\\..+|/.*)?"))
73          .or(regex("/features(\\..+|/.*)?"))
74          .or(regex("/info(\\..+|/.*)?"))
75          .or(regex("/mappings(\\..+|/.*)?"))
76          .or(regex("/trace(\\..+|/.*)?"))
77          .or(regex("/env(\\..+|/.*)?")) // environment-mvc-endpoint,
78          // environment-manager-mvc-endpoint@org.springframework.cloud.context
79          .or(regex("/pause(\\..+|/.*)?")) // generic-postable-mvc-endpoint
80          .or(regex("/refresh(\\..+|/.*)?"))
81          .or(regex("/resume(\\..+|/.*)?"))
82          .or(regex("/actuator(\\..+|/.*)?")) // hal-json-mvc-endpoint
83          .or(regex("/health(\\..+|/.*)?")) // health-mvc-endpoint
84          .or(regex("/heapdump(\\..+|/.*)?")) // heapdump-mvc-endpoint
85          .or(regex("/jolokia(\\..+|/.*)?")) // jolokia-mvc-endpoint
86          .or(regex("/logfile(\\..+|/.*)?")) // log-file-mvc-endpoint
87          .or(regex("/metrics(\\..+|/.*)?")) // metrics-mvc-endpoint
88          .or(regex("/restart(\\..+|/.*)?")) // restart-mvc-endpoint@org.springframework.cloud.context
89          .or(regex("/shutdown(\\..+|/.*)?")) // shutdown-mvc-endpoint@org.springframework.boot.actuate
90        ;
91      }
92      return result;
93    }
94  
95    public static Boolean isManagementPath(final String managementContextPath, final String servletPath) {
96      return managementPaths(managementContextPath).test(servletPath);
97    }
98  }