From e1263a2f50a0f8b20f85974f53587a275118af30 Mon Sep 17 00:00:00 2001 From: FriedrichFroebel Date: Mon, 18 May 2020 10:45:45 +0200 Subject: [PATCH] send correct log type; check log posting response --- README.md | 6 +++--- build.gradle | 20 ++++++++++++++++++- src/main/java/cmanager/gui/CopyLogDialog.java | 13 +++++++++--- src/main/java/cmanager/okapi/OKAPI.java | 19 ++++++++++++------ .../okapi/responses/UnexpectedLogStatus.java | 17 ++++++++++++++++ 5 files changed, 62 insertions(+), 13 deletions(-) create mode 100644 src/main/java/cmanager/okapi/responses/UnexpectedLogStatus.java diff --git a/README.md b/README.md index 251c8cf..62efea2 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ To create a JAR file, run `gradle jar` (or `./gradlew jar`). The JAR file will b There is experimental support for bundling the application in a way that no local Java installation is needed for executing it. -To create the corresponding image, run `gradle jpackageImage` (or `./gradlew jpackageImage`). The image will be available inside the `build/jpackage/cmanager` directory. You might want to put this directory into a dedicated archive file for redistribution. **Please note that this an incubating feature of Java 14, so at least Java 14 is required and might break due to API changes.** +To create the corresponding image, run `gradle jpackageImage` (or `./gradlew jpackageImage`). The image will be available inside the `build/jpackage/cmanager` directory. You might want to put this directory into a dedicated archive file for redistribution using the `jpackageImageZip` task. **Please note that this an incubating feature of Java 14, so at least Java 14 is required and this feature might break due to API changes.** ## Usage @@ -54,8 +54,8 @@ Run `gradle run` (or `./gradlew run`) from the root directory of the Git reposit ### Starting the application JAR file -Run `java -jar cm-0.2.47.jar` from the directory containing the JAR file. +Run `java -jar cm-0.2.48.jar` from the directory containing the JAR file. ### Starting the Java-independent package -Double-click on `cmanager.exe` from the directory containing this package. *Please note that this version is not being distributed in the release section at the moment.* +Double-click on `cmanager.exe` (on Windows) from the directory containing this package. *Please note that this version is not being distributed in the release section at the moment.* diff --git a/build.gradle b/build.gradle index 8a8fca0..d2b15ba 100644 --- a/build.gradle +++ b/build.gradle @@ -54,7 +54,7 @@ project.ext.ocOkapiPropertiesFile = projectDir.getPath() + "${File.separator}oc_ sourceCompatibility = 1.8 targetCompatibility = 1.8 -version = '0.2.47' +version = '0.2.48' wrapper { gradleVersion = '6.4' @@ -155,3 +155,21 @@ runtime { skipInstaller = true } } + +// Retrieve the system string for indicating the package content. +def getSystemString() { + def osName = System.properties['os.name'].toLowerCase() + if (osName.contains('windows')) return 'windows'; + return osName; +} + +// Provide a dedicated task to create a ZIP file of the `jpackage` image. +task jpackageImageZip(type: Zip) { + group = 'Build' + description = 'Bundles the jpackage image as a ZIP file.' + from "${buildDir}/jpackage/cmanager" + include '**/*' + archiveName "cmanager-${version}_${getSystemString()}.zip" + destinationDir file("${buildDir}/jpackage/") +} +jpackageImageZip.dependsOn jpackageImage diff --git a/src/main/java/cmanager/gui/CopyLogDialog.java b/src/main/java/cmanager/gui/CopyLogDialog.java index ef1233a..f4c82ef 100644 --- a/src/main/java/cmanager/gui/CopyLogDialog.java +++ b/src/main/java/cmanager/gui/CopyLogDialog.java @@ -6,6 +6,7 @@ import cmanager.oc.ShadowList; import cmanager.okapi.OKAPI; import cmanager.okapi.User; +import cmanager.okapi.responses.UnexpectedLogStatus; import cmanager.settings.Settings; import java.awt.BorderLayout; import java.awt.Color; @@ -123,10 +124,16 @@ public void run() { // copy the log OKAPI.postLog(User.getOKAPIUser(), oc, log); - // remember that we copied the log so the - // user can not - // double post it by accident + // remember that we copied the log so the user can + // not double post it by accident logsCopied.add(log); + } catch (UnexpectedLogStatus exception) { + // Handle general log problems separately to provide + // a better error message. + ExceptionPanel.showErrorDialog( + THIS, + exception.getResponseMessage(), + "Unexpected log status"); } catch (Throwable t) { ExceptionPanel.showErrorDialog(THIS, t); } diff --git a/src/main/java/cmanager/okapi/OKAPI.java b/src/main/java/cmanager/okapi/OKAPI.java index 88bc367..2791ac1 100644 --- a/src/main/java/cmanager/okapi/OKAPI.java +++ b/src/main/java/cmanager/okapi/OKAPI.java @@ -14,7 +14,9 @@ import cmanager.okapi.responses.CachesAroundDocument; import cmanager.okapi.responses.ErrorDocument; import cmanager.okapi.responses.FoundStatusDocument; +import cmanager.okapi.responses.LogSubmissionDocument; import cmanager.okapi.responses.UUIDDocument; +import cmanager.okapi.responses.UnexpectedLogStatus; import cmanager.okapi.responses.UsernameDocument; import cmanager.xml.Element; import cmanager.xml.Parser; @@ -353,7 +355,8 @@ public static String getUsername(TokenProviderI tp) } public static void postLog(TokenProviderI tp, Geocache cache, GeocacheLog log) - throws MalFormedException, InterruptedException, ExecutionException, IOException { + throws MalFormedException, InterruptedException, ExecutionException, IOException, + UnexpectedLogStatus { String url = BASE_URL + "/logs/submit" @@ -361,7 +364,7 @@ public static void postLog(TokenProviderI tp, Geocache cache, GeocacheLog log) + "&cache_code=" + URLEncoder.encode(cache.getCode(), "UTF-8") + "&logtype=" - + URLEncoder.encode("Found it", "UTF-8") + + URLEncoder.encode(log.getTypeStr(), "UTF-8") + "&comment=" + URLEncoder.encode(log.getText(), "UTF-8") + "&when=" @@ -375,11 +378,15 @@ public static void postLog(TokenProviderI tp, Geocache cache, GeocacheLog log) final String response = authedHttpGet(tp, url); - /*final LogSubmissionDocument document = new Gson().fromJson(response, LogSubmissionDocument.class); - if (document == null) return; + final LogSubmissionDocument document = + new Gson().fromJson(response, LogSubmissionDocument.class); + if (document == null) { + System.out.println("Problems with handling posted log. Response document is null."); + return; + } if (!document.isSuccess()) { - System.out.println("Error while logging cache: " + document.getMessage()); - }*/ + throw new UnexpectedLogStatus(document.getMessage()); + } } public static Coordinate getHomeCoordinates(TokenProviderI tp) diff --git a/src/main/java/cmanager/okapi/responses/UnexpectedLogStatus.java b/src/main/java/cmanager/okapi/responses/UnexpectedLogStatus.java new file mode 100644 index 0000000..fb8d51d --- /dev/null +++ b/src/main/java/cmanager/okapi/responses/UnexpectedLogStatus.java @@ -0,0 +1,17 @@ +package cmanager.okapi.responses; + +public class UnexpectedLogStatus extends Exception { + + private static final long serialVersionUID = -1132973286480626832L; + + private String responseMessage; + + public UnexpectedLogStatus(String responseMessage) { + super("Unexpected log status"); + this.responseMessage = responseMessage; + } + + public String getResponseMessage() { + return responseMessage; + } +}