1 25 26 29 30 package net.killingar.servlet.filter; 31 32 import javax.naming.Context ; 33 import javax.naming.InitialContext ; 34 import javax.naming.NamingException ; 35 import javax.sql.DataSource ; 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 datasource = "java:comp/env/jdbc/RedirectDS"; 44 private static Context ctx; 45 private static DataSource ds; 46 private static Map map = new HashMap(); 47 private static long lastUpdateTime = 0; 48 49 50 53 public static Connection getNewConnection() throws SQLException 54 { 55 try 56 { 57 if (ctx == null) 58 ctx = new InitialContext (); 59 if (ds == null) 60 ds = (DataSource ) ctx.lookup(datasource); 61 return ds.getConnection(); 62 } 63 catch (NamingException e) 64 { 65 e.printStackTrace(); 66 throw new SQLException(e.getMessage()); 67 } 68 } 69 70 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 , javax.servlet.ServletException 87 { 88 update(); 89 90 HttpServletRequest request = (HttpServletRequest)inRequest; 91 HttpServletResponse response = (HttpServletResponse)inResponse; 92 93 String s = (String )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 from, String 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 |