TransactionTokenInterceptor implements HandlerInterceptor { @Override public boolean preHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler) { if (!(handler instanceof HandlerMethod)) { return true; } logger.trace("preHandle"); HandlerMethod handlerMethod = (HandlerMethod) handler; TransactionTokenInfo tokenInfo = tokenInfoStore.getTransactionTokenInfo( handlerMethod); TransactionToken receivedToken = INVALID_TOKEN; if (tokenInfo.needValidate()) { receivedToken = createReceivedToken(request); if (!validateToken(receivedToken, tokenStore, tokenInfo)) { processTransactionTokenError(receivedToken); } } if (tokenInfo.getTransactionTokenType() == TransactionTokenType.BEGIN) { String tokenStr = request.getParameter(TOKEN_REQUEST_PARAMETER); if (null != tokenStr) { removeToken(new TransactionToken(tokenStr)); } } TransactionTokenContextImpl tokenContext = new TransactionTokenContextImpl(tokenInfo, receivedToken); request.setAttribute(TOKEN_CONTEXT_REQUEST_ATTRIBUTE_NAME, tokenContext); return true; } TransactionTokenInterceptor();  TransactionTokenInterceptor(int sizePerTokenName);  TransactionTokenInterceptor(TokenStringGenerator generator,
            TransactionTokenInfoStore tokenInfoStore,
            TransactionTokenStore tokenStore); @Override boolean preHandle(final HttpServletRequest request,
            final HttpServletResponse response, final Object handler); @Override void postHandle(final HttpServletRequest request,
            final HttpServletResponse response, final Object handler,
            final ModelAndView modelAndView); @Override void afterCompletion(final HttpServletRequest request,
            final HttpServletResponse response, final Object handler,
            final Exception ex); static final String TOKEN_CONTEXT_REQUEST_ATTRIBUTE_NAME; static final String NEXT_TOKEN_REQUEST_ATTRIBUTE_NAME; static final String TOKEN_REQUEST_PARAMETER; }
@Test public void testPreHandleIncorrectHandler() throws Exception { boolean result = interceptor.preHandle(request, response, null); assertThat(result, is(true)); }
@Test public void testPreHandleWithoutTokenValidate() throws Exception { boolean result = interceptor.preHandle(request, response, new HandlerMethod(new TransactionTokenSampleController(), TransactionTokenSampleController.class .getDeclaredMethod("first", SampleForm.class, Model.class))); assertThat(result, is(true)); }
@Test public void testPreHandleWithTokenValidate() throws Exception { HttpSessionTransactionTokenStore tokenStore = new HttpSessionTransactionTokenStore(); TransactionToken inputToken = new TransactionToken("testTokenAttr", "111", "222"); tokenStore.store(inputToken); request.setParameter( TransactionTokenInterceptor.TOKEN_REQUEST_PARAMETER, "testTokenAttr~111~222"); interceptor = new TransactionTokenInterceptor(new TokenStringGenerator(), new TransactionTokenInfoStore(), tokenStore); boolean result = interceptor.preHandle(request, response, new HandlerMethod(new TransactionTokenSampleController(), TransactionTokenSampleController.class .getDeclaredMethod("third", SampleForm.class, Model.class))); assertThat(result, is(true)); assertThat(request.getAttribute( TransactionTokenInterceptor.TOKEN_CONTEXT_REQUEST_ATTRIBUTE_NAME), is(notNullValue())); }
@Test(expected = InvalidTransactionTokenException.class) public void testPreHandleWithTokenValidateFail() throws Exception { HttpSessionTransactionTokenStore tokenStore = new HttpSessionTransactionTokenStore(); interceptor = new TransactionTokenInterceptor(new TokenStringGenerator(), new TransactionTokenInfoStore(), tokenStore); interceptor.preHandle(request, response, new HandlerMethod(new TransactionTokenSampleController(), TransactionTokenSampleController.class .getDeclaredMethod("third", SampleForm.class, Model.class))); }
@Test public void testPreHandleTokenIsSentInHiddenFieldAtBeginPhase() throws Exception { HttpSessionTransactionTokenStore tokenStore = new HttpSessionTransactionTokenStore(); TransactionToken inputToken = new TransactionToken("tokenName1", "111", "222"); tokenStore.store(inputToken); assertThat(tokenStore.getSession().getAttribute(tokenStore .createSessionAttributeName(inputToken)), is(notNullValue())); request.setParameter( TransactionTokenInterceptor.TOKEN_REQUEST_PARAMETER, "tokenName1~111~222"); interceptor = new TransactionTokenInterceptor(new TokenStringGenerator(), new TransactionTokenInfoStore(), tokenStore); interceptor.preHandle(request, response, new HandlerMethod(new TransactionTokenSampleController(), TransactionTokenSampleController.class .getDeclaredMethod("first", SampleForm.class, Model.class))); assertThat(tokenStore.getSession().getAttribute(tokenStore .createSessionAttributeName(inputToken)), is(nullValue())); }
@Test public void testPreHandleValidTokenOnCheck() throws Exception { HttpSessionTransactionTokenStore tokenStore = new HttpSessionTransactionTokenStore(); TransactionToken inputToken = new TransactionToken("testTokenAttr", "111", "222"); tokenStore.store(inputToken); assertThat(tokenStore.getSession().getAttribute(tokenStore .createSessionAttributeName(inputToken)), is(notNullValue())); request.setParameter( TransactionTokenInterceptor.TOKEN_REQUEST_PARAMETER, "testTokenAttr~111~222"); interceptor = new TransactionTokenInterceptor(new TokenStringGenerator(), new TransactionTokenInfoStore(), tokenStore); boolean result = interceptor.preHandle(request, response, new HandlerMethod(new TransactionTokenSampleController(), TransactionTokenSampleController.class .getDeclaredMethod("fifth", SampleForm.class, Model.class))); assertThat(result, is(true)); TransactionTokenContext transactionTokenCtx = (TransactionTokenContext) request .getAttribute( TransactionTokenInterceptor.TOKEN_CONTEXT_REQUEST_ATTRIBUTE_NAME); TransactionToken token = transactionTokenCtx.getReceivedToken(); assertThat(token, is(notNullValue())); assertThat(token.getTokenName(), is("testTokenAttr")); assertThat(token.getTokenKey(), is("111")); assertThat(token.getTokenValue(), is("222")); }