Changeset View
Changeset View
Standalone View
Standalone View
java/src/main/java/org/softwareheritage/graph/server/App.java
Show All 9 Lines | |||||
import org.softwareheritage.graph.Stats; | import org.softwareheritage.graph.Stats; | ||||
import org.softwareheritage.graph.SWHID; | import org.softwareheritage.graph.SWHID; | ||||
import java.io.IOException; | import java.io.IOException; | ||||
import java.util.List; | import java.util.List; | ||||
import java.util.Map; | import java.util.Map; | ||||
/** | /** | ||||
* Web framework of the swh-graph server REST API. | * Web framework of the swh-graph server RPC API. | ||||
* | * | ||||
* @author The Software Heritage developers | * @author The Software Heritage developers | ||||
*/ | */ | ||||
public class App { | public class App { | ||||
/** | /** | ||||
* Main entrypoint. | * Main entrypoint. | ||||
* | * | ||||
▲ Show 20 Lines • Show All 127 Lines • ▼ Show 20 Lines | private static void startServer(String graphPath, int port, boolean showTimings) throws IOException { | ||||
app.exception(IllegalArgumentException.class, (e, ctx) -> { | app.exception(IllegalArgumentException.class, (e, ctx) -> { | ||||
ctx.status(400); | ctx.status(400); | ||||
ctx.result(e.getMessage()); | ctx.result(e.getMessage()); | ||||
}); | }); | ||||
} | } | ||||
/** | /** | ||||
* Checks query strings names provided to the REST API. | * Checks query strings names provided to the RPC API. | ||||
* | * | ||||
* @param ctx Javalin HTTP request context | * @param ctx Javalin HTTP request context | ||||
* @param allowedFmt a regular expression describing allowed query strings names | * @param allowedFmt a regular expression describing allowed query strings names | ||||
* @throws IllegalArgumentException unknown query string provided | * @throws IllegalArgumentException unknown query string provided | ||||
*/ | */ | ||||
private static void checkQueryStrings(Context ctx, String allowedFmt) { | private static void checkQueryStrings(Context ctx, String allowedFmt) { | ||||
Map<String, List<String>> queryParamMap = ctx.queryParamMap(); | Map<String, List<String>> queryParamMap = ctx.queryParamMap(); | ||||
for (String key : queryParamMap.keySet()) { | for (String key : queryParamMap.keySet()) { | ||||
if (!key.matches(allowedFmt)) { | if (!key.matches(allowedFmt)) { | ||||
throw new IllegalArgumentException("Unknown query string: " + key); | throw new IllegalArgumentException("Unknown query string: " + key); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
/** | /** | ||||
* Formats endpoint result into final JSON for the REST API. | * Formats endpoint result into final JSON for the RPC API. | ||||
* <p> | * <p> | ||||
* Removes unwanted information if necessary, such as timings (to prevent use of side channels | * Removes unwanted information if necessary, such as timings (to prevent use of side channels | ||||
* attacks). | * attacks). | ||||
* | * | ||||
* @param output endpoint operation output which needs formatting | * @param output endpoint operation output which needs formatting | ||||
* @param showTimings true if timings should be in results metadata, false otherwise | * @param showTimings true if timings should be in results metadata, false otherwise | ||||
* @return final Object with desired JSON format | * @return final Object with desired JSON format | ||||
*/ | */ | ||||
Show All 10 Lines |