comparison 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
comparison
equal deleted inserted replaced
1607:fa066aaa068c 1608:f7e3adae4907
1 package goodjava.webserver.handlers;
2
3 import java.util.regex.Pattern;
4 import goodjava.webserver.Handler;
5 import goodjava.webserver.Request;
6 import goodjava.webserver.Response;
7
8
9 public final class RegexHandler implements Handler {
10 private final Pattern ptn;
11 private final Handler ifMatch;
12 private final Handler ifNotMatch;
13
14 public RegexHandler(Pattern ptn,Handler ifMatch,Handler ifNotMatch) {
15 this.ptn = ptn;
16 this.ifMatch = ifMatch;
17 this.ifNotMatch = ifNotMatch;
18 }
19
20 public RegexHandler(String ptn,Handler ifMatch,Handler ifNotMatch) {
21 this(Pattern.compile(ptn),ifMatch,ifNotMatch);
22 }
23
24 public Response handle(Request request) {
25 return ptn.matcher(request.path).find() ? ifMatch.handle(request) : ifNotMatch.handle(request);
26 }
27 }