KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > presentation > filters > BreadCrumbFilter


1 package org.roller.presentation.filters;
2
3 import org.apache.commons.collections.ArrayStack;
4 import org.roller.presentation.RollerSession;
5 import org.roller.util.StringUtils;
6 import org.roller.util.Utilities;
7
8 import java.io.IOException JavaDoc;
9
10 import javax.servlet.Filter JavaDoc;
11 import javax.servlet.FilterChain JavaDoc;
12 import javax.servlet.FilterConfig JavaDoc;
13 import javax.servlet.ServletException JavaDoc;
14 import javax.servlet.ServletOutputStream JavaDoc;
15 import javax.servlet.ServletRequest JavaDoc;
16 import javax.servlet.ServletResponse JavaDoc;
17 import javax.servlet.http.HttpServletRequest JavaDoc;
18 import javax.servlet.http.HttpServletResponse JavaDoc;
19 import javax.servlet.http.HttpSession JavaDoc;
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.roller.config.RollerConfig;
23
24 /**
25  * Intercepts requests and places URL
26  * into breadcrumb stack.
27  *
28  * @web.filter name="BreadCrumbFilter"
29  * *web.filter-mapping url-pattern="/*.do"
30  * @web.filter-init-param name="maxStackSize" value="3"
31  *
32  * @author <a HREF="mailto:lance@brainopolis.com">Lance Lavandowska</a>
33  *
34 **/

35 public final class BreadCrumbFilter implements Filter JavaDoc
36 {
37     private static Log mLogger =
38         LogFactory.getFactory().getInstance(BreadCrumbFilter.class);
39     
40     private int mMaxStackSize = 10;
41
42     
43     public void doFilter(
44         ServletRequest JavaDoc req, ServletResponse JavaDoc resp, FilterChain JavaDoc chain)
45         throws IOException JavaDoc, ServletException JavaDoc
46     {
47         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) req;
48         HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) resp;
49
50         HttpSession JavaDoc ses = request.getSession(false);
51         ArrayStack stack = null;
52         if (ses != null)
53         {
54             stack = (ArrayStack)ses.getAttribute(RollerSession.BREADCRUMB);
55         }
56         if (stack == null)
57         {
58             stack = new ArrayStack();
59         }
60
61         // This gives you a chance to look at your breadcrumb trail
62
if (request.getQueryString() != null
63             && request.getQueryString().equals("BreadCrumb"))
64         {
65             response.setContentType("text/html; charset=UTF-8");
66             ServletOutputStream JavaDoc out = response.getOutputStream();
67
68             for (int i=0; i<stack.size(); i++)
69             {
70                 out.println(stack.peek(i).toString() +"<br>");
71             }
72             out.flush();
73             out.close();
74         }
75         else
76         {
77             chain.doFilter(req, resp);
78         }
79
80         StringBuffer JavaDoc url = request.getRequestURL();
81         // now that we've successfully returned,
82
// add url to stack if it isn't a Velocity page
83
String JavaDoc servletPath = request.getServletPath();
84         if (servletPath.indexOf("page") == -1 &&
85             servletPath.indexOf("comments") == -1)
86         {
87             if (request.getQueryString() != null)
88             {
89                 url.append("?").append( request.getQueryString() );
90             }
91             if (stack.size() == mMaxStackSize)
92             {
93                 stack.remove(mMaxStackSize-1);
94             }
95             stack.push(url.toString());
96         }
97         if (ses != null)
98         {
99             ses.setAttribute(RollerSession.BREADCRUMB, stack);
100         }
101     }
102
103     /**
104      * Initialize controller values of filter.
105     **/

106     public void init(FilterConfig JavaDoc filterConfig)
107     {
108         mLogger.debug("Initializing Breadcrumb Filter");
109
110         String JavaDoc stackSize = RollerConfig.getProperty("breadcrumbs.stacksize");
111         if (!StringUtils.isEmpty(stackSize))
112         {
113             int mSS = Utilities.stringToInt(stackSize);
114             if (mSS != 0)
115             {
116                 mMaxStackSize = mSS;
117                 mLogger.info("set breadcrumb stack size to "+mSS);
118             }
119         }
120     }
121
122     /** destroy any instance values other than filterConfig **/
123     public void destroy()
124     {
125         mMaxStackSize = 10;
126     }
127 }
128
Popular Tags