3333import org .jooby .internal .RouteImpl ;
3434import org .jooby .internal .RouteMatcher ;
3535import org .jooby .internal .RoutePattern ;
36+ import org .jooby .util .Collectors ;
3637
3738import com .google .common .base .Strings ;
3839import com .google .common .collect .ImmutableList ;
165166 * });
166167 * </pre>
167168 *
168- * <h2>External route </h2>
169+ * <h2>Mvc Route </h2>
169170 * <p>
170- * An external route can be defined by using a {@link Class route class}, like :
171+ * A Mvc Route use annotations to define routes :
171172 * </p>
172173 *
173174 * <pre>
174- * get("/", handler(ExternalRoute.class)); //or
175- *
176- * ...
177- * // ExternalRoute.java
178- * public class ExternalRoute implements Route.Handler {
179- * public void handle(Request req, Response rsp) throws Exception {
180- * rsp.send("Hello Jooby");
181- * }
182- * }
175+ * {
176+ * use(MyRoute.class);
177+ * }
183178 * </pre>
184179 *
185- * <h2>Mvc Route</h2>
186- * <p>
187- * A Mvc Route use annotations to define routes:
188- * </p>
189- *
180+ * MyRoute.java:
190181 * <pre>
191- * route(MyRoute.class);
192- * ...
193- * // MyRoute.java
194182 * {@literal @}Path("/")
195183 * public class MyRoute {
196184 *
@@ -759,6 +747,8 @@ class Definition {
759747 */
760748 private String pattern ;
761749
750+ private List <RoutePattern > excludes = Collections .emptyList ();
751+
762752 /**
763753 * Creates a new route definition.
764754 *
@@ -871,7 +861,11 @@ public List<String> vars() {
871861 public Optional <Route > matches (final String verb ,
872862 final String path , final MediaType contentType ,
873863 final List <MediaType > accept ) {
874- RouteMatcher matcher = compiledPattern .matcher (verb .toUpperCase () + path );
864+ String fpath = verb .toUpperCase () + path ;
865+ if (excludes (fpath )) {
866+ return Optional .empty ();
867+ }
868+ RouteMatcher matcher = compiledPattern .matcher (fpath );
875869 if (matcher .matches ()) {
876870 List <MediaType > result = MediaType .matcher (accept ).filter (this .produces );
877871 if (result .size () > 0 && canConsume (contentType )) {
@@ -1032,6 +1026,36 @@ public Definition produces(final List<MediaType> produces) {
10321026 return this ;
10331027 }
10341028
1029+ /**
1030+ * Excludes one or more path pattern from this route, useful for filter:
1031+ *
1032+ * <pre>
1033+ * {
1034+ * use("*", req {@literal ->} {
1035+ * ...
1036+ * }).excludes("/logout");
1037+ * }
1038+ * </pre>
1039+ *
1040+ * @param excludes A path pattern.
1041+ * @return This route definition.
1042+ */
1043+ public Definition excludes (final String ... excludes ) {
1044+ this .excludes = Arrays .asList (excludes ).stream ()
1045+ .map (it -> new RoutePattern (method , it ))
1046+ .collect (Collectors .toList ());
1047+ return this ;
1048+ }
1049+
1050+ private boolean excludes (final String path ) {
1051+ for (RoutePattern pattern : excludes ) {
1052+ if (pattern .matcher (path ).matches ()) {
1053+ return true ;
1054+ }
1055+ }
1056+ return false ;
1057+ }
1058+
10351059 /**
10361060 * @return All the types this route can consumes.
10371061 */
@@ -1051,7 +1075,8 @@ public String toString() {
10511075 StringBuilder buffer = new StringBuilder ();
10521076 buffer .append (method ()).append (" " ).append (pattern ()).append ("\n " );
10531077 buffer .append (" name: " ).append (name ()).append ("\n " );
1054- buffer .append (" consume: " ).append (consumes ()).append ("\n " );
1078+ buffer .append (" excludes: " ).append (excludes ).append ("\n " );
1079+ buffer .append (" consumes: " ).append (consumes ()).append ("\n " );
10551080 buffer .append (" produces: " ).append (produces ()).append ("\n " );
10561081 return buffer .toString ();
10571082 }
0 commit comments