source: backend/GlobeGuru-backend/src/main/java/Server.java@ df7f390

Last change on this file since df7f390 was cd64b06, checked in by Kristijan <kristijanzafirovski26@…>, 5 days ago

Added info scraping for escape travel

  • Property mode set to 100644
File size: 9.4 KB
RevLine 
[c164f8f]1import com.fasterxml.jackson.databind.JsonNode;
2import com.fasterxml.jackson.databind.ObjectMapper;
3import com.fasterxml.jackson.databind.node.ObjectNode;
4import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken;
5import com.google.api.client.googleapis.auth.oauth2.GoogleIdTokenVerifier;
6import com.google.api.client.http.HttpTransport;
7import com.google.api.client.http.javanet.NetHttpTransport;
8import com.google.api.client.json.JsonFactory;
9import com.google.api.client.json.jackson2.JacksonFactory;
10
11import com.sun.net.httpserver.HttpExchange;
12import com.sun.net.httpserver.HttpHandler;
13import com.sun.net.httpserver.HttpServer;
14
15import java.io.File;
16import java.io.IOException;
17import java.io.OutputStream;
18
19import java.net.InetSocketAddress;
20import java.net.URL;
21import java.nio.charset.StandardCharsets;
22import java.nio.file.Files;
23import java.nio.file.Path;
24import java.nio.file.Paths;
25import java.security.GeneralSecurityException;
26import java.sql.SQLException;
27import java.time.LocalDate;
28import java.time.LocalDateTime;
29import java.time.format.DateTimeFormatter;
[53bad7e]30import java.time.temporal.ChronoUnit;
[c164f8f]31import java.util.*;
32import java.util.concurrent.ExecutorService;
33import java.util.concurrent.Executors;
34import java.util.concurrent.Future;
35
36public class Server {
[53bad7e]37 private static final ExecutorService executorService = Executors.newFixedThreadPool(1);
[c164f8f]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");
[53bad7e]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
[c164f8f]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 {
[cd64b06]198 byte[] responseBytes = response.getBytes(StandardCharsets.UTF_8);
[c164f8f]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");
[cd64b06]202 exchange.getResponseHeaders().add("Transfer-Encoding", "chunked");
203 exchange.sendResponseHeaders(statusCode, 0);
204 try (OutputStream os = exchange.getResponseBody()) {
205 os.write(responseBytes);
206 }
[c164f8f]207 }
208}
209
210
211
212
Note: See TracBrowser for help on using the repository browser.