forked from RunningJon/outsourcer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIModel.java
165 lines (143 loc) · 3.64 KB
/
UIModel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import java.util.Map;
import java.util.Random;
import java.sql.*;
import java.util.Date;
import java.util.TimeZone;
import java.text.SimpleDateFormat;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class UIModel
{
public static void logger(String uri, String sessionID)
{
Date now = new Date();
System.out.println(now.toString() + "|" + sessionID + "|" + uri);
}
public static String getSessionID(Map<String, String> header)
{
String sessionID = "0";
String cookies = header.get("cookie");
if (cookies != null)
{
String[] cookieValues = cookies.split(";");
for (int i = 0; i < cookieValues.length; i++)
{
String[] split = cookieValues[i].trim().split("=");
if (split.length == 2)
{
if (split[0].equals("OutsourcerSessionID"))
{
sessionID = split[1];
}
}
}
}
return sessionID;
}
public static void insertSession(String sessionID) throws SQLException
{
try
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
String expireDate = sdf.format(new Date(System.currentTimeMillis()+15*60*1000));
File file = new File(UI.sessions);
if(!file.exists())
{
file.createNewFile();
}
FileWriter writer = new FileWriter(file, true);
writer.write(sessionID + "|'" + expireDate + "'\n");
writer.flush();
writer.close();
}
catch (IOException ex)
{
throw new SQLException(ex.getMessage());
}
}
public static boolean authenticate(String username, String password) throws SQLException
{
boolean authenticated = false;
username = username.toLowerCase();
try
{
//don't use the pool to authenticate
Connection conn = CommonDB.connectGP(UI.configFile, username, password);
String strSQL = "SELECT rolname FROM pg_roles WHERE rolsuper AND rolcanlogin AND rolname = '" + username + "'";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(strSQL);
while (rs.next())
{
authenticated = true;
}
conn.close();
}
catch (SQLException ex)
{
throw new SQLException(ex.getMessage());
}
return authenticated;
}
public static String setSessionID(Map<String, String> header)
{
String sessionID = getSessionID(header);
if (sessionID.equals("0"))
{
Random session = new Random();
int sessionInt = session.nextInt(1000000);
sessionID = Integer.toString(sessionInt);
}
return sessionID;
}
public static boolean keepAlive(String sessionID) throws SQLException
{
boolean alive = false;
boolean activeSession = false;
try
{
Connection conn = UIConnectionFactory.getConnection();
Statement stmt = conn.createStatement();
//make sure session is valid
String strSQL = "SELECT session_id FROM os.sessions WHERE session_id = " + sessionID + " AND expire_date > current_timestamp AT TIME ZONE 'UTC' LIMIT 1";
ResultSet rs = stmt.executeQuery(strSQL);
while (rs.next())
{
activeSession = true;
}
conn.close();
if (activeSession)
{
insertSession(sessionID);
alive = true;
}
}
catch (SQLException ex)
{
System.out.println(ex.getMessage());
throw new SQLException(ex.getMessage());
}
return alive;
}
public static String getVersion()
{
String version = "AO";
try
{
Connection conn = UIConnectionFactory.getConnection();
Statement stmt = conn.createStatement();
String strSQL = "SELECT CASE WHEN position ('HAWQ' IN version()) > 0 THEN 'HAWQ' ELSE 'AO' END";
ResultSet rs = stmt.executeQuery(strSQL);
while (rs.next())
{
version = rs.getString(1);
}
conn.close();
}
catch (SQLException ex)
{
}
return version;
}
}