Skip to content

Commit

Permalink
Adapt for new translations format
Browse files Browse the repository at this point in the history
  • Loading branch information
diegomallada1 committed Oct 17, 2024
1 parent a14f42b commit dfb1005
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
27 changes: 18 additions & 9 deletions library/src/main/java/com/fidesmo/fdsm/FidesmoApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,11 @@ public CloseableHttpResponse transmit(HttpRequestBase request) throws IOExceptio
}

CloseableHttpResponse response = http.execute(request, context);
int responsecode = response.getStatusLine().getStatusCode();
if (responsecode < 200 || responsecode > 299) {
int responseCode = response.getStatusLine().getStatusCode();
if (responseCode < 200 || responseCode > 299) {
String message = response.getStatusLine() + "\n" + IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8);
response.close();
throw new HttpResponseException(responsecode, message);
throw new HttpResponseException(responseCode, message);
}
return response;
}
Expand Down Expand Up @@ -193,6 +193,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");

try (CloseableHttpResponse response = transmit(req)) {
if (apidump != null) {
Expand Down Expand Up @@ -241,15 +242,23 @@ 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.size() > 0) {
Map<String, Object> langs = mapper.convertValue(n, new TypeReference<Map<String, Object>>() {
});
Map.Entry<String, Object> first = langs.entrySet().iterator().next();
return langs.getOrDefault(Locale.getDefault().getLanguage(), langs.getOrDefault("en", first.getValue())).toString();
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<String, Object> langs = mapper.convertValue(jsonNodeFormat, new TypeReference<Map<String, Object>>() {
});
Map.Entry<String, Object> 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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ public String toString() {
}
}

public static ServiceDeliverySession.DeliveryResult deliverService(final RunnableFuture<DeliveryResult> serviceDelivery) {
public static DeliveryResult deliverService(final RunnableFuture<DeliveryResult> serviceDelivery) {
Thread cleanup = new Thread(() -> {
System.err.println("\nCtrl-C received, cancelling delivery");
serviceDelivery.cancel(true);
Expand All @@ -548,7 +548,7 @@ public static ServiceDeliverySession.DeliveryResult deliverService(final Runnabl
try {
// Run in current thread
serviceDelivery.run();
ServiceDeliverySession.DeliveryResult result = serviceDelivery.get();
DeliveryResult result = serviceDelivery.get();
ran = true;
return result;
} catch (ExecutionException e) {
Expand Down
10 changes: 5 additions & 5 deletions tool/src/main/java/com/fidesmo/fdsm/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -494,15 +494,15 @@ private static List<FidesmoApp> queryApps(FidesmoApiClient client, List<byte[]>
List<FidesmoApp> result = new ArrayList<>();
// Construct list in one go
for (byte[] app : apps) {
JsonNode appdesc = client.rpc(client.getURI(FidesmoApiClient.APP_INFO_URL, HexUtils.bin2hex(app)));
JsonNode appDesc = client.rpc(client.getURI(FidesmoApiClient.APP_INFO_URL, HexUtils.bin2hex(app)));
// Multilanguague
String appID = HexUtils.bin2hex(app);
String appName = FidesmoApiClient.lamei18n(appdesc.get("name"));
String appVendor = FidesmoApiClient.lamei18n(appdesc.get("organization").get("name"));
String appName = FidesmoApiClient.lamei18n(appDesc.get("name"));
String appVendor = FidesmoApiClient.lamei18n(appDesc.get("organization").get("name"));
FidesmoApp fidesmoApp = new FidesmoApp(app, appName, appVendor);
// Fetch services
JsonNode services = client.rpc(client.getURI(FidesmoApiClient.APP_SERVICES_URL, appID));
if (services.size() > 0) {
if (!services.isEmpty()) {
for (JsonNode s : services) {
if (verbose) {
JsonNode service = client.rpc(client.getURI(FidesmoApiClient.SERVICE_URL, appID, s.asText()));
Expand All @@ -523,7 +523,7 @@ private static void printApps(List<FidesmoApp> apps, PrintStream out, boolean ve
out.println("# appId - name and vendor");
for (FidesmoApp app : apps) {
out.println(HexUtils.bin2hex(app.id).toLowerCase() + " - " + app.name + " (by " + app.vendor + ")");
if (app.services.size() > 0) {
if (!app.services.isEmpty()) {
if (verbose) {
for (FidesmoService service : app.services) {
out.println(" " + service.name + " - " + service.description);
Expand Down

0 comments on commit dfb1005

Please sign in to comment.