/** * Set an error code and print out the usage docs to the response. * * @param statusCode * @param response */ private static void setUsageError(int statusCode, HttpServletResponse response) { setUsageError(statusCode, null, response); }
@Override protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { setUsageError(405, response); return; }
private void handledeActivateAction(HttpServletRequest request, HttpServletResponse response) throws IOException { String filter_id = request.getParameter("filter_id"); if (filter_id == null) { // return error, endpoint is required setUsageError(404, "ERROR: No endpoint defined.", response); } else { String revision = request.getParameter("revision"); if (revision == null) { setUsageError(404, "ERROR: No revision defined.", response); } else { int revisionNumber = -1; try { revisionNumber = Integer.parseInt(revision); } catch (Exception e) { setUsageError(400, "ERROR: revision must be an integer.", response); return; } try { scriptDAO.deActivateFilter(filter_id, revisionNumber); } catch (Exception e) { setUsageError(400, "ERROR: " + e.getMessage(), response); return; } response.sendRedirect(redirectPath.get()); } } }
private void handleCanaryAction(HttpServletRequest request, HttpServletResponse response) throws IOException { String filter_id = request.getParameter("filter_id"); if (filter_id == null) { // return error, endpoint is required setUsageError(404, "ERROR: No endpoint defined.", response); } else { String revision = request.getParameter("revision"); if (revision == null) { setUsageError(404, "ERROR: No revision defined.", response); } else { int revisionNumber = -1; try { revisionNumber = Integer.parseInt(revision); } catch (Exception e) { setUsageError(400, "ERROR: revision must be an integer.", response); return; } scriptDAO.setCanaryFilter(filter_id, revisionNumber); response.sendRedirect(redirectPath.get()); } } }
private void handleActivateAction(HttpServletRequest request, HttpServletResponse response) throws IOException { String filter_id = request.getParameter("filter_id"); if (filter_id == null) { // return error, endpoint is required setUsageError(404, "ERROR: No endpoint defined.", response); } else { String revision = request.getParameter("revision"); if (revision == null) { setUsageError(404, "ERROR: No revision defined.", response); } else { int revisionNumber = -1; try { revisionNumber = Integer.parseInt(revision); } catch (Exception e) { setUsageError(400, "ERROR: revision must be an integer.", response); return; } try { scriptDAO.setFilterActive(filter_id, revisionNumber); } catch (Exception e) { setUsageError(400, "ERROR: " + e.getMessage(), response); return; } response.sendRedirect(redirectPath.get()); // Map<String, Object> scriptJson = createEndpointScriptJSON(filterInfo); // response.getWriter().write(JsonUtility.jsonFromMap(scriptJson)); } } }
/** * Determine if the incoming action + method is a correct combination. If not, output the usage docs and set an error code on the response. * * @param request * @param response * @return true if valid, false if not */ private static boolean isValidAction(HttpServletRequest request, HttpServletResponse response) { String action = request.getParameter("action"); if (action != null) { return new ValidActionEvaluation(request, response).isValid(); } else { setUsageError(400, "ERROR: Invalid arguments.", response); return false; } }
if (filter_id == null) { setUsageError(404, "ERROR: No endpoint defined.", response); } else { String revision = request.getParameter("revision"); revisionNumber = Integer.parseInt(revision); } catch (Exception e) { setUsageError(400, "ERROR: revision must be an integer.", response); return; setUsageError(404, "ERROR: No scripts found.", response); } else { if (script.getFilterCode() == null) { setUsageError(500, "ERROR: script files not found", response); } else {
private void handleUploadAction(HttpServletRequest request, HttpServletResponse response) throws IOException { String filter = handlePostBody(request, response); if (filter != null) { FilterInfo filterInfo = null; try { filterInfo = FilterVerifier.getInstance().verifyFilter(filter); } catch (IllegalAccessException e) { logger.error(e.getMessage(), e); setUsageError(500, "ERROR: Unable to process uploaded data. " + e.getMessage(), response); } catch (InstantiationException e) { logger.error(e.getMessage(), e); setUsageError(500, "ERROR: Bad Filter. " + e.getMessage(), response); } filterInfo = scriptDAO.addFilter(filter, filterInfo.getFilterType(), filterInfo.getFilterName(), filterInfo.getFilterDisablePropertyName(), filterInfo.getFilterOrder()); if (filterInfo == null) { setUsageError(500, "ERROR: Unable to process uploaded data.", response); return; } response.sendRedirect(redirectPath.get()); } }
setUsageError(404, "ERROR: No scripts found for endpoint: " + filter_id, response); } else {
private String handlePostBody(HttpServletRequest request, HttpServletResponse response) throws IOException { FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); org.apache.commons.fileupload.FileItemIterator it = null; try { it = upload.getItemIterator(request); while (it.hasNext()) { FileItemStream stream = it.next(); InputStream input = stream.openStream(); // NOTE: we are going to pull the entire stream into memory // this will NOT work if we have huge scripts, but we expect these to be measured in KBs, not MBs or larger byte[] uploadedBytes = getBytesFromInputStream(input); input.close(); if (uploadedBytes.length == 0) { setUsageError(400, "ERROR: Body contained no data.", response); return null; } return new String(uploadedBytes); } } catch (FileUploadException e) { throw new IOException(e.getMessage()); } return null; }