17
17
package com .example .appengine .postgresql ;
18
18
19
19
import com .google .common .base .Stopwatch ;
20
-
21
20
import java .io .IOException ;
22
21
import java .io .PrintWriter ;
23
- import java .net .Inet4Address ;
24
- import java .net .Inet6Address ;
25
- import java .net .InetAddress ;
26
22
import java .sql .Connection ;
27
23
import java .sql .DriverManager ;
28
24
import java .sql .PreparedStatement ;
31
27
import java .sql .Timestamp ;
32
28
import java .util .Date ;
33
29
import java .util .concurrent .TimeUnit ;
34
-
35
30
import javax .servlet .ServletException ;
36
31
import javax .servlet .annotation .WebServlet ;
37
32
import javax .servlet .http .HttpServlet ;
42
37
@ SuppressWarnings ("serial" )
43
38
// With @WebServlet annotation the webapp/WEB-INF/web.xml is no longer required.
44
39
@ WebServlet (name = "PostgreSQL" ,
45
- description = "PostgreSQL: Write low order IP address to PostgreSQL" ,
40
+ description = "PostgreSQL: Write timestamps of visitors to PostgreSQL" ,
46
41
urlPatterns = "/postgresql" )
47
42
public class PostgreSqlServlet extends HttpServlet {
48
43
@@ -52,11 +47,11 @@ pub
6D40
lic class PostgreSqlServlet extends HttpServlet {
52
47
public void doGet (HttpServletRequest req , HttpServletResponse resp ) throws IOException ,
53
48
ServletException {
54
49
55
- final String createTableSql = "CREATE TABLE IF NOT EXISTS visits ( visit_id SERIAL NOT NULL, "
56
- + "user_ip VARCHAR(46) NOT NULL, ts timestamp NOT NULL, "
50
+ final String createTableSql = "CREATE TABLE IF NOT EXISTS visits ( "
51
+ + "visit_id SERIAL NOT NULL, ts timestamp NOT NULL, "
57
52
+ "PRIMARY KEY (visit_id) );" ;
58
- final String createVisitSql = "INSERT INTO visits (user_ip, ts) VALUES (?, ?);" ;
59
- final String selectSql = "SELECT user_ip, ts FROM visits ORDER BY ts DESC "
53
+ final String createVisitSql = "INSERT INTO visits (ts) VALUES (?);" ;
54
+ final String selectSql = "SELECT ts FROM visits ORDER BY ts DESC "
60
55
+ "LIMIT 10;" ;
61
56
62
57
String path = req .getRequestURI ();
@@ -67,55 +62,34 @@ public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOExc
67
62
PrintWriter out = resp .getWriter ();
68
63
resp .setContentType ("text/plain" );
69
64
70
- // store only the first two octets of a users ip address
71
- String userIp = req .getRemoteAddr ();
72
- InetAddress address = InetAddress .getByName (userIp );
73
- if (address instanceof Inet6Address ) {
74
- // nest indexOf calls to find the second occurrence of a character in a string
75
- // an alternative is to use Apache Commons Lang: StringUtils.ordinalIndexOf()
76
- userIp = userIp .substring (0 , userIp .indexOf (":" , userIp .indexOf (":" ) + 1 )) + ":*:*:*:*:*:*" ;
77
- } else if (address instanceof Inet4Address ) {
78
- userIp = userIp .substring (0 , userIp .indexOf ("." , userIp .indexOf ("." ) + 1 )) + ".*.*" ;
79
- }
80
-
81
65
Stopwatch stopwatch = Stopwatch .createStarted ();
82
66
try (PreparedStatement statementCreateVisit = conn .prepareStatement (createVisitSql )) {
83
67
conn .createStatement ().executeUpdate (createTableSql );
84
- statementCreateVisit .setString (1 , userIp );
85
- statementCreateVisit .setTimestamp (2 , new Timestamp (new Date ().getTime ()));
68
+ statementCreateVisit .setTimestamp (1 , new Timestamp (new Date ().getTime ()));
86
69
statementCreateVisit .executeUpdate ();
87
70
88
71
try (ResultSet rs = conn .prepareStatement (selectSql ).executeQuery ()) {
89
72
stopwatch .stop ();
90
73
out .print ("Last 10 visits:\n " );
91
74
while (rs .next ()) {
92
- String savedIp = rs .getString ("user_ip" );
93
75
String timeStamp = rs .getString ("ts" );
94
- out .println ("Time: " + timeStamp + " Addr : " + savedIp );
76
+ out .println ("Visited at time : " + timeStamp );
95
77
}
96
- out .println ("Elapsed: " + stopwatch .elapsed (TimeUnit .MILLISECONDS ));
97
78
}
98
79
} catch (SQLException e ) {
99
80
throw new ServletException ("SQL error" , e );
100
81
}
82
+ out .println ("Query time (ms):" + stopwatch .elapsed (TimeUnit .MILLISECONDS ));
101
83
}
102
84
103
85
@ Override
104
86
public void init () throws ServletException {
87
+ String url = System .getProperty ("postgresql" );
88
+ log ("connecting to: " + url );
105
89
try {
106
- String url = System .getProperty ("postgresql" );
107
- log ("connecting to: " + url );
108
- try {
109
- Class .forName ("org.postgresql.Driver" );
110
- conn = DriverManager .getConnection (url );
111
- } catch (ClassNotFoundException e ) {
112
- throw new ServletException ("Error loading JDBC Driver" , e );
113
- } catch (SQLException e ) {
114
- throw new ServletException ("Unable to connect to PostGre" , e );
115
- }
116
-
117
- } finally {
118
- // Nothing really to do here.
90
+ conn = DriverManager .getConnection (url );
91
+ } catch (SQLException e ) {
92
+ throw new ServletException ("Unable to connect to PostgreSQL" , e );
119
93
}
120
94
}
121
95
}
0 commit comments