Skip to content

Instantly share code, notes, and snippets.

@decebals
Last active August 29, 2015 14:26
Show Gist options
  • Save decebals/56759f2074031a56f76f to your computer and use it in GitHub Desktop.
Save decebals/56759f2074031a56f76f to your computer and use it in GitHub Desktop.
An utility class that help me to create complex regex paths using only includes and excludes.
package ro.fortsoft.matilda.util;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @author Decebal Suiu
*/
public class PathRegexBuilder {
private List<String> includes;
private List<String> excludes;
public PathRegexBuilder() {
includes = new ArrayList<>();
excludes = new ArrayList<>();
}
public PathRegexBuilder includes(String... includes) {
this.includes.addAll(Arrays.asList(includes));
return this;
}
public PathRegexBuilder excludes(String... excludes) {
this.excludes.addAll(Arrays.asList(excludes));
return this;
}
public String build() {
StringBuilder regex = new StringBuilder();
// add includes
StringBuilder regexIncludes = new StringBuilder();
if (!includes.isEmpty()) {
regexIncludes.append('^'); // the beginning of a line
regexIncludes.append('(');
for (String include : includes) {
regexIncludes.append(include);
regexIncludes.append('|'); // or
}
regexIncludes.deleteCharAt(regexIncludes.length() - 1);
regexIncludes.append(").*");
}
if (!excludes.isEmpty()) {
}
// add excludes
StringBuilder regexExcludes = new StringBuilder();
if (!excludes.isEmpty()) {
regexExcludes.append('^'); // the beginning of a line
regexExcludes.append("(?!"); // zero-width negative lookahead
for (String exclude : excludes) {
regexExcludes.append(exclude);
regexExcludes.append('|'); // or
}
regexExcludes.deleteCharAt(regexExcludes.length() - 1);
regexExcludes.append(").+");
}
if (regexIncludes.length() == 0) {
regex.append(regexExcludes);
} else {
if (regexExcludes.length() == 0) {
regex.append(regexIncludes);
} else {
regex.append("(?=");
regex.append(regexIncludes);
regex.append(')');
regex.append(regexExcludes);
}
}
return regex.toString();
}
@Override
public String toString() {
return "RegexBuilder{" +
"includes=" + includes +
", excludes=" + excludes +
'}';
}
}
@decebals
Copy link
Author

decebals commented Aug 5, 2015

How to use (snippet from my PIppoApplication.java):

@Override
protected void onInit() {
    ...

    // authentication customer filter
    GET(securePaths(), routeContext -> {
        if (routeContext.getSession("customer") == null) {
            routeContext.redirect("/login");
        } else {
            routeContext.next();
        }
    }).named("secureFilter");

    // authentication user filter
    GET(secureAdminPaths(), routeContext -> {
        if (routeContext.getSession("user") == null) {
            routeContext.redirect("/admin/login");
        } else {
            routeContext.next();
        }
    }).named("secureAdminFilter");

    ...
}

private String securePaths() {
    return new PathRegexBuilder()
        .excludes(
            "/login",
            "/admin",
            "/webjars",
            "/public"
        )
        .build();
}

private String secureAdminPaths() {
    return new PathRegexBuilder()
        .includes(
            "/admin"
        )
        .excludes(
            "/admin/login",
            "/webjars",
            "/public"
        )
        .build();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment