Skip to content

Commit

Permalink
#15371 Return app config by domain if app id isn't specified (#85)
Browse files Browse the repository at this point in the history
* #15371 Return app config by domain if app id isn't specified

* #15371 Use Referer instead of Host header

* #16033. Fixed max resolution for bivariate layers

* 15371 change header from referer to X-Forwarded-Host

---------

Co-authored-by: kshmidman <[email protected]>
Co-authored-by: Polina Krukovich <[email protected]>
  • Loading branch information
3 people authored Oct 13, 2023
1 parent 7ffc8de commit 8897060
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.util.List;
import java.util.UUID;
Expand All @@ -30,6 +31,7 @@ public class UserProfileClient extends RestClientWithBearerAuth {
private static final String FEATURES_URL = "/features";
private static final String APP_URL = "/apps";
private static final String APP_URL_WITH_ID = "/apps/{id}";
private static final String APP_CONFIG_URL = "/apps/configuration";
private static final String DEFAULT_APP_ID_URL = "/apps/default_id";
private static final String CURRENT_USER_URL = "/users/current_user";
private final RestTemplate userProfileRestTemplate;
Expand Down Expand Up @@ -117,6 +119,19 @@ public AppDto getApp(UUID id) {
return sendAppRequest(APP_URL_WITH_ID, id, GET, null).getBody();
}

public AppDto getApp(String domain) {
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(APP_CONFIG_URL)
.queryParam("domain", domain);
LOG.info("Requester domain: {}", domain);
ResponseEntity<AppDto> response = userProfileRestTemplate
.exchange(builder.build().toString(), GET,
httpEntityWithUserBearerAuthIfPresentAndNoCacheHeader(null),
new ParameterizedTypeReference<>() {
});

return response.getBody();
}

public AppDto createApp(AppDto input) {
return sendAppRequest(APP_URL, null, POST, input).getBody();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.net.URI;
import java.util.List;
import java.util.ListIterator;
import java.util.UUID;
Expand Down Expand Up @@ -121,18 +122,25 @@ public List<LayerDetailsDto> getListOfLayers(@PathVariable("id") UUID appId) {
array = @ArraySchema(schema = @Schema(implementation = LayerDetailsDto.class))))
@PutMapping("/{id}/layers")
public List<LayerDetailsDto> updateListOfLayers(@PathVariable("id") UUID appId,
@RequestBody List<AppLayerUpdateDto> layers) {
@RequestBody List<AppLayerUpdateDto> layers) {
List<Layer> updatedLayers = layersApiService.updateApplicationLayers(appId, layers);
return updatedLayers.stream().map(LayerDetailsDto::fromLayer).toList();
}

@Operation(summary = "Get application config with features and user settings by id. Returns default app if no " +
"appId is provided", tags = {"Applications"})
@Operation(summary = "Get application config with features and user settings by id. Returns default app if " +
"no appId is provided and requester domain is unknown", tags = {"Applications"})
@ApiResponse(responseCode = "200",
content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
schema = @Schema(implementation = AppDto.class)))
@GetMapping(path = "/configuration")
public AppDto getAppConfig(@RequestParam(name = "appId", required = false) UUID appId) {
return applicationService.getAppConfig(appId);
public AppDto getAppConfig(@RequestParam(name = "appId", required = false) UUID appId,
@RequestHeader(name = "X-Forwarded-Host", required = false) String xForwardedHost) {
String domain = null;
try {
domain = new URI(xForwardedHost).getHost();
} catch (Exception ignored) {
}

return applicationService.getAppConfig(appId, domain);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private List<AnalyticsResponseDto> organizeResponse(List<Function> functions, Li

private List<AnalyticsStatisticsConfigurationDto> getAnalyticsConfigurationForApplication(UUID appUuid) {
try {
return Arrays.asList(objectMapper.treeToValue(applicationService.getAppConfig(appUuid)
return Arrays.asList(objectMapper.treeToValue(applicationService.getAppConfig(appUuid, null)
.getFeatures()
.stream()
.map(FeatureDto::getConfiguration)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,30 @@ public class ApplicationService {

private final UserProfileClient userProfileClient;

public AppDto getAppConfig(UUID appId) {
if (appId == null) {
appId = UUID.fromString(Objects.requireNonNull(userProfileClient.getDefaultAppId().getBody()));
public AppDto getAppConfig(UUID appId, String domain) {
AppDto appDto = null;
if (appId != null) {
appDto = userProfileClient.getApp(appId);
} else {
// TODO: one UPS call can be made instead of three in case requester domain is unknown.
// Corresponding UPS changes required
if (domain != null) {
appDto = userProfileClient.getApp(domain);
}
if (appDto == null) { // requester domain is unknown
appId = UUID.fromString(Objects.requireNonNull(userProfileClient.getDefaultAppId().getBody()));
appDto = userProfileClient.getApp(appId);
}
}
AppDto appDto = userProfileClient.getApp(appId);

if (AuthenticationUtil.isUserAuthenticated()) {
appDto.setUser(userProfileClient.getCurrentUser());
}

// TODO: remove this logic when feature configs from UPS are received within List<FeatureDto> features parameter
List<FeatureDto> features = userProfileClient.getUserAppFeatures(appId);
features.forEach(feature -> feature.setConfiguration(appDto.getFeaturesConfig().get(feature.getName())));
AppDto finalAppDto = appDto;
features.forEach(feature -> feature.setConfiguration(finalAppDto.getFeaturesConfig().get(feature.getName())));
appDto.setFeatures(features);
appDto.setFeaturesConfig(null);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ public void getAppConfigUnauthenticated() throws IOException {
MediaType.APPLICATION_JSON));

//WHEN
AppDto result = appsController.getAppConfig(appID);
AppDto result = appsController.getAppConfig(appID, null);

//THEN
assertNotNull(result.getDescription());
Expand Down Expand Up @@ -385,7 +385,7 @@ public void getAppConfigAuthenticated() throws IOException {
MediaType.APPLICATION_JSON));

//WHEN
AppDto result = appsController.getAppConfig(appID);
AppDto result = appsController.getAppConfig(appID, null);

//THEN
assertNotNull(result.getDescription());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public void calculateAnalyticsUsingAppConfigTest() throws JsonProcessingExceptio
array[1] = new AnalyticsStatisticsConfigurationDto("sumXWhereNoY", "population", "count");
array[2] = new AnalyticsStatisticsConfigurationDto("sumX", "population", null);

when(applicationService.getAppConfig(any(UUID.class))).thenReturn(appDto);
when(applicationService.getAppConfig(any(UUID.class), any())).thenReturn(appDto);
when(objectMapperMock.treeToValue(any(JsonNode.class), same(AnalyticsStatisticsConfigurationDto[].class))).thenReturn(array);
when(insightsApiGraphqlClient.analyticsTabQuery(geoJSONArgumentCaptor.capture(),
functionArgsArgumentCaptor.capture())).thenReturn(completableFuture);
Expand Down Expand Up @@ -228,7 +228,7 @@ public void calculateAnalyticsUsingAppConfigTestException() throws ExecutionExce

CompletableFuture<List<AnalyticsTabQuery.Function>> completableFuture = mock(CompletableFuture.class);

when(applicationService.getAppConfig(any(UUID.class))).thenReturn(appDto);
when(applicationService.getAppConfig(any(UUID.class), any())).thenReturn(appDto);
when(objectMapperMock.treeToValue(any(JsonNode.class), same(AnalyticsStatisticsConfigurationDto[].class))).thenReturn(array);
when(insightsApiGraphqlClient.analyticsTabQuery(any(GeoJSON.class), anyList())).thenReturn(completableFuture);

Expand Down

0 comments on commit 8897060

Please sign in to comment.