Skip to content

Commit

Permalink
refactor(emm): Use HttpTools
Browse files Browse the repository at this point in the history
  • Loading branch information
olovy committed Nov 21, 2024
1 parent f027bfd commit f5bd22d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 29 deletions.
22 changes: 9 additions & 13 deletions emm/src/main/java/whelk/Dump.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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");
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -158,15 +155,17 @@ 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;
}
}

}
} 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);
Expand Down Expand Up @@ -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))
Expand Down
9 changes: 3 additions & 6 deletions emm/src/main/java/whelk/EmmChangeSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}

/**
Expand Down
15 changes: 5 additions & 10 deletions emm/src/main/java/whelk/EmmServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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;
Expand All @@ -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;
}

Expand Down

0 comments on commit f5bd22d

Please sign in to comment.