|
| 1 | +package ee.cyber.cdoc2.converter; |
| 2 | + |
| 3 | +import ee.cyber.cdoc2.converter.util.CommonService; |
| 4 | +import ee.cyber.cdoc2.converter.util.Util; |
| 5 | +import org.slf4j.Logger; |
| 6 | +import org.slf4j.LoggerFactory; |
| 7 | +import org.digidoc4j.Container; |
| 8 | +import org.digidoc4j.ContainerOpener; |
| 9 | +import org.digidoc4j.ContainerValidationResult; |
| 10 | +import org.digidoc4j.DataFile; |
| 11 | +import picocli.CommandLine; |
| 12 | +import picocli.CommandLine.Command; |
| 13 | +import picocli.CommandLine.Option; |
| 14 | + |
| 15 | +import java.io.File; |
| 16 | +import java.nio.file.Path; |
| 17 | +import java.util.ArrayList; |
| 18 | +import java.util.List; |
| 19 | +import java.util.concurrent.Callable; |
| 20 | + |
| 21 | +@Command(name = "asic-convert") |
| 22 | +@SuppressWarnings("squid:S106") |
| 23 | +public class AsicConverterCmd implements Callable<Void> { |
| 24 | + |
| 25 | + private static final Logger log = LoggerFactory.getLogger(AsicConverterCmd.class); |
| 26 | + |
| 27 | + @Option(names = {"-f", "-a", "--asic" }, required = true, |
| 28 | + paramLabel = "ASIC", description = "the ASIC file") |
| 29 | + File asicFile; |
| 30 | + |
| 31 | + @Option(names = {"-o", "--out", "--cdoc2"}, required = false, |
| 32 | + paramLabel = "CDOC2", description = "the CDOC2 file") |
| 33 | + File cdoc2FileOption; |
| 34 | + |
| 35 | + @Option(names = { "--tmp"}, required = false, |
| 36 | + paramLabel = "DIR", |
| 37 | + description = "temp directory used for temporary files extracted from ASIC container") |
| 38 | + Path tempDir = null; |
| 39 | + |
| 40 | + @Option(names= {"--label"}, required = false, |
| 41 | + paramLabel = "label", description = "cdoc2 recipient label") |
| 42 | + String labelOption; |
| 43 | + |
| 44 | + @Option(names = { "-h", "--help" }, usageHelp = false, description = "display a help message") |
| 45 | + private boolean helpRequested = false; |
| 46 | + |
| 47 | + @Override |
| 48 | + public Void call() throws Exception { |
| 49 | + |
| 50 | + char[] password = CommonService.askPassword(); |
| 51 | + String label = CommonService.getLabel(labelOption); |
| 52 | + |
| 53 | + File cdoc2File = CommonService.getCdoc2File(cdoc2FileOption, asicFile); |
| 54 | + |
| 55 | + if (!asicFile.getPath().contains(".asic")) { |
| 56 | + throw new CommandLine.TypeConversionException( |
| 57 | + "Missing required file extension .asice for conversion" |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + encrypt(cdoc2File, asicFile, label, password); |
| 62 | + System.out.println("Created cdoc2 " + cdoc2File.getAbsolutePath()); |
| 63 | + |
| 64 | + return null; |
| 65 | + } |
| 66 | + |
| 67 | + private void encrypt(File cdoc2OutFile, File incomingFile, String label, char[] password) |
| 68 | + throws Exception{ |
| 69 | + |
| 70 | + Container container = ContainerOpener.open(incomingFile.getPath()); |
| 71 | + ContainerValidationResult result = container.validate(); |
| 72 | + |
| 73 | + boolean isContainerValid = result.isValid(); |
| 74 | + if (!isContainerValid) { |
| 75 | + Util.encrypt(cdoc2OutFile, List.of(incomingFile), label, password); |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + List<File> extractedFiles = checkSignatureOrExtractFiles(container, incomingFile); |
| 80 | + Util.encrypt(cdoc2OutFile, extractedFiles, label, password); |
| 81 | + } |
| 82 | + |
| 83 | + private List<File> checkSignatureOrExtractFiles( |
| 84 | + Container container, |
| 85 | + File incomingFile |
| 86 | + ) { |
| 87 | + if (!container.getSignatures().isEmpty()) { |
| 88 | + return List.of(incomingFile); |
| 89 | + } else { |
| 90 | + List<DataFile> dataFiles = container.getDataFiles(); |
| 91 | + List<File> extractedFiles = new ArrayList<>(); |
| 92 | + for (DataFile dataFile : dataFiles) { |
| 93 | + String incomingFilePath = incomingFile.getPath(); |
| 94 | + String outFilePath = |
| 95 | + incomingFilePath.substring(0, incomingFilePath.lastIndexOf("/") + 1); |
| 96 | + String extractedFile = outFilePath + "/" + dataFile.getName(); |
| 97 | + dataFile.saveAs(extractedFile); |
| 98 | + extractedFiles.add(new File(extractedFile)); |
| 99 | + } |
| 100 | + return extractedFiles; |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments