/**
 * Copyright 2015 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.managedvms.disk;

import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.List;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

// [START example]
@SuppressWarnings("serial")
@WebServlet(name = "disk", value = "")
public class DiskServlet extends HttpServlet {

  @Override
  public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    String instanceId =
        System.getenv().containsKey("GAE_MODULE_INSTANCE")
            ? System.getenv("GAE_MODULE_INSTANCE") : "1";
    String userId = req.getRemoteAddr() + "\n";
    Path tmpFile = Paths.get("/tmp/seen.txt");
    Files.write(tmpFile, userId.getBytes(), StandardOpenOption.CREATE, StandardOpenOption.APPEND);
    StringBuffer sb = new StringBuffer();
    List<String> strings = Files.readAllLines(tmpFile, StandardCharsets.US_ASCII);
    for (String s : strings) {
      sb.append(s + "\n");
    }
    PrintWriter out = resp.getWriter();
    resp.setContentType("text/plain");
    out.print("Instance: " + instanceId + "\nSeen:\n" + sb.toString());
  }
}
// [END example]

@@ -701,16 +701,105 @@
 Writer;%0A
+import java.net.Inet4Address;%0Aimport java.net.Inet6Address;%0Aimport java.net.InetAddress;%0A
 import j
@@ -1535,16 +1535,77 @@
  : %221%22;%0A
+    // store only the first two octets of a users ip address%0A
     Stri
@@ -1612,17 +1612,17 @@
 ng userI
-d
+p
  = req.g
@@ -1639,16 +1639,524 @@
 dr()
- + %22%5Cn%22;
+;%0A    InetAddress address = InetAddress.getByName(userIp);%0A    if (address instanceof Inet6Address) %7B%0A      // nest indexOf calls to find the second occurrence of a character in a string%0A      // an alternative is to use Apache Commons Lang: StringUtils.ordinalIndexOf()%0A      userIp = userIp.substring(0, userIp.indexOf(%22:%22, userIp.indexOf(%22:%22) + 1)) + %22:*:*:*:*:*:*%22;%0A    %7D else if (address instanceof Inet4Address) %7B%0A      userIp = userIp.substring(0, userIp.indexOf(%22.%22, userIp.indexOf(%22.%22) + 1)) + %22.*.*%22;%0A    %7D
 %0A   
@@ -2219,23 +2219,49 @@
 ite(
-tmpFile, userId
+%0A        tmpFile,%0A        (userIp + %22%5Cn%22)
 .get
@@ -2268,16 +2268,24 @@
 Bytes(),
+%0A       
  Standar
@@ -2303,16 +2303,24 @@
 .CREATE,
+%0A       
  Standar

Fix issue with truncating recorded ip addresses in disk