KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > util > FilenameGuardFilter


1 package org.apache.activemq.util;
2
3 import java.io.IOException JavaDoc;
4
5 import javax.servlet.Filter JavaDoc;
6 import javax.servlet.FilterChain JavaDoc;
7 import javax.servlet.FilterConfig JavaDoc;
8 import javax.servlet.ServletException JavaDoc;
9 import javax.servlet.ServletRequest JavaDoc;
10 import javax.servlet.ServletResponse JavaDoc;
11 import javax.servlet.http.HttpServletRequest JavaDoc;
12 import javax.servlet.http.HttpServletRequestWrapper JavaDoc;
13
14 import org.mortbay.log.Log;
15
16 public class FilenameGuardFilter implements Filter JavaDoc {
17
18     public void destroy() {
19         // nothing to destroy
20
}
21
22     public void init(FilterConfig JavaDoc config) throws ServletException JavaDoc {
23         // nothing to init
24
}
25
26     public void doFilter(ServletRequest JavaDoc request, ServletResponse JavaDoc response, FilterChain JavaDoc chain) throws IOException JavaDoc, ServletException JavaDoc {
27         if (request instanceof HttpServletRequest JavaDoc) {
28             HttpServletRequest JavaDoc httpRequest = (HttpServletRequest JavaDoc)request;
29             GuardedHttpServletRequest guardedRequest = new GuardedHttpServletRequest(httpRequest);
30             chain.doFilter(guardedRequest, response);
31         } else {
32             chain.doFilter(request, response);
33         }
34     }
35     
36     private static class GuardedHttpServletRequest extends HttpServletRequestWrapper JavaDoc {
37         
38         public GuardedHttpServletRequest(HttpServletRequest JavaDoc httpRequest) {
39             super(httpRequest);
40         }
41
42         private String JavaDoc guard(String JavaDoc filename) {
43             String JavaDoc guarded = filename.replace(":", "_");
44             if (Log.isDebugEnabled())
45             {
46                 Log.debug("guarded " + filename + " to "+ guarded);
47             }
48             return guarded;
49         }
50         
51         @Override JavaDoc
52         public String JavaDoc getParameter(String JavaDoc name) {
53             if (name.equals("Destination")) {
54                 return guard(super.getParameter(name));
55             } else {
56                 return super.getParameter(name);
57             }
58         }
59         
60         @Override JavaDoc
61         public String JavaDoc getPathInfo() {
62             return guard(super.getPathInfo());
63         }
64         
65         @Override JavaDoc
66         public String JavaDoc getPathTranslated() {
67             return guard(super.getPathTranslated());
68         }
69         
70         @Override JavaDoc
71         public String JavaDoc getRequestURI() {
72             return guard(super.getRequestURI());
73         }
74     }
75 }
76
Popular Tags