/** * {@inheritDoc} */ @Override public void service(final HttpServletRequest request, final HttpServletResponse response) throws ServletException { throw new ServletException("Something bad has just happened"); } }
/** * {@inheritDoc} */ @Override public void doFilter(final HttpServletRequest request, final HttpServletResponse response, final FilterChain filterChain) throws IOException, ServletException { throw new AccessDeniedException(); } }
/** * {@inheritDoc} */ @Override public void service(final HttpServletRequest request, final HttpServletResponse response) throws ServletException { throw new NotFoundException(); } }
/** * Encodes given string for URL/HTTP. * * @param text text to be encoded * @return encoded string */ public static String urlEncode(final String text) { try { return encode(text, StandardCharsets.UTF_8.name()); } catch (UnsupportedEncodingException e) { // $COVERAGE-IGNORE$ throw new UnexpectedSituationException("Encoding is not supported.", e); // $COVERAGE-IGNORE$ } }
private Servlet instantiateServlet(final Class<? extends HttpServlet> serverClass) throws ServletInitializationException { try { return serverClass.newInstance(); } catch (InstantiationException | IllegalAccessException e) { throw new ServletInitializationException(e); } }
private Filter instantiateFilter(final Class<? extends Filter> filterClass) throws FilterInitializationException { try { return filterClass.newInstance(); } catch (InstantiationException | IllegalAccessException e) { throw new FilterInitializationException(e); } }
/** * Throws exception in case of invalid request. * * @param request */ private void validateRequest(final HttpServletRequest request) { if (!isMethodSupported(request.getMethod())) { throw new MethodNotAllowedException(); } }
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); } } }
/** * Decodes given string for URL/HTTP. * * @param text text to be decoded * @return decoded string */ public static String urlDecode(final String text) { try { return decode(text, StandardCharsets.UTF_8.name()); } catch (UnsupportedEncodingException e) { // $COVERAGE-IGNORE$ throw new UnexpectedSituationException("Encoding is not supported.", e); // $COVERAGE-IGNORE$ } }
@Test public void shouldCaptureIntermediateExceptions() { ServerConfig serverConfig = null; // WIll throw NPE HttpErrorHandlerResolver httpErrorHandlerResolver = new HttpErrorHandlerResolverImpl(serverConfig); HttpErrorHandler handler = httpErrorHandlerResolver.getHandler(new NotFoundException()); assertThat(handler, is(instanceOf(HttpError500Handler.class))); } }
/** * {@inheritDoc} */ @Override public void service(final HttpServletRequest request, final HttpServletResponse response) throws ServletException { throw new AccessDeniedException(); } }
/** * {@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); } } }
private Servlet getServlet(final ServletMapping servletMapping, final ServletConfigImpl servletConfig) { Servlet servlet; try { servlet = servletContainer.getServletForClass(servletMapping.getServletClass(), servletConfig); } catch (ServletInitializationException | ServletException e) { throw new UnexpectedSituationException(e); } return servlet; }
private void handleDirectoryIndex(final HttpServletResponseImpl response, final HttpServletRequestImpl request, final String requestedPath) throws IOException { DirectoryIndexDescriptor indexDescriptor = loadDirectoryIndexResource(requestedPath); if (indexDescriptor == null) { throw new NotFoundException(); } else { if (!pathHelper.isDirectoryPath(requestedPath)) { sendRedirectToDirectorySlashedPath(response, requestedPath); } else { indexDescriptor.getResourceProvider().load( indexDescriptor.getDirectoryPath(), request, response); } } }
@Test public void shouldOfferStringConstructor() { Exception exception = new ServletException("SomeMessage"); assertThat(exception.getMessage(), is("SomeMessage")); }
@Test public void shouldOfferStringThrowableConstructor() { Throwable e = new Exception(); Exception exception = new UnexpectedSituationException("SomeMessage", e); assertThat(exception.getMessage(), is("SomeMessage")); assertThat(exception.getCause(), is(e)); }
@Test public void shouldThrowableConstructor() { Throwable e = new Exception(); Exception exception = new ServletException(e); assertThat(exception.getCause(), is(e)); } }
@Test public void shouldOfferStringConstructor() { Exception exception = new UnexpectedSituationException("SomeMessage"); assertThat(exception.getMessage(), is("SomeMessage")); }
@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 shouldThrowableConstructor() { Throwable e = new Exception(); Exception exception = new UnexpectedSituationException(e); assertThat(exception.getCause(), is(e)); }