diff --git a/Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/CompositeResolver.java b/Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/CompositeResolver.java index f6dbc0d6a..0d20d1326 100644 --- a/Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/CompositeResolver.java +++ b/Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/CompositeResolver.java @@ -39,28 +39,22 @@ import java.util.function.Predicate; public final class CompositeResolver implements Resolver { private final List prefixes; - private final List>> resolvers; - private final List> canAccess; + private final List resolvers; CompositeResolver() { this.prefixes = new ArrayList<>(); this.resolvers = new ArrayList<>(); - this.canAccess = new ArrayList<>(); } public static CompositeResolver.Builder builder() { return new Builder(); } - private Optional>> getResolver(URIPath target) { + private Optional getResolver(URIPath target) { return target.getPart(0).flatMap(this::findResolver); } - private Optional> getAccessCheck(URIPath target) { - return target.getPart(0).flatMap(this::findAccessCheck); - } - - private Optional>> findResolver(String prefix) { + private Optional findResolver(String prefix) { for (int i = 0; i < prefixes.size(); i++) { if (prefixes.get(i).equals(prefix)) { return Optional.of(resolvers.get(i)); @@ -69,21 +63,11 @@ public final class CompositeResolver implements Resolver { return Optional.empty(); } - private Optional> findAccessCheck(String prefix) { - for (int i = 0; i < prefixes.size(); i++) { - if (prefixes.get(i).equals(prefix)) { - return Optional.of(canAccess.get(i)); - } - } - return Optional.empty(); - } - void add(String prefix, Resolver resolver) { if (prefix == null) throw new IllegalArgumentException("Prefix can not be null"); if (resolver == null) throw new IllegalArgumentException("Resolver can not be null"); prefixes.add(prefix); - resolvers.add(resolver::resolve); - canAccess.add(resolver::canAccess); + resolvers.add(resolver); } void add(String prefix, Function resolver, Predicate accessCheck) { @@ -93,22 +77,27 @@ public final class CompositeResolver implements Resolver { } if (accessCheck == null) throw new IllegalArgumentException("Predicate accessCheck can not be null"); prefixes.add(prefix); - resolvers.add(request -> Optional.ofNullable(resolver.apply(request))); - canAccess.add(accessCheck); + resolvers.add(new FunctionalResolverWrapper(request -> Optional.ofNullable(resolver.apply(request)), accessCheck)); } @Override public boolean canAccess(Request request) { Request forThis = request.omitFirstInPath(); - return getAccessCheck(forThis.getPath()) - .map(resolver -> resolver.test(forThis)) + return getResolver(forThis.getPath()) + .map(resolver -> resolver.canAccess(forThis)) .orElse(true); } @Override public Optional resolve(Request request) { Request forThis = request.omitFirstInPath(); - return getResolver(forThis.getPath()).flatMap(resolver -> resolver.apply(forThis)); + return getResolver(forThis.getPath()).flatMap(resolver -> resolver.resolve(forThis)); + } + + @Override + public boolean requiresAuth(Request request) { + Request forThis = request.omitFirstInPath(); + return getResolver(forThis.getPath()).map(resolver -> resolver.requiresAuth(forThis)).orElse(true); } public static class Builder { diff --git a/Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/FunctionalResolverWrapper.java b/Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/FunctionalResolverWrapper.java new file mode 100644 index 000000000..d9a867c5a --- /dev/null +++ b/Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/FunctionalResolverWrapper.java @@ -0,0 +1,44 @@ +/* + * This file is part of Player Analytics (Plan). + * + * Plan is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License v3 as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Plan is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Plan. If not, see . + */ +package com.djrapitops.plan.delivery.web.resolver; + +import com.djrapitops.plan.delivery.web.resolver.request.Request; + +import java.util.Optional; +import java.util.function.Function; +import java.util.function.Predicate; + +public class FunctionalResolverWrapper implements Resolver { + + private final Function> resolver; + private final Predicate accessCheck; + + public FunctionalResolverWrapper(Function> resolver, Predicate accessCheck) { + this.resolver = resolver; + this.accessCheck = accessCheck; + } + + @Override + public boolean canAccess(Request request) { + return accessCheck.test(request); + } + + @Override + public Optional resolve(Request request) { + return resolver.apply(request); + } +}