KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > doc > javadoc > RedirectFilter


1 /*
2  * Copyright (c) 1998-2003 Caucho Technology -- all rights reserved
3  *
4  * Caucho Technology permits redistribution, modification and use
5  * of this file in source and binary form ("the Software") under the
6  * Caucho Developer Source License ("the License"). The following
7  * conditions must be met:
8  *
9  * 1. Each copy or derived work of the Software must preserve the copyright
10  * notice and this notice unmodified.
11  *
12  * 2. Redistributions of the Software in source or binary form must include
13  * an unmodified copy of the License, normally in a plain ASCII text
14  *
15  * 3. The names "Resin" or "Caucho" are trademarks of Caucho Technology and
16  * may not be used to endorse products derived from this software.
17  * "Resin" or "Caucho" may not appear in the names of products derived
18  * from this software.
19  *
20  * This Software is provided "AS IS," without a warranty of any kind.
21  * ALL EXPRESS OR IMPLIED REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
22  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.
24  *
25  * CAUCHO TECHNOLOGY AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
26  * SUFFERED BY LICENSEE OR ANY THIRD PARTY AS A RESULT OF USING OR
27  * DISTRIBUTING SOFTWARE. IN NO EVENT WILL CAUCHO OR ITS LICENSORS BE LIABLE
28  * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
29  * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
30  * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
31  * INABILITY TO USE SOFTWARE, EVEN IF HE HAS BEEN ADVISED OF THE POSSIBILITY
32  * OF SUCH DAMAGES.
33  *
34  * @author Sam
35  */

36
37 package com.caucho.doc.javadoc;
38
39 import com.caucho.log.Log;
40 import com.caucho.util.L10N;
41
42 import java.io.IOException JavaDoc;
43
44 import java.util.logging.Logger JavaDoc;
45
46 import javax.naming.Context JavaDoc;
47 import javax.naming.InitialContext JavaDoc;
48 import javax.naming.NamingException JavaDoc;
49
50 import javax.servlet.Filter JavaDoc;
51 import javax.servlet.FilterChain JavaDoc;
52 import javax.servlet.FilterConfig JavaDoc;
53 import javax.servlet.ServletException JavaDoc;
54 import javax.servlet.ServletRequest JavaDoc;
55 import javax.servlet.ServletResponse JavaDoc;
56 import javax.servlet.http.HttpServletRequest JavaDoc;
57 import javax.servlet.http.HttpServletResponse JavaDoc;
58 import com.caucho.util.CharBuffer;
59
60 /**
61  * Redirect to the search servlet if the first component of the path
62  * is not a recognized api.
63  */

64 public class RedirectFilter implements Filter JavaDoc {
65   static protected final Logger JavaDoc log = Log.open(RedirectFilter.class);
66   static final L10N L = new L10N(RedirectFilter.class);
67
68   private final static String JavaDoc STORE_JNDINAME = "resin-javadoc/store";
69
70   private Store _store;
71
72   public void init(FilterConfig JavaDoc filterConfig)
73     throws ServletException JavaDoc
74   {
75     try {
76       Context JavaDoc env = (Context JavaDoc) new InitialContext JavaDoc().lookup("java:comp/env");
77
78       _store = (Store) env.lookup(STORE_JNDINAME);
79
80       if (_store == null)
81         throw new ServletException JavaDoc(L.l("`{0}' is an unknown Store",STORE_JNDINAME));
82     } catch (NamingException JavaDoc ex) {
83       throw new ServletException JavaDoc(ex);
84     }
85   }
86
87   public void doFilter(ServletRequest JavaDoc request,
88                        ServletResponse JavaDoc response,
89                        FilterChain JavaDoc chain)
90     throws ServletException JavaDoc, IOException JavaDoc
91   {
92     HttpServletRequest JavaDoc req = (HttpServletRequest JavaDoc) request;
93     HttpServletResponse JavaDoc res = (HttpServletResponse JavaDoc) response;
94
95     String JavaDoc url = req.getPathInfo();
96     int i = url.indexOf('/');
97     if (i > -1) {
98       String JavaDoc api = url.substring(0,i);
99       url = url.substring(i);
100       if (_store.getApi(api) == null) {
101         // redirect to search
102
CharBuffer redirect = CharBuffer.allocate();
103         redirect.append(req.getRequestURI());
104         redirect.setLength(redirect.length() - req.getPathInfo().length());
105         redirect.append("index.jsp?query=");
106         // rewrite it
107
redirect.append(url);
108         res.sendRedirect(res.encodeRedirectURL(url));
109       }
110     }
111
112     chain.doFilter(request, response);
113   }
114
115   
116   public void destroy()
117   {
118   }
119   
120 }
121
122
Popular Tags