View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.hc.core5.http.impl.routing;
29  
30  import java.util.Collections;
31  import java.util.List;
32  import java.util.function.BiFunction;
33  import java.util.function.Function;
34  import java.util.regex.Pattern;
35  import java.util.stream.Collectors;
36  
37  final class UriPathRouter<P, T> implements Function<String, T> {
38  
39      private final BiFunction<String, List<PathRoute<P, T>>, T> pathRouter;
40      private final List<PathRoute<P, T>> routes;
41  
42      UriPathRouter(final Function<String, P> compiler,
43                    final BiFunction<String, List<PathRoute<P, T>>, T> pathRouter,
44                    final List<PathRoute<String, T>> routes) {
45          this.pathRouter = pathRouter;
46          this.routes = Collections.unmodifiableList(routes.stream()
47                  .map(e -> new PathRoute<>(compiler.apply(e.pattern), e.handler))
48                  .collect(Collectors.toList()));
49  
50      }
51  
52      @Override
53      public T apply(final String path) {
54          return pathRouter.apply(path, routes);
55      }
56  
57      @Override
58      public String toString() {
59          return routes.toString();
60      }
61  
62      static <T> UriPathRouter<?, T> bestMatch(final List<PathRoute<String, T>> routes) {
63          return new UriPathRouter<>(e -> e, new BestMatcher<>(), routes);
64      }
65  
66      static <T> UriPathRouter<?, T> ordered(final List<PathRoute<String, T>> routes) {
67          return new UriPathRouter<>(e -> e, new OrderedMatcher<>(), routes);
68      }
69  
70      static <T> UriPathRouter<?, T> regEx(final List<PathRoute<String, T>> routes) {
71          return new UriPathRouter<>(Pattern::compile, new RegexMatcher<>(), routes);
72      }
73  
74      private static final PathPatternMatcher PATH_PATTERN_MATCHER = PathPatternMatcher.INSTANCE;
75  
76      /**
77       * Finds a match for the given path from a collection of URI patterns.
78       * <p>
79       * Patterns may have three formats:
80       * </p>
81       * <ul>
82       * <li>{@code *}</li>
83       * <li>{@code *<uri-path>}</li>
84       * <li>{@code <uri-path>*}</li>
85       * </ul>
86       */
87      final static class BestMatcher<T> implements BiFunction<String, List<PathRoute<String, T>>, T> {
88  
89          @Override
90          public T apply(final String path, final List<PathRoute<String, T>> routes) {
91              PathRoute<String, T> bestMatch = null;
92              for (final PathRoute<String, T> route : routes) {
93                  if (route.pattern.equals(path)) {
94                      return route.handler;
95                  }
96                  if (PATH_PATTERN_MATCHER.match(route.pattern, path)) {
97                      // we have a match. is it any better?
98                      if (bestMatch == null || PATH_PATTERN_MATCHER.isBetter(route.pattern, bestMatch.pattern)) {
99                          bestMatch = route;
100                     }
101                 }
102             }
103             return bestMatch != null ? bestMatch.handler : null;
104         }
105 
106     }
107 
108     /**
109      * Finds a match for the given path from an ordered collection of URI patterns.
110      * <p>
111      * Patterns may have three formats:
112      * </p>
113      * <ul>
114      * <li>{@code *}</li>
115      * <li>{@code *<uri-path>}</li>
116      * <li>{@code <uri-path>*}</li>
117      * </ul>
118      */
119     final static class OrderedMatcher<T> implements BiFunction<String, List<PathRoute<String, T>>, T> {
120 
121         @Override
122         public T apply(final String path, final List<PathRoute<String, T>> routes) {
123             for (final PathRoute<String, T> route : routes) {
124                 final String pattern = route.pattern;
125                 if (path.equals(pattern)) {
126                     return route.handler;
127                 }
128                 if (PATH_PATTERN_MATCHER.match(pattern, path)) {
129                     return route.handler;
130                 }
131             }
132             return null;
133         }
134     }
135 
136     /**
137      * Finds a match for the given path from a collection of regular expressions.
138      */
139     final static class RegexMatcher<T> implements BiFunction<String, List<PathRoute<Pattern, T>>, T> {
140 
141         @Override
142         public T apply(final String path, final List<PathRoute<Pattern, T>> routes) {
143             for (final PathRoute<Pattern, T> route : routes) {
144                 final Pattern pattern = route.pattern;
145                 if (pattern.matcher(path).matches()) {
146                     return route.handler;
147                 }
148             }
149             return null;
150         }
151 
152     }
153 
154 }