Search in sources :

Example 31 with ApiException

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

the class AvatarService method deleteAvatar.

@SneakyThrows
@Transactional
@Audit(messageTemplate = "Avatar ''{0}'' deleted.", expressions = "${avatarId}")
public void deleteAvatar(Integer avatarId) {
    final Avatar avatar = getExistingAvatar(avatarId);
    if (!avatar.getAssignments().isEmpty()) {
        throw new ApiException(new Error(ErrorCode.AVATAR_IN_USE, avatarId));
    }
    // TODO: 21.11.2017 !!!!!!!!!!!! HACK TO GET FILENAME FROM URL..... !!!!!!!!!!!!!!!
    final String fileName = getFileNameFromUrl(avatar.getUrl());
    final Path avatarImageFilePath = properties.getAvatar().getTargetDirectory().resolve(fileName);
    Files.deleteIfExists(avatarImageFilePath);
    avatarRepository.delete(avatar);
}
Also used : Path(java.nio.file.Path) ProgrammingError(com.faforever.api.error.ProgrammingError) Error(com.faforever.api.error.Error) Avatar(com.faforever.api.data.domain.Avatar) ApiException(com.faforever.api.error.ApiException) NotFoundApiException(com.faforever.api.error.NotFoundApiException) Audit(com.faforever.api.security.Audit) SneakyThrows(lombok.SneakyThrows) Transactional(org.springframework.transaction.annotation.Transactional)

Example 32 with ApiException

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

the class AvatarService method validateImageFile.

private void validateImageFile(String originalFilename, long avatarImageFileSize) {
    final int maxFileNameLength = properties.getAvatar().getMaxNameLength();
    final Integer maxFileSizeBytes = properties.getAvatar().getMaxSizeBytes();
    String extension = com.google.common.io.Files.getFileExtension(originalFilename);
    if (avatarImageFileSize > maxFileSizeBytes) {
        throw new ApiException(new Error(ErrorCode.FILE_SIZE_EXCEEDED, maxFileSizeBytes, avatarImageFileSize));
    }
    if (!properties.getAvatar().getAllowedExtensions().contains(extension)) {
        throw new ApiException(new Error(ErrorCode.UPLOAD_INVALID_FILE_EXTENSIONS, properties.getAvatar().getAllowedExtensions()));
    }
    if (originalFilename.length() > maxFileNameLength) {
        throw new ApiException(new Error(ErrorCode.FILE_NAME_TOO_LONG, maxFileNameLength, originalFilename.length()));
    }
}
Also used : ProgrammingError(com.faforever.api.error.ProgrammingError) Error(com.faforever.api.error.Error) ApiException(com.faforever.api.error.ApiException) NotFoundApiException(com.faforever.api.error.NotFoundApiException)

Example 33 with ApiException

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

the class ClanService method create.

@SneakyThrows
Clan create(String name, String tag, String description, Player creator) {
    if (!creator.getClanMemberships().isEmpty()) {
        throw new ApiException(new Error(ErrorCode.CLAN_CREATE_CREATOR_IS_IN_A_CLAN));
    }
    if (clanRepository.findOneByName(name).isPresent()) {
        throw new ApiException(new Error(ErrorCode.CLAN_NAME_EXISTS, name));
    }
    if (clanRepository.findOneByTag(tag).isPresent()) {
        throw new ApiException(new Error(ErrorCode.CLAN_TAG_EXISTS, name));
    }
    Clan clan = new Clan();
    clan.setName(name);
    clan.setTag(tag);
    clan.setDescription(description);
    clan.setFounder(creator);
    clan.setLeader(creator);
    ClanMembership membership = new ClanMembership();
    membership.setClan(clan);
    membership.setPlayer(creator);
    clan.setMemberships(Collections.singletonList(membership));
    // clan membership is saved over cascading, otherwise validation will fail
    clanRepository.save(clan);
    return clan;
}
Also used : Clan(com.faforever.api.data.domain.Clan) Error(com.faforever.api.error.Error) ProgrammingError(com.faforever.api.error.ProgrammingError) ClanMembership(com.faforever.api.data.domain.ClanMembership) ApiException(com.faforever.api.error.ApiException) SneakyThrows(lombok.SneakyThrows)

Example 34 with ApiException

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

the class AchievementService method updateSteps.

private UpdatedAchievementResponse updateSteps(int playerId, String achievementId, int steps, BiFunction<Integer, Integer, Integer> stepsFunction) {
    Achievement achievement = achievementRepository.getOne(achievementId);
    if (achievement.getType() != AchievementType.INCREMENTAL) {
        throw new ApiException(new Error(ACHIEVEMENT_NOT_INCREMENTAL, achievementId));
    }
    PlayerAchievement playerAchievement = getOrCreatePlayerAchievement(playerId, achievement, AchievementState.REVEALED);
    int currentSteps = MoreObjects.firstNonNull(playerAchievement.getCurrentSteps(), 0);
    int newCurrentSteps = stepsFunction.apply(currentSteps, steps);
    boolean newlyUnlocked = false;
    if (newCurrentSteps >= achievement.getTotalSteps()) {
        playerAchievement.setState(AchievementState.UNLOCKED);
        playerAchievement.setCurrentSteps(achievement.getTotalSteps());
        newlyUnlocked = playerAchievement.getState() != AchievementState.UNLOCKED;
    } else {
        playerAchievement.setCurrentSteps(newCurrentSteps);
    }
    playerAchievementRepository.save(playerAchievement);
    return new UpdatedAchievementResponse(achievementId, newlyUnlocked, playerAchievement.getState(), playerAchievement.getCurrentSteps());
}
Also used : Error(com.faforever.api.error.Error) PlayerAchievement(com.faforever.api.data.domain.PlayerAchievement) PlayerAchievement(com.faforever.api.data.domain.PlayerAchievement) Achievement(com.faforever.api.data.domain.Achievement) ApiException(com.faforever.api.error.ApiException)

Example 35 with ApiException

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

the class MapServiceTest method mapWithMoreRootFoldersInZip.

@Test
public void mapWithMoreRootFoldersInZip() throws IOException {
    String zipFilename = "scmp_037_invalid.zip";
    when(mapRepository.findOneByDisplayName(any())).thenReturn(Optional.empty());
    try (InputStream inputStream = loadMapResourceAsStream(zipFilename)) {
        byte[] mapData = ByteStreams.toByteArray(inputStream);
        try {
            instance.uploadMap(mapData, zipFilename, author, true);
        } catch (ApiException e) {
            assertThat(e, apiExceptionWithCode(ErrorCode.MAP_INVALID_ZIP));
        }
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ApiException(com.faforever.api.error.ApiException) Test(org.junit.Test)

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