1 25 26 package net.killingar.forum.filter; 27 28 import javax.servlet.*; 29 import javax.servlet.http.HttpServletRequest ; 30 31 public class WikiRewriteFilter implements Filter 32 { 33 private FilterConfig filterConfig; 34 35 public void doFilter(final ServletRequest request, final ServletResponse response, FilterChain chain) throws java.io.IOException , javax.servlet.ServletException 36 { 37 HttpServletRequest httpRequest = (HttpServletRequest )request; 38 39 boolean documents = false; 40 String url = httpRequest.getRequestURI(); 41 if (url.regionMatches(true, 0, "/wiki/", 0, "/wiki/".length())) 42 url = url.substring("/wiki/".length()); 43 else if (url.regionMatches(true, 0, "/documents/", 0, "/documents/".length())) 44 { 45 documents = true; 46 url = url.substring("/documents/".length()); 47 } 48 else 49 throw new RuntimeException ("invalid url"); 50 51 String context = null; 52 String document = null; 53 int tmp = url.indexOf('/'); 54 if (tmp != -1) 55 { 56 context = url.substring(0, tmp); 57 url = url.substring(tmp+1); 58 tmp = url.indexOf('/'); 59 if (tmp != -1) 60 document = url.substring(tmp); 61 else 62 document = url; 63 } 64 else 65 context = url; 66 67 if ("".equals(document)) 68 document = null; 69 if ("".equals(context)) 70 context = null; 71 72 if (documents) 73 { 74 document = context; 75 context = null; 76 } 77 78 StringBuffer path = new StringBuffer ("/wiki.view.action?"); 79 80 if (document != null) 81 { 82 path.append("wiki="); 83 path.append(document); 84 } 85 if (context != null) 86 { 87 if (document != null) 88 path.append("&"); 89 90 path.append("context="); 91 path.append(context); 92 } 93 94 RequestDispatcher disp = filterConfig.getServletContext().getRequestDispatcher(path.toString()); 95 96 disp.forward(request, response); 97 98 } 100 101 public void setFilterConfig(final FilterConfig filterConfig) 102 { 103 this.filterConfig = filterConfig; 104 } 105 106 public FilterConfig getFilterConfig() 107 { 108 return filterConfig; 109 } 110 111 public void init(FilterConfig filterConfig) 112 { 113 setFilterConfig(filterConfig); 114 } 115 116 public void destroy() 117 { 118 } 119 } | Popular Tags |