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

Traversal robustness #20

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
33 changes: 27 additions & 6 deletions lib/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,11 @@ export function registerTraversal(docUri, suppliedParams = {}) {
ignoreObjectPrefix: [],
ignoreObjectUri: [],
ignoreObjectType: [],
ignoreObjectExtension: ["gif", "jpeg", "jpg", "mei", "mp3", "mp4", "png", "pdf", "tei", "wav", "jpg", "jpeg"],
// NEW: don't load these expecting RDF
followPropertyPrefix: [],
followPropertyUri: [],
ignorePpropertyPrefix: [],
ignorePropertyPrefix: [],
ignorePropertyUri: [],
objectives: {},
numHops: MAX_TRAVERSAL_HOPS,
Expand Down Expand Up @@ -281,22 +283,31 @@ export function traverse(docUri, params) {
type: TRAVERSAL_UNNECCESSARY
});
return; // file not modified, i.e. etag matched, no updates required
}
} else if (response.status == 404) {
dispatch({
type: TRAVERSAL_FAILED
});
return; // no document found
} //console.log(response.headers.get("Content-Type"));
// Content-Type is not a compulsory field, and .get returns null if it's absent


console.log(response.headers.get("Content-Type")); // attempt to decide content type (either explicitly provided or by file suffix)
const CType = response.headers.get("Content-Type") || "";
const assumeNQ = true; // This is a hack to deal with unrecognisable files. For now, assume it's RDF. Revisit this.
// attempt to decide content type (either explicitly provided or by file suffix)
// and proceed with traversal accordingly

if (docUri.endsWith(".json") || docUri.endsWith(".jsonld") || docUri.endsWith(".json-ld") || response.headers.get("Content-Type").startsWith("application/ld+json") || response.headers.get("Content-Type").startsWith("application/json")) {
if (docUri.endsWith(".json") || docUri.endsWith(".jsonld") || docUri.endsWith(".json-ld") || CType.startsWith("application/ld+json") || CType.startsWith("application/json")) {
// treat as JSON-LD document
dispatch(traverseJSONLD(dispatch, docUri, params, response.json()));
} else if (docUri.endsWith(".ttl") || docUri.endsWith(".n3") || docUri.endsWith(".rdf") || docUri.endsWith(".nt") || response.headers.get("Content-Type").startsWith("application/rdf+xml") || response.headers.get("Content-Type").startsWith("application/x-turtle") || response.headers.get("Content-Type").startsWith("text/turtle")) {
} else if (docUri.endsWith(".ttl") || docUri.endsWith(".n3") || docUri.endsWith(".rdf") || docUri.endsWith(".nt") || CType.startsWith("application/rdf+xml") || CType.startsWith("application/x-turtle") || CType.startsWith("text/turtle")) {
// treat as RDF document
// TODO: Translate RDF to JSON-LD, then proceed with traverseJSONLD as above
dispatch({
type: TRAVERSAL_FAILED
});
console.log("Can't handle this document: (We currently only support nq and JSON-LD)", docUri, response); // dispatch(traverseRDF(dispatch, docUri, params, response.text()));
} else if (docUri.endsWith(".nq") || response.headers.get("Content-Type").startsWith("application/nquads")) {
} else if (docUri.endsWith(".nq") || CType.startsWith("application/nquads") || CType === "" && assumeNQ) {
dispatch(traverseRDF(dispatch, docUri, params, response.text()));
} else {
dispatch({
Expand Down Expand Up @@ -500,6 +511,16 @@ function passesTraversalConstraints(obj, params, predicate) {
//console.log("Test 8: predicate not in inclusion list", pref, params["followPropertyUri"]);
return false;
}
} // Does the URL have an extension that implies this isn't RDF?


const suffixExcluded = params["ignoreObjectExtension"].filter(extension => {
return resourceUri.split("#")[0].endsWith(extension);
});

if (suffixExcluded.length) {
console.log("Test 9: object excluded based on extension", obj, params);
return false;
} //console.log("Object passes all traversal constraint tests", obj, params, params["extendObjectPrefix"], params["ignoreObjectPrefix"], params["ignoreObjectUri"]);


Expand Down
32 changes: 24 additions & 8 deletions src/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,9 @@ export function registerTraversal(docUri, suppliedParams = {}) {
const defaultParams = {
extendObjectPrefix: [], extendObjectUri: [], extendObjectType: [],
ignoreObjectPrefix: [], ignoreObjectUri: [], ignoreObjectType: [],
ignoreObjectExtension: ["gif", "jpeg", "jpg", "mei", "mp3", "mp4", "png", "pdf", "tei", "wav", "jpg", "jpeg"], // NEW: don't load these expecting RDF
followPropertyPrefix: [], followPropertyUri: [],
ignorePpropertyPrefix: [], ignorePropertyUri: [],
ignorePropertyPrefix: [], ignorePropertyUri: [],
objectives: {}, numHops: MAX_TRAVERSAL_HOPS,
useEtag: false, etag: ""
};
Expand Down Expand Up @@ -284,27 +285,34 @@ export function traverse(docUri, params) {
if (response.status == 304) {
dispatch({type: TRAVERSAL_UNNECCESSARY});
return; // file not modified, i.e. etag matched, no updates required
} else if (response.status == 404) {
dispatch({type: TRAVERSAL_FAILED});
return; // no document found
}
console.log(response.headers.get("Content-Type"));
//console.log(response.headers.get("Content-Type"));
// Content-Type is not a compulsory field, and .get returns null if it's absent
const CType = response.headers.get("Content-Type") || "";
const assumeNQ = true; // This is a hack to deal with unrecognisable files. For now, assume it's RDF. Revisit this.
// attempt to decide content type (either explicitly provided or by file suffix)
// and proceed with traversal accordingly
if (docUri.endsWith(".json") || docUri.endsWith(".jsonld") || docUri.endsWith(".json-ld") ||
response.headers.get("Content-Type").startsWith("application/ld+json") ||
response.headers.get("Content-Type").startsWith("application/json")) {
CType.startsWith("application/ld+json") ||
CType.startsWith("application/json")) {
// treat as JSON-LD document
dispatch(traverseJSONLD(dispatch, docUri, params, response.json()));
} else if (docUri.endsWith(".ttl") || docUri.endsWith(".n3") || docUri.endsWith(".rdf") ||
docUri.endsWith(".nt") ||
response.headers.get("Content-Type").startsWith("application/rdf+xml") ||
response.headers.get("Content-Type").startsWith("application/x-turtle") ||
response.headers.get("Content-Type").startsWith("text/turtle")) {
CType.startsWith("application/rdf+xml") ||
CType.startsWith("application/x-turtle") ||
CType.startsWith("text/turtle")) {
// treat as RDF document
// TODO: Translate RDF to JSON-LD, then proceed with traverseJSONLD as above
dispatch({type: TRAVERSAL_FAILED});
console.log("Can't handle this document: (We currently only support nq and JSON-LD)", docUri, response)
// dispatch(traverseRDF(dispatch, docUri, params, response.text()));
} else if (docUri.endsWith(".nq") ||
response.headers.get("Content-Type").startsWith("application/nquads")) {
CType.startsWith("application/nquads") ||
(CType === "" && assumeNQ)) {
dispatch(traverseRDF(dispatch, docUri, params, response.text()));
} else {
dispatch({type: TRAVERSAL_FAILED});
Expand Down Expand Up @@ -502,6 +510,14 @@ function passesTraversalConstraints(obj, params, predicate) {
return false;
}
}
// Does the URL have an extension that implies this isn't RDF?
const suffixExcluded = params["ignoreObjectExtension"].filter(extension => {
return resourceUri.split("#")[0].endsWith(extension);
});
if (suffixExcluded.length) {
console.log("Test 9: object excluded based on extension", obj, params);
return false;
}


//console.log("Object passes all traversal constraint tests", obj, params, params["extendObjectPrefix"], params["ignoreObjectPrefix"], params["ignoreObjectUri"]);
Expand Down