Skip to content

Commit

Permalink
Merge pull request #185 from solver-it-sro/running-autogram-in-CLI-mode
Browse files Browse the repository at this point in the history
Running autogram in cli mode
  • Loading branch information
celuchmarek authored Jul 6, 2023
2 parents 07cf66b + 7c84fb9 commit 415d439
Show file tree
Hide file tree
Showing 39 changed files with 1,001 additions and 139 deletions.
22 changes: 22 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,28 @@
// this is because of some escaping problem with zsh
"console": "externalTerminal"
}
},
{
"type": "java",
"name": "Autogram CLI",
"request": "launch",
"mainClass": "digital.slovensko.autogram.Main",
"projectName": "autogram",
"vmArgs": "--add-exports javafx.graphics/com.sun.javafx.tk=ALL-UNNAMED",
"args": "${input:commandLineArguments}",
"osx": {
// this is because of some escaping problem with zsh
"console": "integratedTerminal"
}
},

],
"inputs": [
{
"id": "commandLineArguments",
"type": "promptString",
"description": "Please enter command line arguments",
"default": "--cli --source src/test/resources/digital/slovensko/autogram/crystal_test_data/rozhodnutie_X4564-2.pdf --target target/out.pdf"
}
]
}
19 changes: 19 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,25 @@
},
"problemMatcher": [],
"type": "shell"
},
{
"label": "Run Autogram CLI",
"command": "./mvnw exec:java -Dexec.mainClass=\"digital.slovensko.autogram.Main\" -Dexec.args=\"${input:commandLineArguments}\"",
"type": "shell",
"problemMatcher": [],
"options": {
"env": {
"JAVA_HOME": "${config:java.jdt.ls.java.home}"
}
}
}
],
"inputs": [
{
"id": "commandLineArguments",
"type": "promptString",
"description": "Please enter command line arguments",
"default": "--cli"
}
]
}
8 changes: 8 additions & 0 deletions DEVELOPER.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Info for developers

# Trying out CLI mode

Useful command how to run project from CLI.

```bash
./mvnw exec:java -Dexec.mainClass="digital.slovensko.autogram.Main" -Dexec.args="--cli ..."
```

# More info about inner workings of builds for MacOS

To run signed mac build add follwing to `.vscode/settings.json` (or you can do unsigned build by setting `mac.sign=0` in `build.properties`)
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Autogram

Autogram je multi-platformová (Windows, MacOS, Linux) desktopová JavaFX aplikácia, ktorá slúži na podpisovanie dokumentov v súlade s eIDAS reguláciou. Používateľ ňou môže podpisovať súbory priamo alebo je možné aplikáciu jednoducho zaintegrovať do vlastného (webového) informačného systému pomocou HTTP API.
Autogram je multi-platformová (Windows, MacOS, Linux) desktopová JavaFX aplikácia, ktorá slúži na podpisovanie dokumentov v súlade s eIDAS reguláciou. Používateľ ňou môže podpisovať súbory priamo alebo je možné aplikáciu jednoducho zaintegrovať do vlastného (webového) informačného systému pomocou HTTP API. Podpisovanie je možné spúšať aj z príkazového riadku, čo je vhodné pre hromadné podpisovanie veľkého množstva súborov naraz.

**Inštalačné balíky pre Windows, MacOS a Linux sú dostupné v časti [Releases](https://github.com/slovensko-digital/autogram/releases).** Na použitie na existujúcich štátnych weboch bude potrebné doinštalovať aj [rozšírenie do prehliadača](https://github.com/slovensko-digital/autogram-extension#readme).

Expand All @@ -12,6 +12,10 @@ Swagger dokumentácia pre HTTP API je [dostupná na githube](https://generator3.

Vyvolať spustenie programu je možné priamo z webového prehliadača otvorením adresy so špeciálnym protokolom `autogram://`. Napríklad cez `autogram://go`.

## Konzolový mód

Autogram je možné spúšťať aj z príkazového riadku (CLI mód), ktoré umožňuje aj hromadné podpisovanie súborov. Detailné informácie o prepínačoch je sú popísané v nápovede po spustení `autogram --help`

### Štýlovanie

Aplikácia momentálne podporuje len jeden štýl - štátny IDSK dizajn. Ďalšie štýly sú plánované. Štýlovanie sa však už teraz deje výhradne cez kaskádové štýly, viď [idsk.css](https://github.com/slovensko-digital/autogram/blob/main/src/main/resources/digital/slovensko/autogram/ui/gui/idsk.css)
Expand Down
54 changes: 39 additions & 15 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

Expand All @@ -24,7 +24,9 @@
<slf4j.version>2.0.7</slf4j.version>
<junit.version>5.9.3</junit.version>
<mockito.version>5.4.0</mockito.version>
<commons-cli.version>1.5.0</commons-cli.version>
<xmlunit.version>2.9.1</xmlunit.version>
<jimfs.version>1.2</jimfs.version>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -113,6 +115,11 @@
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>${commons-cli.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.xmlunit/xmlunit-core -->
<dependency>
<groupId>org.xmlunit</groupId>
Expand All @@ -126,6 +133,12 @@
<version>${xmlunit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.jimfs</groupId>
<artifactId>jimfs</artifactId>
<version>${jimfs.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -207,7 +220,7 @@

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.16.0</version>
</plugin>

Expand Down Expand Up @@ -259,8 +272,10 @@
<goal>download-single</goal>
</goals>
<configuration>
<url>https://cdn.jsdelivr.net/npm/pdfjs-dist@${pdfjs.version}/build/pdf.min.js</url>
<toDir>${project.resources[0].directory}/digital/slovensko/autogram/ui/gui/vendor/pdfjs</toDir>
<url>
https://cdn.jsdelivr.net/npm/pdfjs-dist@${pdfjs.version}/build/pdf.min.js</url>
<toDir>
${project.resources[0].directory}/digital/slovensko/autogram/ui/gui/vendor/pdfjs</toDir>
<skipIfExists>true</skipIfExists>
</configuration>
</execution>
Expand All @@ -271,8 +286,10 @@
<goal>download-single</goal>
</goals>
<configuration>
<url>https://cdn.jsdelivr.net/npm/pdfjs-dist@${pdfjs.version}/build/pdf.worker.min.js</url>
<toDir>${project.resources[0].directory}/digital/slovensko/autogram/ui/gui/vendor/pdfjs</toDir>
<url>
https://cdn.jsdelivr.net/npm/pdfjs-dist@${pdfjs.version}/build/pdf.worker.min.js</url>
<toDir>
${project.resources[0].directory}/digital/slovensko/autogram/ui/gui/vendor/pdfjs</toDir>
<skipIfExists>true</skipIfExists>
</configuration>
</execution>
Expand All @@ -285,7 +302,8 @@
<configuration>
<url>https://cdn.jsdelivr.net/npm/pdfjs-dist@${pdfjs.version}</url>
<fromDir>cmaps</fromDir>
<toDir>${project.resources[0].directory}/digital/slovensko/autogram/ui/gui/vendor/pdfjs/cmaps</toDir>
<toDir>
${project.resources[0].directory}/digital/slovensko/autogram/ui/gui/vendor/pdfjs/cmaps</toDir>
<!-- <skip>true</skip> -->
</configuration>
</execution>
Expand Down Expand Up @@ -321,7 +339,8 @@
<goal>jdeps</goal>
</goals>
<configuration>
<!-- The output of jdeps is currently not used anywhere, modules are defined manually -->
<!-- The output of jdeps is currently not used anywhere, modules are
defined manually -->
<skip>true</skip>
<output>${project.build.directory}${file.separator}jdeps.out</output>
<options>
Expand All @@ -332,7 +351,8 @@
<option>${java.version}</option>
<option>-f</option>
<option>.*jar</option>
<option>${project.build.directory}${file.separator}${project.build.finalName}.jar</option>
<option>
${project.build.directory}${file.separator}${project.build.finalName}.jar</option>
</options>
</configuration>
</execution>
Expand Down Expand Up @@ -363,7 +383,8 @@
<module>javafx.fxml</module>
<module>javafx.graphics</module>
<module>javafx.web</module>
<!-- Needed for sun.misc.Unsafe used by JavaFX, shouldn't be used in our code -->
<!-- Needed for sun.misc.Unsafe used by JavaFX, shouldn't be used in
our code -->
<module>jdk.unsupported</module>
<module>jdk.httpserver</module>
</addModules>
Expand Down Expand Up @@ -409,13 +430,16 @@
<version>3.1.0</version>
<configuration>
<executable>bash</executable>
<workingDirectory>${project.build.scriptSourceDirectory}${file.separator}resources</workingDirectory>
<workingDirectory>
${project.build.scriptSourceDirectory}${file.separator}resources</workingDirectory>
<arguments>
<argument>${project.build.scriptSourceDirectory}${file.separator}package.sh</argument>
<argument>${jlink.jdk.path}${file.separator}bin${file.separator}jpackage</argument>
<argument>${project.build.directory}${file.separator}${project.artifactId}-${project.version}-${platform}</argument>
<argument>
${project.build.directory}${file.separator}${project.artifactId}-${project.version}-${platform}</argument>
<argument>${project.build.directory}${file.separator}preparedJDK</argument>
<argument>${project.basedir}${file.separator}src${file.separator}main${file.separator}resources${file.separator}digital${file.separator}slovensko${file.separator}autogram</argument>
<argument>
${project.basedir}${file.separator}src${file.separator}main${file.separator}resources${file.separator}digital${file.separator}slovensko${file.separator}autogram</argument>
<argument>${platform}</argument>
<argument>${project.version}</argument>
<argument>${project.build.directory}</argument>
Expand Down Expand Up @@ -492,4 +516,4 @@
</properties>
</profile>
</profiles>
</project>
</project>
9 changes: 2 additions & 7 deletions src/main/java/digital/slovensko/autogram/Main.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
package digital.slovensko.autogram;

import digital.slovensko.autogram.ui.gui.GUIApp;
import javafx.application.Application;

import java.util.Arrays;
import digital.slovensko.autogram.core.AppStarter;

import static java.util.Objects.requireNonNullElse;

public class Main {
public static void main(String[] args) {
System.out.println("Starting with args: " + Arrays.toString(args));

Application.launch(GUIApp.class, args);
AppStarter.start(args);
}

public static String getVersion() {
Expand Down
77 changes: 77 additions & 0 deletions src/main/java/digital/slovensko/autogram/core/AppStarter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package digital.slovensko.autogram.core;

import digital.slovensko.autogram.ui.cli.CliApp;
import digital.slovensko.autogram.ui.gui.GUIApp;
import javafx.application.Application;
import org.apache.commons.cli.*;

import java.io.PrintWriter;

public class AppStarter {
private static final Options options = new Options().
addOptionGroup(new OptionGroup().
addOption(new Option(null, "url", true, "Start in GUI mode with API server listening on given port and protocol (HTTP/HTTPS). Application starts minimised when is not empty.")).
addOption(new Option("c", "cli", false, "Run application in CLI mode."))
).
addOption("h", "help", false, "Print this command line help.").
addOption("u", "usage", false, "Print usage examples.").
addOption("s", "source", true, "Source file or directory of files to sign.").
addOption("t", "target", true, "Target file or directory for signed files. Type (file/directory) must match the source.").
addOption("f", "force", false, "Overwrite existing file(s).").
addOption(null, "pdfa", false, "Check PDF/A compliance before signing.").
addOption(null, "parents", false, "Create all parent directories for target if needed.").
addOption("d", "driver", true, "PCKS driver for signing. Supported drivers: eid, secure_store, monet, gemalto.");

public static void start(String[] args) {
try {
CommandLine cmd = new DefaultParser().parse(options, args);

if (cmd.hasOption("h")) {
printHelp();
} else if (cmd.hasOption("u")) {
printUsage();
} else if (cmd.hasOption("c")) {
CliApp.start(cmd);
} else {
Application.launch(GUIApp.class, args);
}
} catch (ParseException e) {
System.err.println("Unable to parse program args");
System.err.println(e);
}
}

public static void printHelp() {
final HelpFormatter formatter = new HelpFormatter();
final String syntax = "autogram";
final String footer = """
In CLI mode, signed files are saved with the same name as the source file, but with the suffix "_signed" if no target is specified. If the source is a directory, the target must also be a directory. If the source is a file, the target must also be a file. If the source is a driectory and no target is specified, a target directory is created with the same name as the source directory, but with the suffix "_signed".
If no target is specified and generated target name already exists, number is added to the target's name suffix if --force is not enabled. For example, if the source is "file.pdf" and the target is not specified, the target will be "file_signed.pdf". If the target already exists, the target will be "file_signed (1).pdf". If that target already exists, the target will be "file_signed (2).pdf", and so on.
If --force is enabled, the target will be overwritten if it already exists.
If target is specified with missing parent directories, they are created onyl if --parents is enabled. Otherwise, the signing fails. For example, if the source is "file.pdf" and the target is "target/file_signed.pdf", the target directory "target" must exist. If it does not exist, the signing fails. If --parents is enabled, the target directory "target" is created if it does not exist.
""";

formatter.printHelp(80, syntax, "", options, footer, true);
}

public static void printUsage() {
final HelpFormatter formatter = new HelpFormatter();
final String syntax = """
autogram [options]
autogram --url=http://localhost:32700
autogram --cli [options]
autogram --cli -s target/directory-example/file-example.pdf -t target/output-example/out-example.pdf
autogram --cli -s target/directory-example -t target/output-example -f
autogram --cli -s target/directory-example -t target/non-existent-dir/output-example --parents
autogram --cli -s target/directory-example/file-example.pdf -pdfa
autogram --cli -s target/directory-example/file-example.pdf -d eid
""";
final PrintWriter pw = new PrintWriter(System.out);
formatter.printUsage(pw, 80, syntax);
pw.flush();
}
}
21 changes: 11 additions & 10 deletions src/main/java/digital/slovensko/autogram/core/Autogram.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,19 @@ public Autogram(UI ui, DriverDetector driverDetector) {
}

public void sign(SigningJob job) {
ui.onUIThreadDo(() -> ui.startSigning(job, this));

if (job.shouldCheckPDFCompliance()) {
ui.onWorkThreadDo(() -> checkPDFACompliance(job));
}
ui.onUIThreadDo(()
-> ui.startSigning(job, this));
}

private void checkPDFACompliance(SigningJob job) {
var result = new PDFAStructureValidator().validate(job.getDocument());
if (!result.isCompliant()) {
ui.onUIThreadDo(() -> ui.onPDFAComplianceCheckFailed(job));
}
public void checkPDFACompliance(SigningJob job) {
if(!job.shouldCheckPDFCompliance()) return;

ui.onWorkThreadDo(() -> {
var result = new PDFAStructureValidator().validate(job.getDocument());
if (!result.isCompliant()) {
ui.onUIThreadDo(() -> ui.onPDFAComplianceCheckFailed(job));
}
});
}

public void startVisualization(SigningJob job) {
Expand Down
Loading

0 comments on commit 415d439

Please sign in to comment.