Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(RDF API): Incorrect internal IRIs #4383

Merged
merged 16 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
import io.javalin.Javalin;
import io.javalin.http.Context;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.*;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it is not preferable to use * imports

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IDEA did that automatically after I made some changes, I'll make sure to revert it.

import org.eclipse.rdf4j.rio.RDFFormat;
import org.molgenis.emx2.Database;
import org.molgenis.emx2.MolgenisException;
Expand Down Expand Up @@ -58,12 +55,11 @@ private static void defineApiRoutes(Javalin app, String apiLocation, RDFFormat f
app.head("{schema}" + apiLocation + "/{table}column/{column}/", (ctx) -> rdfHead(ctx, format));
}

private static String rdfHead(Context ctx, RDFFormat format) {
private static void rdfHead(Context ctx, RDFFormat format) {
ctx.contentType(selectFormat(ctx, format).getDefaultMIMEType());
return "";
}

private static int rdfForDatabase(Context ctx, RDFFormat format) throws IOException {
private static void rdfForDatabase(Context ctx, RDFFormat format) throws IOException {
format = selectFormat(ctx, format); // defines format if null

Database db = sessionManager.getSession(ctx.req()).getDatabase();
Expand Down Expand Up @@ -99,10 +95,9 @@ private static int rdfForDatabase(Context ctx, RDFFormat format) throws IOExcept

outputStream.flush();
outputStream.close();
return 200;
}

private static int rdfForSchema(Context ctx, RDFFormat format) throws IOException {
private static void rdfForSchema(Context ctx, RDFFormat format) throws IOException {
format = selectFormat(ctx, format); // defines format if null

Schema schema = getSchema(ctx);
Expand All @@ -118,10 +113,9 @@ private static int rdfForSchema(Context ctx, RDFFormat format) throws IOExceptio
rdf.describeAsRDF(outputStream, null, null, null, schema);
outputStream.flush();
outputStream.close();
return 200;
}

private static int rdfForTable(Context ctx, RDFFormat format) throws IOException {
private static void rdfForTable(Context ctx, RDFFormat format) throws IOException {
format = selectFormat(ctx, format); // defines format if null

Table table = getTableByIdOrName(ctx);
Expand All @@ -138,10 +132,9 @@ private static int rdfForTable(Context ctx, RDFFormat format) throws IOException
rdf.describeAsRDF(outputStream, table, rowId, null, table.getSchema());
outputStream.flush();
outputStream.close();
return 200;
}

private static int rdfForRow(Context ctx, RDFFormat format) throws IOException {
private static void rdfForRow(Context ctx, RDFFormat format) throws IOException {
format = selectFormat(ctx, format); // defines format if null
Table table = getTableByIdOrName(ctx);
String rowId = sanitize(ctx.pathParam("row"));
Expand All @@ -154,10 +147,9 @@ private static int rdfForRow(Context ctx, RDFFormat format) throws IOException {
rdf.describeAsRDF(outputStream, table, rowId, null, table.getSchema());
outputStream.flush();
outputStream.close();
return 200;
}

private static int rdfForColumn(Context ctx, RDFFormat format) throws IOException {
private static void rdfForColumn(Context ctx, RDFFormat format) throws IOException {
format = selectFormat(ctx, format); // defines format if null

Table table = getTableByIdOrName(ctx);
Expand All @@ -172,30 +164,13 @@ private static int rdfForColumn(Context ctx, RDFFormat format) throws IOExceptio
rdf.describeAsRDF(outputStream, table, null, columnName, table.getSchema());
outputStream.flush();
outputStream.close();
return 200;
}

private static String extractBaseURL(Context ctx) {
// NOTE: The request.host() already includes the server port!
String scheme = ctx.scheme();
String port = null;
var parts = ctx.host().split(":", 2);
String host = parts[0];
if (parts.length == 2) {
if (!isWellKnownPort(scheme, parts[1])) {
port = parts[1];
}
}
return scheme
return ctx.scheme()
+ "://"
+ host
+ (port != null ? ":" + port : "")
+ (!ctx.path().isEmpty() ? "/" + ctx.path() + "/" : "/");
}

private static boolean isWellKnownPort(String scheme, String port) {
return (scheme.equals("http") && port.equals("80"))
|| (scheme.equals("https") && port.equals("443"));
+ ctx.host()
+ (!ctx.contextPath().isEmpty() ? "/" + ctx.contextPath() + "/" : "/");
}

private static RDFFormat selectFormat(Context ctx, RDFFormat format) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ public void testMolgenisWebservice_robotsDotTxt() {
}

@Test
public void testRdfApi() {
public void testRdfApiRequest() {
final String urlPrefix = "http://localhost:" + PORT;

final String defaultContentType = "text/turtle";
Expand Down Expand Up @@ -865,16 +865,40 @@ public void testRdfApi() {
.head(urlPrefix + "/pet store/api/jsonld");
rdfApiContentTypeRequest(200, jsonldContentType, ttlContentType)
.head(urlPrefix + "/pet store/api/ttl");
}

// Validate actual output.
String result =
@Test
void testRdfApiContent() {
// Output from global API call.
String resultBase =
given()
.sessionId(SESSION_ID)
.when()
.get("http://localhost:" + PORT + "/api/rdf?schemas=pet store")
.getBody()
.asString();
assertFalse(result.contains("CatalogueOntologies"));

// Output schema API call.
String resultSchema =
given()
.sessionId(SESSION_ID)
.when()
.get("http://localhost:" + PORT + "/pet store/api/rdf")
.getBody()
.asString();

assertAll(
// Validate base API.
() -> assertFalse(resultBase.contains("CatalogueOntologies")),
() ->
assertTrue(
resultBase.contains(
"http://localhost:" + PORT + "/pet%20store/api/rdf/Category/column/name")),
// Validate schema API.
() ->
assertTrue(
resultSchema.contains(
"http://localhost:" + PORT + "/pet%20store/api/rdf/Category/column/name")));
}

/**
Expand Down