Search in sources :

Example 1 with Resolver

use of com.djrapitops.plan.delivery.web.resolver.Resolver in project Plan by plan-player-analytics.

the class ResponseResolver method tryToGetResponse.

/**
 * @throws NotFoundException   In some cases when page was not found, not all.
 * @throws ForbiddenException  If the user is not allowed to see the page
 * @throws BadRequestException If the request did not have required things.
 */
private Response tryToGetResponse(Request request) {
    if ("OPTIONS".equalsIgnoreCase(request.getMethod())) {
        // https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS
        return Response.builder().setStatus(204).build();
    }
    Optional<WebUser> user = request.getUser();
    List<Resolver> foundResolvers = resolverService.getResolvers(request.getPath().asString());
    if (foundResolvers.isEmpty())
        return responseFactory.pageNotFound404();
    for (Resolver resolver : foundResolvers) {
        boolean isAuthRequired = webServer.get().isAuthRequired() && resolver.requiresAuth(request);
        if (isAuthRequired) {
            if (!user.isPresent()) {
                if (webServer.get().isUsingHTTPS()) {
                    throw new WebUserAuthException(FailReason.NO_USER_PRESENT);
                } else {
                    return responseFactory.forbidden403();
                }
            }
            if (resolver.canAccess(request)) {
                Optional<Response> resolved = resolver.resolve(request);
                if (resolved.isPresent())
                    return resolved.get();
            } else {
                return responseFactory.forbidden403();
            }
        } else {
            Optional<Response> resolved = resolver.resolve(request);
            if (resolved.isPresent())
                return resolved.get();
        }
    }
    return responseFactory.pageNotFound404();
}
Also used : Response(com.djrapitops.plan.delivery.web.resolver.Response) RootJSONResolver(com.djrapitops.plan.delivery.webserver.resolver.json.RootJSONResolver) Resolver(com.djrapitops.plan.delivery.web.resolver.Resolver) NoAuthResolver(com.djrapitops.plan.delivery.web.resolver.NoAuthResolver) WebUserAuthException(com.djrapitops.plan.exceptions.WebUserAuthException) WebUser(com.djrapitops.plan.delivery.web.resolver.request.WebUser)

Aggregations

NoAuthResolver (com.djrapitops.plan.delivery.web.resolver.NoAuthResolver)1 Resolver (com.djrapitops.plan.delivery.web.resolver.Resolver)1 Response (com.djrapitops.plan.delivery.web.resolver.Response)1 WebUser (com.djrapitops.plan.delivery.web.resolver.request.WebUser)1 RootJSONResolver (com.djrapitops.plan.delivery.webserver.resolver.json.RootJSONResolver)1 WebUserAuthException (com.djrapitops.plan.exceptions.WebUserAuthException)1