com.clouway.friendlyserve.FkRegex Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fserve Show documentation
Show all versions of fserve Show documentation
Friendly Serving HTTP Library.
package com.clouway.friendlyserve;
import com.google.common.base.Optional;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Fork by regular expression pattern.
*
* Use this class in combination with {@link TkFork},
* for example:
*
*
Take take = new TkFork(
* new FkRegex("/home", new TkHome()),
* new FkRegex("/account", new TkAccount())
* );
*
*
* @author Miroslav Genov ([email protected])
*/
public class FkRegex implements Fork {
private final Pattern pattern;
private final Take take;
public FkRegex(String pattern, Take take) {
this(Pattern.compile(pattern, Pattern.CASE_INSENSITIVE | Pattern.DOTALL), take);
}
public FkRegex(Pattern pattern, Take take) {
this.pattern = pattern;
this.take = take;
}
@Override
public Optional route(Request request) throws IOException {
String path = request.path();
final Matcher matcher = this.pattern.matcher(path);
if (matcher.matches()) {
return Optional.fromNullable(take.ack(request));
}
return Optional.absent();
}
}