mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2024-12-21 05:50:18 +08:00
Split ServerParameterJSONHandler to own utility
This commit is contained in:
parent
bec64497ed
commit
38f4b4d084
@ -0,0 +1,69 @@
|
||||
/*
|
||||
* 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/>.
|
||||
*/
|
||||
package com.djrapitops.plan.system;
|
||||
|
||||
import com.djrapitops.plan.api.exceptions.connection.BadRequestException;
|
||||
import com.djrapitops.plan.db.access.queries.objects.ServerQueries;
|
||||
import com.djrapitops.plan.system.database.DBSystem;
|
||||
import com.djrapitops.plan.system.info.server.Server;
|
||||
import com.djrapitops.plan.system.webserver.RequestTarget;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Utility for getting server identifier from different sources.
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
@Singleton
|
||||
public class Identifiers {
|
||||
|
||||
protected final DBSystem dbSystem;
|
||||
|
||||
@Inject
|
||||
public Identifiers(DBSystem dbSystem) {
|
||||
this.dbSystem = dbSystem;
|
||||
}
|
||||
|
||||
public UUID getServerUUID(RequestTarget target) throws BadRequestException {
|
||||
Optional<String> serverUUID = target.getParameter("serverUUID");
|
||||
if (serverUUID.isPresent()) {
|
||||
return getServerUUIDDirectly(serverUUID.get());
|
||||
} else {
|
||||
return getServerUUIDFromName(target); // Preferred
|
||||
}
|
||||
}
|
||||
|
||||
private UUID getServerUUIDFromName(RequestTarget target) throws BadRequestException {
|
||||
String serverName = target.getParameter("serverName")
|
||||
.orElseThrow(() -> new BadRequestException("'serverName' parameter was not defined."));
|
||||
return dbSystem.getDatabase().query(ServerQueries.fetchServerMatchingIdentifier(serverName))
|
||||
.map(Server::getUuid)
|
||||
.orElseThrow(() -> new BadRequestException("'serverName' was not found in the database.: '" + serverName + "'"));
|
||||
}
|
||||
|
||||
private UUID getServerUUIDDirectly(String serverUUIDString) throws BadRequestException {
|
||||
try {
|
||||
return UUID.fromString(serverUUIDString);
|
||||
} catch (IllegalArgumentException malformedUUIDException) {
|
||||
throw new BadRequestException("'serverName' was not a valid UUID: " + malformedUUIDException.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
@ -19,11 +19,12 @@ package com.djrapitops.plan.system.webserver.pages.json;
|
||||
import com.djrapitops.plan.api.exceptions.WebUserAuthException;
|
||||
import com.djrapitops.plan.api.exceptions.connection.BadRequestException;
|
||||
import com.djrapitops.plan.api.exceptions.connection.WebException;
|
||||
import com.djrapitops.plan.system.database.DBSystem;
|
||||
import com.djrapitops.plan.system.Identifiers;
|
||||
import com.djrapitops.plan.system.json.GraphJSONParser;
|
||||
import com.djrapitops.plan.system.webserver.Request;
|
||||
import com.djrapitops.plan.system.webserver.RequestTarget;
|
||||
import com.djrapitops.plan.system.webserver.auth.Authentication;
|
||||
import com.djrapitops.plan.system.webserver.pages.PageHandler;
|
||||
import com.djrapitops.plan.system.webserver.response.Response;
|
||||
import com.djrapitops.plan.system.webserver.response.data.JSONResponse;
|
||||
|
||||
@ -37,22 +38,23 @@ import java.util.UUID;
|
||||
* @author Rsl1122
|
||||
*/
|
||||
@Singleton
|
||||
public class GraphsJSONHandler extends ServerParameterJSONHandler {
|
||||
public class GraphsJSONHandler implements PageHandler {
|
||||
|
||||
private final Identifiers identifiers;
|
||||
private final GraphJSONParser graphJSON;
|
||||
|
||||
@Inject
|
||||
public GraphsJSONHandler(
|
||||
DBSystem dbSystem,
|
||||
Identifiers identifiers,
|
||||
GraphJSONParser graphJSON
|
||||
) {
|
||||
super(dbSystem);
|
||||
this.identifiers = identifiers;
|
||||
this.graphJSON = graphJSON;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response getResponse(Request request, RequestTarget target) throws WebException {
|
||||
UUID serverUUID = getServerUUID(target); // Can throw BadRequestException
|
||||
UUID serverUUID = identifiers.getServerUUID(target); // Can throw BadRequestException
|
||||
String type = target.getParameter("type")
|
||||
.orElseThrow(() -> new BadRequestException("'type' parameter was not defined."));
|
||||
String graphDataJSON = generateGraphDataJSONOfType(type, serverUUID);
|
||||
|
@ -18,11 +18,12 @@ package com.djrapitops.plan.system.webserver.pages.json;
|
||||
|
||||
import com.djrapitops.plan.api.exceptions.WebUserAuthException;
|
||||
import com.djrapitops.plan.api.exceptions.connection.WebException;
|
||||
import com.djrapitops.plan.system.database.DBSystem;
|
||||
import com.djrapitops.plan.system.Identifiers;
|
||||
import com.djrapitops.plan.system.json.OnlineActivityOverviewJSONParser;
|
||||
import com.djrapitops.plan.system.webserver.Request;
|
||||
import com.djrapitops.plan.system.webserver.RequestTarget;
|
||||
import com.djrapitops.plan.system.webserver.auth.Authentication;
|
||||
import com.djrapitops.plan.system.webserver.pages.PageHandler;
|
||||
import com.djrapitops.plan.system.webserver.response.Response;
|
||||
import com.djrapitops.plan.system.webserver.response.data.JSONResponse;
|
||||
|
||||
@ -36,22 +37,23 @@ import java.util.UUID;
|
||||
* @author Rsl1122
|
||||
*/
|
||||
@Singleton
|
||||
public class OnlineActivityOverviewJSONHandler extends ServerParameterJSONHandler {
|
||||
public class OnlineActivityOverviewJSONHandler implements PageHandler {
|
||||
|
||||
private final Identifiers identifiers;
|
||||
private final OnlineActivityOverviewJSONParser jsonParser;
|
||||
|
||||
@Inject
|
||||
public OnlineActivityOverviewJSONHandler(
|
||||
DBSystem dbSystem,
|
||||
Identifiers identifiers,
|
||||
OnlineActivityOverviewJSONParser jsonParser
|
||||
) {
|
||||
super(dbSystem);
|
||||
this.identifiers = identifiers;
|
||||
this.jsonParser = jsonParser;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response getResponse(Request request, RequestTarget target) throws WebException {
|
||||
UUID serverUUID = getServerUUID(target); // Can throw BadRequestException
|
||||
UUID serverUUID = identifiers.getServerUUID(target); // Can throw BadRequestException
|
||||
return new JSONResponse<>(jsonParser.createJSONAsMap(serverUUID));
|
||||
}
|
||||
|
||||
|
@ -18,12 +18,13 @@ package com.djrapitops.plan.system.webserver.pages.json;
|
||||
|
||||
import com.djrapitops.plan.api.exceptions.WebUserAuthException;
|
||||
import com.djrapitops.plan.api.exceptions.connection.WebException;
|
||||
import com.djrapitops.plan.system.database.DBSystem;
|
||||
import com.djrapitops.plan.system.Identifiers;
|
||||
import com.djrapitops.plan.system.json.JSONFactory;
|
||||
import com.djrapitops.plan.system.json.PlayersTableJSONParser;
|
||||
import com.djrapitops.plan.system.webserver.Request;
|
||||
import com.djrapitops.plan.system.webserver.RequestTarget;
|
||||
import com.djrapitops.plan.system.webserver.auth.Authentication;
|
||||
import com.djrapitops.plan.system.webserver.pages.PageHandler;
|
||||
import com.djrapitops.plan.system.webserver.response.Response;
|
||||
import com.djrapitops.plan.system.webserver.response.data.JSONResponse;
|
||||
|
||||
@ -38,22 +39,23 @@ import java.util.UUID;
|
||||
* @see PlayersTableJSONParser For JSON parsing of /server players table.
|
||||
*/
|
||||
@Singleton
|
||||
public class PlayersTableJSONHandler extends ServerParameterJSONHandler {
|
||||
public class PlayersTableJSONHandler implements PageHandler {
|
||||
|
||||
private final Identifiers identifiers;
|
||||
private final JSONFactory jsonFactory;
|
||||
|
||||
@Inject
|
||||
public PlayersTableJSONHandler(
|
||||
DBSystem dbSystem,
|
||||
Identifiers identifiers,
|
||||
JSONFactory jsonFactory
|
||||
) {
|
||||
super(dbSystem);
|
||||
this.identifiers = identifiers;
|
||||
this.jsonFactory = jsonFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response getResponse(Request request, RequestTarget target) throws WebException {
|
||||
UUID serverUUID = getServerUUID(target); // Can throw BadRequestException
|
||||
UUID serverUUID = identifiers.getServerUUID(target); // Can throw BadRequestException
|
||||
return new JSONResponse(jsonFactory.serverPlayersTableJSON(serverUUID));
|
||||
}
|
||||
|
||||
|
@ -18,11 +18,12 @@ package com.djrapitops.plan.system.webserver.pages.json;
|
||||
|
||||
import com.djrapitops.plan.api.exceptions.WebUserAuthException;
|
||||
import com.djrapitops.plan.api.exceptions.connection.WebException;
|
||||
import com.djrapitops.plan.system.database.DBSystem;
|
||||
import com.djrapitops.plan.system.Identifiers;
|
||||
import com.djrapitops.plan.system.json.ServerOverviewJSONParser;
|
||||
import com.djrapitops.plan.system.webserver.Request;
|
||||
import com.djrapitops.plan.system.webserver.RequestTarget;
|
||||
import com.djrapitops.plan.system.webserver.auth.Authentication;
|
||||
import com.djrapitops.plan.system.webserver.pages.PageHandler;
|
||||
import com.djrapitops.plan.system.webserver.response.Response;
|
||||
import com.djrapitops.plan.system.webserver.response.data.JSONResponse;
|
||||
|
||||
@ -36,22 +37,23 @@ import java.util.UUID;
|
||||
* @author Rsl1122
|
||||
*/
|
||||
@Singleton
|
||||
public class ServerOverviewJSONHandler extends ServerParameterJSONHandler {
|
||||
public class ServerOverviewJSONHandler implements PageHandler {
|
||||
|
||||
private final Identifiers identifiers;
|
||||
private final ServerOverviewJSONParser jsonParser;
|
||||
|
||||
@Inject
|
||||
public ServerOverviewJSONHandler(
|
||||
DBSystem dbSystem,
|
||||
Identifiers identifiers,
|
||||
ServerOverviewJSONParser jsonParser
|
||||
) {
|
||||
super(dbSystem);
|
||||
this.identifiers = identifiers;
|
||||
this.jsonParser = jsonParser;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response getResponse(Request request, RequestTarget target) throws WebException {
|
||||
UUID serverUUID = getServerUUID(target); // Can throw BadRequestException
|
||||
UUID serverUUID = identifiers.getServerUUID(target); // Can throw BadRequestException
|
||||
return new JSONResponse<>(jsonParser.createJSONAsMap(serverUUID));
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user