diff --git a/app/controllers/nwbib/Application.java b/app/controllers/nwbib/Application.java index e8cf5fd..ff78281 100644 --- a/app/controllers/nwbib/Application.java +++ b/app/controllers/nwbib/Application.java @@ -951,20 +951,43 @@ private static List starredIds() { } public static Promise 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 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 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 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"); + } } diff --git a/conf/nwbib.routes b/conf/nwbib.routes index b445db6..65a9e1e 100644 --- a/conf/nwbib.routes +++ b/conf/nwbib.routes @@ -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 ?= "") \ No newline at end of file +PUT /:id controllers.nwbib.Application.put(id, secret ?= "") +DELETE /:id controllers.nwbib.Application.delete(id, secret ?= "") \ No newline at end of file