annotate src/goodjava/webserver/handlers/RegexHandler.java @ 1638:f53301a50084

use default slave ip
author fffilimonov
date Thu, 16 Dec 2021 23:00:29 -0600
parents f7e3adae4907
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1608
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 package goodjava.webserver.handlers;
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 import java.util.regex.Pattern;
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 import goodjava.webserver.Handler;
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 import goodjava.webserver.Request;
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 import goodjava.webserver.Response;
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 public final class RegexHandler implements Handler {
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 private final Pattern ptn;
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 private final Handler ifMatch;
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 private final Handler ifNotMatch;
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 public RegexHandler(Pattern ptn,Handler ifMatch,Handler ifNotMatch) {
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 this.ptn = ptn;
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 this.ifMatch = ifMatch;
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 this.ifNotMatch = ifNotMatch;
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 }
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 public RegexHandler(String ptn,Handler ifMatch,Handler ifNotMatch) {
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 this(Pattern.compile(ptn),ifMatch,ifNotMatch);
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 }
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 public Response handle(Request request) {
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 return ptn.matcher(request.path).find() ? ifMatch.handle(request) : ifNotMatch.handle(request);
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 }
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 }