protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); }
protected JSONObject errorToJson(Throwable error) throws Exception { JSONObject errorObj = new JSONObject(); errorObj.put("message", error.getMessage()); if( null != error.getCause() ) { errorObj.put("cause", errorToJson(error.getCause())); } return errorObj; } }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String paths[] = request.getRequestURI().split("/"); String path = paths[ paths.length - 1 ]; logger.info(this.getClass().getName()+" "+path); try { if( "delete".equalsIgnoreCase(path) ) { performDelete(request, response); } else if( "fromName".equalsIgnoreCase(path) ) { performFromName(request, response); } else { throw new Exception("Unknown request: "+path); } } catch(Exception e) { logger.info("Error in Contribution Servlet",e); sendErrorResponse(response, e); } }
protected void performFromName(HttpServletRequest request, HttpServletResponse response) throws Exception { String[] names = request.getParameterValues("name"); if( null == names || names.length < 1 ) { throw new Exception("Parameter 'name' not provided"); } if( names.length > 1 ) { throw new Exception("Parameter 'name' provided multiple times"); } if (null != contributions) { JSONObject result = contributions.fromName(names[0], userRepository); sendJsonResponse(response, result); } else { throw new Exception("Contribution record access requests can not be fulfill because of failed initialization - look for earlier exceptions."); } }
protected void performDelete(HttpServletRequest request, HttpServletResponse response) throws Exception { String[] ids = request.getParameterValues("id"); String[] placeIds = request.getParameterValues("placeId"); if( null == ids || ids.length < 1 ) { throw new Exception("Parameter 'id' not provided"); } if( ids.length > 1 ) { throw new Exception("Parameter 'id' provided multiple times"); } if( null == placeIds ) { placeIds = new String[0]; } if( placeIds.length > 1 ) { throw new Exception("Parameter 'place_id' provided multiple times"); } contributions.deleteContribution(ids[0], (placeIds.length > 0 ? placeIds[0] : null)); JSONObject result = new JSONObject(); result.put("id", ids[0]); if( placeIds.length > 0 ) { result.put("place_id", placeIds[0]); } sendJsonResponse(response, result); }
protected void sendErrorResponse(HttpServletResponse response, Throwable error) throws ServletException { response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", (new Date()).getTime()); response.setContentType("text/plain"); response.setCharacterEncoding("utf-8"); try { JSONObject result = new JSONObject(); result.put("error", errorToJson(error)); OutputStreamWriter osw = new OutputStreamWriter( response.getOutputStream(), "UTF-8" ); result.write(osw); osw.flush(); } catch(Exception e) { throw new ServletException("Failure generating error",e); } }