KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > deliver > applications > filters > CacheEvictionFilter


1 /* ===============================================================================
2 *
3 * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4 *
5 * ===============================================================================
6 *
7 * Copyright (C)
8 *
9 * This program is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License version 2, as published by the
11 * Free Software Foundation. See the file LICENSE.html for more information.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19 * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20 *
21 * ===============================================================================
22 */

23
24 package org.infoglue.deliver.applications.filters;
25
26 import java.io.IOException JavaDoc;
27 import java.net.URLDecoder JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Enumeration JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.StringTokenizer JavaDoc;
32
33 import javax.servlet.Filter JavaDoc;
34 import javax.servlet.FilterChain JavaDoc;
35 import javax.servlet.FilterConfig JavaDoc;
36 import javax.servlet.ServletException JavaDoc;
37 import javax.servlet.ServletRequest JavaDoc;
38 import javax.servlet.ServletResponse JavaDoc;
39 import javax.servlet.http.HttpServletRequest JavaDoc;
40 import javax.servlet.http.HttpServletResponse JavaDoc;
41
42 import org.apache.log4j.Logger;
43 import org.infoglue.deliver.util.CacheController;
44
45 /**
46  * This filter empties the caches waiting to be emptied.
47  *
48  * @author Mattias Bogeblad
49  */

50
51 public class CacheEvictionFilter implements Filter JavaDoc
52 {
53     public final static Logger logger = Logger.getLogger(CacheEvictionFilter.class.getName());
54
55     private static String JavaDoc FILTER_URIS_PARAMETER = "FilterURIs";
56     
57     private FilterConfig JavaDoc filterConfig = null;
58     private URIMatcher uriMatcher = null;
59     
60     public void init(FilterConfig JavaDoc filterConfig) throws ServletException JavaDoc
61     {
62         this.filterConfig = filterConfig;
63         String JavaDoc filterURIs = filterConfig.getInitParameter(FILTER_URIS_PARAMETER);
64         uriMatcher = URIMatcher.compilePatterns(splitString(filterURIs, ","), false);
65     }
66
67     public void doFilter(ServletRequest JavaDoc servletRequest, ServletResponse JavaDoc servletResponse, FilterChain JavaDoc filterChain) throws IOException JavaDoc, ServletException JavaDoc
68     {
69         HttpServletRequest JavaDoc httpRequest = (HttpServletRequest JavaDoc) servletRequest;
70         HttpServletResponse JavaDoc httpResponse = (HttpServletResponse JavaDoc) servletResponse;
71                 
72         try
73         {
74             String JavaDoc requestURI = URLDecoder.decode(getContextRelativeURI(httpRequest), "UTF-8");
75             String JavaDoc userAgent = httpRequest.getHeader("user-agent");
76             if(userAgent != null)
77                 userAgent = userAgent.toLowerCase();
78
79             if(logger.isInfoEnabled())
80                 logger.info("userAgent:" + userAgent);
81             
82             if (!uriMatcher.matches(requestURI) && userAgent != null && userAgent.indexOf("java") == -1 && userAgent.indexOf("axis") == -1)
83             {
84                 CacheController.evictWaitingCache();
85             }
86         }
87         catch (Exception JavaDoc e)
88         {
89             logger.error("CacheEvictionFilter threw error:" + e.getMessage(), e);
90         }
91         
92         filterChain.doFilter(httpRequest, httpResponse);
93     }
94
95     public void destroy()
96     {
97     }
98
99     private String JavaDoc getContextRelativeURI(HttpServletRequest JavaDoc request)
100     {
101         String JavaDoc requestURI = request.getRequestURI();
102         String JavaDoc contextPath = request.getContextPath();
103         if (contextPath != null && requestURI.length() > 0)
104         {
105             requestURI = requestURI.substring(contextPath.length(), requestURI.length());
106         }
107         
108         if (requestURI.length() == 0)
109             return "/";
110         
111         return requestURI;
112     }
113     
114     private String JavaDoc[] splitString(String JavaDoc str, String JavaDoc delimiter)
115     {
116         List JavaDoc list = new ArrayList JavaDoc();
117         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(str, delimiter);
118         while (st.hasMoreTokens())
119         {
120             // Updated to handle portal-url:s
121
String JavaDoc t = st.nextToken();
122             if (t.startsWith("_"))
123             {
124                 break;
125             }
126             else
127             {
128                 // Not related to portal - add
129
list.add(t.trim());
130             }
131         }
132         return (String JavaDoc[]) list.toArray(new String JavaDoc[list.size()]);
133     }
134
135 }
Popular Tags