use of com.djrapitops.plan.delivery.web.resolver.Response in project Plan by plan-player-analytics.
the class RequestHandler method getResponse.
public Response getResponse(HttpExchange exchange) {
if (ipWhitelist == null) {
ipWhitelist = config.isTrue(WebserverSettings.IP_WHITELIST) ? config.get(WebserverSettings.WHITELIST) : Collections.emptyList();
}
String accessor = getAccessorAddress(exchange);
Request request = null;
Response response;
try {
request = buildRequest(exchange);
if (bruteForceGuard.shouldPreventRequest(accessor)) {
response = responseFactory.failedLoginAttempts403();
} else if (!ipWhitelist.isEmpty() && !ipWhitelist.contains(accessor)) {
response = responseFactory.ipWhitelist403(accessor);
logger.info(locale.getString(PluginLang.WEB_SERVER_NOTIFY_IP_WHITELIST_BLOCK, accessor, exchange.getRequestURI().toString()));
} else {
response = responseResolver.getResponse(request);
}
} catch (WebUserAuthException thrownByAuthentication) {
FailReason failReason = thrownByAuthentication.getFailReason();
if (failReason == FailReason.USER_PASS_MISMATCH) {
bruteForceGuard.increaseAttemptCountOnFailedLogin(accessor);
response = responseFactory.badRequest(failReason.getReason(), "/auth/login");
} else {
String from = exchange.getRequestURI().toASCIIString();
String directTo = StringUtils.startsWithAny(from, "/auth/", "/login") ? "/login" : "/login?from=." + from;
response = Response.builder().redirectTo(directTo).setHeader("Set-Cookie", "auth=expired; Path=/; Max-Age=0; SameSite=Lax; Secure;").build();
}
}
if (bruteForceGuard.shouldPreventRequest(accessor)) {
response = responseFactory.failedLoginAttempts403();
}
if (// Not failed
response.getCode() != 401 && // Not blocked
response.getCode() != 403 && request != null && // Logged in
request.getUser().isPresent()) {
bruteForceGuard.resetAttemptCount(accessor);
}
return response;
}
use of com.djrapitops.plan.delivery.web.resolver.Response in project Plan by plan-player-analytics.
the class QueryJSONResolver method getResponse.
private Response getResponse(Request request) {
Optional<Response> cachedResult = checkForCachedResult(request);
if (cachedResult.isPresent())
return cachedResult.get();
InputQueryDto inputQuery = parseInputQuery(request);
List<InputFilterDto> queries = inputQuery.getFilters();
Filter.Result result = filters.apply(queries);
List<Filter.ResultPath> resultPath = result.getInverseResultPath();
Collections.reverse(resultPath);
return buildAndStoreResponse(inputQuery.getView(), result, resultPath);
}
use of com.djrapitops.plan.delivery.web.resolver.Response in project Plan by plan-player-analytics.
the class RequestHandler method handle.
@Override
public void handle(HttpExchange exchange) {
try {
Response response = getResponse(exchange);
response.getHeaders().putIfAbsent("Access-Control-Allow-Origin", config.get(WebserverSettings.CORS_ALLOW_ORIGIN));
response.getHeaders().putIfAbsent("Access-Control-Allow-Methods", "GET, OPTIONS");
response.getHeaders().putIfAbsent("Access-Control-Allow-Credentials", "true");
response.getHeaders().putIfAbsent("X-Robots-Tag", "noindex, nofollow");
ResponseSender sender = new ResponseSender(addresses, exchange, response);
sender.send();
} catch (Exception e) {
if (config.isTrue(PluginSettings.DEV_MODE)) {
logger.warn("THIS ERROR IS ONLY LOGGED IN DEV MODE:");
errorLogger.warn(e, ErrorContext.builder().whatToDo("THIS ERROR IS ONLY LOGGED IN DEV MODE").related(exchange.getRequestMethod(), exchange.getRemoteAddress(), exchange.getRequestHeaders(), exchange.getResponseHeaders(), exchange.getRequestURI()).build());
}
} finally {
exchange.close();
}
}
use of com.djrapitops.plan.delivery.web.resolver.Response 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();
}
Aggregations