View Javadoc

1   /*
2    * CertCache.java
3    *
4    * Created on December 21, 2004, 12:54 PM
5    */
6   
7   package gov.bnl.gums.admin;
8   
9   import java.security.cert.X509Certificate;
10  import javax.servlet.Filter;
11  import javax.servlet.ServletContext;
12  import org.apache.commons.logging.Log;
13  import org.apache.commons.logging.LogFactory;
14  
15  /***
16   *
17   * @author carcassi
18   */
19  public class CertCache implements Filter {
20      private static Log log = LogFactory.getLog(CertCache.class);
21      
22      /*** Creates a new instance of CertCache */
23      public CertCache() {
24      }
25  
26      public void doFilter(javax.servlet.ServletRequest servletRequest, javax.servlet.ServletResponse servletResponse, javax.servlet.FilterChain filterChain) throws java.io.IOException, javax.servlet.ServletException {
27          setUserCertificate(null);
28          if (servletRequest.getAttribute("javax.servlet.request.X509Certificate") != null){
29              X509Certificate cert = ((X509Certificate[]) servletRequest.getAttribute("javax.servlet.request.X509Certificate"))[0];
30              setUserCertificate(cert);
31          }
32          filterChain.doFilter(servletRequest, servletResponse);
33      }
34      
35      private static ServletContext context;
36  
37      public void init(javax.servlet.FilterConfig filterConfig) throws javax.servlet.ServletException {
38          context = filterConfig.getServletContext();
39      }
40  
41      public void destroy() {
42      }
43      
44      private static ThreadLocal certificate = new ThreadLocal();
45      static X509Certificate getUserCertificate() {
46          return (X509Certificate) certificate.get();
47      }
48      
49      static void setUserCertificate(X509Certificate cert) {
50          certificate.set(cert);
51      }
52      
53      
54      static String getUserDN() {
55          return CertToolkit.getUserDN(getUserCertificate());
56      }
57      
58      static String getConfPath() {
59          String base = context.getRealPath("/");
60          log.trace("Path to the web app: '" + base + "'");
61          String fullpath = base + "/WEB-INF/classes/gums.config";
62          log.trace("URL to config file: '" + fullpath + "'");
63          return fullpath;
64      }
65      
66  }