@Test public void switchToHeadWhenGetRouteDoesNotExist() throws Exception { DefaultPageErrorHandler handler = new DefaultPageErrorHandler(); handler.configuration = mock(ApplicationConfiguration.class); when(handler.configuration.isDev()).thenReturn(false); handler.router = mock(Router.class); Route reqRoute = new Route(HttpMethod.HEAD, "/", null, null); when(handler.router.getRouteFor(HttpMethod.HEAD, "/")).thenReturn(new Route(HttpMethod.HEAD, "/", null, null)); when(handler.router.getRouteFor(HttpMethod.GET, "/")).thenReturn(new Route(HttpMethod.GET, "/", null, null)); RequestContext rc = new RequestContext(reqRoute, Collections.<Filter>emptyList(), Collections.<Interceptor<?>, Object>emptyMap(), new Object[0], null); Result result = handler.call(reqRoute, rc); assertThat(result).isNotNull(); assertThat(result.getStatusCode()).isEqualTo(Status.NOT_FOUND); assertThat(result.getRenderable().length()).isEqualTo(0); }
@Override public Result call(Route route, RequestContext context) throws Exception { if (configuration.isDev() && context.request().accepts(MimeTypes.HTML) && pipeline != null) { File error = getFirstErrorFile(); if (error != null) { logger().debug("Error file detected, preparing rendering"); try { return renderPipelineError(error); } catch (IOException e) { LOGGER.error("An exception occurred while generating the error page for {} {}", route.getHttpMethod(), route.getUrl(), e); return renderInternalError(context.context(), route, e); } } } try { Result result = context.proceed(); if (result.getStatusCode() == NOT_FOUND && result.getRenderable() instanceof NoHttpBody) { if (route.getHttpMethod() == HttpMethod.HEAD) { return switchToGet(route, context); } return renderNotFound(route, result); } return result; } catch (InvocationTargetException e) { Throwable cause = e.getCause(); LOGGER.error("An exception occurred while processing request {} {}", route.getHttpMethod(), route.getUrl(), cause); if (cause instanceof HttpException) { LOGGER.error("A HTTP exception occurred while processing request {} {}", route.getHttpMethod(), route.getUrl(), e); return ((HttpException) cause).toResult(); } for (ExceptionMapper mapper : mappers) { if (mapper.getExceptionClass().equals(cause.getClass())) { return mapper.toResult((Exception) cause); } } return renderInternalError(context.context(), route, e); } catch (Exception e) { LOGGER.error("An exception occurred while processing request {} {}", route.getHttpMethod(), route.getUrl(), e); Throwable cause = e.getCause(); for (ExceptionMapper mapper : mappers) { if (mapper.getExceptionClass().equals(cause.getClass())) { return mapper.toResult((Exception) cause); } } return renderInternalError(context.context(), route, e); } }
DefaultPageErrorHandler extends DefaultController implements Filter { @Override public Result call(Route route, RequestContext context) throws Exception { if (configuration.isDev() && context.request().accepts(MimeTypes.HTML) && pipeline != null) { File error = getFirstErrorFile(); if (error != null) { logger().debug("Error file detected, preparing rendering"); try { return renderPipelineError(error); } catch (IOException e) { LOGGER.error("An exception occurred while generating the error page for {} {}", route.getHttpMethod(), route.getUrl(), e); return renderInternalError(context.context(), route, e); } } } try { Result result = context.proceed(); if (result.getStatusCode() == NOT_FOUND && result.getRenderable() instanceof NoHttpBody) { if (route.getHttpMethod() == HttpMethod.HEAD) { return switchToGet(route, context); } return renderNotFound(route, result); } return result; } catch (InvocationTargetException e) { Throwable cause = e.getCause(); LOGGER.error("An exception occurred while processing request {} {}", route.getHttpMethod(), route.getUrl(), cause); if (cause instanceof HttpException) { LOGGER.error("A HTTP exception occurred while processing request {} {}", route.getHttpMethod(), route.getUrl(), e); return ((HttpException) cause).toResult(); } for (ExceptionMapper mapper : mappers) { if (mapper.getExceptionClass().equals(cause.getClass())) { return mapper.toResult((Exception) cause); } } return renderInternalError(context.context(), route, e); } catch (Exception e) { LOGGER.error("An exception occurred while processing request {} {}", route.getHttpMethod(), route.getUrl(), e); Throwable cause = e.getCause(); for (ExceptionMapper mapper : mappers) { if (mapper.getExceptionClass().equals(cause.getClass())) { return mapper.toResult((Exception) cause); } } return renderInternalError(context.context(), route, e); } } }
DefaultPageErrorHandler extends DefaultController implements Filter { @Override public Result call(Route route, RequestContext context) throws Exception { if (configuration.isDev() && context.request().accepts(MimeTypes.HTML) && pipeline != null) { File error = getFirstErrorFile(); if (error != null) { logger().debug("Error file detected, preparing rendering"); try { return renderPipelineError(error); } catch (IOException e) { LOGGER.error("An exception occurred while generating the error page for {} {}", route.getHttpMethod(), route.getUrl(), e); return renderInternalError(context.context(), route, e); } } } try { Result result = context.proceed(); if (result.getStatusCode() == NOT_FOUND && result.getRenderable() instanceof NoHttpBody) { if (route.getHttpMethod() == HttpMethod.HEAD) { return switchToGet(route, context); } return renderNotFound(route, result); } return result; } catch (InvocationTargetException e) { Throwable cause = e.getCause(); LOGGER.error("An exception occurred while processing request {} {}", route.getHttpMethod(), route.getUrl(), cause); if (cause instanceof HttpException) { LOGGER.error("A HTTP exception occurred while processing request {} {}", route.getHttpMethod(), route.getUrl(), e); return ((HttpException) cause).toResult(); } for (ExceptionMapper mapper : mappers) { if (mapper.getExceptionClass().equals(cause.getClass())) { return mapper.toResult((Exception) cause); } } return renderInternalError(context.context(), route, e); } catch (Exception e) { LOGGER.error("An exception occurred while processing request {} {}", route.getHttpMethod(), route.getUrl(), e); Throwable cause = e.getCause(); for (ExceptionMapper mapper : mappers) { if (mapper.getExceptionClass().equals(cause.getClass())) { return mapper.toResult((Exception) cause); } } return renderInternalError(context.context(), route, e); } }  }
DefaultPageErrorHandler extends DefaultController implements Filter { @Override public Result call(Route route, RequestContext context) throws Exception { if (configuration.isDev() && context.request().accepts(MimeTypes.HTML) && pipeline != null) { File error = getFirstErrorFile(); if (error != null) { logger().debug("Error file detected, preparing rendering"); try { return renderPipelineError(error); } catch (IOException e) { LOGGER.error("An exception occurred while generating the error page for {} {}", route.getHttpMethod(), route.getUrl(), e); return renderInternalError(context.context(), route, e); } } } try { Result result = context.proceed(); if (result.getStatusCode() == NOT_FOUND && result.getRenderable() instanceof NoHttpBody) { if (route.getHttpMethod() == HttpMethod.HEAD) { return switchToGet(route, context); } return renderNotFound(route, result); } return result; } catch (InvocationTargetException e) { Throwable cause = e.getCause(); LOGGER.error("An exception occurred while processing request {} {}", route.getHttpMethod(), route.getUrl(), cause); if (cause instanceof HttpException) { LOGGER.error("A HTTP exception occurred while processing request {} {}", route.getHttpMethod(), route.getUrl(), e); return ((HttpException) cause).toResult(); } for (ExceptionMapper mapper : mappers) { if (mapper.getExceptionClass().equals(cause.getClass())) { return mapper.toResult((Exception) cause); } } return renderInternalError(context.context(), route, e); } catch (Exception e) { LOGGER.error("An exception occurred while processing request {} {}", route.getHttpMethod(), route.getUrl(), e); Throwable cause = e.getCause(); for (ExceptionMapper mapper : mappers) { if (mapper.getExceptionClass().equals(cause.getClass())) { return mapper.toResult((Exception) cause); } } return renderInternalError(context.context(), route, e); } }  @Validate void start(); File getFirstErrorFile(); @Override Result call(Route route, RequestContext context); @Override Pattern uri(); @Override int priority(); }
DefaultPageErrorHandler extends DefaultController implements Filter { @Override public Result call(Route route, RequestContext context) throws Exception { if (configuration.isDev() && context.request().accepts(MimeTypes.HTML) && pipeline != null) { File error = getFirstErrorFile(); if (error != null) { logger().debug("Error file detected, preparing rendering"); try { return renderPipelineError(error); } catch (IOException e) { LOGGER.error("An exception occurred while generating the error page for {} {}", route.getHttpMethod(), route.getUrl(), e); return renderInternalError(context.context(), route, e); } } } try { Result result = context.proceed(); if (result.getStatusCode() == NOT_FOUND && result.getRenderable() instanceof NoHttpBody) { if (route.getHttpMethod() == HttpMethod.HEAD) { return switchToGet(route, context); } return renderNotFound(route, result); } return result; } catch (InvocationTargetException e) { Throwable cause = e.getCause(); LOGGER.error("An exception occurred while processing request {} {}", route.getHttpMethod(), route.getUrl(), cause); if (cause instanceof HttpException) { LOGGER.error("A HTTP exception occurred while processing request {} {}", route.getHttpMethod(), route.getUrl(), e); return ((HttpException) cause).toResult(); } for (ExceptionMapper mapper : mappers) { if (mapper.getExceptionClass().equals(cause.getClass())) { return mapper.toResult((Exception) cause); } } return renderInternalError(context.context(), route, e); } catch (Exception e) { LOGGER.error("An exception occurred while processing request {} {}", route.getHttpMethod(), route.getUrl(), e); Throwable cause = e.getCause(); for (ExceptionMapper mapper : mappers) { if (mapper.getExceptionClass().equals(cause.getClass())) { return mapper.toResult((Exception) cause); } } return renderInternalError(context.context(), route, e); } }  @Validate void start(); File getFirstErrorFile(); @Override Result call(Route route, RequestContext context); @Override Pattern uri(); @Override int priority(); static final Pattern ALL_REQUESTS; static final String EMPTY_CONTENT; }