public static String getIpFromRequest(HttpServletRequest request) {
   String ip;
   boolean found = false;
   if ((ip = request.getHeader("x-forwarded-for")) != null) {
     StrTokenizer tokenizer = new StrTokenizer(ip, ",");
     while (tokenizer.hasNext()) {
       ip = tokenizer.nextToken().trim();
       if (isIPv4Valid(ip) && !isIPv4Private(ip)) {
         found = true;
         break;
       }
     }
   }
   if (!found) {
     ip = request.getRemoteAddr();
   }
   return ip;
 }
Example #2
0
 /**
  * Read an item list DAO from a file.
  *
  * @param file A file of item IDs, one per line.
  * @return The item list DAO.
  * @throws java.io.IOException if there is an error reading the list of items.
  */
 public static MapItemNameDAO fromCSVFile(File file) throws IOException {
   LineCursor cursor = LineCursor.openFile(file, CompressionMode.AUTO);
   try {
     ImmutableMap.Builder<Long, String> names = ImmutableMap.builder();
     StrTokenizer tok = StrTokenizer.getCSVInstance();
     for (String line : cursor) {
       tok.reset(line);
       long item = Long.parseLong(tok.next());
       String title = tok.nextToken();
       if (title != null) {
         names.put(item, title);
       }
     }
     return new MapItemNameDAO(names.build());
   } catch (NoSuchElementException ex) {
     throw new IOException(
         String.format("%s:%s: not enough columns", file, cursor.getLineNumber()), ex);
   } catch (NumberFormatException ex) {
     throw new IOException(
         String.format("%s:%s: id not an integer", file, cursor.getLineNumber()), ex);
   } finally {
     cursor.close();
   }
 }