SessionHelpers { public ServletRequest prepareRequest(ServletRequest request, ServletResponse response, ServletContext originalServletContext) { if (request instanceof HttpServletRequest && response instanceof HttpServletResponse) { HttpRequestWrapper wrappedRequest = (HttpRequestWrapper)request.getAttribute(REQUEST_WRAPPED_ATTRIBUTE); ServletContext servletContext = originalServletContext; if (servletContext == null) { logger.info("ServletContext was null when prepareRequest() was called from Filter. " + "This means that filter's init() method was not called at initialization time. " + "This may result in unexpected behavior if this filter was invoked after" + "servlet dispatch forward() and include() calls."); servletContext = ((HttpServletRequest)request).getServletContext(); } if (wrappedRequest == null || servletContext != wrappedRequest.getServletContext()) { if (interceptListeners) { findListenersByIntercepting(servletContext, (HttpServletRequest)request); } wrappedRequest = wrapRequest(request, servletContext); wrappedRequest.setResponse(wrapResponse(response, wrappedRequest)); request.setAttribute(REQUEST_WRAPPED_ATTRIBUTE, wrappedRequest); return wrappedRequest; } } return request; }  ServletRequest prepareRequest(ServletRequest request, ServletResponse response,
      ServletContext originalServletContext); ServletResponse prepareResponse(ServletRequest request, ServletResponse response); MethodHandle[] initSessionManagement(ServletContext servletContext); MethodHandle[] initSessionManagement(ServletContext servletContext, boolean reset); void commitRequest(ServletRequest request, ServletRequest oldRequest); @SuppressWarnings("unchecked") void interceptHttpListener(EventListener caller, HttpSessionEvent event); void onAddListener(ServletContext servletContext, Object listener);  }
@Test public void testPrepareRequestNonHttp() { ServletRequest request = mock(ServletRequest.class); when(request.getServletContext()).thenReturn(servletContext); ServletResponse response = mock(ServletResponse.class); ServletContext originalServletContext = mock(ServletContext.class); ServletRequest result = SessionHelpersFacade.prepareRequest(request, response, originalServletContext); assertNotNull(result); assertSame(request, result); }
@Test public void testPrepareRequestNotInitializedFilter() { ServletRequest request = mock(HttpServletRequest.class); when(request.getServletContext()).thenReturn(servletContext); ServletResponse response = mock(HttpServletResponse.class); ServletRequest result = SessionHelpersFacade.prepareRequest(request, response, null); assertNotNull(result); assertNotSame(request, result); assertTrue(result instanceof HttpRequestWrapper); verify(request).setAttribute(eq(SessionHelpers.REQUEST_WRAPPED_ATTRIBUTE), refEq(result)); }
@Test public void testPrepareRequest() { ServletRequest request = mock(HttpServletRequest.class); when(request.getServletContext()).thenReturn(servletContext); ServletResponse response = mock(HttpServletResponse.class); ServletContext originalServletContext = mock(ServletContext.class); when(originalServletContext.getAttribute(Attributes.SESSION_MANAGER)).thenReturn(sessionManager); ServletRequest result = SessionHelpersFacade.prepareRequest(request, response, originalServletContext); assertNotNull(result); assertNotSame(request, result); assertTrue(result instanceof HttpRequestWrapper); verify(request).setAttribute(eq(SessionHelpers.REQUEST_WRAPPED_ATTRIBUTE), refEq(result)); }
@Test public void testPrepareAlreadyWappedRequestInSameContext() { HttpRequestWrapper request = mock(HttpRequestWrapper.class); when(request.getServletContext()).thenReturn(servletContext); ServletResponse response = mock(HttpServletResponse.class); ServletContext originalServletContext = mock(ServletContext.class); when(originalServletContext.getAttribute(SessionHelpers.SESSION_HELPERS)).thenReturn(new SessionHelpers()); when(request.getServletContext()).thenReturn(originalServletContext); when(request.getAttribute(SessionHelpers.REQUEST_WRAPPED_ATTRIBUTE)).thenReturn(request); ServletRequest result = SessionHelpersFacade.prepareRequest(request, response, originalServletContext); assertNotNull(result); assertSame(request, result); assertTrue(result instanceof HttpRequestWrapper); }
@Test public void testPreparePreviouslyWappedRequestButRewrappedOnTop() { HttpRequestWrapper request = mock(HttpRequestWrapper.class); ServletResponse response = mock(HttpServletResponse.class); ServletContext originalServletContext = mock(ServletContext.class); when(request.getServletContext()).thenReturn(originalServletContext); ServletRequest reWrappedRequest = mock(HttpServletRequest.class); ServletContext another = mock(ServletContext.class); when(another.getAttribute(SessionHelpers.SESSION_HELPERS)).thenReturn(new SessionHelpers()); when(reWrappedRequest.getServletContext()).thenReturn(another); when(reWrappedRequest.getAttribute(SessionHelpers.REQUEST_WRAPPED_ATTRIBUTE)).thenReturn(request); ServletRequest result = SessionHelpersFacade.prepareRequest(reWrappedRequest, response, originalServletContext); assertNotNull(result); assertNotSame(request, result); assertSame(reWrappedRequest, result); }
@Test public void testPrepareWappedRequestDifferentServletContext() { HttpRequestWrapper request = mock(HttpRequestWrapper.class); ServletResponse response = mock(HttpServletResponse.class); ServletContext originalServletContext = mock(ServletContext.class); originalServletContext.setAttribute(SessionHelpers.REQUEST_WRAPPED_ATTRIBUTE, request); when(originalServletContext.getAttribute(Attributes.SESSION_MANAGER)).thenReturn(sessionManager); ServletRequest reWrappedRequest = mock(HttpServletRequest.class); when(reWrappedRequest.getAttribute(SessionHelpers.REQUEST_WRAPPED_ATTRIBUTE)).thenReturn(request); ServletContext another = mock(ServletContext.class); when(another.getAttribute(SessionHelpers.SESSION_HELPERS)).thenReturn(new SessionHelpers()); SessionManager anotherSessionManager = mock(SessionManager.class); SessionConfiguration anotherSessionConfiguration = new SessionConfiguration(); when(sessionManager.getConfiguration()).thenReturn(anotherSessionConfiguration); when(another.getAttribute(Attributes.SESSION_MANAGER)).thenReturn(anotherSessionManager); when(reWrappedRequest.getServletContext()).thenReturn(another); ServletRequest result = SessionHelpersFacade.prepareRequest(reWrappedRequest, response, originalServletContext); assertNotNull(result); assertNotSame(reWrappedRequest, result); assertNotSame(request, result); assertTrue(result instanceof HttpRequestWrapper); }