diff src/goodjava/webserver/handlers/RegexHandler.java @ 1608:f7e3adae4907

add BasicAuthHandler
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 01 May 2021 19:52:56 -0600
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/goodjava/webserver/handlers/RegexHandler.java	Sat May 01 19:52:56 2021 -0600
@@ -0,0 +1,27 @@
+package goodjava.webserver.handlers;
+
+import java.util.regex.Pattern;
+import goodjava.webserver.Handler;
+import goodjava.webserver.Request;
+import goodjava.webserver.Response;
+
+
+public final class RegexHandler implements Handler {
+	private final Pattern ptn;
+	private final Handler ifMatch;
+	private final Handler ifNotMatch;
+
+	public RegexHandler(Pattern ptn,Handler ifMatch,Handler ifNotMatch) {
+		this.ptn = ptn;
+		this.ifMatch = ifMatch;
+		this.ifNotMatch = ifNotMatch;
+	}
+
+	public RegexHandler(String ptn,Handler ifMatch,Handler ifNotMatch) {
+		this(Pattern.compile(ptn),ifMatch,ifNotMatch);
+	}
+
+	public Response handle(Request request) {
+		return ptn.matcher(request.path).find() ? ifMatch.handle(request) : ifNotMatch.handle(request);
+	}
+}