View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5    *  tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    *
8    *  All Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
19   */
20  package org.waarp.openr66.context.task.javatask;
21  
22  import org.waarp.common.database.exception.WaarpDatabaseException;
23  import org.waarp.common.digest.FilesystemBasedDigest;
24  import org.waarp.common.digest.FilesystemBasedDigest.DigestAlgo;
25  import org.waarp.common.logging.WaarpLogger;
26  import org.waarp.common.logging.WaarpLoggerFactory;
27  import org.waarp.openr66.context.task.AbstractExecJavaTask;
28  
29  import java.io.IOException;
30  import java.util.regex.Matcher;
31  import java.util.regex.Pattern;
32  
33  /**
34   * Add a digest in the TransferInformation to the current Task.</br>
35   * This should be called on caller side in pre-task since the transfer
36   * information will be transfered just
37   * after.</br>
38   * The second argument is -digest followed by one of ADLER32 CRC32 MD2 MD5 SHA1
39   * SHA256 SHA384 SHA512, default
40   * being MD5.</br>
41   * The third argument, optional, is "-format" followed by a string containing
42   * "#DIGEST#" to be replaced by the
43   * digest and starting with - or +, meaning this will be added at the beginning
44   * or the end of the generated
45   * new string. Default is equivalent to "-format -##DIGEST##".</br>
46   * </br>
47   * To be called as: <task><type>EXECJAVA</type><path>org.waarp.openr66.context.task.javatask.AddDigestJavaTask
48   * -digest ADLER32|CRC32|MD2|MD5|SHA1|SHA256|SHA384|SHA512 [-format
49   * (-/+)##DIGEST##]</path></task>
50   */
51  public class AddDigestJavaTask extends AbstractExecJavaTask {
52    /**
53     * Internal Logger
54     */
55    private static final WaarpLogger logger =
56        WaarpLoggerFactory.getLogger(AddDigestJavaTask.class);
57  
58    private static final String S_DIGEST = "#DIGEST#";
59    private static final Pattern DIGEST_PATTERN =
60        Pattern.compile(S_DIGEST, Pattern.LITERAL);
61  
62    @Override
63    public final void run() {
64      logger.debug("{}", this);
65      final String[] args = BLANK.split(fullarg);
66      final String fileInfo;
67      String format = "-##DIGEST##";
68      String algo = "MD5";
69      int way = -1;
70      for (int i = 0; i < args.length; i++) {
71        if ("-format".equalsIgnoreCase(args[i])) {
72          format = args[i + 1];
73          if (format.charAt(0) == '-') {
74            way = -1;
75            format = format.substring(1);
76          } else if (format.charAt(0) == '+') {
77            way = 1;
78            format = format.substring(1);
79          }
80          i++;
81        } else if ("-digest".equals(args[i])) {
82          algo = args[i + 1].toUpperCase();
83          i++;
84        }
85      }
86      final DigestAlgo digest;
87      try {
88        digest = DigestAlgo.valueOf(algo);
89      } catch (final Exception e) {
90        logger.error("Bad algorithm format: " + algo);
91        status = 3;
92        return;
93      }
94      final String key;
95      try {
96        key = FilesystemBasedDigest.getHex(
97            FilesystemBasedDigest.getHash(session.getFile().getTrueFile(), true,
98                                          digest));
99      } catch (final IOException e1) {
100       logger.error("Digest not correctly computed: " + algo + ": {}",
101                    e1.getMessage());
102       status = 4;
103       return;
104     }
105     logger.debug("Replace Digest in {} way: {} digest: {} key: {}", format, way,
106                  algo, key);
107     if (format.isEmpty()) {
108       fileInfo = session.getRunner().getFileInformation();
109     } else {
110       if (way < 0) {
111         fileInfo = DIGEST_PATTERN.matcher(format)
112                                  .replaceAll(Matcher.quoteReplacement(key)) +
113                    ' ' + session.getRunner().getFileInformation();
114       } else {
115         fileInfo = session.getRunner().getFileInformation() + ' ' +
116                    DIGEST_PATTERN.matcher(format)
117                                  .replaceAll(Matcher.quoteReplacement(key));
118       }
119     }
120     session.getRunner().setFileInformation(fileInfo);
121     session.getRunner().addToTransferMap("digest", key);
122     try {
123       session.getRunner().update();
124     } catch (final WaarpDatabaseException e) {
125       logger.error("Digest cannot be saved to fileInformation:" + fileInfo);
126       status = 2;
127       return;
128     }
129     logger.debug("Digest saved to fileInformation: {}", fileInfo);
130     status = 0;
131   }
132 }