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;
21  
22  import org.waarp.openr66.context.R66Session;
23  import org.waarp.openr66.protocol.exception.OpenR66ProtocolNetworkException;
24  import org.waarp.openr66.s3.WaarpR66S3Client;
25  import org.waarp.openr66.s3.taskfactory.S3TaskFactory;
26  import org.waarp.openr66.s3.taskfactory.S3TaskFactory.S3TaskType;
27  
28  import java.io.File;
29  
30  /**
31   * S3 PUT Task<br>
32   * <p>
33   * Result of arguments will be as a S3 PUT command.<br>
34   * Format is the following:<br>
35   * "-URL url of S3 service <br>
36   * -accessKey access Key for S3 service <br>
37   * -secretKey secret Key for S3 service <br>
38   * -bucketName bucket Name where to retrieve the object <br>
39   * -targetName source Name from the bucket to select the final Object <br>
40   * [-setTags 'name:value,...' without space]" <br>
41   * <br>
42   * <br>
43   * The order of actions will be:<br>
44   * 1) connection to S3 service using access Key and Secret Key<br>
45   * 2) Store to the bucket the current File as the target Object<br>
46   * 3) if setTags is set, the informations are added as tags to the final S3 Object<br>
47   * 4) the current File is unchanged<br>
48   */
49  public class S3PutTask extends S3AbstractTask {
50    private static final S3TaskFactory.S3TaskType taskType =
51        S3TaskFactory.S3TaskType.S3PUT;
52  
53    /**
54     * Constructor
55     *
56     * @param argRule
57     * @param delay
58     * @param argTransfer
59     * @param session
60     */
61    public S3PutTask(final String argRule, final int delay,
62                     final String argTransfer, final R66Session session) {
63      super(TaskType.EXTENDED, delay, argRule, argTransfer, session);
64    }
65  
66    /**
67     * The order of actions will be:<br>
68     * 1) connection to S3 service using access Key and Secret Key<br>
69     * 2) Store to the bucket the current File as the target Object<br>
70     * 3) if setTags is set, the informations are added as tags to the final S3 Object<br>
71     * 4) the current File is unchanged<br>
72     */
73    @Override
74    public void run() {
75      try {
76        internalRun();
77        futureCompletion.setSuccess();
78      } catch (final OpenR66ProtocolNetworkException e) {
79        finalizeInError(e, "Error while S3 Action");
80      }
81    }
82  
83    void internalRun() throws OpenR66ProtocolNetworkException {
84      if (!getParams()) {
85        throw new OpenR66ProtocolNetworkException(
86            "Error while Parameters parsing");
87      }
88      final WaarpR66S3Client s3Client =
89          new WaarpR66S3Client(taskUtil.getAccessKey(), taskUtil.getSecretKey(),
90                               taskUtil.getUrl());
91      final File file = session.getFile().getTrueFile();
92      s3Client.createFile(taskUtil.getBucketName(), taskUtil.getTargetName(),
93                          file, taskUtil.getSetTags());
94      logger.debug("PUT {} to {}", file.getAbsolutePath(),
95                   taskUtil.getTargetName());
96    }
97  
98    @Override
99    public S3TaskType getS3TaskType() {
100     return taskType;
101   }
102 }