-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMainService3.java
More file actions
134 lines (112 loc) · 4.23 KB
/
MainService3.java
File metadata and controls
134 lines (112 loc) · 4.23 KB
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
package httpd.v1;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
/**
* The subclass of the HTTP Server v1.3.
*
* This class contains all of the business logic of the service, separate from
* the server logic. It is still quite messy as it is handling the function of
* all four disparate endpoints. We will eventually separate them.
*/
public class MainService3 extends HTTPServer3 {
public MainService3(Socket client) {
super(client);
}
protected void doRequest(Request req, Response res) throws Exception {
switch (req.uri) {
case "/":
res.out.println("Hello! Welcome to the server.");
break;
case "/calc":
if (req.qs.containsKey("op") &&
req.qs.containsKey("a") &&
req.qs.containsKey("b")) {
String op = req.qs.get("op");
double a = Double.parseDouble(req.qs.get("a"));
double b = Double.parseDouble(req.qs.get("b"));
switch (op) {
case "add": res.out.println(a + b); break;
case "subtract": res.out.println(a - b); break;
case "multiply": res.out.println(a * b); break;
case "divide": res.out.println(a / b); break;
case "exponent": res.out.println(Math.pow(a, b)); break;
default:
res.setStatus(400);
}
} else {
res.setStatus(400);
}
break;
case "/add":
case "/subtract":
case "/multiply":
case "/divide":
case "/exponent":
res.setStatus(301);
res.headers.put("Location", "/calc?op=" + req.uri.substring(1) + "&" + toQueryString(req.qs));
break;
case "/students":
if (req.qs.containsKey("major") &&
req.qs.containsKey("gpa")) {
String url = "jdbc:derby://localhost:64413/EECS";
String query = "SELECT * FROM Roumani.Sis "
+ "WHERE major = ? "
+ "AND gpa >= ?";
String major = req.qs.get("major");
double gpa = Double.parseDouble(req.qs.get("gpa"));
try (Connection connection = DriverManager.getConnection(url)) {
log.printf("Connected to database: %s\n", connection.getMetaData().getURL());
try (PreparedStatement statement = connection.prepareStatement(query)) {
statement.setString(1, major);
statement.setDouble(2, gpa);
JsonObject jsonRoot = new JsonObject();
JsonArray students = new JsonArray();
try (ResultSet rs = statement.executeQuery()) {
while (rs.next()) {
JsonObject student = new JsonObject();
student.addProperty("id", rs.getInt("id"));
student.addProperty("surname", rs.getString("surname"));
student.addProperty("givenName", rs.getString("givenname"));
student.addProperty("gpa", rs.getDouble("gpa"));
student.addProperty("yearAdmitted", rs.getInt("yearadmitted"));
students.add(student);
}
}
jsonRoot.add("students", students);
res.headers.put("Content-Type", "application/json");
res.out.println(jsonRoot);
}
} catch (SQLException e) {
log.println(e);
res.setStatus(500);
} finally {
log.println("Disconnected from database.");
}
} else {
res.setStatus(400);
}
break;
default:
res.setStatus(404);
}
}
/// Main
public static void main(String[] args) throws Exception {
int port = 0;
InetAddress host = InetAddress.getLocalHost(); // .getLoopbackAddress();
try (ServerSocket server = new ServerSocket(port, 0, host)) {
log.printf("Server listening on %s:%d\n", server.getInetAddress(), server.getLocalPort());
while (true) {
(new MainService3(server.accept())).start();
}
}
}
}