KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > killingar > servlet > filter > RedirectFilter


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 /**
27  * Base class for managers.
28  */

29
30 package net.killingar.servlet.filter;
31
32 import javax.naming.Context JavaDoc;
33 import javax.naming.InitialContext JavaDoc;
34 import javax.naming.NamingException JavaDoc;
35 import javax.sql.DataSource JavaDoc;
36 import java.sql.*;
37 import java.util.*;
38 import javax.servlet.*;
39 import javax.servlet.http.*;
40
41 public class RedirectFilter extends GenericFilter
42 {
43     private static final String JavaDoc datasource = "java:comp/env/jdbc/RedirectDS";
44     private static Context JavaDoc ctx;
45     private static DataSource JavaDoc ds;
46     private static Map map = new HashMap();
47     private static long lastUpdateTime = 0;
48
49
50     /**
51      * Get a new connection.
52      */

53     public static Connection getNewConnection() throws SQLException
54     {
55         try
56         {
57             if (ctx == null)
58                 ctx = new InitialContext JavaDoc();
59             if (ds == null)
60         ds = (DataSource JavaDoc) ctx.lookup(datasource);
61             return ds.getConnection();
62         }
63         catch (NamingException JavaDoc e)
64         {
65             e.printStackTrace();
66             throw new SQLException(e.getMessage());
67         }
68     }
69
70     /**
71      * Close objects
72      */

73     public static void closeAll(Connection c, Statement statement, ResultSet result)
74     {
75         try
76         {
77             if (result != null)result.close();
78             if (statement != null)statement.close();
79             if (c != null)c.close();
80         }
81         catch (SQLException e){}
82     }
83     public static void closeAll(Connection c, Statement statement) throws SQLException { closeAll(c, statement, null); }
84     public static void closeAll(Connection c) throws SQLException { closeAll(c, null, null); }
85
86     public void doFilter(final ServletRequest inRequest, final ServletResponse inResponse, FilterChain inChain) throws java.io.IOException JavaDoc, javax.servlet.ServletException JavaDoc
87     {
88         update();
89
90         HttpServletRequest request = (HttpServletRequest)inRequest;
91         HttpServletResponse response = (HttpServletResponse)inResponse;
92
93         String JavaDoc s = (String JavaDoc)map.get(request.getServerName());
94         if (s != null)
95         {
96             response.sendRedirect(s);
97             return;
98         }
99
100         inChain.doFilter(inRequest, inResponse);
101     }
102
103     private static void update()
104     {
105         if (System.currentTimeMillis()-lastUpdateTime < 1000)
106             return;
107
108         Connection c = null;
109         PreparedStatement statement = null;
110         ResultSet result = null;
111         try
112         {
113             c = getNewConnection();
114             statement = c.prepareStatement("select * from RedirectMapping where LastChanged > ?");
115             statement.setTimestamp(1, new Timestamp(lastUpdateTime));
116             lastUpdateTime = System.currentTimeMillis();
117             result = statement.executeQuery();
118
119             while (result.next())
120             {
121                 map.put(result.getString("fromHost"), result.getString("toAddress"));
122             }
123         }
124         catch (SQLException e)
125         {
126         }
127         finally
128         {
129             closeAll(c, statement, result);
130         }
131     }
132
133     public static Map getMap()
134     {
135         update();
136         return Collections.unmodifiableMap(map);
137     }
138
139     public static void set(String JavaDoc from, String JavaDoc to) throws SQLException
140     {
141         Connection c = null;
142         PreparedStatement statement = null;
143         try
144         {
145             c = getNewConnection();
146             statement = c.prepareStatement("delete from RedirectMapping where fromHost = ?");
147             statement.setString(1, from);
148             statement.executeUpdate();
149             statement.close();
150
151             statement = c.prepareStatement("insert into RedirectMapping(fromHost, toAddress, lastChanged) values(?, ?, NOW())");
152             statement.setString(1, from);
153             statement.setString(2, to);
154             statement.executeUpdate();
155
156             map.put(from, to);
157         }
158         finally
159         {
160             closeAll(c, statement, null);
161         }
162     }
163 }
Popular Tags