Skip to content

Commit

Permalink
Merge branch 'release/0.4.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Brutus5000 committed Jul 11, 2018
2 parents 408418e + 9183e6d commit 73b3af8
Show file tree
Hide file tree
Showing 18 changed files with 875 additions and 186 deletions.
16 changes: 16 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.faforever</groupId>
<artifactId>faf-moderator-client</artifactId>
<version>0.3.0</version>
<version>0.4.0</version>
<packaging>jar</packaging>

<name>faf-moderator-client</name>
Expand All @@ -29,7 +29,7 @@
<jitpack.jasminb.jsonapi-converter.version>0.8</jitpack.jasminb.jsonapi-converter.version>
<org.mapstruct.version>1.2.0.Final</org.mapstruct.version>
<org.apache.httpcomponents.httpclient.version>4.5.3</org.apache.httpcomponents.httpclient.version>
<com.fasterxml.jackson.datatype.version>2.8.8</com.fasterxml.jackson.datatype.version>
<jackson.version>2.9.2</jackson.version>
<org.jetbrains.annotations.version>13.0</org.jetbrains.annotations.version>
<javax.inject.version>1</javax.inject.version>
</properties>
Expand Down Expand Up @@ -63,7 +63,12 @@
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>${com.fasterxml.jackson.datatype.version}</version>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.github.jasminb</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.faforever.commons.api.dto.LegacyAccessLevel;
import com.faforever.commons.api.dto.Player;
import com.faforever.moderatorclient.api.dto.UpdateDto;
import com.faforever.moderatorclient.mapstruct.CycleAvoidingMappingContext;
import com.github.jasminb.jsonapi.JSONAPIDocument;
import com.github.jasminb.jsonapi.ResourceConverter;
Expand All @@ -10,6 +11,8 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
Expand Down Expand Up @@ -75,6 +78,10 @@ public FafApiCommunicationService(ResourceConverter resourceConverter, CycleAvoi
.rootUri(apiBaseUrl);
}

public RestOperations getRestOperations() {
return restOperations;
}

@SneakyThrows
private void authorize(String username, String password) {
log.debug("Configuring OAuth2 login with player = '{}', password=[hidden]", username);
Expand Down Expand Up @@ -132,6 +139,13 @@ public Object postRelationship(ElideRouteBuilder<?> routeBuilder, Object object)
return entity.getBody();
}

@SneakyThrows
public <T> T patch(ElideRouteBuilder<T> routeBuilder, UpdateDto<T> object) {
authorizedLatch.await();
cycleAvoidingMappingContext.clearCache();
return restOperations.exchange(routeBuilder.build(), HttpMethod.PATCH, new HttpEntity<>(object), routeBuilder.getDtoClass()).getBody();
}

@SneakyThrows
public <T> T patch(ElideRouteBuilder<T> routeBuilder, T object) {
authorizedLatch.await();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
package com.faforever.moderatorclient.api.domain;

import com.faforever.commons.api.dto.Avatar;
import com.faforever.commons.api.dto.AvatarAssignment;
import com.faforever.moderatorclient.api.ElideRouteBuilder;
import com.faforever.moderatorclient.api.FafApiCommunicationService;
import com.faforever.moderatorclient.api.dto.AvatarAssignmentUpdate;
import com.faforever.moderatorclient.api.dto.AvatarMetadata;
import com.faforever.moderatorclient.mapstruct.AvatarAssignmentMapper;
import com.faforever.moderatorclient.ui.domain.AvatarAssignmentFX;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;

import java.io.File;
import java.util.List;

@Service
@Slf4j
public class AvatarService {
private final FafApiCommunicationService fafApi;
private final AvatarAssignmentMapper avatarAssignmentMapper;

public AvatarService(FafApiCommunicationService fafApi) {
public AvatarService(FafApiCommunicationService fafApi, AvatarAssignmentMapper avatarAssignmentMapper) {
this.fafApi = fafApi;
this.avatarAssignmentMapper = avatarAssignmentMapper;
}

public List<Avatar> getAll() {
Expand Down Expand Up @@ -59,4 +73,94 @@ public List<Avatar> findAvatarsByAssignedUser(@NotNull String pattern) {
log.trace("found {} avatars", result.size());
return result;
}

public void uploadAvatar(String name, File avatarImageFile) {
HttpEntity<LinkedMultiValueMap<String, Object>> requestEntity = createAvatarMultipartRequest(name, avatarImageFile);
final String route = "/avatars/upload";
log.debug("Sending API request: {}", route);
fafApi.getRestOperations().exchange(
route,
HttpMethod.POST,
requestEntity,
String.class
);
}

public void reuploadAvatar(String avatarId, String name, File avatarImageFile) {
HttpEntity<LinkedMultiValueMap<String, Object>> requestEntity = createAvatarMultipartRequest(name, avatarImageFile);
final String route = "/avatars/{0}/upload";
log.debug("Sending API request: {}", route);
fafApi.getRestOperations().exchange(
route,
HttpMethod.POST,
requestEntity,
String.class,
avatarId
);
}

public void deleteAvatar(String avatarId) {
final String route = "/avatars/{0}";
log.debug("Sending API request: {}", route);
fafApi.getRestOperations().delete(route, avatarId);
}

public List<Avatar> getAllAvatarsWithPlayerAssignments() {
return fafApi.getAll(ElideRouteBuilder.of(Avatar.class)
.addInclude("assignments")
.addInclude("assignments.player"));
}

public void updateAvatarMetadata(String avatarId, String name) {
fafApi.patch(ElideRouteBuilder.of(Avatar.class).id(avatarId),
(Avatar) new Avatar().setTooltip(name).setId(avatarId));
}

@NotNull
private HttpEntity<LinkedMultiValueMap<String, Object>> createAvatarMultipartRequest(String name, File avatarImageFile) {
LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
map.add("file", new FileSystemResource(avatarImageFile));
map.add("metadata", new AvatarMetadata().setName(name));
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
return new HttpEntity<>(map, headers);
}

public String createAvatarAssignment(AvatarAssignmentFX avatarAssignmentFX) {
return createAvatarAssignment(avatarAssignmentMapper.map(avatarAssignmentFX));
}

private String createAvatarAssignment(AvatarAssignment avatarAssignment) {
log.debug("Creating avatar assignment");
return fafApi.post(ElideRouteBuilder.of(AvatarAssignment.class), avatarAssignment).getId();
}

public void patchAvatarAssignment(AvatarAssignmentFX avatarAssignmentFX) {
patchAvatarAssignment(avatarAssignmentMapper.map(avatarAssignmentFX));
}

private void patchAvatarAssignment(AvatarAssignment avatarAssignment) {
log.debug("Patching avatar assignmenet id: " + avatarAssignment.getId());
ElideRouteBuilder<AvatarAssignment> routeBuilder = ElideRouteBuilder.of(AvatarAssignment.class)
.id(avatarAssignment.getId());
fafApi.patch(routeBuilder, avatarAssignment);
}

public void patchAvatarAssignment(AvatarAssignmentUpdate avatarAssignmentUpdate) {
log.debug("Patching avatar assignmenet id: " + avatarAssignmentUpdate.getId());
ElideRouteBuilder<AvatarAssignment> routeBuilder = ElideRouteBuilder.of(AvatarAssignment.class)
.id(avatarAssignmentUpdate.getId());
fafApi.patch(routeBuilder, avatarAssignmentUpdate);
}

public void removeAvatarAssignment(AvatarAssignmentFX avatarAssignmentFX) {
removeAvatarAssignment(avatarAssignmentMapper.map(avatarAssignmentFX));
}

public void removeAvatarAssignment(AvatarAssignment avatarAssignment) {
log.debug("Removing avatar assignmenet id: " + avatarAssignment.getId());
ElideRouteBuilder<AvatarAssignment> routeBuilder = ElideRouteBuilder.of(AvatarAssignment.class)
.id(avatarAssignment.getId());
fafApi.delete(routeBuilder);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public String createUserNote(UserNote userNote) {
}

public UserNoteFX patchUserNote(UserNote userNote) {
log.debug("Patching UserNote of id: ", userNote.getId());
log.debug("Patching UserNote of id: " + userNote.getId());
return userNoteMapper.map(fafApi.patch(ElideRouteBuilder.of(UserNote.class).id(userNote.getId()), userNote));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.faforever.moderatorclient.api.dto;

import com.faforever.commons.api.dto.AvatarAssignment;
import com.github.jasminb.jsonapi.annotations.Id;
import com.github.jasminb.jsonapi.annotations.Type;
import lombok.Value;

import java.time.OffsetDateTime;
import java.util.Optional;

@Value
@Type("avatarAssignment")
public class AvatarAssignmentUpdate implements UpdateDto<AvatarAssignment> {
@Id
private String id;
private Optional<Boolean> selected;
private Optional<OffsetDateTime> expiresAt;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.faforever.moderatorclient.api.dto;

import lombok.Data;

@Data
public class AvatarMetadata {
private String name;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.faforever.moderatorclient.api.dto;

public interface UpdateDto<T> {
String getId();
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.core.type.filter.AnnotationTypeFilter;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.github.nocatch.NoCatch.noCatch;
import static java.lang.Class.forName;
Expand All @@ -21,15 +21,18 @@ public class JsonApiConfig {
@Bean
public ResourceConverter resourceConverter(ObjectMapper objectMapper) {
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
return new ResourceConverter(objectMapper, findJsonApiTypes("com.faforever.commons.api.dto"));
return new ResourceConverter(objectMapper, findJsonApiTypes("com.faforever.moderatorclient.api.dto", "com.faforever.commons.api.dto"));
}

private Class<?>[] findJsonApiTypes(String scanPackage) {
ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);
provider.addIncludeFilter(new AnnotationTypeFilter(Type.class));
List<Class> classes = provider.findCandidateComponents(scanPackage).stream()
.map(beanDefinition -> noCatch(() -> (Class) forName(beanDefinition.getBeanClassName())))
.collect(Collectors.toList());
private Class<?>[] findJsonApiTypes(String... scanPackages) {
List<Class<?>> classes = new ArrayList<>();
for (String packageName : scanPackages) {
ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);
provider.addIncludeFilter(new AnnotationTypeFilter(Type.class));
provider.findCandidateComponents(packageName).stream()
.map(beanDefinition -> noCatch(() -> (Class) forName(beanDefinition.getBeanClassName())))
.forEach(classes::add);
}
return classes.toArray(new Class<?>[classes.size()]);
}
}
Loading

0 comments on commit 73b3af8

Please sign in to comment.