Add option to remove a added route from HttpServerRoutes
See original GitHub issueAt the moment it’s not possible to remove a previously added route from HttpServerRoutes
Motivation
I’m currently working on an “Webflux light” for OSGI. Because of the dynamic nature of OSGI (Components are dynamic added/removed) I need a way to remove a previous added route when the component that added the route is removed.
While the OSGI side is pretty easy to implement, the problem is that there is no way currently to remove a previous added route …
Desired solution
Extend HttpServerRoutes to a remove option. Remove option should be overloaded to be able to remove by:
(Examples, not thread safe)
- Predicate instance
public HttpServerRoutes remove(Predicate<? super HttpServerRequest> condition) {
for(HttpRouteHandler h: handlers){
if(h.condition.equals(condition)){
handlers.remove(h);
}
}
return this;
}
- Handler instance
public HttpServerRoutes remove(BiFunction<? super HttpServerRequest, ? super HttpServerResponse, ? extends Publisher<Void>> handler) {
for(HttpRouteHandler h: handlers){
if(h.handler.equals(handler)){
handlers.remove(h);
}
}
return this;
}
- Path
public HttpServerRoutes remove(String path) {
for(HttpRouteHandler h: handlers){
if(h.path != null && h.path.equals(path)){
handlers.remove(h);
}
}
return this;
}
Considered alternatives
Extend HttpServerRoutes and add a new variant for each method that uses a publisher as a argument
public interface HttpServerRoute {
Predicate<? super HttpServerRequest> condition();
BiFunction<? super HttpServerRequest, ? super HttpServerResponse, ? extends Publisher<Void>> handler()
}
public void route(Publisher<HttpServerRoute> publisher) {
publisher.subscribe(new Subscriber<HttpServerRoute>() {
private final List<HttpRouteHandler> publisherHandlers = new CopyOnWriteArrayList<>();
@Override
public void onSubscribe(Subscription subscription) {
subscription.request(Integer.MAX_VALUE);
}
@Override
public void onNext(HttpServerRoute httpServerRoute) {
publisherHandlers.add(addRoute(httpServerRoute.condition(), httpServerRoute.handler()));
}
@Override
public void onError(Throwable t) {
// TODO: Remove all handlers added ??????
}
@Override
public void onComplete() {
// Remove all handler added by the publisher
initialOrderHandlers.removeAll(publisherHandlers);
handlers.removeAll(publisherHandlers);
}
});
}
or
Extend HttpServerRoutes and add a new variant for each method that returns a Subscription or similar.
public Subscription publishRoute(Predicate<? super HttpServerRequest> condition,
BiFunction<? super HttpServerRequest, ? super HttpServerResponse, ? extends Publisher<Void>> handler) {
Objects.requireNonNull(condition, "condition");
Objects.requireNonNull(handler, "handler");
if (condition instanceof HttpPredicate) {
HttpRouteHandler httpRouteHandler = new HttpRouteHandler(condition,
handler,
(HttpPredicate) condition, ((HttpPredicate) condition).uri);
handlers.add(httpRouteHandler);
initialOrderHandlers.add(httpRouteHandler);
return new Subscription() {
@Override
public void request(long n) {
}
@Override
public void cancel() {
handlers.remove(httpRouteHandler);
}
};
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (6 by maintainers)
I’m currently on vacation. I will update the issue asap
I’m willing to provide a pull request if this is something you would consider to add