-
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.
Allow file or URI arguments to metaschema-cli (#298)
* 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]> * CLI commands allow files or URIs for #297 This PR adjusts the commands and supporting databind classes and ports them to use URIs. The commands will use the new UriUtils class and the toUri() function to use a remote URI supported as a URL and load the model, schema, or target file. If a local file, it will convert the file path to valid URI. Integration tests have been added to validate this functionality works with the update CLI commands. * Specific error handling for different URI failures Per discussion with @david-waltermire, update the exception handling and associated error messages to identify if a bad domain for a remote URI is used or a valid domain is used but a bad URL. Co-authored-by: David Waltermire <[email protected]> * Feedback: use buffered input stream for YAML URIs * Feedback: remove infeasible UriUtilsTest cases * Feedback: arg count check for AbstractValidateContentCommand * Feedback: docs for UriUtils.toUri() --------- Co-authored-by: David Waltermire <[email protected]>
- Loading branch information
1 parent
2846899
commit 9360d97
Showing
11 changed files
with
343 additions
and
114 deletions.
There are no files selected for viewing
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
73 changes: 73 additions & 0 deletions
73
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,73 @@ | ||
/* | ||
* 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 | ||
} | ||
|
||
/** | ||
* Process a string to a local file path or remote location. If the location is | ||
* convertible to a URI, return the {@link URI}. Normalize the resulting URI | ||
* with the base URI, if provided. | ||
* | ||
* @param location | ||
* a string defining a remote or local file-based location | ||
* @param baseUri | ||
* the base URI to use for URI normalization | ||
* @throws URISyntaxException | ||
* an error if the location string is not convertible to URI | ||
* @return a new URI | ||
*/ | ||
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()); | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
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,82 @@ | ||
/* | ||
* 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)); | ||
} | ||
}; | ||
return values.stream(); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("provideValuesTestToUri") | ||
void testToUri(@NonNull String location, boolean expectedResult) { | ||
boolean result = INVALID; | ||
Path cwd = Paths.get(""); | ||
try { | ||
URI uri = UriUtils.toUri(location, cwd.toAbsolutePath().toUri()); | ||
result = VALID; | ||
System.out.println(String.format("%s -> %s", location, uri.toASCIIString())); | ||
} catch (Exception ex) { | ||
ex.printStackTrace(); | ||
} | ||
assertEquals(result, expectedResult); | ||
} | ||
} |
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
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
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
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
Oops, something went wrong.