01 /*
02 * SPDX-License-Identifier: Apache-2.0
03 *
04 * Copyright 2019-2022 Andres Almiray.
05 *
06 * Licensed under the Apache License, Version 2.0 (the "License");
07 * you may not use this file except in compliance with the License.
08 * You may obtain a copy of the License at
09 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 package org.kordamp.gradle.plugin.oci.tasks.object
19
20 import com.oracle.bmc.objectstorage.ObjectStorageClient
21 import com.oracle.bmc.objectstorage.requests.PutObjectRequest
22 import com.oracle.bmc.objectstorage.responses.PutObjectResponse
23 import groovy.transform.CompileStatic
24 import org.kordamp.gradle.plugin.oci.tasks.AbstractOCITask
25 import org.kordamp.gradle.plugin.oci.tasks.interfaces.OCITask
26 import org.kordamp.gradle.plugin.oci.tasks.traits.BucketNameAwareTrait
27 import org.kordamp.gradle.plugin.oci.tasks.traits.FileAwareTrait
28 import org.kordamp.gradle.plugin.oci.tasks.traits.NamespaceNameAwareTrait
29 import org.kordamp.gradle.plugin.oci.tasks.traits.ObjectNameAwareTrait
30 import org.kordamp.gradle.plugin.oci.tasks.traits.OptionalContentEncodingAwareTrait
31 import org.kordamp.gradle.plugin.oci.tasks.traits.OptionalContentLanguageAwareTrait
32 import org.kordamp.gradle.plugin.oci.tasks.traits.OptionalContentMD5AwareTrait
33 import org.kordamp.gradle.plugin.oci.tasks.traits.OptionalContentTypeAwareTrait
34 import org.kordamp.jipsy.annotations.TypeProviderFor
35
36 import static org.kordamp.gradle.util.StringUtils.isNotBlank
37
38 /**
39 * @author Andres Almiray
40 * @since 0.3.0
41 */
42 @CompileStatic
43 @TypeProviderFor(OCITask)
44 class PutObjectTask extends AbstractOCITask implements NamespaceNameAwareTrait,
45 BucketNameAwareTrait,
46 ObjectNameAwareTrait,
47 FileAwareTrait,
48 OptionalContentMD5AwareTrait,
49 OptionalContentLanguageAwareTrait,
50 OptionalContentEncodingAwareTrait,
51 OptionalContentTypeAwareTrait {
52 static final String TASK_DESCRIPTION = 'Puts an Object on a Bucket.'
53
54 @Override
55 protected void doExecuteTask() {
56 validateNamespaceName()
57 validateBucketName()
58 validateObjectName()
59 validateFile()
60
61 File theFile = getResolvedFile().get().asFile
62 PutObjectRequest.Builder builder = PutObjectRequest.builder()
63 .namespaceName(getResolvedNamespaceName().get())
64 .bucketName(getResolvedBucketName().get())
65 .objectName(getResolvedObjectName().get())
66 .contentLength(theFile.size())
67 .putObjectBody(new FileInputStream(theFile))
68
69 String s = getResolvedContentEncoding().get()
70 if (isNotBlank(s)) {
71 builder = builder.contentEncoding(s)
72 }
73 s = getResolvedContentLanguage().get()
74 if (isNotBlank(s)) {
75 builder = builder.contentLanguage(s)
76 }
77 s = getResolvedContentMD5().get()
78 if (isNotBlank(s)) {
79 builder = builder.contentMD5(s)
80 }
81 s = getResolvedContentType().get()
82 if (isNotBlank(s)) {
83 builder = builder.contentType(s)
84 }
85
86 ObjectStorageClient client = createObjectStorageClient()
87 PutObjectResponse response = client.putObject(builder.build())
88
89 println(getResolvedObjectName().get() + ':')
90 printKeyValue('ETag', response.ETag, 1)
91 printKeyValue('Last Modified', response.lastModified, 1)
92 printKeyValue('Content MD5', response.opcContentMd5, 1)
93 }
94 }
|