-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
15 changed files
with
336 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/main/java/com/faforever/api/mautic/MauticApiErrorHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.faforever.api.mautic; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.springframework.http.client.ClientHttpResponse; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.client.DefaultResponseErrorHandler; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
@Component | ||
public class MauticApiErrorHandler extends DefaultResponseErrorHandler { | ||
|
||
private final ObjectMapper objectMapper; | ||
|
||
public MauticApiErrorHandler(ObjectMapper objectMapper) { | ||
this.objectMapper = objectMapper; | ||
} | ||
|
||
@Override | ||
public void handleError(ClientHttpResponse response) throws IOException { | ||
try (InputStream inputStream = response.getBody()) { | ||
MauticErrorResponse errorResponse = objectMapper.readValue(inputStream, MauticErrorResponse.class); | ||
throw new MauticApiException(errorResponse.getErrors()); | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/faforever/api/mautic/MauticApiException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.faforever.api.mautic; | ||
|
||
import com.faforever.api.mautic.MauticErrorResponse.Error; | ||
import lombok.Getter; | ||
|
||
import java.util.List; | ||
|
||
@Getter | ||
public class MauticApiException extends RuntimeException { | ||
private final List<Error> errors; | ||
|
||
public MauticApiException(List<Error> errors) { | ||
super(errors.toString()); | ||
this.errors = errors; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/faforever/api/mautic/MauticErrorResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.faforever.api.mautic; | ||
|
||
import lombok.Data; | ||
import lombok.ToString; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@Data | ||
public class MauticErrorResponse { | ||
private List<Error> errors; | ||
|
||
@Data | ||
@ToString | ||
public static class Error { | ||
private int code; | ||
private String message; | ||
private Map<String, Object> details; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.faforever.api.mautic; | ||
|
||
import com.faforever.api.config.FafApiProperties; | ||
import com.faforever.api.config.FafApiProperties.Mautic; | ||
import com.google.common.annotations.VisibleForTesting; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.boot.web.client.RestTemplateBuilder; | ||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; | ||
import org.springframework.scheduling.annotation.Async; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.client.ResponseErrorHandler; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import javax.inject.Inject; | ||
import java.time.OffsetDateTime; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
/** | ||
* Provides access to a Mautic (Open Source Marketing Automation) instance via REST API. | ||
*/ | ||
@Service | ||
@ConditionalOnProperty(value = "faf-server.mautic.client-id") | ||
public class MauticService { | ||
|
||
private final RestTemplate restOperations; | ||
|
||
@Inject | ||
public MauticService(MappingJackson2HttpMessageConverter mauticApiMessageConverter, | ||
ResponseErrorHandler mauticApiErrorHandler, FafApiProperties properties) { | ||
this(mauticApiMessageConverter, mauticApiErrorHandler, properties, new RestTemplateBuilder()); | ||
} | ||
|
||
@VisibleForTesting | ||
MauticService(MappingJackson2HttpMessageConverter mauticApiMessageConverter, | ||
ResponseErrorHandler mauticApiErrorHandler, FafApiProperties properties, | ||
RestTemplateBuilder restTemplateBuilder) { | ||
|
||
Mautic mauticProperties = properties.getMautic(); | ||
|
||
restTemplateBuilder | ||
.additionalMessageConverters(mauticApiMessageConverter) | ||
.errorHandler(mauticApiErrorHandler) | ||
.rootUri(mauticProperties.getBaseUrl()); | ||
|
||
// TODO use as soon as this is solved: https://github.com/mautic/mautic/issues/5743 | ||
// ClientCredentialsResourceDetails details = new ClientCredentialsResourceDetails(); | ||
// details.setClientId(mauticProperties.getClientId()); | ||
// details.setClientSecret(mauticProperties.getClientSecret()); | ||
// details.setClientAuthenticationScheme(AuthenticationScheme.header); | ||
// details.setAccessTokenUri(mauticProperties.getAccessTokenUrl()); | ||
// restOperations = restTemplateBuilder.configure(new OAuth2RestTemplate(details)); | ||
|
||
// TODO for new, client ID needs to be a username and client secret the user's password. | ||
restTemplateBuilder = restTemplateBuilder.basicAuthorization(mauticProperties.getClientId(), mauticProperties.getClientSecret()); | ||
|
||
restOperations = restTemplateBuilder.build(); | ||
} | ||
|
||
@Async | ||
public CompletableFuture<Object> createOrUpdateContact(String email, String fafUserId, String fafUserName, String ipAddress, OffsetDateTime lastActive) { | ||
Map<String, Object> body = new HashMap<>(); | ||
|
||
// These are Mautic default fields. For some reason, these are camel case. | ||
Optional.ofNullable(email).ifPresent(s -> body.put("email", email)); | ||
Optional.ofNullable(ipAddress).ifPresent(s -> body.put("ipAddress", ipAddress)); | ||
Optional.ofNullable(lastActive).ifPresent(s -> body.put("lastActive", lastActive)); | ||
|
||
// These are Mautic "custom fields" that need to be created explicitly. For some reason, these are underscore case by default. | ||
Optional.ofNullable(fafUserId).ifPresent(s -> body.put("faf_user_id", fafUserId)); | ||
Optional.ofNullable(fafUserName).ifPresent(s -> body.put("faf_username", fafUserName)); | ||
|
||
return CompletableFuture.completedFuture(restOperations.postForObject("/contacts/new", body, Object.class)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/main/java/com/faforever/api/utils/RemoteAddressUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.faforever.api.utils; | ||
|
||
import lombok.experimental.UtilityClass; | ||
import org.springframework.util.Assert; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
|
||
@UtilityClass | ||
public class RemoteAddressUtil { | ||
/** Returns the remote address of the request, considering the {@code X-Forwarded-For} header. */ | ||
public String getRemoteAddress(HttpServletRequest request) { | ||
Assert.notNull(request, "The parameter 'request' must not be null"); | ||
|
||
String remoteAddr = request.getHeader("X-FORWARDED-FOR"); | ||
if (remoteAddr == null || remoteAddr.isEmpty()) { | ||
remoteAddr = request.getRemoteAddr(); | ||
} | ||
|
||
return remoteAddr; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.