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  
21  package org.waarp.vitam.ingest;
22  
23  import fr.gouv.vitam.common.exception.InvalidParseOperationException;
24  import fr.gouv.vitam.ingest.external.client.IngestExternalClient;
25  import org.apache.commons.cli.CommandLine;
26  import org.apache.commons.cli.CommandLineParser;
27  import org.apache.commons.cli.DefaultParser;
28  import org.apache.commons.cli.HelpFormatter;
29  import org.apache.commons.cli.Options;
30  import org.apache.commons.cli.ParseException;
31  import org.waarp.common.logging.WaarpLogger;
32  import org.waarp.common.logging.WaarpLoggerFactory;
33  import org.waarp.common.utility.Version;
34  import org.waarp.openr66.configuration.FileBasedConfiguration;
35  import org.waarp.openr66.context.task.AbstractExecJavaTask;
36  import org.waarp.openr66.protocol.configuration.Configuration;
37  import org.waarp.vitam.common.WaarpCommon;
38  import org.waarp.vitam.common.WaarpCommon.TaskOption;
39  
40  import java.io.File;
41  
42  /**
43   * IngestTask is a one shot command that will initiate one specific Ingest
44   * operation from a Waarp post task on reception.
45   */
46  public class IngestTask {
47    /**
48     * Internal Logger
49     */
50    private static final WaarpLogger logger =
51        WaarpLoggerFactory.getLogger(IngestTask.class);
52    private static File waarpConfigurationFile;
53    private static int statusMain;
54    private final TaskOption taskOption;
55    private final String contextId;
56    private final String action;
57    private final boolean checkAtr;
58    private final IngestRequestFactory factory;
59    private final IngestManager ingestManager;
60  
61    /**
62     * Unique constructor
63     *
64     * @param taskOption
65     * @param contextId
66     * @param action
67     * @param checkAtr
68     * @param factory
69     * @param ingestManager
70     */
71    public IngestTask(final TaskOption taskOption, final String contextId,
72                      final String action, final boolean checkAtr,
73                      final IngestRequestFactory factory,
74                      final IngestManager ingestManager) {
75      this.taskOption = taskOption;
76      this.contextId = contextId;
77      this.action = action;
78      this.checkAtr = checkAtr;
79      this.factory = factory;
80      this.ingestManager = ingestManager;
81    }
82  
83    /**
84     * Will try to start the IngestTask according to arguments, else print
85     * the help message
86     *
87     * @param args
88     */
89    public static void main(String[] args) {
90      Options options = getOptions();
91  
92      if (args.length == 0 || WaarpCommon.checkHelp(args)) {
93        printHelp(options);
94        statusMain = 2;
95        return;
96      }
97      final IngestTask ingestTask;
98      try {
99        ingestTask = getIngestTask(options, args);
100     } catch (ParseException e) {
101       logger
102           .error("Error while initializing " + IngestTask.class.getSimpleName(),
103                  e);
104       printHelp(options);
105       statusMain = 2;
106       return;
107     }
108     if (!FileBasedConfiguration
109         .setSubmitClientConfigurationFromXml(Configuration.configuration,
110                                              waarpConfigurationFile
111                                                  .getAbsolutePath())) {
112       logger.error("Cannot load Waarp Configuration");
113       statusMain = 2;
114       return;
115     }
116     statusMain = ingestTask.invoke();
117   }
118 
119   /**
120    * Define the options associated
121    *
122    * @return the Options
123    */
124   private static Options getOptions() {
125     Options options = new Options();
126     TaskOption.setStandardTaskOptions(options);
127     options.addOption("x", "context", true, "Context Id, shall be one" +
128                                             " of DEFAULT_WORKFLOW (default), " +
129                                             "HOLDING_SCHEME, FILING_SCHEME")
130            .addOption("k", "checkatr", false, "If set, after RequestId sent, " +
131                                               "will check for ATR if first step is ok")
132            .addOption("n", "action", true, "Action, shall be always RESUME");
133     options.addOption(IngestRequestFactory.getDirectoryOption());
134     return options;
135   }
136 
137   /**
138    * Helper to print help
139    *
140    * @param options
141    */
142   private static void printHelp(Options options) {
143     HelpFormatter formatter = new HelpFormatter();
144     formatter.printHelp(IngestTask.class.getSimpleName(),
145                         "Version: " + Version.fullIdentifier(), options,
146                         WaarpCommon.FOR_MANDATORY_ARGUMENTS, true);
147   }
148 
149   /**
150    * Build the IngestTask according to arguments
151    *
152    * @param options
153    * @param args
154    *
155    * @return the new IngestTask
156    *
157    * @throws ParseException
158    */
159   private static IngestTask getIngestTask(Options options, String[] args)
160       throws ParseException {
161     CommandLineParser parser = new DefaultParser();
162     CommandLine cmd = parser.parse(options, args);
163     IngestRequestFactory.parseDirectoryOption(cmd);
164     final String contextId = cmd.getOptionValue('x', "DEFAULT_WORKFLOW");
165     if (!IngestRequest.CONTEXT.checkCorrectness(contextId)) {
166       throw new ParseException(
167           "Context should be one of DEFAULT_WORKFLOW (Sip ingest), " +
168           "HOLDING_SCHEME (tree) or FILING_SCHEME");
169     }
170     final String action = cmd.getOptionValue('n', IngestRequest.RESUME);
171     TaskOption taskOption = TaskOption.getTaskOption(cmd, args);
172     waarpConfigurationFile = new File(taskOption.getWaarpConfigurationFile());
173     return new IngestTask(taskOption, contextId, action, cmd.hasOption('k'),
174                           IngestRequestFactory.getInstance(),
175                           new IngestManager());
176   }
177 
178   /**
179    * Launch the IngestMonitor
180    *
181    * @return 0 if OK, 1 if Warning, 2 if error
182    */
183   public int invoke() {
184     try (IngestExternalClient client = factory.getClient()) {
185       IngestRequest ingestRequest =
186           new IngestRequest(taskOption, contextId, action, checkAtr, factory);
187       return ingestManager.ingestLocally(factory, ingestRequest, client);
188     } catch (InvalidParseOperationException e) {
189       logger.error("Issue since IngestRequest cannot be saved", e);
190     }
191     return 2;
192   }
193 
194   /**
195    * Equivalent JavaTask
196    */
197   public static class JavaTask extends AbstractExecJavaTask {
198     @Override
199     public void run() {
200       String[] args;
201       if (!fullarg.contains("-s ") && !fullarg.contains("--session ")) {
202         try {
203           String key = this.session.getRunner().getKey();
204           args = BLANK.split(fullarg + " -s " + key);
205         } catch (Throwable e) {
206           args = BLANK.split(fullarg);
207         }
208       } else {
209         args = BLANK.split(fullarg);
210       }
211       main(args);
212       status = statusMain;
213     }
214   }
215 }