Permalink
Browse files

Support accept headers for http about page (#345)

* Combine json page with html page based on accept header

* Support old path too
  • Loading branch information...
klabarge committed Aug 14, 2018
1 parent 8f2e617 commit c2109381b1f822d85d7bef267d9655a4609fa4cb
Showing with 29 additions and 47 deletions.
  1. +28 −3 src/qz/ws/HttpAboutServlet.java
  2. +0 −38 src/qz/ws/JsonAboutServlet.java
  3. +1 −6 src/qz/ws/PrintSocketServer.java
@@ -8,20 +8,31 @@
import org.slf4j.LoggerFactory;
import qz.common.AboutInfo;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;

/**
* HTTP JSON endpoint for serving QZ Tray information
*/
public class HttpAboutServlet extends DefaultServlet {

private static final Logger log = LoggerFactory.getLogger(PrintSocketServer.class);

private static final int JSON_INDENT = 2;


@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
if ("application/json".equals(request.getHeader("Accept")) || "/json".equals(request.getServletPath())) {
generateJsonResponse(request, response);
} else {
generateHtmlResponse(request, response);
}
}

private void generateHtmlResponse(HttpServletRequest request, HttpServletResponse response) {
StringBuilder display = new StringBuilder();

display.append("<html><body>")
@@ -51,6 +62,20 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
}
}

private void generateJsonResponse(HttpServletRequest request, HttpServletResponse response) {
JSONObject aboutData = AboutInfo.gatherAbout(request.getServerName());

try {
response.setStatus(HttpServletResponse.SC_OK);
response.setContentType("application/json");
response.getOutputStream().write(aboutData.toString(JSON_INDENT).getBytes(StandardCharsets.UTF_8));
}
catch(Exception e) {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
log.warn("Exception occurred writing JSONObject {}", aboutData);
}
}

private StringBuilder generateFromKeys(JSONObject obj, boolean printTitle) throws JSONException {
StringBuilder rows = new StringBuilder();

This file was deleted.

Oops, something went wrong.
@@ -168,12 +168,7 @@ public Object createWebSocket(ServletUpgradeRequest req, ServletUpgradeResponse
ServletHolder httpServlet = new ServletHolder(new HttpAboutServlet());
httpServlet.setInitParameter("resourceBase","/");
context.addServlet(httpServlet, "/");

// Handle JSON data page
ServletHolder jsonServlet = new ServletHolder(new JsonAboutServlet());
jsonServlet.setInitParameter("resourceBase","/json/");
context.addServlet(jsonServlet, "/json");
context.addServlet(jsonServlet, "/json/");
context.addServlet(httpServlet, "/json");

server.setHandler(context);
server.setStopAtShutdown(true);

0 comments on commit c210938

Please sign in to comment.