Add search functionality to Database, add search HTTP handler, add better JSON method to HttpResponse, add missing handlers to Server auth ignore list.
This commit is contained in:
parent
bf1c71f176
commit
f6c6930e50
6 changed files with 75 additions and 6 deletions
|
|
@ -17,10 +17,10 @@ The server includes a (working but very WIP) HTTP API for third-party clients an
|
||||||
- [x] Authentication
|
- [x] Authentication
|
||||||
- [x] Library info
|
- [x] Library info
|
||||||
- [x] Game info
|
- [x] Game info
|
||||||
|
- [x] Search
|
||||||
- [x] Downloads
|
- [x] Downloads
|
||||||
- [ ] Resumable downloads
|
- [ ] Resumable downloads
|
||||||
- [ ] User Management
|
- [ ] User Management
|
||||||
- [ ] Search
|
|
||||||
|
|
||||||
### Web interface
|
### Web interface
|
||||||
- [x] Authentication
|
- [x] Authentication
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@ package org.adrianvictor.livingroom.data;
|
||||||
|
|
||||||
import org.adrianvictor.livingroom.Logger;
|
import org.adrianvictor.livingroom.Logger;
|
||||||
import org.adrianvictor.livingroom.Main;
|
import org.adrianvictor.livingroom.Main;
|
||||||
import org.adrianvictor.livingroom.config.AppConfig;
|
|
||||||
import org.adrianvictor.livingroom.data.catalog.Item;
|
import org.adrianvictor.livingroom.data.catalog.Item;
|
||||||
import org.adrianvictor.livingroom.data.catalog.Property;
|
import org.adrianvictor.livingroom.data.catalog.Property;
|
||||||
import org.json.simple.JSONObject;
|
import org.json.simple.JSONObject;
|
||||||
|
|
@ -96,7 +95,7 @@ public class Database {
|
||||||
String sql = "SELECT id FROM games WHERE name=?";
|
String sql = "SELECT id FROM games WHERE name=?";
|
||||||
|
|
||||||
try (Connection conn = DriverManager.getConnection(url);
|
try (Connection conn = DriverManager.getConnection(url);
|
||||||
PreparedStatement stmt = conn.prepareStatement(sql);) {
|
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||||
stmt.setString(1, name);
|
stmt.setString(1, name);
|
||||||
try (ResultSet rs = stmt.executeQuery()) {
|
try (ResultSet rs = stmt.executeQuery()) {
|
||||||
return rs.next();
|
return rs.next();
|
||||||
|
|
@ -111,7 +110,7 @@ public class Database {
|
||||||
String sql = "SELECT * FROM games WHERE id=?";
|
String sql = "SELECT * FROM games WHERE id=?";
|
||||||
|
|
||||||
try (Connection conn = DriverManager.getConnection(url);
|
try (Connection conn = DriverManager.getConnection(url);
|
||||||
PreparedStatement stmt = conn.prepareStatement(sql);) {
|
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||||
stmt.setString(1, String.valueOf(id));
|
stmt.setString(1, String.valueOf(id));
|
||||||
try (ResultSet rs = stmt.executeQuery()) {
|
try (ResultSet rs = stmt.executeQuery()) {
|
||||||
List<Item> processed = processResultSet(rs);
|
List<Item> processed = processResultSet(rs);
|
||||||
|
|
@ -127,9 +126,28 @@ public class Database {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<Item> search(String term) {
|
||||||
|
String sql = "SELECT * FROM games WHERE name LIKE ?";
|
||||||
|
List<Item> result = new ArrayList<>();
|
||||||
|
|
||||||
|
try (Connection conn = DriverManager.getConnection(url);
|
||||||
|
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||||
|
|
||||||
|
stmt.setString(1, "%" + term + "%"); // partial match
|
||||||
|
|
||||||
|
try (ResultSet rs = stmt.executeQuery()) {
|
||||||
|
result.addAll(processResultSet(rs));
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
Logger.error(e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
public List<Item> getAllGames() {
|
public List<Item> getAllGames() {
|
||||||
String sql = "SELECT * FROM games";
|
String sql = "SELECT * FROM games";
|
||||||
JSONParser parser = new JSONParser();
|
|
||||||
List<Item> result = new ArrayList<>();
|
List<Item> result = new ArrayList<>();
|
||||||
|
|
||||||
try (Connection conn = DriverManager.getConnection(url);
|
try (Connection conn = DriverManager.getConnection(url);
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@ public class Handlers {
|
||||||
map.put(new StaticWebHandler(), "/static");
|
map.put(new StaticWebHandler(), "/static");
|
||||||
map.put(new WebRedirectHandler(), "/");
|
map.put(new WebRedirectHandler(), "/");
|
||||||
map.put(new LoginHandler(), "/login");
|
map.put(new LoginHandler(), "/login");
|
||||||
|
map.put(new SearchHandler(), "/search");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static HashMap<Handler, String> getAll() {
|
public static HashMap<Handler, String> getAll() {
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
package org.adrianvictor.livingroom.http;
|
package org.adrianvictor.livingroom.http;
|
||||||
|
|
||||||
|
import org.json.simple.JSONObject;
|
||||||
|
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
@ -33,6 +35,17 @@ public record HttpResponse(
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static HttpResponse json(int status, Map<String, Object> extraFields) {
|
||||||
|
JSONObject json = new JSONObject();
|
||||||
|
json.putAll(extraFields);
|
||||||
|
|
||||||
|
return new HttpResponse(
|
||||||
|
status,
|
||||||
|
json.toJSONString().getBytes(StandardCharsets.UTF_8),
|
||||||
|
Map.of("Content-Type", "application/json")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
public static HttpResponse ok(byte[] body, String contentType) {
|
public static HttpResponse ok(byte[] body, String contentType) {
|
||||||
return ok(body, contentType, new HashMap<>());
|
return ok(body, contentType, new HashMap<>());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -68,7 +68,7 @@ public class Server {
|
||||||
exchange.getResponseHeaders().add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
|
exchange.getResponseHeaders().add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
|
||||||
exchange.getResponseHeaders().add("Access-Control-Allow-Headers", "Content-Type");
|
exchange.getResponseHeaders().add("Access-Control-Allow-Headers", "Content-Type");
|
||||||
|
|
||||||
if (!(handler instanceof WebHandler) && !(handler instanceof LoginHandler)) {
|
if (!(handler instanceof WebHandler) && !(handler instanceof LoginHandler) && !(handler instanceof StaticWebHandler) && !(handler instanceof WebRedirectHandler)) {
|
||||||
Session session = AuthenticationHelper.getAuthenticatedSession(exchange);
|
Session session = AuthenticationHelper.getAuthenticatedSession(exchange);
|
||||||
if (session == null) {
|
if (session == null) {
|
||||||
try {
|
try {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,37 @@
|
||||||
|
package org.adrianvictor.livingroom.http.handlers;
|
||||||
|
|
||||||
|
import com.sun.net.httpserver.HttpExchange;
|
||||||
|
import org.adrianvictor.livingroom.data.Database;
|
||||||
|
import org.adrianvictor.livingroom.data.catalog.Item;
|
||||||
|
import org.adrianvictor.livingroom.http.Handler;
|
||||||
|
import org.adrianvictor.livingroom.http.HttpResponse;
|
||||||
|
import org.json.simple.JSONArray;
|
||||||
|
import org.json.simple.JSONObject;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class SearchHandler implements Handler {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public HttpResponse result(String baseAddress, String path, HttpExchange exchange) {
|
||||||
|
String[] parts = path.split("/");
|
||||||
|
|
||||||
|
if (parts.length < 1) {
|
||||||
|
Map<String, Object> map = new HashMap<>();
|
||||||
|
map.put("message", "Invalid usage. Usage: ./query");
|
||||||
|
return HttpResponse.json(400, map);
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Item> games = Database.getInstance().search(parts[1]);
|
||||||
|
JSONArray json = new JSONArray();
|
||||||
|
for (Item g : games) {
|
||||||
|
json.add(new JSONObject(g.getPropertiesString()));
|
||||||
|
}
|
||||||
|
Map<String, Object> extraFields = new HashMap<>();
|
||||||
|
extraFields.put("result", json.toJSONString());
|
||||||
|
extraFields.put("success", "true");
|
||||||
|
return HttpResponse.json(200, extraFields);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue