Skip to content

Commit a4a294d

Browse files
committed
Template utility methods
1 parent d5d6e78 commit a4a294d

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

access-grant/src/main/java/com/inrupt/client/accessgrant/AccessRequest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,33 @@ static AccessRequest parse(final String serialization) throws IOException {
145145
}
146146
}
147147

148+
/**
149+
* Produce a URI template that can be used consistently with the JCL.
150+
*
151+
* @param dataPath the data path relative to a storage root
152+
* @return a URI template
153+
*/
154+
public static String template(final String dataPath) {
155+
Objects.requireNonNull(dataPath, "dataPath may not be null!");
156+
if (!dataPath.startsWith("/")) {
157+
return "https://{domain}/{+path}/" + dataPath;
158+
}
159+
return template("/" + dataPath);
160+
}
161+
162+
/**
163+
* Produce a Map of template values that can be used with a URI template resolver.
164+
*
165+
* @param domain the domain of the user's storage
166+
* @param path the base path of the user's storage
167+
* @return a map of template values
168+
*/
169+
public static Map<String, String> templateValues(final String domain, final String path) {
170+
Objects.requireNonNull(domain, "domain may not be null!");
171+
Objects.requireNonNull(path, "path may not be null!");
172+
return Map.of("domain", domain, "path", path);
173+
}
174+
148175
/**
149176
* A collection of parameters used for creating access requests.
150177
*

access-grant/src/test/java/com/inrupt/client/accessgrant/AccessRequestTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,4 +272,18 @@ void testInvalidStream() throws IOException {
272272
void testInvalidString() throws IOException {
273273
assertThrows(IllegalArgumentException.class, () -> AccessRequest.of("not json"));
274274
}
275+
276+
@Test
277+
void testTemplate() {
278+
assertEquals("https://{domain}/{+path}/custom-path", AccessRequest.template("custom-path"));
279+
assertEquals("https://{domain}/{+path}/custom-path", AccessRequest.template("/custom-path"));
280+
assertEquals("https://{domain}/{+path}/./custom-path", AccessRequest.template("./custom-path"));
281+
}
282+
283+
@Test
284+
void testTemplateValues() {
285+
final var values = AccessRequest.templateValues("mydomain.com", "/storage/path");
286+
assertEquals("mydomain.com", values.get("domain"));
287+
assertEquals("/storage/path", values.get("path"));
288+
}
275289
}

0 commit comments

Comments
 (0)