diff --git a/library/src/main/java/com/fidesmo/fdsm/FidesmoApiClient.java b/library/src/main/java/com/fidesmo/fdsm/FidesmoApiClient.java index 8874973..fd2d132 100644 --- a/library/src/main/java/com/fidesmo/fdsm/FidesmoApiClient.java +++ b/library/src/main/java/com/fidesmo/fdsm/FidesmoApiClient.java @@ -51,9 +51,10 @@ import java.nio.charset.StandardCharsets; import java.util.Locale; import java.util.Map; +import java.text.MessageFormat; public class FidesmoApiClient { - public static final String APIv2 = "https://api.fidesmo.com/"; + public static final String APIv3 = "https://api.fidesmo.com/v3"; public static final String APPS_URL = "apps%s"; public static final String APP_INFO_URL = "apps/%s"; @@ -95,7 +96,7 @@ public class FidesmoApiClient { @Deprecated public FidesmoApiClient() { - this(APIv2, null, null); + this(APIv3, null, null); } public FidesmoApiClient(String url, ClientAuthentication authentication, OutputStream apidump) { @@ -111,7 +112,7 @@ public FidesmoApiClient(String url, ClientAuthentication authentication, OutputS } public FidesmoApiClient(ClientAuthentication authentication, OutputStream apidump) { - this(APIv2, authentication, apidump); + this(APIv3, authentication, apidump); } public CloseableHttpResponse get(URI uri) throws IOException { @@ -193,7 +194,7 @@ public JsonNode rpc(URI uri, JsonNode request) throws IOException { req.setHeader(HttpHeaders.ACCEPT, ContentType.APPLICATION_JSON.toString()); req.setHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString()); - req.setHeader("Protocol-Requirements", "parametrisedTranslations"); + req.setHeader(HttpHeaders.ACCEPT_LANGUAGE, Locale.getDefault().toLanguageTag()); try (CloseableHttpResponse response = transmit(req)) { if (apidump != null) { @@ -242,25 +243,40 @@ public static String getVersion() { // Prefer English if system locale is not present // to convert a possible multilanguage node to a string - public static String lamei18n(JsonNode n) { + public static String lamei18n(JsonNode n) { // For missing values if (n == null) return ""; if (!n.isEmpty()) { - //Check if JSON comes in new Translation format, it can be multilanguage - JsonNode jsonNodeFormat = (n.has("text")) ? n.get("text") : n; - try { - Map langs = mapper.convertValue(jsonNodeFormat, new TypeReference>() { + boolean isNewFormat = n.has("text"); + if (isNewFormat) { + return FidesmoApiClient.buildMessageWithParams(n); + } + else { + Map langs = mapper.convertValue(n, new TypeReference>() { }); Map.Entry first = langs.entrySet().iterator().next(); return langs.getOrDefault(Locale.getDefault().getLanguage(), langs.getOrDefault("en", first.getValue())).toString(); } - catch (Exception e) { - return jsonNodeFormat.asText(); - } - } else { return n.asText(); } } + + + public static String buildMessageWithParams(JsonNode n) { + + String text = n.path("text").asText(); + + JsonNode paramsNode = n.path("params"); + + // Convert the params array to String[] + String[] params = new String[paramsNode.size()]; + for (int i = 0; i < paramsNode.size(); i++) { + params[i] = paramsNode.get(i).asText(); + } + + MessageFormat mf = new MessageFormat(text.replace("'", "''")); + return mf.format(params); + } } diff --git a/library/src/main/java/com/fidesmo/fdsm/ServiceDeliverySession.java b/library/src/main/java/com/fidesmo/fdsm/ServiceDeliverySession.java index 18e259e..dd36bb4 100644 --- a/library/src/main/java/com/fidesmo/fdsm/ServiceDeliverySession.java +++ b/library/src/main/java/com/fidesmo/fdsm/ServiceDeliverySession.java @@ -170,7 +170,7 @@ public DeliveryResult deliver(BIBO bibo, String appId, String serviceId) throws JsonNode delivery = client.rpc(client.getURI(FidesmoApiClient.SERVICE_DELIVER_URL), deliveryRequest); String sessionId = delivery.get("sessionId").asText(); - + logger.info("Delivering: {}", FidesmoApiClient.lamei18n(description.get("title"))); logger.info("Session ID: {}", sessionId); diff --git a/library/src/test/java/com/fidesmo/fdsm/TranslationsTest.java b/library/src/test/java/com/fidesmo/fdsm/TranslationsTest.java new file mode 100644 index 0000000..06c0906 --- /dev/null +++ b/library/src/test/java/com/fidesmo/fdsm/TranslationsTest.java @@ -0,0 +1,68 @@ +package com.fidesmo.fdsm; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import org.testng.annotations.Test; +import com.fasterxml.jackson.databind.ObjectMapper; + +import static org.testng.Assert.assertEquals; + +public class TranslationsTest { + + // @Test + public void returnMessagesInOldFormat() throws JsonProcessingException { + String jsonString = "{ \"fr\" : \"Connecter votre carte de paiement\",\n" + + " \"it\" : \"Associa carta di pagamento\",\n" + + " \"nl\" : \"Koppel uw betaalkaart\",\n" + + " \"de\" : \"Karte verbinden\",\n" + + " \"ru\" : \"Подключить платежную карту\",\n" + + " \"sv\" : \"Anslut betalkort\",\n" + + " \"en\" : \"Connect payment card\"\n}"; + + ObjectMapper objectMapper = new ObjectMapper(); + JsonNode jsonNode = objectMapper.readTree(jsonString); + + assertEquals(FidesmoApiClient.lamei18n(jsonNode), "Connect payment card"); + } + @Test + public void returnMessagesInNewFormatWithParams() throws JsonProcessingException { + String jsonString = "{ \"id\": \"service.statuses.success\", \"text\": \"Congrats! Now you can pay with your {0}\", \"params\": [ \"Ring!\" ] }"; + + // Parse JSON to JsonNode + ObjectMapper objectMapper = new ObjectMapper(); + JsonNode jsonNode = objectMapper.readTree(jsonString); + + assertEquals(FidesmoApiClient.lamei18n(jsonNode), "Congrats! Now you can pay with your Ring!"); + } + + @Test + public void returnMessagesInNewFormatWithMultipleParams() throws JsonProcessingException { + String jsonString = "{ \"id\": \"service.statuses.success\", \"text\": \"Congrats! Now you can pay with your {0} from {1}\", \"params\": [ \"Ring!\", \"your bank\" ] }"; + + // Parse JSON to JsonNode + ObjectMapper objectMapper = new ObjectMapper(); + JsonNode jsonNode = objectMapper.readTree(jsonString); + + assertEquals(FidesmoApiClient.lamei18n(jsonNode), "Congrats! Now you can pay with your Ring! from your bank"); + } + + @Test + public void returnMessagesInNewFormatWithApostrophes() throws JsonProcessingException { + String jsonString = "{ \"id\": \"service.statuses.success\", \"text\": \"You're now able to pay with your {0}\", \"params\": [ \"Ring!\"] }"; + + ObjectMapper objectMapper = new ObjectMapper(); + JsonNode jsonNode = objectMapper.readTree(jsonString); + + assertEquals(FidesmoApiClient.lamei18n(jsonNode), "You're now able to pay with your Ring!"); + } + + @Test + public void returnMessagesInNewFormatWithoutParams() throws JsonProcessingException { + String jsonString = "{ \"id\": \"service.statuses.success\", \"text\": \"Your card has been removed\"}"; + + ObjectMapper objectMapper = new ObjectMapper(); + JsonNode jsonNode = objectMapper.readTree(jsonString); + + assertEquals(FidesmoApiClient.lamei18n(jsonNode), "Your card has been removed"); + } +} diff --git a/tool/src/main/java/com/fidesmo/fdsm/CommandLineInterface.java b/tool/src/main/java/com/fidesmo/fdsm/CommandLineInterface.java index 2a6e271..c9f826a 100644 --- a/tool/src/main/java/com/fidesmo/fdsm/CommandLineInterface.java +++ b/tool/src/main/java/com/fidesmo/fdsm/CommandLineInterface.java @@ -111,7 +111,7 @@ protected static void inspectEnvironment(OptionSet args) { // API URL try { - String url = new URL(System.getenv().getOrDefault(ENV_FIDESMO_API_URL, FidesmoApiClient.APIv2)).toString(); + String url = new URL(System.getenv().getOrDefault(ENV_FIDESMO_API_URL, FidesmoApiClient.APIv3)).toString(); // normalize URL apiurl = url.endsWith("/") ? url : url + "/"; } catch (MalformedURLException e) { diff --git a/tool/src/main/java/com/fidesmo/fdsm/Main.java b/tool/src/main/java/com/fidesmo/fdsm/Main.java index d594c4b..834c138 100644 --- a/tool/src/main/java/com/fidesmo/fdsm/Main.java +++ b/tool/src/main/java/com/fidesmo/fdsm/Main.java @@ -326,9 +326,7 @@ public static void main(String[] argv) { DeliveryUrl delivery = DeliveryUrl.parse(args.valueOf(OPT_RUN)); if (delivery.isWebSocket()) { - if (fidesmoMetadata.isPresent()) { - fidesmoMetadata.get().selectEmpty(bibo); - } + fidesmoMetadata.ifPresent(fidesmoCard -> fidesmoCard.selectEmpty(bibo)); boolean success = WsClient.execute(new URI(delivery.getService()), bibo, auth).join().isSuccess(); if (!success) { fail("Fail to run a script");