-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add UriUtils class for URI/Path translation
Currently, we have the metaschema-cli only support local file paths for arguments for content inputs and outputs. This loader will support the use of MetaschemaLoader.load() with URIs and unify local file path and URL access (primarily https://, http://) with the same command line tooling for similar remote and local access. Co-Authored-By: David Waltermire <[email protected]>
- Loading branch information
1 parent
9babb01
commit ce0482a
Showing
2 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
core/src/main/java/gov/nist/secauto/metaschema/core/util/UriUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Portions of this software was developed by employees of the National Institute | ||
* of Standards and Technology (NIST), an agency of the Federal Government and is | ||
* being made available as a public service. Pursuant to title 17 United States | ||
* Code Section 105, works of NIST employees are not subject to copyright | ||
* protection in the United States. This software may be subject to foreign | ||
* copyright. Permission in the United States and in foreign countries, to the | ||
* extent that NIST may hold copyright, to use, copy, modify, create derivative | ||
* works, and distribute this software and its documentation without fee is hereby | ||
* granted on a non-exclusive basis, provided that this notice and disclaimer | ||
* of warranty appears in all copies. | ||
* | ||
* THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER | ||
* EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY | ||
* THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF | ||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM | ||
* INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE | ||
* SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT | ||
* SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, | ||
* INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, | ||
* OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, | ||
* CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR | ||
* PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT | ||
* OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. | ||
*/ | ||
|
||
package gov.nist.secauto.metaschema.core.util; | ||
|
||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.nio.file.InvalidPathException; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
|
||
public final class UriUtils { | ||
|
||
private UriUtils() { | ||
// disable construction | ||
} | ||
|
||
public static URI toUri(@NonNull String location, @NonNull URI baseUri) throws URISyntaxException { | ||
URI asUri; | ||
try { | ||
asUri = new URI(location); | ||
} catch (URISyntaxException ex) { | ||
// the location is not a valid URI | ||
try { | ||
// try to parse the location as a local file path | ||
Path path = Paths.get(location); | ||
asUri = path.toUri(); | ||
} catch (InvalidPathException ex2) { | ||
// not a local file path, so rethrow the original URI expection | ||
throw ex; | ||
} | ||
} | ||
return baseUri.resolve(asUri.normalize()); | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
core/src/test/java/gov/nist/secauto/metaschema/core/util/UriUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Portions of this software was developed by employees of the National Institute | ||
* of Standards and Technology (NIST), an agency of the Federal Government and is | ||
* being made available as a public service. Pursuant to title 17 United States | ||
* Code Section 105, works of NIST employees are not subject to copyright | ||
* protection in the United States. This software may be subject to foreign | ||
* copyright. Permission in the United States and in foreign countries, to the | ||
* extent that NIST may hold copyright, to use, copy, modify, create derivative | ||
* works, and distribute this software and its documentation without fee is hereby | ||
* granted on a non-exclusive basis, provided that this notice and disclaimer | ||
* of warranty appears in all copies. | ||
* | ||
* THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER | ||
* EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY | ||
* THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF | ||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM | ||
* INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE | ||
* SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT | ||
* SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, | ||
* INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, | ||
* OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, | ||
* CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR | ||
* PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT | ||
* OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. | ||
*/ | ||
|
||
package gov.nist.secauto.metaschema.core.util; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import java.net.URI; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.stream.Stream; | ||
|
||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
|
||
class UriUtilsTest { | ||
private static final boolean VALID = true; | ||
private static final boolean INVALID = false; | ||
|
||
private static Stream<Arguments> provideValuesTestToUri() { | ||
List<Arguments> values = new LinkedList<>() { | ||
{ | ||
add(Arguments.of("http://example.org/valid", VALID)); | ||
add(Arguments.of("https://example.org/valid", VALID)); | ||
add(Arguments.of("http://example.org/valid", VALID)); | ||
add(Arguments.of("ftp://example.org/valid", VALID)); | ||
add(Arguments.of("ssh://example.org/valid", VALID)); | ||
add(Arguments.of("example.org/good", VALID)); | ||
add(Arguments.of("bad.txt", VALID)); | ||
add(Arguments.of("relative\\windows\\path\\resource.txt", VALID)); | ||
add(Arguments.of("C:\\absolute\\valid.txt", VALID)); | ||
add(Arguments.of("local/relative/path/is/invalid.txt", VALID)); | ||
add(Arguments.of("/absolute/local/path/is/invalid.txt", VALID)); | ||
add(Arguments.of("1;", VALID)); | ||
add(Arguments.of(":", INVALID)); | ||
add(Arguments.of(">", INVALID)); | ||
add(Arguments.of("<", INVALID)); | ||
} | ||
}; | ||
return values.stream(); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("provideValuesTestToUri") | ||
void testToUri(@NonNull String location, boolean expectedResult) { | ||
boolean valid = false; | ||
Path cwd = Paths.get(""); | ||
try { | ||
URI uri = UriUtils.toUri(location, cwd.toAbsolutePath().toUri()); | ||
valid = true; | ||
System.out.println(String.format("%s -> %s", location, uri.toASCIIString())); | ||
} catch (Exception ex) { | ||
ex.printStackTrace(); | ||
} | ||
assertEquals(valid, expectedResult); | ||
} | ||
} |