@Test public void shouldRejectUnauthenticedUsersFromRestrictedOperation() throws IOException { when(requestMock.getMethod()).thenReturn("GET"); when(requestMock.getPathInfo()).thenReturn("/restricted"); servlet.service(requestMock, responseMock); verify(responseMock).setStatus(401); verify(responseMock).setContentType("application/json"); verify(responseMock).getCharacterEncoding(); verify(responseMock).setCharacterEncoding(null); verify(responseMock).getWriter(); assertThat(JsonObject.parse(responseBody.toString()).requiredString("message")) .isEqualTo("Login required"); }
@Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException { try ( MDC.MDCCloseable ignored = MDC.putCloseable("clientIp", req.getRemoteAddr()); MDC.MDCCloseable ignored2 = MDC.putCloseable("requestPath", req.getContextPath() + req.getServletPath() + req.getPathInfo()) ) { controllerException.verifyNoExceptions(); invokeAction(new ServletHttpExchange(req, resp)); } }
ApiServlet extends HttpServlet implements UserContext { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException { try ( MDC.MDCCloseable ignored = MDC.putCloseable("clientIp", req.getRemoteAddr()); MDC.MDCCloseable ignored2 = MDC.putCloseable("requestPath", req.getContextPath() + req.getServletPath() + req.getPathInfo()) ) { controllerException.verifyNoExceptions(); invokeAction(new ServletHttpExchange(req, resp)); } } }
ApiServlet extends HttpServlet implements UserContext { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException { try ( MDC.MDCCloseable ignored = MDC.putCloseable("clientIp", req.getRemoteAddr()); MDC.MDCCloseable ignored2 = MDC.putCloseable("requestPath", req.getContextPath() + req.getServletPath() + req.getPathInfo()) ) { controllerException.verifyNoExceptions(); invokeAction(new ServletHttpExchange(req, resp)); } } ApiServlet();  ApiServlet(Object controller); }
ApiServlet extends HttpServlet implements UserContext { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException { try ( MDC.MDCCloseable ignored = MDC.putCloseable("clientIp", req.getRemoteAddr()); MDC.MDCCloseable ignored2 = MDC.putCloseable("requestPath", req.getContextPath() + req.getServletPath() + req.getPathInfo()) ) { controllerException.verifyNoExceptions(); invokeAction(new ServletHttpExchange(req, resp)); } } ApiServlet();  ApiServlet(Object controller); ApiControllerContext getContext(); @Override boolean isUserLoggedIn(ApiHttpExchange exchange); @Override boolean isUserInRole(ApiHttpExchange exchange, String role); void registerController(Object controller); void registerControllers(Object... controllers); void registerControllerList(List<Object> controllers); @Override void init(ServletConfig config); void registerMBeans(); void registerMBeans(MBeanServer mBeanServer); @Override TimerRegistry getTimerRegistry(); void setTimerRegistry(TimerRegistry timerRegistry); }
ApiServlet extends HttpServlet implements UserContext { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException { try ( MDC.MDCCloseable ignored = MDC.putCloseable("clientIp", req.getRemoteAddr()); MDC.MDCCloseable ignored2 = MDC.putCloseable("requestPath", req.getContextPath() + req.getServletPath() + req.getPathInfo()) ) { controllerException.verifyNoExceptions(); invokeAction(new ServletHttpExchange(req, resp)); } } ApiServlet();  ApiServlet(Object controller); ApiControllerContext getContext(); @Override boolean isUserLoggedIn(ApiHttpExchange exchange); @Override boolean isUserInRole(ApiHttpExchange exchange, String role); void registerController(Object controller); void registerControllers(Object... controllers); void registerControllerList(List<Object> controllers); @Override void init(ServletConfig config); void registerMBeans(); void registerMBeans(MBeanServer mBeanServer); @Override TimerRegistry getTimerRegistry(); void setTimerRegistry(TimerRegistry timerRegistry);  }