From f5bd22d4c4fc34d7145bbae822ecd53fd7d548cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olov=20Ylinenp=C3=A4=C3=A4?= Date: Thu, 21 Nov 2024 16:50:38 +0100 Subject: [PATCH] refactor(emm): Use HttpTools --- emm/src/main/java/whelk/Dump.java | 22 +++++++++------------- emm/src/main/java/whelk/EmmChangeSet.java | 9 +++------ emm/src/main/java/whelk/EmmServlet.java | 15 +++++---------- 3 files changed, 17 insertions(+), 29 deletions(-) diff --git a/emm/src/main/java/whelk/Dump.java b/emm/src/main/java/whelk/Dump.java index 5c5315a4c7..ab459906d4 100644 --- a/emm/src/main/java/whelk/Dump.java +++ b/emm/src/main/java/whelk/Dump.java @@ -2,6 +2,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import whelk.util.http.HttpTools; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -29,8 +30,6 @@ import java.util.Map; import java.util.Set; -import static whelk.util.Jackson.mapper; - public class Dump { /* Here is how these dumps work: * When someone asks for a dump of particular category, we check if we have one already. @@ -55,6 +54,7 @@ public class Dump { */ private static final Logger logger = LogManager.getLogger(Dump.class); private static final String DUMP_END_MARKER = "_DUMP_END_MARKER\n"; // Must be 17 bytes + private static final String JSON_CONTENT_TYPE = "application/json"; public static void sendDumpResponse(Whelk whelk, String apiBaseUrl, HttpServletRequest req, HttpServletResponse res) throws IOException, SQLException { String dump = req.getParameter("dump"); @@ -104,10 +104,7 @@ private static void sendDumpIndexResponse(String apiBaseUrl, HttpServletResponse responseObject.put("categories", categoriesList); responseObject.put("warning", "This description of the available dump categories is a temporary one which will NOT look like this for long. Be careful not to rely on the format or even existence of this particular page."); - String jsonResponse = mapper.writeValueAsString(responseObject); - BufferedWriter writer = new BufferedWriter( res.getWriter() ); - writer.write(jsonResponse); - writer.close(); + HttpTools.sendResponse(res, responseObject, JSON_CONTENT_TYPE); } private static void sendDumpPageResponse(Whelk whelk, String apiBaseUrl, String dump, Path dumpFilePath, long offsetLines, HttpServletResponse res) throws IOException { @@ -158,8 +155,8 @@ private static void sendDumpPageResponse(Whelk whelk, String apiBaseUrl, String if (17 == file.read(lineBuffer)) { recordIdsOnPage.add(new String(lineBuffer, StandardCharsets.UTF_8).trim()); } else { - logger.error("Suspected corrupt dump (non-17-byte line detected): " + dumpFilePath); - res.sendError(500); + logger.error("Suspected corrupt dump (non-17-byte line detected): {}", dumpFilePath); + HttpTools.sendError(res, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ""); return; } } @@ -167,6 +164,8 @@ private static void sendDumpPageResponse(Whelk whelk, String apiBaseUrl, String } } catch (IOException | InterruptedException e) { logger.error("Failed reading dumpfile: " + dumpFilePath, e); + HttpTools.sendError(res, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ""); + return; } BasicFileAttributes attributes = Files.readAttributes(dumpFilePath, BasicFileAttributes.class); @@ -221,12 +220,9 @@ private static void sendFormattedResponse(Whelk whelk, String apiBaseUrl, String } - String jsonResponse = mapper.writeValueAsString(responseObject); - BufferedWriter writer = new BufferedWriter( res.getWriter() ); - writer.write(jsonResponse); - writer.close(); + HttpTools.sendResponse(res, responseObject, JSON_CONTENT_TYPE); } - + private static void invalidateIfOld(Path dumpFilePath) { try { if (!Files.exists(dumpFilePath)) diff --git a/emm/src/main/java/whelk/EmmChangeSet.java b/emm/src/main/java/whelk/EmmChangeSet.java index ece6e119e0..bdef523b89 100644 --- a/emm/src/main/java/whelk/EmmChangeSet.java +++ b/emm/src/main/java/whelk/EmmChangeSet.java @@ -2,9 +2,9 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import whelk.util.http.HttpTools; import javax.servlet.http.HttpServletResponse; -import java.io.BufferedWriter; import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; @@ -18,7 +18,7 @@ import java.util.LinkedHashMap; import java.util.List; -import static whelk.util.Jackson.mapper; +import static whelk.EmmServlet.AS2_CONTENT_TYPE; public class EmmChangeSet { public static final int TARGET_HITS_PER_PAGE = 100; @@ -64,10 +64,7 @@ static void sendChangeSet(Whelk whelk, HttpServletResponse res, String until, St orderedItems.add(activityInStream); } - String jsonResponse = mapper.writeValueAsString(responseObject); - BufferedWriter writer = new BufferedWriter( res.getWriter() ); - writer.write(jsonResponse); - writer.close(); + HttpTools.sendResponse(res, responseObject, AS2_CONTENT_TYPE); } /** diff --git a/emm/src/main/java/whelk/EmmServlet.java b/emm/src/main/java/whelk/EmmServlet.java index 274cd85db8..007c92c8f3 100644 --- a/emm/src/main/java/whelk/EmmServlet.java +++ b/emm/src/main/java/whelk/EmmServlet.java @@ -2,19 +2,20 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import whelk.util.http.HttpTools; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import java.io.BufferedWriter; import java.util.ArrayList; import java.util.LinkedHashMap; -import static whelk.util.Jackson.mapper; - public class EmmServlet extends HttpServlet { private final Logger logger = LogManager.getLogger(this.getClass()); private final Whelk whelk; + + public static final String AS2_CONTENT_TYPE = "application/activity+json"; + public EmmServlet() { whelk = Whelk.createLoadedCoreWhelk(); } @@ -31,9 +32,6 @@ public void doGet(HttpServletRequest req, HttpServletResponse res) { String until = req.getParameter("until"); String apiBaseUrl = req.getRequestURL().toString(); - res.setCharacterEncoding("utf-8"); - res.setContentType("application/activity+json"); - if (dump != null) { Dump.sendDumpResponse(whelk, apiBaseUrl, req, res); return; @@ -54,10 +52,7 @@ public void doGet(HttpServletRequest req, HttpServletResponse res) { first.put("id", apiBaseUrl+"?until="+System.currentTimeMillis()); responseObject.put("first", first); - String jsonResponse = mapper.writeValueAsString(responseObject); - BufferedWriter writer = new BufferedWriter( res.getWriter() ); - writer.write(jsonResponse); - writer.close(); + HttpTools.sendResponse(res, responseObject, AS2_CONTENT_TYPE); return; }