Skip to content

Commit

Permalink
Add DELETE route for index deletions from Strapi (RPB-230)
Browse files Browse the repository at this point in the history
  • Loading branch information
fsteeg committed Dec 19, 2024
1 parent b2e8c88 commit f389ec4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
45 changes: 34 additions & 11 deletions app/controllers/nwbib/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -951,20 +951,43 @@ private static List<String> starredIds() {
}

public static Promise<Result> put(String id, String secret) throws FileNotFoundException, RecognitionException, IOException {
File input = new File("conf/output/test-output-strapi.json");
File output = new File("conf/output/test-output-0.json");
Files.write(Paths.get(input.getAbsolutePath()), request().body().asJson().toString().getBytes(Charsets.UTF_8));
ETL.main(new String[] {"conf/rpb-test-titel-to-lobid.flux"});
String result = Files.readAllLines(Paths.get(output.getAbsolutePath())).stream().collect(Collectors.joining("\n"));
boolean authorized = !secret.trim().isEmpty() && secret.equals(CONFIG.getString("secret"));
if (authorized) {
Cache.remove(String.format("/%s", id));
String url = "http://weywot3:9200/resources-rpb-test/resource/"
+ URLEncoder.encode("https://lobid.org/resources/" + id, "UTF-8");
WSRequest request = WS.url(url).setHeader("Content-Type", "application/json");
return request.put(result).map(response -> status(response.getStatus(), response.getBody()));
return transformAndIndex(id, request().body().asJson());
} else {
return Promise.pure(unauthorized(secret));
}
}

public static Promise<Result> delete(String id, String secret) throws FileNotFoundException, RecognitionException, IOException {
boolean authorized = !secret.trim().isEmpty() && secret.equals(CONFIG.getString("secret"));
if (authorized) {
return deleteFromIndex(id);
} else {
return Promise.pure(unauthorized());
return Promise.pure(unauthorized(secret));
}
}

private static Promise<Result> deleteFromIndex(String id) throws UnsupportedEncodingException {
Cache.remove(String.format("/%s", id));
WSRequest request = WS.url(elasticsearchUrl(id)).setHeader("Content-Type", "application/json");
return request.delete().map(response -> status(response.getStatus(), response.getBody()));
}

private static Promise<Result> transformAndIndex(String id, JsonNode jsonBody)
throws IOException, FileNotFoundException, RecognitionException, UnsupportedEncodingException {
File input = new File("conf/output/test-output-strapi.json");
File output = new File("conf/output/test-output-0.json");
Files.write(Paths.get(input.getAbsolutePath()), jsonBody.toString().getBytes(Charsets.UTF_8));
ETL.main(new String[] {"conf/rpb-test-titel-to-lobid.flux"});
String result = Files.readAllLines(Paths.get(output.getAbsolutePath())).stream().collect(Collectors.joining("\n"));
Cache.remove(String.format("/%s", id));
WSRequest request = WS.url(elasticsearchUrl(id)).setHeader("Content-Type", "application/json");
return request.put(result).map(response -> status(response.getStatus(), response.getBody()));
}

private static String elasticsearchUrl(String id) throws UnsupportedEncodingException {
return "http://weywot3:9200/resources-rpb-test/resource/"
+ URLEncoder.encode("https://lobid.org/resources/" + id, "UTF-8");
}
}
3 changes: 2 additions & 1 deletion conf/nwbib.routes
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ GET /cgi-bin/wwwalleg/:name.pl controllers.nwbib.Application.showPl(name, d
GET /sw/:rpbId controllers.nwbib.Application.showSw(rpbId)
GET /o:id controllers.nwbib.Application.searchSpatial(id, from:Int?=0, size:Int?=25, format?="html")
GET /:id controllers.nwbib.Application.show(id, format ?= "")
PUT /:id controllers.nwbib.Application.put(id, secret ?= "")
PUT /:id controllers.nwbib.Application.put(id, secret ?= "")
DELETE /:id controllers.nwbib.Application.delete(id, secret ?= "")

0 comments on commit f389ec4

Please sign in to comment.