Skip to content

Commit

Permalink
~ feedback from shapetrees-js@2ef2476
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Prud'hommeaux committed Dec 24, 2021
1 parent 58ab9c5 commit f27fb53
Show file tree
Hide file tree
Showing 28 changed files with 200 additions and 156 deletions.
24 changes: 14 additions & 10 deletions asTypescript/packages/client-http/src/HttpResourceAccessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,11 @@ export class HttpResourceAccessor implements ResourceAccessor {
if (location.isPresent()) {
try {
url = new URL(location.get());
} catch (ex) {
if (ex instanceof MalformedURLException) {
} catch (e) {
if (e instanceof MalformedURLException) {
throw new ShapeTreeException(500, "Retrieving <" + url + "> yielded a Location header \"" + location.get() + "\" which doesn't parse as a URL: " + e.getMessage());
}
}}

}
// Determine whether the resource exists based on the response. Even if the resource
// doesn't exist, additional context and processing is done to provide the appropriate
Expand Down Expand Up @@ -354,7 +355,8 @@ export class HttpResourceAccessor implements ResourceAccessor {
} catch (ex) {
if (ex instanceof Exception) {
throw new ShapeTreeException(500, ex.getMessage());
}
}}

}

/**
Expand Down Expand Up @@ -453,10 +455,11 @@ export class HttpResourceAccessor implements ResourceAccessor {
let managerUrlString: string = optManagerString.get();
try {
return Optional.of(new URL(url, managerUrlString));
} catch (ex) {
if (ex instanceof MalformedURLException) {
} catch (e) {
if (e instanceof MalformedURLException) {
throw new ShapeTreeException(500, "Malformed relative URL <" + managerUrlString + "> (resolved from <" + url + ">)");
}
}}

}

/**
Expand All @@ -482,10 +485,11 @@ export class HttpResourceAccessor implements ResourceAccessor {
}
try {
managedResourceUrl = new URL(managerUrl, managedUrlString);
} catch (ex) {
if (ex instanceof MalformedURLException) {
} catch (e) {
if (e instanceof MalformedURLException) {
throw new ShapeTreeException(500, "Can't calculate managed resource for shape tree manager <" + managerUrl + ">");
}
}}

return managedResourceUrl;
}

Expand Down
7 changes: 4 additions & 3 deletions asTypescript/packages/core/src/ManageableResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ export class ManageableResource extends InstanceResource {
const rel: string = this.isContainer() ? ".." : ".";
try {
return new URL(this.getUrl(), rel);
} catch (ex) {
if (ex instanceof MalformedURLException) {
} catch (e) {
if (e instanceof MalformedURLException) {
throw new ShapeTreeException(500, "Malformed focus node when resolving <" + rel + "> against <" + this.getUrl() + ">");
}
}}

}

public getManagerResourceUrl(): URL | null {
Expand Down
6 changes: 4 additions & 2 deletions asTypescript/packages/core/src/ShapeTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ export class ShapeTree {
} catch (ex) {
if (ex instanceof Exception) {
throw new ShapeTreeException(500, "Error parsing ShEx schema - " + ex.getMessage());
}
}}

}
// Tell ShExJava we want to use Jena as our graph library
let jenaRDF: JenaRDF = new org.apache.commons.rdf.jena.JenaRDF();
Expand Down Expand Up @@ -151,7 +152,8 @@ export class ShapeTree {
} catch (ex) {
if (ex instanceof MalformedURLException) {
throw new ShapeTreeException(500, "Error reporting validation success on malformed URL <" + focusUriString + ">: " + ex.getMessage());
}
}}

return new ValidationResult(valid, this, this, matchingFocusNode);
}
}
Expand Down
3 changes: 2 additions & 1 deletion asTypescript/packages/core/src/ShapeTreeAssignment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ export class ShapeTreeAssignment {
} catch (ex) {
if (ex instanceof NullPointerException || ex instanceof IllegalStateException) {
throw new ShapeTreeException(500, "Failed to initialize shape tree assignment: " + ex.getMessage());
}
}}

}

public static getFromGraph(url: URL, managerGraph: Graph): ShapeTreeAssignment /* throws MalformedURLException, ShapeTreeException */ {
Expand Down
9 changes: 6 additions & 3 deletions asTypescript/packages/core/src/ShapeTreeFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ export class ShapeTreeFactory {
} catch (ex) {
if (ex instanceof MalformedURLException || ex instanceof ShapeTreeException) {
throw new ShapeTreeException(500, "List <" + shapeTreeUrl + "> contains malformed URL: " + ex.getMessage());
}
}}

}

/**
Expand All @@ -117,7 +118,8 @@ export class ShapeTreeFactory {
} catch (ex) {
if (ex instanceof MalformedURLException) {
throw new ShapeTreeException(500, "ShapeTree <" + shapeTreeUrl + "> references malformed URL <" + referencedShapeTreeUrlString + ">: " + ex.getMessage());
}
}}

let viaShapePath: string = getStringValue(resourceModel, referenceResource, ShapeTreeVocabulary.VIA_SHAPE_PATH);
let viaPredicate: URL = getUrlValue(resourceModel, referenceResource, ShapeTreeVocabulary.VIA_PREDICATE, shapeTreeUrl);
referencedShapeTree = new ShapeTreeReference(referencedShapeTreeUrl, viaShapePath, viaPredicate);
Expand Down Expand Up @@ -147,7 +149,8 @@ export class ShapeTreeFactory {
} catch (ex) {
if (ex instanceof MalformedURLException) {
throw new IllegalStateException("Malformed ShapeTree <" + shapeTreeUrl + ">: Jena URIResource <" + object + "> didn't parse as URL - " + ex.getMessage());
}
}}

} else {
throw new ShapeTreeException(500, "Malformed ShapeTree <" + shapeTreeUrl + ">: expected " + object + " to be a URL");
}
Expand Down
10 changes: 6 additions & 4 deletions asTypescript/packages/core/src/ShapeTreeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ export class ShapeTreeManager {
} catch (ex) {
if (ex instanceof MalformedURLException) {
throw new IllegalStateException("Minted illegal URL <" + assignmentString + "> - " + ex.getMessage());
}
}}

return assignmentUrl;
}

Expand Down Expand Up @@ -164,10 +165,11 @@ export class ShapeTreeManager {
let assignment: ShapeTreeAssignment = null;
try {
assignment = ShapeTreeAssignment.getFromGraph(new URL(assignmentNode.getObject().getURI()), managerGraph);
} catch (ex) {
if (ex instanceof MalformedURLException) {
} catch (e) {
if (e instanceof MalformedURLException) {
throw new ShapeTreeException(500, "Object of { " + s + " " + stAssignment + " " + assignmentNode.getObject() + " } must be a URL.");
}
}}

manager.assignments.add(assignment);
}
return manager;
Expand Down
3 changes: 2 additions & 1 deletion asTypescript/packages/core/src/ShapeTreeManagerDelta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ export class ShapeTreeManagerDelta {
} catch (ex) {
if (ex instanceof URISyntaxException) {
throw new ShapeTreeException(500, "Unable to convert assignment URLs for comparison: " + ex.getMessage());
}
}}

if (assignmentUri === targetAssignmentUri) {
return targetAssignment;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export class HttpExternalDocumentLoader implements ExternalDocumentLoader {
throw new ShapeTreeException(500, "Error retrieving <" + resourceUrl + ">: " + ex.getMessage());
} else if (ex instanceof URISyntaxException) {
throw new ShapeTreeException(500, "Malformed URL <" + resourceUrl + ">: " + ex.getMessage());
}
}}

}
}
16 changes: 10 additions & 6 deletions asTypescript/packages/core/src/helpers/GraphHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,11 @@ export class GraphHelper {
let reader: StringReader = new StringReader(rawContent);
RDFDataMgr.read(model.getGraph(), reader, baseURI.toString(), GraphHelper.getLangForContentType(contentType));
return model;
} catch (ex) {
if (ex instanceof RiotException) {
} catch (rex) {
if (rex instanceof RiotException) {
throw new ShapeTreeException(422, "Error processing input - " + rex.getMessage());
}
}}

}

/**
Expand Down Expand Up @@ -164,7 +165,8 @@ export class GraphHelper {
} catch (ex) {
if (ex instanceof URISyntaxException) {
throw new IllegalStateException("can't convert URL <" + url + "> to IRI: " + ex);
}
}}

}

/**
Expand All @@ -183,7 +185,8 @@ export class GraphHelper {
} catch (ex) {
if (ex instanceof MalformedURLException || ex instanceof URISyntaxException) {
throw new IllegalStateException("Unable to remove fragment from URL: " + ex.getMessage());
}
}}

}

public static knownUrl(urlString: string): URL {
Expand All @@ -192,6 +195,7 @@ export class GraphHelper {
} catch (ex) {
if (ex instanceof MalformedURLException) {
throw new IllegalStateException("Expected known URL <" + urlString + "> to parse as valid URL - " + ex.toString());
}
}}

}
}
17 changes: 10 additions & 7 deletions asTypescript/packages/core/src/helpers/RequestHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,11 @@ export class RequestHelper {
try {
const focusNodeUrl: URL = new URL(baseUrl, focusNodeUrlString);
focusNodeUrls.add(focusNodeUrl);
} catch (ex) {
if (ex instanceof MalformedURLException) {
} catch (e) {
if (e instanceof MalformedURLException) {
throw new ShapeTreeException(500, "Malformed focus node when resolving <" + focusNodeUrlString + "> against <" + baseUrl + ">");
}
}}

}
}
return focusNodeUrls;
Expand All @@ -120,10 +121,11 @@ export class RequestHelper {
try {
const targetShapeTreeUrl: URL = new URL(targetShapeTreeUrlString);
targetShapeTreeUrls.add(targetShapeTreeUrl);
} catch (ex) {
if (ex instanceof MalformedURLException) {
} catch (e) {
if (e instanceof MalformedURLException) {
throw new ShapeTreeException(500, "Malformed focus node when resolving <" + targetShapeTreeUrlString + "> against <" + baseUrl + ">");
}
}}

}
}
return targetShapeTreeUrls;
Expand Down Expand Up @@ -158,7 +160,8 @@ export class RequestHelper {
} catch (ex) {
if (ex instanceof MalformedURLException) {
throw new ShapeTreeException(500, "normalized to malformed URL <" + urlString + "> - " + ex.getMessage());
}
}}

}

/**
Expand Down
20 changes: 12 additions & 8 deletions asTypescript/packages/javahttp/src/JavaHttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ export class JavaHttpClient implements HttpClient {
} catch (ex) {
if (ex instanceof NullPointerException) {
log.error("Exception retrieving body string");
}
}}

return new DocumentResponse(new ResourceAttributes(response.headers().map()), body, response.statusCode());
}

Expand Down Expand Up @@ -75,16 +76,18 @@ export class JavaHttpClient implements HttpClient {
let sc: SSLContext = null;
try {
sc = SSLContext.getInstance("TLSv1.2");
} catch (ex) {
if (ex instanceof NoSuchAlgorithmException) {
} catch (e) {
if (e instanceof NoSuchAlgorithmException) {
e.printStackTrace();
}
}}

try {
sc.init(null, trustAllCerts, new java.security.SecureRandom());
} catch (ex) {
if (ex instanceof KeyManagementException) {
} catch (e) {
if (e instanceof KeyManagementException) {
e.printStackTrace();
}
}}

HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
// Create all-trusting host name verifier
let validHosts: HostnameVerifier = new HostnameVerifier() {
Expand Down Expand Up @@ -142,7 +145,8 @@ export class JavaHttpClient implements HttpClient {
throw new ShapeTreeException(500, ex.getMessage());
} else if (ex instanceof URISyntaxException) {
throw new ShapeTreeException(500, "Malformed URL <" + request.resourceURL + ">: " + ex.getMessage());
}
}}

}

protected static check(resp: java.net.http.HttpResponse): java.net.http.HttpResponse {
Expand Down
3 changes: 2 additions & 1 deletion asTypescript/packages/javahttp/src/JavaHttpClientFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ export class JavaHttpClientFactory implements HttpClientFactory, ExternalDocumen
} catch (ex) {
if (ex instanceof Exception) {
throw new ShapeTreeException(500, ex.getMessage());
}
}}

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ export class JavaHttpValidatingShapeTreeInterceptor {
} else if (ex instanceof Exception) {
log.error("Error processing shape tree request: ", ex);
return createErrorResponse(new ShapeTreeException(500, ex.getMessage()), clientRequest);
}
}}

} else {
log.warn("No handler for method [{}] - passing through request", shapeTreeRequest.getMethod());
return JavaHttpClient.check(httpClient.send(clientRequest, java.net.http.HttpResponse.BodyHandlers.ofString()));
Expand Down Expand Up @@ -120,7 +121,8 @@ export class JavaHttpValidatingShapeTreeInterceptor {
} catch (ex) {
if (ex instanceof MalformedURLException) {
throw new IllegalStateException("request has a malformed URL <" + request.uri() + ">: " + ex.getMessage());
}
}}

}

override public getHeaders(): ResourceAttributes {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,14 @@ public interface ShapeTreeClient {
* @param context ShapeTreeContext that would be used for authentication purposes
* @param targetResource The target resource to be created or updated
* @param focusNodes One or more nodes/subjects to use as the focus for shape validation
* @param targetShapeTrees The shape trees that a proposed resource to be created should be validated against
* @param isContainer Specifies whether a newly created resource should be created as a container or not
* @param bodyString String representation of the body of the resource to create or update
* @param contentType Content type to parse the bodyString parameter as
* @param targetShapeTrees The shape trees that a proposed resource to be created should be validated against
* @param isContainer Specifies whether a newly created resource should be created as a container or not
* @return DocumentResponse containing status and response header / attributes
* @throws ShapeTreeException
*/
DocumentResponse putManagedInstance(ShapeTreeContext context, URL targetResource, List<URL> focusNodes, List<URL> targetShapeTrees, Boolean isContainer, String bodyString, String contentType) throws ShapeTreeException;
DocumentResponse putManagedInstance(ShapeTreeContext context, URL targetResource, List<URL> focusNodes, String bodyString, String contentType, List<URL> targetShapeTrees, Boolean isContainer) throws ShapeTreeException;

/**
* Updates a resource via HTTP PUT that has been validated against an associated shape tree
Expand All @@ -103,7 +103,7 @@ public interface ShapeTreeClient {
* @return DocumentResponse containing status and response header / attributes
* @throws ShapeTreeException
*/
DocumentResponse putManagedInstance(ShapeTreeContext context, URL targetResource, List<URL> focusNodes, String bodyString, String contentType) throws ShapeTreeException;
DocumentResponse updateManagedInstance(ShapeTreeContext context, URL targetResource, List<URL> focusNodes, String bodyString, String contentType) throws ShapeTreeException;

/**
* Updates a resource via HTTP PATCH that has been validated against an associated shape tree
Expand Down
Loading

0 comments on commit f27fb53

Please sign in to comment.