From 8c778c76c6c737888d4d7811d7b6d59fd88621c5 Mon Sep 17 00:00:00 2001 From: Shun Fan Date: Fri, 29 Jan 2016 10:39:08 -0800 Subject: [PATCH] Add comments to cloudstorage Add comments to datastore, and truncate user ip Add comments to cloudsql, and truncate user ip --- .../managedvms/cloudsql/CloudSqlServlet.java | 20 ++++++++++++++++--- .../cloudstorage/UploadServlet.java | 4 ++++ managed_vms/datastore/pom.xml | 8 ++++++-- .../datastore/DatastoreServlet.java | 17 +++++++++++++++- 4 files changed, 43 insertions(+), 6 deletions(-) diff --git a/managed_vms/cloudsql/src/main/java/com/example/managedvms/cloudsql/CloudSqlServlet.java b/managed_vms/cloudsql/src/main/java/com/example/managedvms/cloudsql/CloudSqlServlet.java index c4bdd569f4d..eb8e6fcd9f1 100644 --- a/managed_vms/cloudsql/src/main/java/com/example/managedvms/cloudsql/CloudSqlServlet.java +++ b/managed_vms/cloudsql/src/main/java/com/example/managedvms/cloudsql/CloudSqlServlet.java @@ -18,6 +18,9 @@ import java.io.IOException; import java.io.PrintWriter; +import java.net.Inet4Address; +import java.net.Inet6Address; +import java.net.InetAddress; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; @@ -40,6 +43,15 @@ public class CloudSqlServlet extends HttpServlet { @Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { + // store only the first two octets of a users ip address + String userIp = req.getRemoteAddr(); + InetAddress address = InetAddress.getByName(userIp); + if (address instanceof Inet6Address) { + userIp = userIp.substring(0, userIp.indexOf(":", 2)) + ":*:*:*:*:*:*"; + } else if (address instanceof Inet4Address) { + userIp = userIp.substring(0, userIp.indexOf(".", 2)) + ".*.*"; + } + final String createTableSql = "CREATE TABLE IF NOT EXISTS visits ( visit_id INT NOT NULL " + "AUTO_INCREMENT, user_ip VARCHAR(46) NOT NULL, timestamp DATETIME NOT NULL, " + "PRIMARY KEY (visit_id) )"; @@ -49,18 +61,20 @@ public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOExc PrintWriter out = resp.getWriter(); resp.setContentType("text/plain"); String url = System.getenv("SQL_DATABASE_URL"); + try (Connection conn = DriverManager.getConnection(url); PreparedStatement statementCreateVisit = conn.prepareStatement(createVisitSql)) { conn.createStatement().executeUpdate(createTableSql); - statementCreateVisit.setString(1, req.getRemoteAddr()); + statementCreateVisit.setString(1, userIp); statementCreateVisit.setTimestamp(2, new Timestamp(new Date().getTime())); statementCreateVisit.executeUpdate(); + try (ResultSet rs = conn.prepareStatement(selectSql).executeQuery()) { out.print("Last 10 visits:\n"); while (rs.next()) { - String userIp = rs.getString("user_ip"); + String savedIp = rs.getString("user_ip"); String timeStamp = rs.getString("timestamp"); - out.print("Time: " + timeStamp + " Addr: " + userIp + "\n"); + out.print("Time: " + timeStamp + " Addr: " + savedIp + "\n"); } } } catch (SQLException e) { diff --git a/managed_vms/cloudstorage/src/main/java/com/example/managedvms/cloudstorage/UploadServlet.java b/managed_vms/cloudstorage/src/main/java/com/example/managedvms/cloudstorage/UploadServlet.java index e1f522e9ec4..2f9a3715e5e 100644 --- a/managed_vms/cloudstorage/src/main/java/com/example/managedvms/cloudstorage/UploadServlet.java +++ b/managed_vms/cloudstorage/src/main/java/com/example/managedvms/cloudstorage/UploadServlet.java @@ -52,6 +52,8 @@ public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOEx ServletException { final Part filePart = req.getPart("file"); final String fileName = filePart.getSubmittedFileName(); + + // Modify access list to allow all users with link to read file List acls = new ArrayList<>(); acls.add(Acl.of(Acl.User.ofAllUsers(), Acl.Role.READER)); // the inputstream is closed by default, so we don't need to close it here @@ -60,6 +62,8 @@ public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOEx BlobInfo.builder(BUCKET_NAME, fileName).acl(acls).build(), filePart.getInputStream()); blobInfo = storage.get(BUCKET_NAME, fileName); + + // return the public download link resp.getWriter().print(blobInfo.mediaLink()); } } diff --git a/managed_vms/datastore/pom.xml b/managed_vms/datastore/pom.xml index cf0702e3143..63164ea0cc9 100644 --- a/managed_vms/datastore/pom.xml +++ b/managed_vms/datastore/pom.xml @@ -14,7 +14,7 @@ jar provided - + com.google.gcloud gcloud-java-datastore @@ -62,7 +62,11 @@ true - check + + + check + + diff --git a/managed_vms/datastore/src/main/java/com/example/managedvms/datastore/DatastoreServlet.java b/managed_vms/datastore/src/main/java/com/example/managedvms/datastore/DatastoreServlet.java index bc1955395ce..f726af444d5 100644 --- a/managed_vms/datastore/src/main/java/com/example/managedvms/datastore/DatastoreServlet.java +++ b/managed_vms/datastore/src/main/java/com/example/managedvms/datastore/DatastoreServlet.java @@ -29,6 +29,9 @@ import java.io.IOException; import java.io.PrintWriter; +import java.net.Inet4Address; +import java.net.Inet6Address; +import java.net.InetAddress; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; @@ -44,17 +47,29 @@ public class DatastoreServlet extends HttpServlet { @Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { + // store only the first two octets of a users ip address + String userIp = req.getRemoteAddr(); + InetAddress address = InetAddress.getByName(userIp); + if (address instanceof Inet6Address) { + userIp = userIp.substring(0, userIp.indexOf(":", 2)) + ":*:*:*:*:*:*"; + } else if (address instanceof Inet4Address) { + userIp = userIp.substring(0, userIp.indexOf(".", 2)) + ".*.*"; + } + Datastore datastore = DatastoreOptions.defaultInstance().service(); KeyFactory keyFactory = datastore.newKeyFactory().kind("visit"); IncompleteKey key = keyFactory.kind("visit").newKey(); + // Record a visit to the datastore, storing the IP and timestamp. FullEntity curVisit = FullEntity.builder(key) - .set("user_ip", req.getRemoteAddr()).set("timestamp", DateTime.now()).build(); + .set("user_ip", userIp).set("timestamp", DateTime.now()).build(); datastore.add(curVisit); + // Retrieve the last 10 visits from the datastore, ordered by timestamp. Query query = Query.entityQueryBuilder().kind("visit") .orderBy(StructuredQuery.OrderBy.desc("timestamp")).limit(10).build(); QueryResults results = datastore.run(query); + resp.setContentType("text/plain"); PrintWriter out = resp.getWriter(); out.print("Last 10 visits:\n");