protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setHeader("Cache-Control", "no-cache, no-store, must-revalidate, private"); resp.setHeader("Pragma", "no-cache"); resp.setDateHeader("Expires", 0); resp.setContentType("text/html; charset=UTF-8"); if (API.allowedBotHosts != null && !API.allowedBotHosts.contains(req.getRemoteHost())) { resp.sendError(HttpServletResponse.SC_FORBIDDEN); return; } try (PrintWriter writer = resp.getWriter()) { writer.print(header1); writer.print(buildLinks(req)); writer.print(header2); String requestType = Convert.nullToEmpty(req.getParameter("requestType")); APIServlet.APIRequestHandler requestHandler = APIServlet.apiRequestHandlers.get(requestType); StringBuilder bufJSCalls = new StringBuilder(); if (requestHandler != null) { writer.print( form( requestType, true, requestHandler.getClass().getName(), requestHandler.getParameters(), requestHandler.requirePost())); bufJSCalls.append("apiCalls.push(\"").append(requestType).append("\");\n"); } else { String requestTag = Convert.nullToEmpty(req.getParameter("requestTag")); Set<String> taggedTypes = requestTags.get(requestTag); for (String type : (taggedTypes != null ? taggedTypes : allRequestTypes)) { requestHandler = APIServlet.apiRequestHandlers.get(type); writer.print( form( type, false, requestHandler.getClass().getName(), APIServlet.apiRequestHandlers.get(type).getParameters(), APIServlet.apiRequestHandlers.get(type).requirePost())); bufJSCalls.append("apiCalls.push(\"").append(type).append("\");\n"); } } writer.print(footer1); writer.print(bufJSCalls.toString()); writer.print(footer2); } }
@Override JSONStreamAware processRequest(HttpServletRequest req) throws ParameterException { String query = Convert.nullToEmpty(req.getParameter("query")); int firstIndex = ParameterParser.getFirstIndex(req); int lastIndex = ParameterParser.getLastIndex(req); boolean includeCounts = !"false".equalsIgnoreCase(req.getParameter("includeCounts")); JSONObject response = new JSONObject(); JSONArray jsonArray = new JSONArray(); try (DbIterator<Currency> currencies = Currency.searchCurrencies(query, firstIndex, lastIndex)) { while (currencies.hasNext()) { jsonArray.add(JSONData.currency(currencies.next(), includeCounts)); } } response.put("currencies", jsonArray); return response; }
private static String buildLinks(HttpServletRequest req) { StringBuilder buf = new StringBuilder(); String requestTag = Convert.nullToEmpty(req.getParameter("requestTag")); buf.append("<li"); if (requestTag.equals("")) { buf.append(" class=\"active\""); } buf.append("><a href=\"/test\">All</a></li>"); for (APITag apiTag : APITag.values()) { if (requestTags.get(apiTag.name()) != null) { buf.append("<li"); if (requestTag.equals(apiTag.name())) { buf.append(" class=\"active\""); } buf.append("><a href=\"/test?requestTag=").append(apiTag.name()).append("\">"); buf.append(apiTag.getDisplayName()).append("</a></li>").append(" "); } } return buf.toString(); }