|
16 | 16 |
|
17 | 17 | package com.example.cloudsql; |
18 | 18 |
|
| 19 | +import com.google.common.base.Stopwatch; |
| 20 | + |
19 | 21 | import java.io.IOException; |
20 | 22 | import java.io.PrintWriter; |
21 | 23 | import java.net.Inet4Address; |
|
29 | 31 | import java.sql.Timestamp; |
30 | 32 | import java.util.Date; |
31 | 33 | import java.util.Properties; |
| 34 | +import java.util.concurrent.TimeUnit; |
32 | 35 |
|
33 | 36 | import javax.servlet.ServletException; |
34 | 37 | import javax.servlet.annotation.WebServlet; |
|
40 | 43 | @SuppressWarnings("serial") |
41 | 44 | @WebServlet(name = "cloudsql", value = "") |
42 | 45 | public class CloudSqlServlet extends HttpServlet { |
43 | | - String url; |
| 46 | + Connection conn; |
44 | 47 |
|
45 | 48 | @Override |
46 | 49 | public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, |
47 | 50 | ServletException { |
48 | | - // store only the first two octets of a users ip address |
49 | | - String userIp = req.getRemoteAddr(); |
50 | | - InetAddress address = InetAddress.getByName(userIp); |
51 | | - if (address instanceof Inet6Address) { |
52 | | - // nest indexOf calls to find the second occurrence of a character in a |
53 | | - // string |
54 | | - // an alternative is to use Apache Commons Lang: |
55 | | - // StringUtils.ordinalIndexOf() |
56 | | - userIp = userIp.substring(0, userIp.indexOf(":", userIp.indexOf(":") + 1)) + ":*:*:*:*:*:*"; |
57 | | - } else if (address instanceof Inet4Address) { |
58 | | - userIp = userIp.substring(0, userIp.indexOf(".", userIp.indexOf(".") + 1)) + ".*.*"; |
59 | | - } |
60 | | - |
61 | 51 | final String createTableSql = "CREATE TABLE IF NOT EXISTS visits ( visit_id INT NOT NULL " |
62 | 52 | + "AUTO_INCREMENT, user_ip VARCHAR(46) NOT NULL, timestamp DATETIME NOT NULL, " |
63 | 53 | + "PRIMARY KEY (visit_id) )"; |
64 | 54 | final String createVisitSql = "INSERT INTO visits (user_ip, timestamp) VALUES (?, ?)"; |
65 | 55 | final String selectSql = "SELECT user_ip, timestamp FROM visits ORDER BY timestamp DESC " |
66 | 56 | + "LIMIT 10"; |
| 57 | + |
| 58 | + String path = req.getRequestURI(); |
| 59 | + if (path.startsWith("/favicon.ico")) { |
| 60 | + return; // ignore the request for favicon.ico |
| 61 | + } |
| 62 | + |
67 | 63 | PrintWriter out = resp.getWriter(); |
68 | 64 | resp.setContentType("text/plain"); |
69 | 65 |
|
70 | | - try (Connection conn = DriverManager.getConnection(url); |
71 | | - PreparedStatement statementCreateVisit = conn.prepareStatement(createVisitSql)) { |
| 66 | + // store only the first two octets of a users ip address |
| 67 | + String userIp = req.getRemoteAddr(); |
| 68 | + InetAddress address = InetAddress.getByName(userIp); |
| 69 | + if (address instanceof Inet6Address) { |
| 70 | + // nest indexOf calls to find the second occurrence of a character in a string |
| 71 | + // an alternative is to use Apache Commons Lang: StringUtils.ordinalIndexOf() |
| 72 | + userIp = userIp.substring(0, userIp.indexOf(":", userIp.indexOf(":") + 1)) + ":*:*:*:*:*:*"; |
| 73 | + } else if (address instanceof Inet4Address) { |
| 74 | + userIp = userIp.substring(0, userIp.indexOf(".", userIp.indexOf(".") + 1)) + ".*.*"; |
| 75 | + } |
| 76 | + |
| 77 | + Stopwatch stopwatch = Stopwatch.createStarted(); |
| 78 | + try (PreparedStatement statementCreateVisit = conn.prepareStatement(createVisitSql)) { |
72 | 79 | conn.createStatement().executeUpdate(createTableSql); |
73 | 80 | statementCreateVisit.setString(1, userIp); |
74 | 81 | statementCreateVisit.setTimestamp(2, new Timestamp(new Date().getTime())); |
75 | 82 | statementCreateVisit.executeUpdate(); |
76 | 83 |
|
77 | 84 | try (ResultSet rs = conn.prepareStatement(selectSql).executeQuery()) { |
| 85 | + stopwatch.stop(); |
78 | 86 | out.print("Last 10 visits:\n"); |
79 | 87 | while (rs.next()) { |
80 | 88 | String savedIp = rs.getString("user_ip"); |
81 | 89 | String timeStamp = rs.getString("timestamp"); |
82 | 90 | out.print("Time: " + timeStamp + " Addr: " + savedIp + "\n"); |
83 | 91 | } |
| 92 | + out.println("Elapsed: " + stopwatch.elapsed(TimeUnit.MILLISECONDS)); |
84 | 93 | } |
85 | 94 | } catch (SQLException e) { |
86 | 95 | throw new ServletException("SQL error", e); |
87 | 96 | } |
88 | 97 | } |
89 | 98 |
|
90 | 99 | @Override |
91 | | - public void init() { |
| 100 | + public void init() throws ServletException { |
92 | 101 | try { |
| 102 | + String url; |
| 103 | + |
93 | 104 | Properties properties = new Properties(); |
94 | | - properties.load( |
95 | | - getServletContext().getResourceAsStream("/WEB-INF/classes/config.properties")); |
96 | | - url = properties.getProperty("sqlUrl"); |
97 | | - } catch (IOException e) { |
98 | | - log("no property", e); // Servlet Init should never fail. |
| 105 | + try { |
| 106 | + properties.load( |
| 107 | + getServletContext().getResourceAsStream("/WEB-INF/classes/config.properties")); |
| 108 | + url = properties.getProperty("sqlUrl"); |
| 109 | + } catch (IOException e) { |
| 110 | + log("no property", e); // Servlet Init should never fail. |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + log("connecting to: " + url); |
| 115 | + try { |
| 116 | + Class.forName("com.mysql.jdbc.Driver"); |
| 117 | + conn = DriverManager.getConnection(url); |
| 118 | + } catch (ClassNotFoundException e) { |
| 119 | + throw new ServletException("Error loading JDBC Driver", e); |
| 120 | + } catch (SQLException e) { |
| 121 | + throw new ServletException("Unable to connect to PostGre", e); |
| 122 | + } |
| 123 | + |
| 124 | + } finally { |
| 125 | + // Nothing really to do here. |
99 | 126 | } |
100 | 127 | } |
101 | 128 | } |
|
0 commit comments