Search in sources :

Example 16 with ApiException

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

the class UserService method claimPasswordResetToken.

@SneakyThrows
void claimPasswordResetToken(String token) {
    log.debug("Trying to reset password with token: {}", token);
    Map<String, String> claims = fafTokenService.resolveToken(FafTokenType.PASSWORD_RESET, token);
    int userId = Integer.parseInt(claims.get(KEY_USER_ID));
    String newPassword = claims.get(KEY_PASSWORD);
    User user = userRepository.findById(userId).orElseThrow(() -> new ApiException(new Error(TOKEN_INVALID)));
    setPassword(user, newPassword);
}
Also used : User(com.faforever.api.data.domain.User) Error(com.faforever.api.error.Error) ApiException(com.faforever.api.error.ApiException) SneakyThrows(lombok.SneakyThrows)

Example 17 with ApiException

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

the class VotingService method ableToVote.

private List<Error> ableToVote(Player player, int votingSubjectId) {
    VotingSubject subject = votingSubjectRepository.findById(votingSubjectId).orElseThrow(() -> new ApiException(new Error(ErrorCode.VOTING_SUBJECT_DOES_NOT_EXIST, votingSubjectId)));
    List<Error> errors = new ArrayList<>();
    Optional<Vote> byPlayerAndVotingSubject = voteRepository.findByPlayerAndVotingSubjectId(player, votingSubjectId);
    if (byPlayerAndVotingSubject.isPresent()) {
        errors.add(new Error(ErrorCode.VOTED_TWICE));
    }
    int gamesPlayed = gamePlayerStatsRepository.countByPlayerAndGameValidity(player, Validity.VALID);
    if (subject.getBeginOfVoteTime().isAfter(OffsetDateTime.now())) {
        errors.add(new Error(ErrorCode.VOTE_DID_NOT_START_YET, subject.getBeginOfVoteTime()));
    }
    if (subject.getEndOfVoteTime().isBefore(OffsetDateTime.now())) {
        errors.add(new Error(ErrorCode.VOTE_ALREADY_ENDED, subject.getEndOfVoteTime()));
    }
    if (gamesPlayed < subject.getMinGamesToVote()) {
        errors.add(new Error(ErrorCode.NOT_ENOUGH_GAMES, gamesPlayed, subject.getMinGamesToVote()));
    }
    return errors;
}
Also used : Vote(com.faforever.api.data.domain.Vote) ArrayList(java.util.ArrayList) Error(com.faforever.api.error.Error) VotingSubject(com.faforever.api.data.domain.VotingSubject) ApiException(com.faforever.api.error.ApiException)

Example 18 with ApiException

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

the class ModService method processUploadedMod.

@SneakyThrows
@Transactional
@CacheEvict(value = { Mod.TYPE_NAME, ModVersion.TYPE_NAME }, allEntries = true)
public void processUploadedMod(Path uploadedFile, Player uploader) {
    log.debug("Player '{}' uploaded a mod", uploader);
    ModReader modReader = new ModReader();
    com.faforever.commons.mod.Mod modInfo = modReader.readZip(uploadedFile);
    validateModInfo(modInfo);
    validateModStructure(uploadedFile);
    log.debug("Mod uploaded by user '{}' is valid: {}", uploader, modInfo);
    String displayName = modInfo.getName().trim();
    short version = (short) Integer.parseInt(modInfo.getVersion().toString());
    if (!canUploadMod(displayName, uploader)) {
        Mod mod = modRepository.findOneByDisplayName(displayName).orElseThrow(() -> new IllegalStateException("Mod could not be found"));
        throw new ApiException(new Error(ErrorCode.MOD_NOT_ORIGINAL_AUTHOR, mod.getAuthor(), displayName));
    }
    if (modExists(displayName, version)) {
        throw new ApiException(new Error(ErrorCode.MOD_VERSION_EXISTS, displayName, version));
    }
    String uuid = modInfo.getUid();
    if (modUidExists(uuid)) {
        throw new ApiException(new Error(ErrorCode.MOD_UID_EXISTS, uuid));
    }
    String zipFileName = generateZipFileName(displayName, version);
    Path targetPath = properties.getMod().getTargetDirectory().resolve(zipFileName);
    if (Files.exists(targetPath)) {
        throw new ApiException(new Error(ErrorCode.MOD_NAME_CONFLICT, zipFileName));
    }
    Optional<Path> thumbnailPath = extractThumbnail(uploadedFile, version, displayName, modInfo.getIcon());
    log.debug("Moving uploaded mod '{}' to: {}", modInfo.getName(), targetPath);
    Files.createDirectories(targetPath.getParent(), FilePermissionUtil.directoryPermissionFileAttributes());
    Files.move(uploadedFile, targetPath);
    FilePermissionUtil.setDefaultFilePermission(targetPath);
    try {
        store(modInfo, thumbnailPath, uploader, zipFileName);
    } catch (Exception exception) {
        try {
            Files.delete(targetPath);
        } catch (IOException ioException) {
            log.warn("Could not delete file " + targetPath, ioException);
        }
        throw exception;
    }
}
Also used : Path(java.nio.file.Path) Mod(com.faforever.api.data.domain.Mod) Error(com.faforever.api.error.Error) IOException(java.io.IOException) ApiException(com.faforever.api.error.ApiException) IOException(java.io.IOException) ModReader(com.faforever.commons.mod.ModReader) ApiException(com.faforever.api.error.ApiException) CacheEvict(org.springframework.cache.annotation.CacheEvict) SneakyThrows(lombok.SneakyThrows) Transactional(org.springframework.transaction.annotation.Transactional)

Example 19 with ApiException

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

the class MapService method updateMapEntities.

private void updateMapEntities(MapUploadData progressData) {
    LuaValue scenarioInfo = progressData.getLuaScenarioInfo();
    Map map = progressData.getMapEntity();
    if (map == null) {
        map = new Map();
    }
    map.setDisplayName(scenarioInfo.get(ScenarioMapInfo.NAME).toString()).setMapType(scenarioInfo.get(ScenarioMapInfo.TYPE).tojstring()).setBattleType(scenarioInfo.get(ScenarioMapInfo.CONFIGURATIONS).get(ScenarioMapInfo.CONFIGURATION_STANDARD).get(ScenarioMapInfo.CONFIGURATION_STANDARD_TEAMS).get(1).get(ScenarioMapInfo.CONFIGURATION_STANDARD_TEAMS_NAME).tojstring()).setAuthor(progressData.getAuthorEntity());
    LuaValue size = scenarioInfo.get(ScenarioMapInfo.SIZE);
    MapVersion version = new MapVersion().setDescription(scenarioInfo.get(ScenarioMapInfo.DESCRIPTION).tojstring().replaceAll("<LOC .*?>", "")).setWidth(size.get(1).toint()).setHeight(size.get(2).toint()).setHidden(false).setRanked(progressData.isRanked()).setMaxPlayers(scenarioInfo.get(ScenarioMapInfo.CONFIGURATIONS).get(ScenarioMapInfo.CONFIGURATION_STANDARD).get(ScenarioMapInfo.CONFIGURATION_STANDARD_TEAMS).get(1).get(ScenarioMapInfo.CONFIGURATION_STANDARD_TEAMS_ARMIES).length()).setVersion(scenarioInfo.get(ScenarioMapInfo.MAP_VERSION).toint());
    map.getVersions().add(version);
    version.setMap(map);
    progressData.setMapEntity(map);
    progressData.setMapVersionEntity(version);
    version.setFilename(STUPID_MAP_FOLDER_PREFIX + progressData.getFinalZipName());
    progressData.setFinalZipFile(this.fafApiProperties.getMap().getTargetDirectory().resolve(progressData.getFinalZipName()));
    if (Files.exists(progressData.getFinalZipFile())) {
        throw new ApiException(new Error(ErrorCode.MAP_NAME_CONFLICT, progressData.getFinalZipName()));
    }
    // this triggers validation
    mapRepository.save(map);
}
Also used : ProgrammingError(com.faforever.api.error.ProgrammingError) Error(com.faforever.api.error.Error) MapVersion(com.faforever.api.data.domain.MapVersion) LuaValue(org.luaj.vm2.LuaValue) Map(com.faforever.api.data.domain.Map) ApiException(com.faforever.api.error.ApiException)

Example 20 with ApiException

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

the class MapService method postProcessLuaFile.

private void postProcessLuaFile(MapUploadData progressData) {
    LuaValue scenarioInfo = progressData.getLuaScenarioInfo();
    Optional<Map> mapEntity = mapRepository.findOneByDisplayName(scenarioInfo.get(ScenarioMapInfo.NAME).toString());
    if (!mapEntity.isPresent()) {
        return;
    }
    Player author = mapEntity.get().getAuthor();
    if (author == null) {
        return;
    }
    if (author.getId() != progressData.getAuthorEntity().getId()) {
        throw new ApiException(new Error(ErrorCode.MAP_NOT_ORIGINAL_AUTHOR, mapEntity.get().getDisplayName()));
    }
    int newVersion = scenarioInfo.get(ScenarioMapInfo.MAP_VERSION).toint();
    if (mapEntity.get().getVersions().stream().anyMatch(mapVersion -> mapVersion.getVersion() == newVersion)) {
        throw new ApiException(new Error(ErrorCode.MAP_VERSION_EXISTS, mapEntity.get().getDisplayName(), newVersion));
    }
    progressData.setMapEntity(mapEntity.get());
}
Also used : Player(com.faforever.api.data.domain.Player) ProgrammingError(com.faforever.api.error.ProgrammingError) Error(com.faforever.api.error.Error) LuaValue(org.luaj.vm2.LuaValue) Map(com.faforever.api.data.domain.Map) ApiException(com.faforever.api.error.ApiException)

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