com.robothy.s3.rest.handler.LocalS3Router Maven / Gradle / Ivy
package com.robothy.s3.rest.handler;
import com.robothy.netty.http.HttpRequest;
import com.robothy.netty.http.HttpRequestHandler;
import com.robothy.netty.router.AbstractRouter;
import com.robothy.netty.router.Route;
import com.robothy.netty.router.Router;
import com.robothy.s3.rest.model.request.BucketRegion;
import com.robothy.s3.rest.utils.VirtualHostParser;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpMethod;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import org.apache.commons.lang3.StringUtils;
class LocalS3Router extends AbstractRouter {
static final String BUCKET_PATH = "/{bucket}";
static final String BUCKET_KEY_PATH = "/{bucket}/{key}";
private final Map>> rules = new HashMap<>();
LocalS3Router() {
}
@Override
public Router route(Route rule) {
this.rules.putIfAbsent(rule.getMethod(), new HashMap<>());
Map> pathRules = this.rules.get(rule.getMethod());
pathRules.putIfAbsent(rule.getPath(), new ArrayList<>());
List routes = pathRules.get(rule.getPath());
routes.add(rule);
return this;
}
@Override
public HttpRequestHandler match(HttpRequest request) {
return matchMethod(request.getMethod())
.map(pathRules -> matchPath(pathRules, request))
.map(rules -> matchHandler(rules, request))
.orElse(notFoundHandler());
}
Optional