Search in sources :

Example 21 with ApiException

use of com.faforever.api.error.ApiException in project faf-java-api by FAForever.

the class MapService method checkLua.

private void checkLua(MapUploadData progressData) {
    List<Error> errors = new ArrayList<>();
    LuaValue scenarioInfo = progressData.getLuaScenarioInfo();
    if (scenarioInfo.get(ScenarioMapInfo.NAME) == LuaValue.NIL) {
        errors.add(new Error(ErrorCode.MAP_NAME_MISSING));
    }
    if (scenarioInfo.get(ScenarioMapInfo.DESCRIPTION) == LuaValue.NIL) {
        errors.add(new Error(ErrorCode.MAP_DESCRIPTION_MISSING));
    }
    if (invalidTeam(scenarioInfo)) {
        errors.add(new Error(ErrorCode.MAP_FIRST_TEAM_FFA));
    }
    if (scenarioInfo.get(ScenarioMapInfo.TYPE) == LuaValue.NIL) {
        errors.add(new Error(ErrorCode.MAP_TYPE_MISSING));
    }
    if (scenarioInfo.get(ScenarioMapInfo.SIZE) == LuaValue.NIL) {
        errors.add(new Error(ErrorCode.MAP_SIZE_MISSING));
    }
    if (scenarioInfo.get(ScenarioMapInfo.MAP_VERSION) == LuaValue.NIL) {
        errors.add(new Error(ErrorCode.MAP_VERSION_MISSING));
    }
    if (!errors.isEmpty()) {
        throw new ApiException(errors.toArray(new Error[errors.size()]));
    }
}
Also used : ArrayList(java.util.ArrayList) ProgrammingError(com.faforever.api.error.ProgrammingError) Error(com.faforever.api.error.Error) LuaValue(org.luaj.vm2.LuaValue) ApiException(com.faforever.api.error.ApiException)

Example 22 with ApiException

use of com.faforever.api.error.ApiException in project faf-java-api by FAForever.

the class MapService method parseScenarioLua.

@SneakyThrows
private void parseScenarioLua(MapUploadData progressData) {
    try (Stream<Path> mapFilesStream = Files.list(progressData.getOriginalMapFolder())) {
        Path scenarioLuaPath = noCatch(() -> mapFilesStream).filter(myFile -> myFile.toString().endsWith("_scenario.lua")).findFirst().orElseThrow(() -> new ApiException(new Error(ErrorCode.MAP_SCENARIO_LUA_MISSING)));
        LuaValue root = noCatch(() -> LuaLoader.loadFile(scenarioLuaPath), IllegalStateException.class);
        progressData.setLuaRoot(root);
    }
}
Also used : Path(java.nio.file.Path) ProgrammingError(com.faforever.api.error.ProgrammingError) Error(com.faforever.api.error.Error) LuaValue(org.luaj.vm2.LuaValue) ApiException(com.faforever.api.error.ApiException) SneakyThrows(lombok.SneakyThrows)

Example 23 with ApiException

use of com.faforever.api.error.ApiException in project faf-java-api by FAForever.

the class ClanService method acceptPlayerInvitationToken.

@SneakyThrows
void acceptPlayerInvitationToken(String stringToken, Authentication authentication) {
    Jwt token = jwtService.decodeAndVerify(stringToken);
    InvitationResult invitation = objectMapper.readValue(token.getClaims(), InvitationResult.class);
    if (invitation.isExpired()) {
        throw new ApiException(new Error(ErrorCode.CLAN_ACCEPT_TOKEN_EXPIRE));
    }
    final Integer clanId = invitation.getClan().getId();
    Player player = playerService.getPlayer(authentication);
    Clan clan = clanRepository.findById(clanId).orElseThrow(() -> new ApiException(new Error(ErrorCode.CLAN_NOT_EXISTS, clanId)));
    Player newMember = playerRepository.findById(invitation.getNewMember().getId()).orElseThrow(() -> new ProgrammingError("ClanMember does not exist: " + invitation.getNewMember().getId()));
    if (player.getId() != newMember.getId()) {
        throw new ApiException(new Error(ErrorCode.CLAN_ACCEPT_WRONG_PLAYER));
    }
    if (newMember.getClan() != null) {
        throw new ApiException(new Error(ErrorCode.CLAN_ACCEPT_PLAYER_IN_A_CLAN));
    }
    ClanMembership membership = new ClanMembership();
    membership.setClan(clan);
    membership.setPlayer(newMember);
    clanMembershipRepository.save(membership);
}
Also used : Player(com.faforever.api.data.domain.Player) Jwt(org.springframework.security.jwt.Jwt) Clan(com.faforever.api.data.domain.Clan) Error(com.faforever.api.error.Error) ProgrammingError(com.faforever.api.error.ProgrammingError) ProgrammingError(com.faforever.api.error.ProgrammingError) ClanMembership(com.faforever.api.data.domain.ClanMembership) InvitationResult(com.faforever.api.clan.result.InvitationResult) ApiException(com.faforever.api.error.ApiException) SneakyThrows(lombok.SneakyThrows)

Example 24 with ApiException

use of com.faforever.api.error.ApiException in project faf-java-api by FAForever.

the class ClanService method generatePlayerInvitationToken.

@SneakyThrows
String generatePlayerInvitationToken(Player requester, int newMemberId, int clanId) {
    Clan clan = clanRepository.findById(clanId).orElseThrow(() -> new ApiException(new Error(ErrorCode.CLAN_NOT_EXISTS, clanId)));
    if (requester.getId() != clan.getLeader().getId()) {
        throw new ApiException(new Error(ErrorCode.CLAN_NOT_LEADER, clanId));
    }
    Player newMember = playerRepository.findById(newMemberId).orElseThrow(() -> new ApiException(new Error(ErrorCode.CLAN_GENERATE_LINK_PLAYER_NOT_FOUND, newMemberId)));
    long expire = Instant.now().plus(fafApiProperties.getClan().getInviteLinkExpireDurationMinutes(), ChronoUnit.MINUTES).toEpochMilli();
    InvitationResult result = new InvitationResult(expire, ClanResult.of(clan), PlayerResult.of(newMember));
    return jwtService.sign(result);
}
Also used : Player(com.faforever.api.data.domain.Player) Clan(com.faforever.api.data.domain.Clan) Error(com.faforever.api.error.Error) ProgrammingError(com.faforever.api.error.ProgrammingError) InvitationResult(com.faforever.api.clan.result.InvitationResult) ApiException(com.faforever.api.error.ApiException) SneakyThrows(lombok.SneakyThrows)

Example 25 with ApiException

use of com.faforever.api.error.ApiException in project faf-java-api by FAForever.

the class AvatarService method copyAvatarFile.

private void copyAvatarFile(InputStream imageDataInputStream, Path imageTargetPath, boolean overwrite) throws IOException {
    CopyOption[] copyOptions = overwrite ? new CopyOption[] { StandardCopyOption.REPLACE_EXISTING } : new CopyOption[0];
    if (!overwrite && Files.exists(imageTargetPath)) {
        throw new ApiException(new Error(ErrorCode.AVATAR_NAME_CONFLICT, imageTargetPath.getFileName().toString()));
    }
    Files.createDirectories(imageTargetPath.getParent(), FilePermissionUtil.directoryPermissionFileAttributes());
    Files.copy(imageDataInputStream, imageTargetPath, copyOptions);
    FilePermissionUtil.setDefaultFilePermission(imageTargetPath);
}
Also used : StandardCopyOption(java.nio.file.StandardCopyOption) CopyOption(java.nio.file.CopyOption) ProgrammingError(com.faforever.api.error.ProgrammingError) Error(com.faforever.api.error.Error) ApiException(com.faforever.api.error.ApiException) NotFoundApiException(com.faforever.api.error.NotFoundApiException)

Aggregations

ApiException (com.faforever.api.error.ApiException)48 Error (com.faforever.api.error.Error)29 Player (com.faforever.api.data.domain.Player)18 Test (org.junit.Test)18 ProgrammingError (com.faforever.api.error.ProgrammingError)13 SneakyThrows (lombok.SneakyThrows)11 Clan (com.faforever.api.data.domain.Clan)10 ClanMembership (com.faforever.api.data.domain.ClanMembership)7 InputStream (java.io.InputStream)7 Path (java.nio.file.Path)7 BufferedInputStream (java.io.BufferedInputStream)6 FileInputStream (java.io.FileInputStream)6 ArrayList (java.util.ArrayList)6 ZipInputStream (java.util.zip.ZipInputStream)6 Map (com.faforever.api.config.FafApiProperties.Map)5 Vote (com.faforever.api.data.domain.Vote)5 VotingSubject (com.faforever.api.data.domain.VotingSubject)5 NotFoundApiException (com.faforever.api.error.NotFoundApiException)5 Jwt (org.springframework.security.jwt.Jwt)5 LuaValue (org.luaj.vm2.LuaValue)4