2018-01-12 10:02:32 +01:00
|
|
|
/*
|
2018-10-28 09:10:56 +01:00
|
|
|
* This file is part of Player Analytics (Plan).
|
|
|
|
*
|
|
|
|
* Plan is free software: you can redistribute it and/or modify
|
2018-11-11 19:55:11 +01:00
|
|
|
* it under the terms of the GNU Lesser General Public License v3 as published by
|
2018-10-28 09:10:56 +01:00
|
|
|
* 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
|
2018-11-11 19:55:11 +01:00
|
|
|
* GNU Lesser General Public License for more details.
|
2018-10-28 09:10:56 +01:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
|
2018-01-12 10:02:32 +01:00
|
|
|
*/
|
2020-03-01 20:24:48 +01:00
|
|
|
package com.djrapitops.plan.delivery.webserver.resolver;
|
2018-01-12 10:02:32 +01:00
|
|
|
|
2019-11-09 16:52:29 +01:00
|
|
|
import com.djrapitops.plan.delivery.rendering.html.Html;
|
2020-02-16 17:18:14 +01:00
|
|
|
import com.djrapitops.plan.delivery.web.resolver.Resolver;
|
|
|
|
import com.djrapitops.plan.delivery.web.resolver.Response;
|
|
|
|
import com.djrapitops.plan.delivery.web.resolver.request.Request;
|
|
|
|
import com.djrapitops.plan.delivery.web.resolver.request.URIPath;
|
|
|
|
import com.djrapitops.plan.delivery.web.resolver.request.WebUser;
|
2020-03-01 20:24:48 +01:00
|
|
|
import com.djrapitops.plan.delivery.webserver.ResponseFactory;
|
2019-08-30 22:14:54 +02:00
|
|
|
import com.djrapitops.plan.identification.Server;
|
|
|
|
import com.djrapitops.plan.identification.ServerInfo;
|
2021-03-12 17:03:00 +01:00
|
|
|
import com.djrapitops.plan.identification.ServerUUID;
|
2019-08-30 22:14:54 +02:00
|
|
|
import com.djrapitops.plan.storage.database.DBSystem;
|
|
|
|
import com.djrapitops.plan.storage.database.queries.objects.ServerQueries;
|
2023-01-14 21:17:50 +01:00
|
|
|
import com.djrapitops.plan.utilities.dev.Untrusted;
|
2018-01-12 10:02:32 +01:00
|
|
|
|
2018-08-27 16:18:17 +02:00
|
|
|
import javax.inject.Inject;
|
2018-08-28 09:51:38 +02:00
|
|
|
import javax.inject.Singleton;
|
2018-01-14 20:31:31 +01:00
|
|
|
import java.util.Optional;
|
2018-01-12 10:02:32 +01:00
|
|
|
|
|
|
|
/**
|
2019-12-05 18:42:20 +01:00
|
|
|
* Resolves /network, /server and /server/${name/uuid} URLs.
|
2018-01-12 10:02:32 +01:00
|
|
|
*
|
2021-02-13 14:16:03 +01:00
|
|
|
* @author AuroraLS3
|
2018-01-12 10:02:32 +01:00
|
|
|
*/
|
2018-08-28 09:51:38 +02:00
|
|
|
@Singleton
|
2020-02-15 19:08:03 +01:00
|
|
|
public class ServerPageResolver implements Resolver {
|
2018-08-27 16:18:17 +02:00
|
|
|
|
|
|
|
private final ResponseFactory responseFactory;
|
2018-10-13 11:19:36 +02:00
|
|
|
private final DBSystem dbSystem;
|
2018-08-27 16:18:17 +02:00
|
|
|
private final ServerInfo serverInfo;
|
|
|
|
|
|
|
|
@Inject
|
2019-12-05 18:42:20 +01:00
|
|
|
public ServerPageResolver(
|
2018-08-27 16:18:17 +02:00
|
|
|
ResponseFactory responseFactory,
|
2018-10-13 11:19:36 +02:00
|
|
|
DBSystem dbSystem,
|
2020-03-28 10:51:11 +01:00
|
|
|
ServerInfo serverInfo
|
2018-08-27 16:18:17 +02:00
|
|
|
) {
|
|
|
|
this.responseFactory = responseFactory;
|
2018-10-13 11:19:36 +02:00
|
|
|
this.dbSystem = dbSystem;
|
2018-08-27 16:18:17 +02:00
|
|
|
this.serverInfo = serverInfo;
|
|
|
|
}
|
2018-01-12 10:02:32 +01:00
|
|
|
|
2018-01-14 20:31:31 +01:00
|
|
|
@Override
|
2020-02-16 17:18:14 +01:00
|
|
|
public boolean canAccess(Request request) {
|
2023-01-14 21:17:50 +01:00
|
|
|
@Untrusted String firstPart = request.getPath().getPart(0).orElse("");
|
2020-02-16 17:18:14 +01:00
|
|
|
WebUser permissions = request.getUser().orElse(new WebUser(""));
|
2020-03-01 20:24:48 +01:00
|
|
|
boolean forServerPage = "server".equalsIgnoreCase(firstPart) && permissions.hasPermission("page.server");
|
|
|
|
boolean forNetworkPage = "network".equalsIgnoreCase(firstPart) && permissions.hasPermission("page.network");
|
2020-02-15 19:08:03 +01:00
|
|
|
return forServerPage || forNetworkPage;
|
2018-01-14 20:31:31 +01:00
|
|
|
}
|
|
|
|
|
2020-02-15 19:08:03 +01:00
|
|
|
@Override
|
2020-02-16 17:18:14 +01:00
|
|
|
public Optional<Response> resolve(Request request) {
|
|
|
|
return getServerUUID(request.getPath())
|
2020-03-28 11:00:44 +01:00
|
|
|
.map(serverUUID -> getServerPage(serverUUID, request))
|
2020-02-15 19:08:03 +01:00
|
|
|
.orElseGet(this::redirectToCurrentServer);
|
2019-02-22 09:35:06 +01:00
|
|
|
}
|
|
|
|
|
2020-02-15 19:08:03 +01:00
|
|
|
private Optional<Response> redirectToCurrentServer() {
|
|
|
|
String directTo = serverInfo.getServer().isProxy()
|
|
|
|
? "/network"
|
|
|
|
: "/server/" + Html.encodeToURL(serverInfo.getServer().getIdentifiableName());
|
2020-03-28 10:51:11 +01:00
|
|
|
return Optional.of(responseFactory.redirectResponse(directTo));
|
2018-01-12 10:02:32 +01:00
|
|
|
}
|
|
|
|
|
2023-01-22 09:18:14 +01:00
|
|
|
private Optional<Response> getServerPage(ServerUUID serverUUID, @Untrusted Request request) {
|
2020-02-15 19:08:03 +01:00
|
|
|
boolean toNetworkPage = serverInfo.getServer().isProxy() && serverInfo.getServerUUID().equals(serverUUID);
|
2020-03-28 11:00:44 +01:00
|
|
|
if (toNetworkPage) {
|
|
|
|
if (request.getPath().getPart(0).map("network"::equals).orElse(false)) {
|
2023-01-22 09:18:14 +01:00
|
|
|
return Optional.of(responseFactory.networkPageResponse(request));
|
2020-03-28 11:00:44 +01:00
|
|
|
} else {
|
|
|
|
// Accessing /server/Server <Bungee ID> which should be redirected to /network
|
|
|
|
return redirectToCurrentServer();
|
|
|
|
}
|
|
|
|
}
|
2023-01-22 09:18:14 +01:00
|
|
|
return Optional.of(responseFactory.serverPageResponse(request, serverUUID));
|
2020-02-15 19:08:03 +01:00
|
|
|
}
|
|
|
|
|
2023-01-14 21:17:50 +01:00
|
|
|
private Optional<ServerUUID> getServerUUID(@Untrusted URIPath path) {
|
2020-02-15 19:08:03 +01:00
|
|
|
if (serverInfo.getServer().isProxy()
|
|
|
|
&& path.getPart(0).map("network"::equals).orElse(false)
|
|
|
|
) {
|
|
|
|
return Optional.of(serverInfo.getServerUUID());
|
|
|
|
}
|
|
|
|
return path.getPart(1).flatMap(serverName -> dbSystem.getDatabase()
|
|
|
|
.query(ServerQueries.fetchServerMatchingIdentifier(serverName))
|
|
|
|
.map(Server::getUuid));
|
2018-01-12 10:02:32 +01:00
|
|
|
}
|
2018-06-12 20:28:40 +02:00
|
|
|
}
|