| 
1608
 | 
     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 }
 |