1 | import com.fasterxml.jackson.databind.JsonNode;
|
---|
2 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
3 | import com.fasterxml.jackson.databind.node.ObjectNode;
|
---|
4 | import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken;
|
---|
5 | import com.google.api.client.googleapis.auth.oauth2.GoogleIdTokenVerifier;
|
---|
6 | import com.google.api.client.http.HttpTransport;
|
---|
7 | import com.google.api.client.http.javanet.NetHttpTransport;
|
---|
8 | import com.google.api.client.json.JsonFactory;
|
---|
9 | import com.google.api.client.json.jackson2.JacksonFactory;
|
---|
10 |
|
---|
11 | import com.sun.net.httpserver.HttpExchange;
|
---|
12 | import com.sun.net.httpserver.HttpHandler;
|
---|
13 | import com.sun.net.httpserver.HttpServer;
|
---|
14 |
|
---|
15 | import java.io.File;
|
---|
16 | import java.io.IOException;
|
---|
17 | import java.io.OutputStream;
|
---|
18 |
|
---|
19 | import java.net.InetSocketAddress;
|
---|
20 | import java.net.URL;
|
---|
21 | import java.nio.charset.StandardCharsets;
|
---|
22 | import java.nio.file.Files;
|
---|
23 | import java.nio.file.Path;
|
---|
24 | import java.nio.file.Paths;
|
---|
25 | import java.security.GeneralSecurityException;
|
---|
26 | import java.sql.SQLException;
|
---|
27 | import java.time.LocalDate;
|
---|
28 | import java.time.LocalDateTime;
|
---|
29 | import java.time.format.DateTimeFormatter;
|
---|
30 | import java.time.temporal.ChronoUnit;
|
---|
31 | import java.util.*;
|
---|
32 | import java.util.concurrent.ExecutorService;
|
---|
33 | import java.util.concurrent.Executors;
|
---|
34 | import java.util.concurrent.Future;
|
---|
35 |
|
---|
36 | public class Server {
|
---|
37 | private static final ExecutorService executorService = Executors.newFixedThreadPool(1);
|
---|
38 | public static void main(String[] args) throws IOException, SQLException {
|
---|
39 | // Start HTTP server
|
---|
40 | DatabaseUtil.initializeDatabase();
|
---|
41 | HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
|
---|
42 | server.createContext("/", new FrontendHandler());
|
---|
43 | server.createContext("/submit", new FrontendHandler());
|
---|
44 | server.createContext("/admin/start-scraper", new FrontendHandler());
|
---|
45 | server.createContext("/admin/last-update-time", new FrontendHandler());
|
---|
46 | server.createContext("/admin/current-options-count", new FrontendHandler());
|
---|
47 | server.createContext("/admin/changed-options-count", new FrontendHandler());
|
---|
48 |
|
---|
49 | server.createContext("/account/login", new AccountHandler());
|
---|
50 | server.createContext("/account/register", new AccountHandler());
|
---|
51 | server.createContext("/account/delete-account", new AccountHandler());
|
---|
52 | server.createContext("/account/session", new AccountHandler());
|
---|
53 |
|
---|
54 |
|
---|
55 | server.createContext("/save-favorite", new SaveFavoriteHandler());
|
---|
56 | server.createContext("/get-saved-trips", new GetSavedTripsHandler());
|
---|
57 | server.createContext("/remove-from-saved", new RemoveFromSavedTripsHandler());
|
---|
58 |
|
---|
59 | server.start();
|
---|
60 | System.out.println("Server started on port 8000");
|
---|
61 |
|
---|
62 | LocalDateTime lastUpdateTime = getLastUpdateTime();
|
---|
63 | LocalDateTime now = LocalDateTime.now();
|
---|
64 | if (ChronoUnit.MONTHS.between(lastUpdateTime, now) >= 3) {
|
---|
65 | System.out.println("Automatic Update");
|
---|
66 | Future<Void> future = executorService.submit(new Scraper());
|
---|
67 | Server.updateLastUpdateTime();
|
---|
68 | executorService.submit(() -> {
|
---|
69 | try {
|
---|
70 | future.get();
|
---|
71 | } catch (Exception e) {
|
---|
72 | e.printStackTrace();
|
---|
73 | }
|
---|
74 | });
|
---|
75 |
|
---|
76 | }
|
---|
77 |
|
---|
78 | }
|
---|
79 | static class RemoveFromSavedTripsHandler implements HttpHandler {
|
---|
80 | @Override
|
---|
81 | public void handle(HttpExchange exchange) throws IOException {
|
---|
82 | if (exchange.getRequestMethod().equalsIgnoreCase("POST")) {
|
---|
83 | String requestBody = new String(exchange.getRequestBody().readAllBytes());
|
---|
84 | ObjectMapper mapper = new ObjectMapper();
|
---|
85 | Map<String, Object> requestData = mapper.readValue(requestBody, Map.class);
|
---|
86 | String userEmail = (String) requestData.get("userEmail");
|
---|
87 | int optionId = (int) requestData.get("optionId");
|
---|
88 |
|
---|
89 | int userId;
|
---|
90 | try {
|
---|
91 | userId = DatabaseUtil.getUserIdByEmail(userEmail);
|
---|
92 | boolean removed = DatabaseUtil.removeFavoriteOption(userId, optionId);
|
---|
93 | if (removed) {
|
---|
94 | sendResponse(exchange, 200, "{\"success\": true}");
|
---|
95 | } else {
|
---|
96 | sendResponse(exchange, 500, "{\"success\": false, \"message\": \"Failed to remove option\"}");
|
---|
97 | }
|
---|
98 | } catch (SQLException e) {
|
---|
99 | e.printStackTrace();
|
---|
100 | sendResponse(exchange, 500, "{\"message\": \"Failed to retrieve user ID.\"}");
|
---|
101 | }
|
---|
102 | } else {
|
---|
103 | sendResponse(exchange, 405, "{\"message\": \"Method not allowed.\"}");
|
---|
104 | }
|
---|
105 | }
|
---|
106 | }
|
---|
107 | static class SaveFavoriteHandler implements HttpHandler {
|
---|
108 | @Override
|
---|
109 | public void handle(HttpExchange exchange) throws IOException {
|
---|
110 | if (exchange.getRequestMethod().equalsIgnoreCase("POST")) {
|
---|
111 | String requestBody = new String(exchange.getRequestBody().readAllBytes());
|
---|
112 | ObjectMapper mapper = new ObjectMapper();
|
---|
113 | Map<String, String> requestData = mapper.readValue(requestBody, Map.class);
|
---|
114 | String optionId = requestData.get("optionId");
|
---|
115 | String userEmail = requestData.get("user");
|
---|
116 |
|
---|
117 | int userId;
|
---|
118 | try {
|
---|
119 | userId = DatabaseUtil.getUserIdByEmail(userEmail);
|
---|
120 | } catch (SQLException e) {
|
---|
121 | e.printStackTrace();
|
---|
122 | sendResponse(exchange, 500, "{\"message\": \"Failed to retrieve user ID.\"}");
|
---|
123 | return;
|
---|
124 | }
|
---|
125 |
|
---|
126 | try {
|
---|
127 | if (DatabaseUtil.saveFavoriteOption(userId, Integer.parseInt(optionId))) {
|
---|
128 | sendResponse(exchange, 200, "{\"message\": \"Favorite saved successfully!\"}");
|
---|
129 | } else {
|
---|
130 | sendResponse(exchange, 500, "{\"message\": \"Failed to save favorite.\"}");
|
---|
131 | }
|
---|
132 | } catch (SQLException e) {
|
---|
133 | e.printStackTrace();
|
---|
134 | sendResponse(exchange, 500, "{\"message\": \"Failed to save favorite.\"}");
|
---|
135 | }
|
---|
136 | } else {
|
---|
137 | sendResponse(exchange, 405, "{\"message\": \"Method not allowed.\"}");
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | }
|
---|
142 | static class GetSavedTripsHandler implements HttpHandler {
|
---|
143 | @Override
|
---|
144 | public void handle(HttpExchange exchange) throws IOException {
|
---|
145 | if (exchange.getRequestMethod().equalsIgnoreCase("POST")) {
|
---|
146 | String requestBody = new String(exchange.getRequestBody().readAllBytes());
|
---|
147 | ObjectMapper mapper = new ObjectMapper();
|
---|
148 | Map<String, String> requestData = mapper.readValue(requestBody, Map.class);
|
---|
149 | String userEmail = requestData.get("userEmail");
|
---|
150 |
|
---|
151 | int userId;
|
---|
152 | try {
|
---|
153 | userId = DatabaseUtil.getUserIdByEmail(userEmail);
|
---|
154 | } catch (SQLException e) {
|
---|
155 | e.printStackTrace();
|
---|
156 | sendResponse(exchange, 500, "{\"message\": \"Failed to retrieve user ID.\"}");
|
---|
157 | return;
|
---|
158 | }
|
---|
159 |
|
---|
160 | List<Option> savedTrips;
|
---|
161 | try {
|
---|
162 | savedTrips = DatabaseUtil.getSavedTripsByUser(userId);
|
---|
163 | } catch (SQLException e) {
|
---|
164 | e.printStackTrace();
|
---|
165 | sendResponse(exchange, 500, "{\"message\": \"Failed to retrieve saved trips.\"}");
|
---|
166 | return;
|
---|
167 | }
|
---|
168 |
|
---|
169 | String responseJson = mapper.writeValueAsString(Map.of("savedTrips", savedTrips));
|
---|
170 | System.out.println(responseJson);
|
---|
171 | sendResponse(exchange, 200, responseJson);
|
---|
172 | } else {
|
---|
173 | sendResponse(exchange, 405, "{\"message\": \"Method not allowed.\"}");
|
---|
174 | }
|
---|
175 | }
|
---|
176 | }
|
---|
177 | public static LocalDateTime getLastUpdateTime() throws IOException {
|
---|
178 | ClassLoader classLoader = Server.class.getClassLoader();
|
---|
179 | ObjectMapper mapper = new ObjectMapper();
|
---|
180 | JsonNode root = mapper.readTree(new File(classLoader.getResource("lastUpdateTime.json").getFile()));
|
---|
181 | String lastUpdateTimeStr = root.get("lastUpdateTime").asText();
|
---|
182 | return LocalDateTime.parse(lastUpdateTimeStr);
|
---|
183 | }
|
---|
184 | public static void updateLastUpdateTime() throws IOException {
|
---|
185 | LocalDateTime now = LocalDateTime.now();
|
---|
186 | ObjectMapper mapper = new ObjectMapper();
|
---|
187 | ObjectNode rootNode = mapper.createObjectNode();
|
---|
188 | rootNode.put("lastUpdateTime", now.toString());
|
---|
189 |
|
---|
190 | File file = new File(Server.class.getClassLoader().getResource("lastUpdateTime.json").getFile());
|
---|
191 |
|
---|
192 | mapper.writerWithDefaultPrettyPrinter().writeValue(file, rootNode);
|
---|
193 |
|
---|
194 | System.out.println("Last update time saved: " + now);
|
---|
195 | }
|
---|
196 |
|
---|
197 | public static void sendResponse(HttpExchange exchange, int statusCode, String response) throws IOException {
|
---|
198 | byte[] responseBytes = response.getBytes(StandardCharsets.UTF_8);
|
---|
199 | exchange.getResponseHeaders().add("Cross-Origin-Opener-Policy", "unsafe-none");
|
---|
200 | exchange.getResponseHeaders().add("Cross-Origin-Embedder-Policy", "require-corp");
|
---|
201 | exchange.getResponseHeaders().add("Content-Type", "application/json");
|
---|
202 | exchange.getResponseHeaders().add("Transfer-Encoding", "chunked");
|
---|
203 | exchange.sendResponseHeaders(statusCode, 0);
|
---|
204 | try (OutputStream os = exchange.getResponseBody()) {
|
---|
205 | os.write(responseBytes);
|
---|
206 | }
|
---|
207 | }
|
---|
208 | }
|
---|
209 |
|
---|
210 |
|
---|
211 |
|
---|
212 |
|
---|