From 808160eed731dd7c0ad2fde0c5d4c7302cb43176 Mon Sep 17 00:00:00 2001 From: Scott Battaglia Date: Tue, 26 Sep 2006 18:20:43 +0000 Subject: [PATCH] CASC-13 support for threadlocal --- .../client/validation/AssertionHolder.java | 43 ++++++++++++++++ .../filter/AssertionThreadLocalFilter.java | 51 +++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 cas-client-core/src/main/java/org/jasig/cas/client/validation/AssertionHolder.java create mode 100644 cas-client-core/src/main/java/org/jasig/cas/client/web/filter/AssertionThreadLocalFilter.java diff --git a/cas-client-core/src/main/java/org/jasig/cas/client/validation/AssertionHolder.java b/cas-client-core/src/main/java/org/jasig/cas/client/validation/AssertionHolder.java new file mode 100644 index 0000000..26f877e --- /dev/null +++ b/cas-client-core/src/main/java/org/jasig/cas/client/validation/AssertionHolder.java @@ -0,0 +1,43 @@ +/* + * Copyright 2006 The JA-SIG Collaborative. All rights reserved. See license + * distributed with this file and available online at + * http://www.ja-sig.org/products/cas/overview/license/index.html + */ +package org.jasig.cas.client.validation; + +/** + * Static holder that places Assertion in a threadlocal. + * + * @author Scott Battaglia + * @version $Revision$ $Date$ + * @since 3.0 + */ +public class AssertionHolder { + + /** + * ThreadLocal to hold the Assertion for Threads to access. + */ + private static final ThreadLocal threadLocal = new ThreadLocal(); + + + /** + * Retrieve the assertion from the ThreadLocal. + */ + public static Assertion getAssertion() { + return (Assertion) threadLocal.get(); + } + + /** + * Add the Assertion to the ThreadLocal. + */ + public static void setAssertion(final Assertion assertion) { + threadLocal.set(assertion); + } + + /** + * Clear the ThreadLocal. + */ + public static void clear() { + threadLocal.set(null); + } +} diff --git a/cas-client-core/src/main/java/org/jasig/cas/client/web/filter/AssertionThreadLocalFilter.java b/cas-client-core/src/main/java/org/jasig/cas/client/web/filter/AssertionThreadLocalFilter.java new file mode 100644 index 0000000..0530ec1 --- /dev/null +++ b/cas-client-core/src/main/java/org/jasig/cas/client/web/filter/AssertionThreadLocalFilter.java @@ -0,0 +1,51 @@ +/* + * Copyright 2006 The JA-SIG Collaborative. All rights reserved. See license + * distributed with this file and available online at + * http://www.ja-sig.org/products/cas/overview/license/index.html + */ +package org.jasig.cas.client.web.filter; + +import org.jasig.cas.client.validation.Assertion; +import org.jasig.cas.client.validation.AssertionHolder; +import org.springframework.web.util.WebUtils; + +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import java.io.IOException; + +/** + * Places the assertion in a ThreadLocal such that other resources can access it that do not have access to the web tier session. + * + * @author Scott Battaglia + * @version $Revision$ $Date$ + * @since 3.0 + */ +public final class AssertionThreadLocalFilter implements Filter { + + public void init(final FilterConfig filterConfig) throws ServletException { + // nothing to do here + } + + public void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse, final FilterChain filterChain) throws IOException, ServletException { + final HttpServletRequest request = (HttpServletRequest) servletRequest; + final Assertion assertion = (Assertion) WebUtils + .getSessionAttribute(request, + AbstractCasFilter.CONST_ASSERTION); + + try { + AssertionHolder.setAssertion(assertion); + filterChain.doFilter(servletRequest, servletResponse); + } finally { + AssertionHolder.clear(); + } + } + + public void destroy() { + // nothing to do + } +}