2019-07-06 14:24:01 +02:00
|
|
|
/*
|
|
|
|
* 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 <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2019-08-30 22:14:54 +02:00
|
|
|
package com.djrapitops.plan.identification;
|
2019-08-30 11:36:38 +02:00
|
|
|
|
2020-03-10 09:41:49 +01:00
|
|
|
import com.djrapitops.plan.delivery.web.resolver.exception.BadRequestException;
|
2020-03-01 19:35:00 +01:00
|
|
|
import com.djrapitops.plan.delivery.web.resolver.request.Request;
|
2019-08-30 22:14:54 +02:00
|
|
|
import com.djrapitops.plan.storage.database.DBSystem;
|
|
|
|
import com.djrapitops.plan.storage.database.queries.objects.ServerQueries;
|
|
|
|
import com.djrapitops.plan.storage.database.queries.objects.UserIdentifierQueries;
|
2019-07-06 14:24:01 +02:00
|
|
|
|
|
|
|
import javax.inject.Inject;
|
|
|
|
import javax.inject.Singleton;
|
2019-10-25 14:12:01 +02:00
|
|
|
import java.util.Optional;
|
2019-07-06 14:24:01 +02:00
|
|
|
import java.util.UUID;
|
2021-02-10 09:04:21 +01:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2019-07-06 14:24:01 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Utility for getting server identifier from different sources.
|
|
|
|
*
|
|
|
|
* @author Rsl1122
|
|
|
|
*/
|
|
|
|
@Singleton
|
|
|
|
public class Identifiers {
|
|
|
|
|
|
|
|
protected final DBSystem dbSystem;
|
2020-03-01 19:35:00 +01:00
|
|
|
private final UUIDUtility uuidUtility;
|
2019-07-06 14:24:01 +02:00
|
|
|
|
|
|
|
@Inject
|
2020-03-01 19:35:00 +01:00
|
|
|
public Identifiers(DBSystem dbSystem, UUIDUtility uuidUtility) {
|
2019-07-06 14:24:01 +02:00
|
|
|
this.dbSystem = dbSystem;
|
2020-03-01 19:35:00 +01:00
|
|
|
this.uuidUtility = uuidUtility;
|
2019-07-06 14:24:01 +02:00
|
|
|
}
|
|
|
|
|
2020-03-06 09:09:06 +01:00
|
|
|
/**
|
|
|
|
* Obtain UUID of the server.
|
|
|
|
*
|
|
|
|
* @param request for Request, URIQuery needs a 'server' parameter.
|
|
|
|
* @return UUID of the server.
|
|
|
|
* @throws BadRequestException If server parameter is not defined or the server is not in the database.
|
|
|
|
*/
|
|
|
|
public UUID getServerUUID(Request request) {
|
2020-05-12 18:25:16 +02:00
|
|
|
String identifier = request.getQuery().get("server")
|
2019-07-19 10:52:17 +02:00
|
|
|
.orElseThrow(() -> new BadRequestException("'server' parameter was not defined."));
|
|
|
|
|
2020-05-12 18:25:16 +02:00
|
|
|
Optional<UUID> parsed = UUIDUtility.parseFromString(identifier);
|
|
|
|
return parsed.orElseGet(() -> getServerUUIDFromName(identifier).orElseThrow(
|
2021-01-24 10:03:01 +01:00
|
|
|
() -> new BadRequestException("Given 'server' was not found in the database.")
|
2020-05-12 18:25:16 +02:00
|
|
|
));
|
2019-07-06 14:24:01 +02:00
|
|
|
}
|
|
|
|
|
2020-05-12 18:25:16 +02:00
|
|
|
/**
|
|
|
|
* Obtain UUID of the server.
|
|
|
|
*
|
|
|
|
* @param identifier Identifier (name or uuid string) of the server
|
|
|
|
* @return UUID of the server.
|
|
|
|
* @throws BadRequestException If the server is not in the database.
|
|
|
|
*/
|
|
|
|
public Optional<UUID> getServerUUID(String identifier) {
|
|
|
|
Optional<UUID> parsed = UUIDUtility.parseFromString(identifier);
|
|
|
|
if (parsed.isPresent()) return parsed;
|
|
|
|
return getServerUUIDFromName(identifier);
|
|
|
|
}
|
|
|
|
|
|
|
|
private Optional<UUID> getServerUUIDFromName(String serverName) {
|
2019-07-06 14:24:01 +02:00
|
|
|
return dbSystem.getDatabase().query(ServerQueries.fetchServerMatchingIdentifier(serverName))
|
2020-05-12 18:25:16 +02:00
|
|
|
.map(Server::getUuid);
|
2019-07-06 14:24:01 +02:00
|
|
|
}
|
2019-07-20 16:40:15 +02:00
|
|
|
|
2020-03-06 09:09:06 +01:00
|
|
|
/**
|
|
|
|
* Obtain UUID of the player.
|
|
|
|
*
|
|
|
|
* @param request for Request, URIQuery needs a 'player' parameter.
|
|
|
|
* @return UUID of the player.
|
|
|
|
* @throws BadRequestException If player parameter is not defined or the player is not in the database.
|
|
|
|
*/
|
|
|
|
public UUID getPlayerUUID(Request request) {
|
2020-03-01 19:35:00 +01:00
|
|
|
String playerIdentifier = request.getQuery().get("player")
|
2019-10-25 14:12:01 +02:00
|
|
|
.orElseThrow(() -> new BadRequestException("'player' parameter was not defined.")).trim();
|
2019-07-20 16:40:15 +02:00
|
|
|
|
2019-10-25 14:12:01 +02:00
|
|
|
Optional<UUID> parsed = UUIDUtility.parseFromString(playerIdentifier);
|
2020-03-01 19:35:00 +01:00
|
|
|
return parsed.orElseGet(() -> getPlayerUUIDFromName(playerIdentifier));
|
2019-07-20 16:40:15 +02:00
|
|
|
}
|
|
|
|
|
2020-03-06 09:09:06 +01:00
|
|
|
private UUID getPlayerUUIDFromName(String playerName) {
|
2019-07-20 16:40:15 +02:00
|
|
|
return dbSystem.getDatabase()
|
|
|
|
.query(UserIdentifierQueries.fetchPlayerUUIDOf(playerName))
|
2021-01-24 10:03:57 +01:00
|
|
|
.orElseThrow(() -> new BadRequestException("Given 'player' was not found in the database."));
|
2019-07-20 16:40:15 +02:00
|
|
|
}
|
2020-03-01 19:35:00 +01:00
|
|
|
|
|
|
|
public UUID getPlayerUUID(String name) {
|
|
|
|
return uuidUtility.getUUIDOf(name);
|
|
|
|
}
|
2021-02-10 09:04:21 +01:00
|
|
|
|
|
|
|
public static long getTimestamp(Request request) {
|
|
|
|
try {
|
|
|
|
long currentTime = System.currentTimeMillis();
|
|
|
|
long timestamp = request.getQuery().get("timestamp")
|
|
|
|
.map(Long::parseLong)
|
|
|
|
.orElse(currentTime);
|
|
|
|
if (currentTime + TimeUnit.SECONDS.toMillis(10L) < timestamp) {
|
|
|
|
throw new BadRequestException("Attempt to get data from the future! " + timestamp + " > " + currentTime);
|
|
|
|
}
|
|
|
|
return timestamp;
|
|
|
|
} catch (NumberFormatException nonNumberTimestamp) {
|
|
|
|
throw new BadRequestException("'timestamp' was not a number: " + nonNumberTimestamp.getMessage());
|
|
|
|
}
|
|
|
|
}
|
2019-07-06 14:24:01 +02:00
|
|
|
}
|