Search in sources :

Example 1 with ProgrammingError

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

the class ClanServiceTest method acceptPlayerInvitationTokenInvalidPlayer.

@Test
public void acceptPlayerInvitationTokenInvalidPlayer() throws IOException {
    String stringToken = "1234";
    Clan clan = ClanFactory.builder().build();
    long expire = System.currentTimeMillis() + 1000 * 3;
    Jwt jwtToken = Mockito.mock(Jwt.class);
    when(jwtToken.getClaims()).thenReturn(String.format("{\"expire\":%s,\"newMember\":{\"id\":2},\"clan\":{\"id\":%s}}", expire, clan.getId()));
    when(jwtService.decodeAndVerify(any())).thenReturn(jwtToken);
    when(clanRepository.findById(clan.getId())).thenReturn(Optional.of(clan));
    try {
        instance.acceptPlayerInvitationToken(stringToken, null);
        fail();
    } catch (ProgrammingError e) {
        assertEquals("ClanMember does not exist: 2", e.getMessage());
    }
    verify(clanMembershipRepository, Mockito.never()).save(any(ClanMembership.class));
}
Also used : Clan(com.faforever.api.data.domain.Clan) Jwt(org.springframework.security.jwt.Jwt) ProgrammingError(com.faforever.api.error.ProgrammingError) ClanMembership(com.faforever.api.data.domain.ClanMembership) Test(org.junit.Test)

Example 2 with ProgrammingError

use of com.faforever.api.error.ProgrammingError 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 3 with ProgrammingError

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

the class AvatarService method readImageDimensions.

private Dimension readImageDimensions(InputStream imageInputStream, String imageFileName) throws IOException {
    final String fileExtension = com.google.common.io.Files.getFileExtension(imageFileName);
    final Iterator<ImageReader> imageReadersBySuffix = ImageIO.getImageReadersBySuffix(fileExtension);
    if (imageReadersBySuffix.hasNext()) {
        final ImageReader imageReader = imageReadersBySuffix.next();
        try {
            imageReader.setInput(new MemoryCacheImageInputStream(imageInputStream));
            final int width = imageReader.getWidth(imageReader.getMinIndex());
            final int height = imageReader.getHeight(imageReader.getMinIndex());
            return new Dimension(width, height);
        } finally {
            imageReader.dispose();
        }
    } else {
        throw new ProgrammingError("Unsupported image format. Could not read dimensions.");
    }
}
Also used : MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) ProgrammingError(com.faforever.api.error.ProgrammingError) Dimension(java.awt.Dimension) ImageReader(javax.imageio.ImageReader)

Aggregations

ProgrammingError (com.faforever.api.error.ProgrammingError)3 Clan (com.faforever.api.data.domain.Clan)2 ClanMembership (com.faforever.api.data.domain.ClanMembership)2 Jwt (org.springframework.security.jwt.Jwt)2 InvitationResult (com.faforever.api.clan.result.InvitationResult)1 Player (com.faforever.api.data.domain.Player)1 ApiException (com.faforever.api.error.ApiException)1 Error (com.faforever.api.error.Error)1 Dimension (java.awt.Dimension)1 ImageReader (javax.imageio.ImageReader)1 MemoryCacheImageInputStream (javax.imageio.stream.MemoryCacheImageInputStream)1 SneakyThrows (lombok.SneakyThrows)1 Test (org.junit.Test)1