diff --git a/src/main/java/at/porscheinformatik/weblate/spring/WeblateMessageSource.java b/src/main/java/at/porscheinformatik/weblate/spring/WeblateMessageSource.java index 02505cb..159496a 100644 --- a/src/main/java/at/porscheinformatik/weblate/spring/WeblateMessageSource.java +++ b/src/main/java/at/porscheinformatik/weblate/spring/WeblateMessageSource.java @@ -34,7 +34,6 @@ import org.springframework.http.ResponseEntity; import org.springframework.http.converter.StringHttpMessageConverter; import org.springframework.util.StringUtils; -import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestTemplate; import com.fasterxml.jackson.annotation.JsonProperty; @@ -178,16 +177,17 @@ public void setInitialCacheTimestamp(long initialCacheTimestamp) { * @return if async enabled */ public boolean isAsync() { - return async; + return async; } /** - * Use async loading - all operations will be performed in a single threaded {@link ExecutorService}. + * Use async loading - all operations will be performed in a single threaded + * {@link ExecutorService}. * * @param async if true loading will be performed asynchronously */ public void setAsync(boolean async) { - this.async = async; + this.async = async; } /** @@ -203,7 +203,14 @@ public Set getExistingLocales() { * This does not clear the cached translations. */ public void reloadExistingLocales() { - CompletableFuture loadTask = CompletableFuture.runAsync(() -> loadCodes(), executor); + CompletableFuture loadTask = CompletableFuture + .runAsync(this::loadCodes, executor) + .handle((val, e) -> { + if (e != null) { + logger.warn("Error reloading locales", e); + } + return val; + }); if (!async) { loadTask.join(); } @@ -359,14 +366,19 @@ private Properties loadTranslations(Locale locale, boolean reload) { * loaded (optional) */ private void loadTranslation(Locale language, Properties properties, long timestamp) { - CompletableFuture> loadCodesTask = CompletableFuture.supplyAsync(() -> loadCodes()); + CompletableFuture> loadCodesTask = CompletableFuture.supplyAsync(this::loadCodes); CompletableFuture loadTask = loadCodesTask.thenCompose(locales -> { String lang = existingLocales.get(language); if (lang != null) { - return loadTranslation(lang, properties, timestamp); + return CompletableFuture.runAsync(() -> loadTranslation(lang, properties, timestamp), executor); } return CompletableFuture.completedStage(null); + }).handle((val, e) -> { + if (e != null) { + logger.warn("Error loading translation for locale " + language, e); + } + return val; }); if (!async) { @@ -381,49 +393,46 @@ private static String formatTimestampIso(long timestamp) { return df.format(new Date(timestamp)); } - private CompletableFuture loadTranslation(String code, Properties properties, long timestamp) { - return CompletableFuture.runAsync(() -> { - String currentQuery = query; - if (timestamp > 0L) { - String timestampStr = formatTimestampIso(timestamp); - currentQuery += " AND (added:>=" + timestampStr + " OR changed:>=" + timestampStr + ")"; - } + private void loadTranslation(String code, Properties properties, long timestamp) { + String currentQuery = query; + if (timestamp > 0L) { + String timestampStr = formatTimestampIso(timestamp); + currentQuery += " AND (added:>=" + timestampStr + " OR changed:>=" + timestampStr + ")"; + } + RequestEntity request = RequestEntity + .get(baseUrl + "/api/translations/{project}/{component}/{languageCode}/units/?q={query}", + project, component, code, currentQuery) + .accept(MediaType.APPLICATION_JSON) + .build(); - try { - RequestEntity request = RequestEntity - .get(baseUrl + "/api/translations/{project}/{component}/{languageCode}/units/?q={query}", - project, component, code, currentQuery) - .accept(MediaType.APPLICATION_JSON) - .build(); - - if (restTemplate == null) { - restTemplate = createRestTemplate(); - } - - UnitsResponse responseBody; - while (true) { - ResponseEntity response = restTemplate.exchange(request, UnitsResponse.class); - responseBody = response.getBody(); - if (!response.getStatusCode().is2xxSuccessful() || responseBody == null) { - logger.warn(String.format("Got empty or non-200 response (status=%s, body=%s)", response.getStatusCode(), - response.getBody())); - break; - } + if (restTemplate == null) { + restTemplate = createRestTemplate(); + } - for (Unit unit : responseBody.results) { - properties.put(unit.code, unit.target[0]); - } + UnitsResponse body; + while (true) { + ResponseEntity response = restTemplate.exchange(request, UnitsResponse.class); + body = response.getBody(); + if (response.getStatusCodeValue() < 200 || response.getStatusCodeValue() >= 300 || body == null) { + logger.warn(String.format("Got empty or non-200 response (status=%s, body=%s)", response.getStatusCode(), + response.getBody())); + break; + } - if (responseBody.next == null) { - break; - } + for (Unit unit : body.results) { + properties.put(unit.code, unit.target[0]); + } + + if (body.next == null) { + break; + } - request = RequestEntity.get(responseBody.next.toURI()).accept(MediaType.APPLICATION_JSON).build(); - } - } catch (RestClientException | URISyntaxException e) { - logger.warn("Could not load translations (code=" + code + ")", e); + try { + request = RequestEntity.get(body.next.toURI()).accept(MediaType.APPLICATION_JSON).build(); + } catch (URISyntaxException e) { + throw new RuntimeException(e); } - }, executor); + } } private Map loadCodes() { @@ -431,26 +440,23 @@ private Map loadCodes() { return existingLocales; } - try { - RequestEntity request = RequestEntity.get(baseUrl + "/api/projects/{project}/languages/", project) - .accept(MediaType.APPLICATION_JSON).build(); + RequestEntity request = RequestEntity.get(baseUrl + "/api/projects/{project}/languages/", project) + .accept(MediaType.APPLICATION_JSON).build(); - if (restTemplate == null) { - restTemplate = createRestTemplate(); - } + if (restTemplate == null) { + restTemplate = createRestTemplate(); + } - ResponseEntity>> response = restTemplate.exchange(request, LIST_MAP_STRING_OBJECT); - List> body = response.getBody(); - if (response.getStatusCode().is2xxSuccessful() && body != null) { - existingLocales = body.stream() - .filter(this::containsTranslations) - .map(this::extractCode) - .filter(Objects::nonNull) - .collect(Collectors.toMap(this::deriveLocaleFromCode, Function.identity())); - } - } catch (RestClientException e) { - logger.warn("Could not load languages", e); + ResponseEntity>> response = restTemplate.exchange(request, LIST_MAP_STRING_OBJECT); + List> body = response.getBody(); + if (response.getStatusCodeValue() >= 200 && response.getStatusCodeValue() < 300 && body != null) { + existingLocales = body.stream() + .filter(this::containsTranslations) + .map(this::extractCode) + .filter(Objects::nonNull) + .collect(Collectors.toMap(this::deriveLocaleFromCode, Function.identity())); } + return existingLocales; }