@Override public void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException { try { // printAllParameters(httpServletRequest); if (doPing(httpServletRequest, httpServletResponse)) return; doIt(httpServletRequest, httpServletResponse); } catch (Throwable t) { t.printStackTrace(); handleException(t, httpServletRequest, httpServletResponse); } }
public ExceptionHandler getExceptionHandler() { if (exceptionHandler == null) { warn("Warning: no Exception Handler set, using basic exception handling only!"); exceptionHandler = new BasicExceptionHandler(getMyLogger()); } return exceptionHandler; }
@Override public void init() throws ServletException { super.init(); if (environment == null) { info("loading environment..."); try { loadEnvironment(); } catch (IOException e) { warn("failed to load environment."); throw new GeneralException("Error loading environment", e); } info("done loading environment."); } if (getInitialization() == null) { info("Caution: no initializer set for " + getClass().getSimpleName() + ". Skipping..."); } else { getInitialization().setEnvironment(getEnvironment()); getInitialization().setServlet(this); getInitialization().init(); setExceptionHandler(getInitialization().getExceptionHandler()); } }
/** * One stop shopping for exception handling. All thrown exceptions are intercepted and run through this. * Depending on their type they are wrapped or passed along. You can change this behavior if you need to. * <p>Note that all runtime exceptions, IOExceptions and ServletExceptions are not modified, * so if you over-ride this and throw one of those exceptions you will not get extra cruft. * <p>Also, a response is passed along. This may be used in over-rides, but is not used in the * basic implementation. If it is null, it should be ignored.</p> * * @param t * @param request * @param response * @throws IOException * @throws ServletException */ protected void handleException(Throwable t, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { error("INTERNAL ERROR: " + (t.getMessage() == null ? "(no message)" : t.getMessage())); // log it appropriately // ok, if it is a strange error, print a stack if you need to. if (isDebugOn()) { t.printStackTrace(); } t.printStackTrace(); getExceptionHandler().handleException(t, request, response); }
/** * This returns true or false for * @param req * @param resp * @return * @throws ServletException */ protected boolean doPing(HttpServletRequest req, HttpServletResponse resp) throws ServletException { if (req.getParameterMap().containsKey(PING_PARAMETER)) { // Check if ping is enabled. Set in AbstractEnvironment?? if (!getEnvironment().isPingable()) { warn("Ping attempted on server. Ping not enabled."); // Throw a servlet exception so it is unclear if the server is down or not. // This means it is un-pingable. throw new ServletException("Internal error"); } info("ping ok"); resp.setStatus(HttpStatus.SC_NO_CONTENT); return true; } // Since every request runs through this, logging that no ping was done would just fill up the logs. return false; }
public void debug(String x) { getMyLogger().debug(x); }
/** * Utility to get constants or code gets really wordy. * * @param key * @return */ public static String CONST(String key) { return getEnvironment().getConstants().get(key); }
@Override public void destroy() { super.destroy(); shutdownCleanup(assetCleanup); }
public static Identifier getIDFromParameters(HttpServletRequest request) { Identifier paramID = null; //DebugUtil.dbg(this, "doIt: no header for authentication, looking at parameters."); // assume that the secret and id are in the request String rawID = request.getParameter(AbstractServlet.CONST(CONSUMER_KEY)); if (isEmpty(rawID)) { return null; } return BasicIdentifier.newID(rawID); }
public boolean isDebugOn() { return getMyLogger().isDebugOn(); }
@Override public void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException { try { // printAllParameters(httpServletRequest); if (doPing(httpServletRequest, httpServletResponse)) return; /* To read the request body for processing use something like this. // Read from request StringBuilder buffer = new StringBuilder(); BufferedReader reader = httpServletRequest.getReader(); String line; while ((line = reader.readLine()) != null) { buffer.append(line); } String data = buffer.toString() */ doIt(httpServletRequest, httpServletResponse); } catch (Throwable t) { handleException(t, httpServletRequest, httpServletResponse); } }
public void warn(String x) { getMyLogger().warn(x); }
public void setDebugOn(boolean setOn) { getMyLogger().setDebugOn(setOn); }
public void error(String x) { getMyLogger().error(x); }
public void info(String x) { getMyLogger().info(x); }