PageRenderTime 81ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/atlassian-pretty-urls-api/src/main/java/com/atlassian/prettyurls/api/route/UrlRouteRuleSet.java

https://bitbucket.org/atlassian/atlassian-pretty-urls
Java | 198 lines | 112 code | 26 blank | 60 comment | 0 complexity | 3ad6330607b0ae12b9a88a460a6b194d MD5 | raw file
  1. package com.atlassian.prettyurls.api.route;
  2. import com.atlassian.plugin.servlet.filter.FilterLocation;
  3. import com.google.common.base.MoreObjects;
  4. import com.google.common.collect.Lists;
  5. import com.sun.jersey.api.uri.UriTemplate;
  6. import java.util.Collections;
  7. import java.util.List;
  8. import static com.atlassian.prettyurls.api.route.UrlRouteRule.ParameterMode.PASS_UNMAPPED;
  9. import static com.google.common.base.Preconditions.checkNotNull;
  10. import static com.google.common.base.Preconditions.checkState;
  11. /**
  12. * A route rule set consists of top level meta information AND a list of rules to try and match
  13. *
  14. * @since 1.11.2
  15. */
  16. public class UrlRouteRuleSet {
  17. private final List<UrlRouteRule> urlRouteRules;
  18. private final List<String> topLevelPaths;
  19. private final UrlRouteRuleSetKey key;
  20. private final FilterLocation filterLocation;
  21. private final RoutePredicate<UrlRouteRuleSet> predicate;
  22. UrlRouteRuleSet(UrlRouteRuleSetKey key, List<String> topLevelPaths, List<UrlRouteRule> urlRouteRules, final FilterLocation filterLocation, final RoutePredicate<UrlRouteRuleSet> predicate) {
  23. this.key = checkNotNull(key);
  24. this.urlRouteRules = checkNotNull(urlRouteRules);
  25. this.topLevelPaths = checkNotNull(topLevelPaths);
  26. this.filterLocation = checkNotNull(filterLocation);
  27. this.predicate = checkNotNull(predicate);
  28. }
  29. public List<String> getTopLevelPaths() {
  30. return Lists.newArrayList(topLevelPaths);
  31. }
  32. public List<UrlRouteRule> getUrlRouteRules() {
  33. return Lists.newArrayList(urlRouteRules);
  34. }
  35. public UrlRouteRuleSetKey getKey() {
  36. return key;
  37. }
  38. public FilterLocation getFilterLocation() {
  39. return filterLocation;
  40. }
  41. public RoutePredicate<UrlRouteRuleSet> getPredicate() {
  42. return predicate;
  43. }
  44. public static class Builder {
  45. private List<UrlRouteRule> urlRouteRules;
  46. private List<String> topLevelPaths;
  47. private UrlRouteRuleSetKey key;
  48. private FilterLocation filterLocation;
  49. private RoutePredicate<UrlRouteRuleSet> predicate;
  50. public Builder() {
  51. this.urlRouteRules = Lists.newArrayList();
  52. this.topLevelPaths = Lists.newArrayList();
  53. this.filterLocation = FilterLocation.BEFORE_DISPATCH;
  54. this.predicate = RoutePredicates.alwaysTrue();
  55. }
  56. public Builder setKey(final UrlRouteRuleSetKey key) {
  57. this.key = checkNotNull(key);
  58. return this;
  59. }
  60. public Builder setLocation(FilterLocation location) {
  61. this.filterLocation = checkNotNull(location);
  62. return this;
  63. }
  64. public Builder setPredicate(RoutePredicate<UrlRouteRuleSet> predicate) {
  65. this.predicate = checkNotNull(predicate);
  66. return this;
  67. }
  68. /**
  69. * You need to add top level paths because this is used as the "quick non regex" match to see if ANY of the
  70. * rules might match. Without this the code needs to perform regex matches and that is slow and hence slow is
  71. * bad.
  72. *
  73. * Your inner rules should therefore contain one or more of the top level paths.
  74. *
  75. * @param path the top level path to match for AN of the rules to match
  76. * @return the builder
  77. */
  78. public Builder addTopLevelPath(final String path) {
  79. checkNotNull(path);
  80. topLevelPaths.add(path.startsWith("/") ? path : "/" + path);
  81. return this;
  82. }
  83. /**
  84. * Add a rule that matches a 'from' url pattern to a destination (to) url pattern.
  85. * @param from The UriTemplate that the route matches on
  86. * @param to The UriTemplate that describes the destination url
  87. */
  88. public Builder addRule(UriTemplate from, UriTemplate to) {
  89. urlRouteRules.add(new UrlRouteRule(from, to, Collections.<String>emptyList(), PASS_UNMAPPED));
  90. return this;
  91. }
  92. /**
  93. * Add a rule that matches a 'from' url pattern to a destination (to) url pattern.
  94. * @param from The UriTemplate that the route matches on
  95. * @param to The UriTemplate that describes the destination url
  96. * @param httpVerbs The http verbs that the rule applies to
  97. * @param routePredicate The route predicate that determines if the route should match
  98. * @param parameterMode The way in which query parameters are passed to the destination (to) url
  99. */
  100. public Builder addRule(UriTemplate from, UriTemplate to, List<String> httpVerbs, RoutePredicate<UrlRouteRule> routePredicate, final UrlRouteRule.ParameterMode parameterMode) {
  101. urlRouteRules.add(new UrlRouteRule(from, to, httpVerbs, parameterMode, routePredicate));
  102. return this;
  103. }
  104. /**
  105. * Add a rule that matches a 'from' url pattern to a destination (to) url pattern.
  106. * @param from The UriTemplate that the route matches on
  107. * @param toUriGenerator A function that returns the 'to' url from the 'from' uri template variables
  108. * @param httpVerbs The http verbs that the rule applies to
  109. */
  110. public Builder addRule(UriTemplate from, DestinationUrlGenerator toUriGenerator, List<String> httpVerbs) {
  111. urlRouteRules.add(new UrlRouteRule(from, toUriGenerator, httpVerbs, PASS_UNMAPPED));
  112. return this;
  113. }
  114. /**
  115. * Add a rule that matches a 'from' url pattern to a destination (to) url pattern.
  116. * @param from The UriTemplate that the route matches on
  117. * @param toUriGenerator A function that returns the 'to' url from the 'from' uri template variables
  118. * @param httpVerbs The http verbs that the rule applies to
  119. * @param routePredicate The route predicate that determines if the route should match
  120. * @param parameterMode The way in which query parameters are passed to the destination (to) url
  121. */
  122. public Builder addRule(UriTemplate from, DestinationUrlGenerator toUriGenerator, List<String> httpVerbs, RoutePredicate<UrlRouteRule> routePredicate, final UrlRouteRule.ParameterMode parameterMode) {
  123. urlRouteRules.add(new UrlRouteRule(from, toUriGenerator, httpVerbs, parameterMode, routePredicate));
  124. return this;
  125. }
  126. /**
  127. * Add a rule that matches a 'from' url pattern to a destination (to) url pattern.
  128. * @param from The UriTemplate that the route matches on
  129. * @param to The UriTemplate that describes the destination url
  130. */
  131. public Builder addRule(String from, String to) {
  132. urlRouteRules.add(new UrlRouteRule(new UriTemplate(from), new UriTemplate(to), Collections.<String>emptyList(), PASS_UNMAPPED));
  133. return this;
  134. }
  135. /**
  136. * Add a rule that matches a 'from' url pattern to a destination (to) url pattern.
  137. * @param from The UriTemplate that the route matches on
  138. * @param to The UriTemplate that describes the destination url
  139. * @param httpVerbs The http verbs that the rule applies to
  140. */
  141. public Builder addRule(String from, String to, List<String> httpVerbs) {
  142. urlRouteRules.add(new UrlRouteRule(new UriTemplate(from), new UriTemplate(to), httpVerbs, PASS_UNMAPPED));
  143. return this;
  144. }
  145. /**
  146. * Add a rule that matches a 'from' url pattern to a destination (to) url pattern.
  147. * @param from The UriTemplate that the route matches on
  148. * @param to The UriTemplate that describes the destination url
  149. * @param httpVerbs The http verbs that the rule applies to
  150. * @param routePredicate The route predicate that determines if the route should match
  151. */
  152. public Builder addRule(String from, String to, List<String> httpVerbs, RoutePredicate routePredicate) {
  153. urlRouteRules.add(new UrlRouteRule(new UriTemplate(from), new UriTemplate(to), httpVerbs, PASS_UNMAPPED, routePredicate));
  154. return this;
  155. }
  156. public UrlRouteRuleSet build() {
  157. checkNotNull(key, "You must provide a key to identify your rule set");
  158. checkState(!topLevelPaths.isEmpty(), "You must provide at least one top level path");
  159. return new UrlRouteRuleSet(key, topLevelPaths, urlRouteRules, filterLocation, predicate);
  160. }
  161. }
  162. @Override
  163. public String toString() {
  164. return MoreObjects.toStringHelper(this)
  165. .add("key", key)
  166. .add("filterLocation", filterLocation)
  167. .add("topLevelPaths", topLevelPaths)
  168. .add("urlRouteRules", urlRouteRules)
  169. .add("predicate", predicate)
  170. .toString();
  171. }
  172. }