33
44import static info .freelibrary .util .Constants .COLON ;
55
6- import com .fasterxml .jackson .core .JsonFactory ;
7- import com .fasterxml .jackson .core .JsonParser ;
8- import com .fasterxml .jackson .core .JsonToken ;
9- import info .freelibrary .iiif .presentation .v3 .Annotation ;
10- import info .freelibrary .iiif .presentation .v3 .AnnotationCollection ;
11- import info .freelibrary .iiif .presentation .v3 .AnnotationPage ;
12- import info .freelibrary .iiif .presentation .v3 .Collection ;
13- import info .freelibrary .iiif .presentation .v3 .Manifest ;
14- import info .freelibrary .iiif .presentation .v3 .ResourceTypes ;
156import info .freelibrary .iiif .presentation .v3 .properties .MediaType ;
16- import info .freelibrary .iiif .presentation .v3 .utils .JSON ;
177import info .freelibrary .iiif .presentation .v3 .utils .MessageCodes ;
188import info .freelibrary .iiif .presentation .v3 .utils .csv .Mapper ;
199import info .freelibrary .iiif .presentation .v3 .utils .csv .MappingException ;
3222import java .net .http .HttpRequest ;
3323import java .net .http .HttpResponse ;
3424import java .nio .charset .StandardCharsets ;
35- import java .nio .file .Files ;
3625import java .nio .file .Path ;
3726import java .util .Base64 ;
38- import java .util .Optional ;
3927import java .util .concurrent .Callable ;
4028import java .util .stream .Stream ;
4129
4230/** A jpv3 executable. */
4331@ CommandLine .Command (name = "jpv3" , mixinStandardHelpOptions = true , version = "jpv3 0.0.1-SNAPSHOT" ,
4432 description = { "" , "A tool for working with IIIF manifests and collection documents:" , "" },
4533 usageHelpWidth = 120 )
46- @ SuppressWarnings ({ PMD .EXCESSIVE_IMPORTS })
4734public final class JPv3 implements Callable <Integer > {
4835
4936 /** The logger for the executable. */
@@ -73,9 +60,9 @@ public final class JPv3 implements Callable<Integer> {
7360 paramLabel = "PASSWORD" , defaultValue = "${env:JPV3_PASSWORD}" )
7461 private String myPassword ;
7562
76- /** The host to which the ZIP file is being uploaded. This is only needed for uploads . */
77- @ CommandLine .Option (names = { "-H" , "--host" }, description = "The host to which the ZIP file is being uploaded " ,
78- paramLabel = "HOST" , defaultValue = "${env:JPV3_HOST}" )
63+ /** The IIIF manifests and collection documents server . */
64+ @ CommandLine .Option (names = { "-H" , "--host" }, description = "The IIIF manifests and collection documents server " ,
65+ paramLabel = "HOST" , defaultValue = "${env:JPV3_HOST}" , required = true )
7966 private URI myHost ;
8067
8168 /** The help flag. */
@@ -91,33 +78,10 @@ public final class JPv3 implements Callable<Integer> {
9178 private boolean myLogsAreVerbose ;
9279
9380 /** Creates a new JPv3 instance. */
94- public JPv3 () {
81+ private JPv3 () {
9582 // This is intentionally empty
9683 }
9784
98- /**
99- * Quickly finds a JSON property value.
100- *
101- * @param aFilePath A path to a JSON file
102- * @param aKey A JSON property key
103- * @return The optional property value, which will be empty if no value was found
104- * @throws IOException If there is trouble parsing the JSON in the supplied file
105- */
106- public static Optional <String > findValue (final Path aFilePath , final String aKey ) throws IOException {
107- final JsonFactory factory = new JsonFactory ();
108-
109- try (JsonParser parser = factory .createParser (aFilePath .toFile ())) {
110- while (!parser .isClosed ()) {
111- if (JsonToken .FIELD_NAME == parser .nextToken () && aKey .equals (parser .currentName ())) {
112- parser .nextToken (); // Increment the parser to the property value token
113- return Optional .ofNullable (parser .getValueAsString ());
114- }
115- }
116- }
117-
118- return Optional .empty ();
119- }
120-
12185 /**
12286 * Runs the jpv3 executable. This is just a toy at the moment. Expect it to fail.
12387 *
@@ -129,34 +93,13 @@ public static void main(final String[] anArgsArray) throws IOException {
12993 System .exit (new CommandLine (new JPv3 ()).execute (anArgsArray ));
13094 }
13195
132- /**
133- * Reads a particular IIIF Presentation JSON file of the supplied type.
134- *
135- * @param aPath A path to a JSON IIIF Presentation file
136- * @param aType A type of IIIF Presentation file
137- * @return The contents of the supplied file
138- * @throws IOException If there is trouble reading the JSON source file
139- */
140- @ SuppressWarnings (PMD .UNUSED_PRIVATE_METHOD )
141- private static String read (final Path aPath , final String aType ) throws IOException {
142- final String content = Files .readString (aPath );
143- return switch (aType ) {
144- case ResourceTypes .MANIFEST -> JSON .readValue (content , Manifest .class ).toString ();
145- case ResourceTypes .COLLECTION -> JSON .readValue (content , Collection .class ).toString ();
146- case ResourceTypes .ANNOTATION -> JSON .readValue (content , Annotation .class ).toString ();
147- case ResourceTypes .ANNOTATION_COLLECTION -> JSON .readValue (content , AnnotationCollection .class ).toString ();
148- case ResourceTypes .ANNOTATION_PAGE -> JSON .readValue (content , AnnotationPage .class ).toString ();
149- default -> LOGGER .getMessage (LOGGER .getMessage (MessageCodes .JPA_159 , aType ));
150- };
151- }
152-
15396 /** Runs the application. */
15497 @ Override
15598 @ SuppressWarnings ({ PMD .CYCLOMATIC_COMPLEXITY , PMD .COGNITIVE_COMPLEXITY })
15699 public Integer call () throws Exception {
157100 // Make sure we have a username and password if we're uploading the resulting ZIP file
158- if ((myAction .myUploadFlag || myAction .myPatchFlag ) && ( StringUtils . trimToNull ( myUsername ) == null ||
159- StringUtils .trimToNull (myPassword ) == null || myHost == null )) {
101+ if ((myAction .myUploadFlag || myAction .myPatchFlag ) &&
102+ ( StringUtils .trimToNull (myUsername ) == null || StringUtils . trimToNull ( myPassword ) == null )) {
160103 throw new CommandLine .ParameterException (new CommandLine (this ),
161104 LOGGER .getMessage (MessageCodes .JPA_174 , Constants .EOL ));
162105 }
0 commit comments