reactor.netty.http.server.DefaultHttpServerRoutes Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of reactor-netty Show documentation
Show all versions of reactor-netty Show documentation
Reactive Streams Netty driver
/*
* Copyright (c) 2011-2019 Pivotal Software Inc, All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package reactor.netty.http.server;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;
import javax.annotation.Nullable;
import org.reactivestreams.Publisher;
import reactor.core.Exceptions;
import reactor.core.publisher.Mono;
/**
* @author Stephane Maldini
*/
final class DefaultHttpServerRoutes implements HttpServerRoutes {
private final CopyOnWriteArrayList handlers =
new CopyOnWriteArrayList<>();
@Override
public HttpServerRoutes directory(String uri, Path directory,
Function interceptor) {
Objects.requireNonNull(directory, "directory");
return route(HttpPredicate.prefix(uri), (req, resp) -> {
String prefix = URI.create(req.uri())
.getPath()
.replaceFirst(uri, "");
if(prefix.charAt(0) == '/'){
prefix = prefix.substring(1);
}
Path p = directory.resolve(prefix);
if (Files.isReadable(p)) {
if (interceptor != null) {
return interceptor.apply(resp)
.sendFile(p);
}
return resp.sendFile(p);
}
return resp.sendNotFound();
});
}
@Override
public HttpServerRoutes route(Predicate super HttpServerRequest> condition,
BiFunction super HttpServerRequest, ? super HttpServerResponse, ? extends Publisher> handler) {
Objects.requireNonNull(condition, "condition");
Objects.requireNonNull(handler, "handler");
if (condition instanceof HttpPredicate) {
handlers.add(new HttpRouteHandler(condition,
handler,
(HttpPredicate) condition));
}
else {
handlers.add(new HttpRouteHandler(condition, handler, null));
}
return this;
}
@Override
public Publisher apply(HttpServerRequest request, HttpServerResponse response) {
final Iterator iterator = handlers.iterator();
HttpRouteHandler cursor;
try {
while (iterator.hasNext()) {
cursor = iterator.next();
if (cursor.test(request)) {
return cursor.apply(request, response);
}
}
}
catch (Throwable t) {
Exceptions.throwIfJvmFatal(t);
return Mono.error(t); //500
}
return response.sendNotFound();
}
/**
*/
static final class HttpRouteHandler
implements BiFunction>,
Predicate {
final Predicate super HttpServerRequest> condition;
final BiFunction super HttpServerRequest, ? super HttpServerResponse, ? extends Publisher>
handler;
final Function super String, Map> resolver;
HttpRouteHandler(Predicate super HttpServerRequest> condition,
BiFunction super HttpServerRequest, ? super HttpServerResponse, ? extends Publisher> handler,
@Nullable Function super String, Map> resolver) {
this.condition = Objects.requireNonNull(condition, "condition");
this.handler = Objects.requireNonNull(handler, "handler");
this.resolver = resolver;
}
@Override
public Publisher apply(HttpServerRequest request,
HttpServerResponse response) {
return handler.apply(request.paramsResolver(resolver), response);
}
@Override
public boolean test(HttpServerRequest o) {
return condition.test(o);
}
}
}