@Test public void testDoFilter() throws Exception { MockFilterConfig filterConfig = new MockFilterConfig(); String requestParameterName = UUID.randomUUID().toString(); MockHttpServletResponse response = new MockHttpServletResponse(); MockHttpServletRequest request = new MockHttpServletRequest(); Filter filter = new BypassSessionTimeoutFilter(); try { filter.init(filterConfig); Assert.fail("This should fail, if there is no requestParameterName"); } catch (ServletException e) { } filterConfig.addInitParameter("requestParameterName", requestParameterName); filter.init(filterConfig); filter.doFilter(request, response, new MockFilterChain()); MockHttpSession session = new MockHttpSession(); request.setSession(session); Assert.assertNull(session.getAttribute("lastInSessionRequestTime")); filter.doFilter(request, response, new MockFilterChain()); Assert.assertNotNull(session.getAttribute("lastInSessionRequestTime")); session.setMaxInactiveInterval(Integer.MAX_VALUE); request.addParameter(requestParameterName, Boolean.TRUE.toString()); filter.doFilter(request, response, new MockFilterChain()); Assert.assertFalse(session.isInvalid()); session.setMaxInactiveInterval(0); session.setAttribute("lastInSessionRequestTime", 0L); request.addParameter(requestParameterName, Boolean.TRUE.toString()); filter.doFilter(request, response, new MockFilterChain()); Assert.assertTrue(session.isInvalid()); }
@Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpSession session; if (request instanceof HttpServletRequest && (session = ((HttpServletRequest) request).getSession(false)) != null) { if (Boolean.parseBoolean(request.getParameter(requestParameterName)) && session.getAttribute(SESSION_PARAMETER) != null) { long lastInSessionRequestTime = (Long) session.getAttribute(SESSION_PARAMETER); if (lastInSessionRequestTime + session.getMaxInactiveInterval() * 1000L < System .currentTimeMillis()) { try { session.invalidate(); } catch (IllegalStateException e) { LOG.debug(e.getMessage()); } ((HttpServletResponse) response).sendError(401); return; } } else { session.setAttribute(SESSION_PARAMETER, System.currentTimeMillis()); } } chain.doFilter(request, response); }
BypassSessionTimeoutFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpSession session; if (request instanceof HttpServletRequest && (session = ((HttpServletRequest) request).getSession(false)) != null) { if (Boolean.parseBoolean(request.getParameter(requestParameterName)) && session.getAttribute(SESSION_PARAMETER) != null) { long lastInSessionRequestTime = (Long) session.getAttribute(SESSION_PARAMETER); if (lastInSessionRequestTime + session.getMaxInactiveInterval() * 1000L < System .currentTimeMillis()) { try { session.invalidate(); } catch (IllegalStateException e) { LOG.debug(e.getMessage()); } ((HttpServletResponse) response).sendError(401); return; } } else { session.setAttribute(SESSION_PARAMETER, System.currentTimeMillis()); } } chain.doFilter(request, response); } }
BypassSessionTimeoutFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpSession session; if (request instanceof HttpServletRequest && (session = ((HttpServletRequest) request).getSession(false)) != null) { if (Boolean.parseBoolean(request.getParameter(requestParameterName)) && session.getAttribute(SESSION_PARAMETER) != null) { long lastInSessionRequestTime = (Long) session.getAttribute(SESSION_PARAMETER); if (lastInSessionRequestTime + session.getMaxInactiveInterval() * 1000L < System .currentTimeMillis()) { try { session.invalidate(); } catch (IllegalStateException e) { LOG.debug(e.getMessage()); } ((HttpServletResponse) response).sendError(401); return; } } else { session.setAttribute(SESSION_PARAMETER, System.currentTimeMillis()); } } chain.doFilter(request, response); }  }
BypassSessionTimeoutFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpSession session; if (request instanceof HttpServletRequest && (session = ((HttpServletRequest) request).getSession(false)) != null) { if (Boolean.parseBoolean(request.getParameter(requestParameterName)) && session.getAttribute(SESSION_PARAMETER) != null) { long lastInSessionRequestTime = (Long) session.getAttribute(SESSION_PARAMETER); if (lastInSessionRequestTime + session.getMaxInactiveInterval() * 1000L < System .currentTimeMillis()) { try { session.invalidate(); } catch (IllegalStateException e) { LOG.debug(e.getMessage()); } ((HttpServletResponse) response).sendError(401); return; } } else { session.setAttribute(SESSION_PARAMETER, System.currentTimeMillis()); } } chain.doFilter(request, response); }  @Override void destroy(); @Override void doFilter(ServletRequest request, ServletResponse response, FilterChain chain); @Override void init(FilterConfig filterConfig); }
BypassSessionTimeoutFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpSession session; if (request instanceof HttpServletRequest && (session = ((HttpServletRequest) request).getSession(false)) != null) { if (Boolean.parseBoolean(request.getParameter(requestParameterName)) && session.getAttribute(SESSION_PARAMETER) != null) { long lastInSessionRequestTime = (Long) session.getAttribute(SESSION_PARAMETER); if (lastInSessionRequestTime + session.getMaxInactiveInterval() * 1000L < System .currentTimeMillis()) { try { session.invalidate(); } catch (IllegalStateException e) { LOG.debug(e.getMessage()); } ((HttpServletResponse) response).sendError(401); return; } } else { session.setAttribute(SESSION_PARAMETER, System.currentTimeMillis()); } } chain.doFilter(request, response); }  @Override void destroy(); @Override void doFilter(ServletRequest request, ServletResponse response, FilterChain chain); @Override void init(FilterConfig filterConfig); static final String SESSION_PARAMETER; }