/** * {@inheritDoc} */ @Override public void service(final HttpServletRequest request, final HttpServletResponse response) throws ServletException { throw new ServletException("Something bad has just happened"); } }
private void sendError(final HttpServletResponse response, final String errorMessage) throws ServletException { try { APIResponse apiResponse = new APIResponse(APIResponse.CODE_ERROR, errorMessage); response.getWriter().print(apiResponse.toString()); } catch (JSONException e) { throw new ServletException(e); } } }
/** * {@inheritDoc} */ @Override public void service(final HttpServletRequest request, final HttpServletResponse response) throws ServletException { try { byte[] message = "<p>Writing to output stream directly, without chunking.</p>".getBytes(StandardCharsets.UTF_8); response.setContentLength(message.length); response.getOutputStream().write(message); } catch (IOException e) { throw new ServletException("Unable to write to output stream", e); } } }
@Test public void shouldOfferStringThrowableConstructor() { Throwable e = new Exception(); Exception exception = new ServletException("SomeMessage", e); assertThat(exception.getMessage(), is("SomeMessage")); assertThat(exception.getCause(), is(e)); }
@Test public void shouldOfferStringConstructor() { Exception exception = new ServletException("SomeMessage"); assertThat(exception.getMessage(), is("SomeMessage")); }
@Test public void shouldThrowableConstructor() { Throwable e = new Exception(); Exception exception = new ServletException(e); assertThat(exception.getCause(), is(e)); } }
/** * {@inheritDoc} */ @Override public void service(final HttpServletRequest request, final HttpServletResponse response) throws ServletException { ServerConfig serverConfig = (ServerConfig) getServletContext().getAttribute(ServerConfig.class.getName()); if (!serverConfig.getAttribute(ATTR_ADMIN_DRIVE_ACCESS_ENABLED).equals(ServerConfigImpl.TRUE)) { response.getWriter().println("Option disabled in configuration."); return; } boolean fileExists = false; if (!request.getQueryString().equals("")) { File f = new File(getPathname(request)); if (f.exists() && f.isFile()) { fileExists = true; try { serveFile(f, response); } catch (IOException e) { throw new ServletException(e); } } } if (!fileExists) { response.setStatus(HttpServletResponse.STATUS_NOT_FOUND); response.getWriter().print("File does not exist."); } }
@Override public void service(final HttpServletRequest request, final HttpServletResponse response) throws ServletException { SmsBox smsBox = new SmsBox(((Activity) getServletContext().getAttribute("android.content.Context"))); List<SmsBox.Message> messages = smsBox.readMessages(ALL_STRING); try { APIResponse apiResponse = new APIResponse(APIResponse.CODE_OK, "OK", computeResult(getMaxResults(request), messages)); response.setContentType(MEDIA_TYPE_APPLICATION_JSON); response.getWriter().print(apiResponse.toString()); } catch (JSONException e) { throw new ServletException(e); } }
response.getWriter().print(new APIResponse().toString()); } catch (JSONException e) { throw new ServletException(e);