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 ; 9 10 import javax.servlet.Filter ; 11 import javax.servlet.FilterChain ; 12 import javax.servlet.FilterConfig ; 13 import javax.servlet.ServletException ; 14 import javax.servlet.ServletOutputStream ; 15 import javax.servlet.ServletRequest ; 16 import javax.servlet.ServletResponse ; 17 import javax.servlet.http.HttpServletRequest ; 18 import javax.servlet.http.HttpServletResponse ; 19 import javax.servlet.http.HttpSession ; 20 import org.apache.commons.logging.Log; 21 import org.apache.commons.logging.LogFactory; 22 import org.roller.config.RollerConfig; 23 24 35 public final class BreadCrumbFilter implements Filter 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 req, ServletResponse resp, FilterChain chain) 45 throws IOException , ServletException 46 { 47 HttpServletRequest request = (HttpServletRequest ) req; 48 HttpServletResponse response = (HttpServletResponse ) resp; 49 50 HttpSession 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 if (request.getQueryString() != null 63 && request.getQueryString().equals("BreadCrumb")) 64 { 65 response.setContentType("text/html; charset=UTF-8"); 66 ServletOutputStream 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 url = request.getRequestURL(); 81 String 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 106 public void init(FilterConfig filterConfig) 107 { 108 mLogger.debug("Initializing Breadcrumb Filter"); 109 110 String 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 123 public void destroy() 124 { 125 mMaxStackSize = 10; 126 } 127 } 128 | Popular Tags |