diff --git a/build.gradle b/build.gradle index 456ad072..875676ca 100644 --- a/build.gradle +++ b/build.gradle @@ -62,12 +62,14 @@ ext { isPublished = { project -> isJavaProject(project) && project.name != 'methanol-blackbox' && - project.name != 'methanol-samples' + project.name != 'methanol-samples' && + project.name != 'progress' } // project has tests that contribute to coverage ? contributesToTests = { project -> isJavaProject(project) && - !(project.name in ['methanol-benchmarks', 'methanol-testutils', 'methanol-samples']) + !(project.name in + ['methanol-benchmarks', 'methanol-testutils', 'methanol-samples', 'progress']) } // project is included in coverage report ? contributesToCoverageReport = { project -> diff --git a/methanol-samples/build.gradle b/methanol-samples/build.gradle index 5807f53f..d6781170 100644 --- a/methanol-samples/build.gradle +++ b/methanol-samples/build.gradle @@ -1,18 +1,5 @@ -plugins { - id 'java' - id 'org.openjfx.javafxplugin' version '0.0.8' -} - -javafx { - version = "14" - modules 'javafx.controls' -} - -dependencies { - implementation project(':methanol') -} - -task runDownloadProgressSample(type: org.javamodularity.moduleplugin.tasks.ModularJavaExec) { - classpath = sourceSets.main.runtimeClasspath - main = "$moduleName/com.github.mizosoft.methanol.samples.DownloadProgress" +subprojects { + dependencies { + implementation project(':methanol') + } } diff --git a/methanol-samples/progress/build.gradle b/methanol-samples/progress/build.gradle new file mode 100644 index 00000000..334b9648 --- /dev/null +++ b/methanol-samples/progress/build.gradle @@ -0,0 +1,18 @@ +plugins { + id 'org.openjfx.javafxplugin' version '0.0.8' +} + +javafx { + version = "14" + modules 'javafx.controls' +} + +task runDownloadProgress(type: org.javamodularity.moduleplugin.tasks.ModularJavaExec) { + classpath = sourceSets.main.runtimeClasspath + main = "$moduleName/com.github.mizosoft.methanol.samples.DownloadProgress" +} + +task runMultipartUploadProgress(type: org.javamodularity.moduleplugin.tasks.ModularJavaExec) { + classpath = sourceSets.main.runtimeClasspath + main = "$moduleName/com.github.mizosoft.methanol.samples.MultipartUploadProgress" +} diff --git a/methanol-samples/src/main/java/com/github/mizosoft/methanol/samples/DownloadProgress.java b/methanol-samples/progress/src/main/java/com/github/mizosoft/methanol/samples/DownloadProgress.java similarity index 99% rename from methanol-samples/src/main/java/com/github/mizosoft/methanol/samples/DownloadProgress.java rename to methanol-samples/progress/src/main/java/com/github/mizosoft/methanol/samples/DownloadProgress.java index 42745f9b..767e3e33 100644 --- a/methanol-samples/src/main/java/com/github/mizosoft/methanol/samples/DownloadProgress.java +++ b/methanol-samples/progress/src/main/java/com/github/mizosoft/methanol/samples/DownloadProgress.java @@ -72,10 +72,10 @@ public void start(Stage primaryStage) { gridPane.setVgap(10.d); gridPane.setPadding(new Insets(18.d)); - var notGrowable = new ColumnConstraints(); - notGrowable.setHgrow(Priority.NEVER); var growable = new ColumnConstraints(); growable.setHgrow(Priority.ALWAYS); + var notGrowable = new ColumnConstraints(); + notGrowable.setHgrow(Priority.NEVER); gridPane.getColumnConstraints().addAll(growable, notGrowable); gridPane.add(urlTextField, 0, 0); @@ -127,6 +127,6 @@ private static BodySubscriber ofContentLength(ResponseInfo info) { } public static void main(String[] args) { - Application.launch(args); + launch(args); } } diff --git a/methanol-samples/progress/src/main/java/com/github/mizosoft/methanol/samples/MultipartUploadProgress.java b/methanol-samples/progress/src/main/java/com/github/mizosoft/methanol/samples/MultipartUploadProgress.java new file mode 100644 index 00000000..e22f0b7c --- /dev/null +++ b/methanol-samples/progress/src/main/java/com/github/mizosoft/methanol/samples/MultipartUploadProgress.java @@ -0,0 +1,213 @@ +/* + * Copyright (c) 2019, 2020 Moataz Abdelnasser + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package com.github.mizosoft.methanol.samples; + +import com.github.mizosoft.methanol.Methanol; +import com.github.mizosoft.methanol.MoreBodyHandlers; +import com.github.mizosoft.methanol.MultipartBodyPublisher; +import com.github.mizosoft.methanol.MutableRequest; +import com.github.mizosoft.methanol.ProgressTracker; +import com.github.mizosoft.methanol.ProgressTracker.MultipartProgress; +import java.io.FileNotFoundException; +import java.io.UncheckedIOException; +import java.net.http.HttpResponse; +import java.net.http.HttpResponse.BodySubscribers; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.concurrent.CompletableFuture; +import java.util.stream.Collectors; +import javafx.application.Application; +import javafx.application.Platform; +import javafx.geometry.Insets; +import javafx.geometry.Pos; +import javafx.scene.Scene; +import javafx.scene.control.Button; +import javafx.scene.control.Label; +import javafx.scene.control.ProgressBar; +import javafx.scene.layout.ColumnConstraints; +import javafx.scene.layout.GridPane; +import javafx.scene.layout.HBox; +import javafx.scene.layout.Priority; +import javafx.stage.FileChooser; +import javafx.stage.Stage; + +public class MultipartUploadProgress extends Application { + private static final int WIDTH = 420; + private static final int HEIGHT = 120; + + private static final String URL = "https://httpbin.org/post"; + + private final Methanol client = Methanol.create(); + private final ProgressTracker tracker = + ProgressTracker.newBuilder() + .bytesTransferredThreshold(10 * 1024) // get an event every 10KB at least + .executor(Platform::runLater) // execute listener on JavaFX Application Thread + .build(); + + private final FileChooser fileChooser = new FileChooser(); + private final List addedFiles = new ArrayList<>(); + + private final ProgressBar mainProgressBar = new ProgressBar(0.d); + private final ProgressBar perFileProgressBar = new ProgressBar(0.d); + private final Label fileNameLabel = new Label("N/A"); + private final Button addFileButton = new Button("Add File"); + private final Button resetButton = new Button("Reset"); + private final Button uploadButton = new Button("Upload"); + private final List