|
| 1 | +/** |
| 2 | + * Copyright 2016 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.managedvms.cloudsql; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.io.PrintWriter; |
| 21 | +import java.net.Inet4Address; |
| 22 | +import java.net.Inet6Address; |
| 23 | +import java.net.InetAddress; |
| 24 | +import java.sql.Connection; |
| 25 | +import java.sql.DriverManager; |
| 26 | +import java.sql.PreparedStatement; |
| 27 | +import java.sql.ResultSet; |
| 28 | +import java.sql.SQLException; |
| 29 | +import java.sql.Timestamp; |
| 30 | +import java.util.Date; |
| 31 | + |
| 32 | +import javax.servlet.ServletException; |
| 33 | +import javax.servlet.annotation.WebServlet; |
| 34 | +import javax.servlet.http.HttpServlet; |
| 35 | +import javax.servlet.http.HttpServletRequest; |
| 36 | +import javax.servlet.http.HttpServletResponse; |
| 37 | + |
| 38 | +// [START example] |
| 39 | +@SuppressWarnings("serial") |
| 40 | +@WebServlet(name = "cloudsql", value = "") |
| 41 | +public class CloudSqlServlet extends HttpServlet { |
| 42 | + |
| 43 | + @Override |
| 44 | + public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, |
| 45 | + ServletException { |
| 46 | + // store only the first two octets of a users ip address |
| 47 | + String userIp = req.getRemoteAddr(); |
| 48 | + InetAddress address = InetAddress.getByName(userIp); |
| 49 | + if (address instanceof Inet6Address) { |
| 50 | + // nest indexOf calls to find the second occurrence of a character in a |
| 51 | + // string |
| 52 | + // an alternative is to use Apache Commons Lang: |
| 53 | + // StringUtils.ordinalIndexOf() |
| 54 | + userIp = userIp.substring(0, userIp.indexOf(":", userIp.indexOf(":") + 1)) + ":*:*:*:*:*:*"; |
| 55 | + } else if (address instanceof Inet4Address) { |
| 56 | + userIp = userIp.substring(0, userIp.indexOf(".", userIp.indexOf(".") + 1)) + ".*.*"; |
| 57 | + } |
| 58 | + |
| 59 | + final String createTableSql = "CREATE TABLE IF NOT EXISTS visits ( visit_id INT NOT NULL " |
| 60 | + + "AUTO_INCREMENT, user_ip VARCHAR(46) NOT NULL, timestamp DATETIME NOT NULL, " |
| 61 | + + "PRIMARY KEY (visit_id) )"; |
| 62 | + final String createVisitSql = "INSERT INTO visits (user_ip, timestamp) VALUES (?, ?)"; |
| 63 | + final String selectSql = "SELECT user_ip, timestamp FROM visits ORDER BY timestamp DESC " |
| 64 | + + "LIMIT 10"; |
| 65 | + PrintWriter out = resp.getWriter(); |
| 66 | + resp.setContentType("text/plain"); |
| 67 | + // Detect if running remotely or locally and select correct connection url |
| 68 | + String url; |
| 69 | + if (System.getenv().containsKey("GAE_MODULE_INSTANCE")) { |
| 70 | + url = System.getenv("SQL_REMOTE_URL"); |
| 71 | + } else { |
| 72 | + url = System.getenv("SQL_LOCAL_URL"); |
| 73 | + } |
| 74 | + |
| 75 | + try (Connection conn = DriverManager.getConnection(url); |
| 76 | + PreparedStatement statementCreateVisit = conn.prepareStatement(createVisitSql)) { |
| 77 | + conn.createStatement().executeUpdate(createTableSql); |
| 78 | + statementCreateVisit.setString(1, userIp); |
| 79 | + statementCreateVisit.setTimestamp(2, new Timestamp(new Date().getTime())); |
| 80 | + statementCreateVisit.executeUpdate(); |
| 81 | + |
| 82 | + try (ResultSet rs = conn.prepareStatement(selectSql).executeQuery()) { |
| 83 | + out.print("Last 10 visits:\n"); |
| 84 | + while (rs.next()) { |
| 85 | + String savedIp = rs.getString("user_ip"); |
| 86 | + String timeStamp = rs.getString("timestamp"); |
| 87 | + out.print("Time: " + timeStamp + " Addr: " + savedIp + "\n"); |
| 88 | + } |
| 89 | + } |
| 90 | + } catch (SQLException e) { |
| 91 | + throw new ServletException("SQL error", e); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | +// [END example] |
0 commit comments