KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > killingar > forum > filter > WikiRewriteFilter


1 /* Copyright 2000-2005 Anders Hovmöller
2  *
3  * The person or persons who have associated their work with
4  * this document (the "Dedicator") hereby dedicate the entire
5  * copyright in the work of authorship identified below (the
6  * "Work") to the public domain.
7  *
8  * Dedicator makes this dedication for the benefit of the
9  * public at large and to the detriment of Dedicator's heirs
10  * and successors. Dedicator intends this dedication to be an
11  * overt act of relinquishment in perpetuity of all present
12  * and future rights under copyright law, whether vested or
13  * contingent, in the Work. Dedicator understands that such
14  * relinquishment of all rights includes the relinquishment of
15  * all rights to enforce (by lawsuit or otherwise) those
16  * copyrights in the Work.
17  *
18  * Dedicator recognizes that, once placed in the public
19  * domain, the Work may be freely reproduced, distributed,
20  * transmitted, used, modified, built upon, or otherwise
21  * exploited by anyone for any purpose, commercial or non-
22  * commercial, and in any way, including by methods that have
23  * not yet been invented or conceived.
24  */

25
26 package net.killingar.forum.filter;
27
28 import javax.servlet.*;
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30
31 public class WikiRewriteFilter implements Filter
32 {
33     private FilterConfig filterConfig;
34
35     public void doFilter(final ServletRequest JavaDoc request, final ServletResponse response, FilterChain chain) throws java.io.IOException JavaDoc, javax.servlet.ServletException JavaDoc
36     {
37         HttpServletRequest JavaDoc httpRequest = (HttpServletRequest JavaDoc)request;
38
39         boolean documents = false;
40         String JavaDoc 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 JavaDoc("invalid url");
50
51         String JavaDoc context = null;
52         String JavaDoc 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 JavaDoc path = new StringBuffer JavaDoc("/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         //chain.doFilter(request, response);
99
}
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