129 lines
5.7 KiB
Java
129 lines
5.7 KiB
Java
/*
|
|
* 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.delivery.webserver.resolver.json;
|
|
|
|
import com.djrapitops.plan.delivery.domain.auth.WebPermission;
|
|
import com.djrapitops.plan.delivery.domain.datatransfer.PlayerJoinAddresses;
|
|
import com.djrapitops.plan.delivery.formatting.Formatter;
|
|
import com.djrapitops.plan.delivery.rendering.json.JSONFactory;
|
|
import com.djrapitops.plan.delivery.web.resolver.MimeType;
|
|
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.URIQuery;
|
|
import com.djrapitops.plan.delivery.web.resolver.request.WebUser;
|
|
import com.djrapitops.plan.delivery.webserver.cache.AsyncJSONResolverService;
|
|
import com.djrapitops.plan.delivery.webserver.cache.DataID;
|
|
import com.djrapitops.plan.delivery.webserver.cache.JSONStorage;
|
|
import com.djrapitops.plan.identification.Identifiers;
|
|
import com.djrapitops.plan.identification.ServerUUID;
|
|
import com.djrapitops.plan.utilities.dev.Untrusted;
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
import io.swagger.v3.oas.annotations.Parameter;
|
|
import io.swagger.v3.oas.annotations.enums.ParameterIn;
|
|
import io.swagger.v3.oas.annotations.media.Content;
|
|
import io.swagger.v3.oas.annotations.media.ExampleObject;
|
|
import io.swagger.v3.oas.annotations.media.Schema;
|
|
import io.swagger.v3.oas.annotations.parameters.RequestBody;
|
|
import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
|
import jakarta.ws.rs.GET;
|
|
import jakarta.ws.rs.Path;
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
import javax.inject.Inject;
|
|
import javax.inject.Singleton;
|
|
import java.util.Optional;
|
|
|
|
/**
|
|
* @author AuroraLS3
|
|
*/
|
|
@Singleton
|
|
@Path("/v1/joinAddresses")
|
|
public class PlayerJoinAddressJSONResolver extends JSONResolver {
|
|
|
|
private final Identifiers identifiers;
|
|
private final AsyncJSONResolverService jsonResolverService;
|
|
private final JSONFactory jsonFactory;
|
|
|
|
@Inject
|
|
public PlayerJoinAddressJSONResolver(Identifiers identifiers, AsyncJSONResolverService jsonResolverService, JSONFactory jsonFactory) {
|
|
this.identifiers = identifiers;
|
|
this.jsonResolverService = jsonResolverService;
|
|
this.jsonFactory = jsonFactory;
|
|
}
|
|
|
|
@Override
|
|
public Formatter<Long> getHttpLastModifiedFormatter() {return jsonResolverService.getHttpLastModifiedFormatter();}
|
|
|
|
@Override
|
|
public boolean canAccess(@Untrusted Request request) {
|
|
WebUser user = request.getUser().orElse(new WebUser(""));
|
|
@Untrusted URIQuery query = request.getQuery();
|
|
Optional<String> listOnly = query.get("listOnly");
|
|
if (query.get("server").isPresent()) {
|
|
if (listOnly.isEmpty()) {
|
|
return user.hasPermission(WebPermission.PAGE_SERVER_RETENTION);
|
|
} else {
|
|
return user.hasPermission(WebPermission.PAGE_SERVER_JOIN_ADDRESSES_GRAPHS_TIME);
|
|
}
|
|
}
|
|
if (listOnly.isEmpty()) {
|
|
return user.hasPermission(WebPermission.PAGE_NETWORK_RETENTION);
|
|
} else {
|
|
return user.hasPermission(WebPermission.PAGE_NETWORK_JOIN_ADDRESSES_GRAPHS_TIME);
|
|
}
|
|
}
|
|
|
|
@GET
|
|
@Operation(
|
|
description = "Get join address information of players for server or network",
|
|
responses = {
|
|
@ApiResponse(responseCode = "200", content = @Content(mediaType = MimeType.JSON, schema = @Schema(implementation = PlayerJoinAddresses.class))),
|
|
@ApiResponse(responseCode = "400", description = "If 'server' parameter is not an existing server")
|
|
},
|
|
parameters = @Parameter(in = ParameterIn.QUERY, name = "server", description = "Server identifier to get data for (optional)", examples = {
|
|
@ExampleObject("Server 1"),
|
|
@ExampleObject("1"),
|
|
@ExampleObject("1fb39d2a-eb82-4868-b245-1fad17d823b3"),
|
|
}),
|
|
requestBody = @RequestBody(content = @Content(examples = @ExampleObject()))
|
|
)
|
|
@Override
|
|
public Optional<Response> resolve(@Untrusted Request request) {
|
|
return Optional.of(getResponse(request));
|
|
}
|
|
|
|
private Response getResponse(@Untrusted Request request) {
|
|
JSONStorage.StoredJSON result = getStoredJSON(request);
|
|
return getCachedOrNewResponse(request, result);
|
|
}
|
|
|
|
@Nullable
|
|
private JSONStorage.StoredJSON getStoredJSON(Request request) {
|
|
Optional<Long> timestamp = Identifiers.getTimestamp(request);
|
|
if (request.getQuery().get("server").isPresent()) {
|
|
ServerUUID serverUUID = identifiers.getServerUUID(request);
|
|
return jsonResolverService.resolve(timestamp, DataID.PLAYER_JOIN_ADDRESSES, serverUUID,
|
|
serverUUID1 -> jsonFactory.playerJoinAddresses(serverUUID1, request.getQuery().get("listOnly").isEmpty())
|
|
);
|
|
}
|
|
// Assume network
|
|
return jsonResolverService.resolve(timestamp, DataID.PLAYER_JOIN_ADDRESSES,
|
|
() -> jsonFactory.playerJoinAddresses(request.getQuery().get("listOnly").isEmpty())
|
|
);
|
|
}
|
|
}
|