1 /*
2  * Copyright 2013-2020 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      https://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.cloud.aws.core.io.s3;
18
19 import com.amazonaws.services.s3.AmazonS3;
20
21 import org.springframework.beans.BeansException;
22 import org.springframework.beans.factory.BeanFactory;
23 import org.springframework.beans.factory.BeanFactoryAware;
24 import org.springframework.beans.factory.InitializingBean;
25 import org.springframework.cloud.aws.core.support.documentation.RuntimeUse;
26 import org.springframework.core.io.ProtocolResolver;
27 import org.springframework.core.io.Resource;
28 import org.springframework.core.io.ResourceLoader;
29 import org.springframework.core.task.SyncTaskExecutor;
30 import org.springframework.core.task.TaskExecutor;
31
32 /**
33  * @author Agim Emruli
34  * @author Alain Sahli
35  * @since 1.0
36  */

37 public class SimpleStorageProtocolResolver
38         implements ProtocolResolver, InitializingBean, BeanFactoryAware {
39
40     private AmazonS3 amazonS3;
41
42     /**
43      * <b>IMPORTANT:</b> If a task executor is set with an unbounded queue there will be a
44      * huge memory consumption. The reason is that each multipart of 5MB will be put in
45      * the queue to be uploaded. Therefore a bounded queue is recommended.
46      */

47     private TaskExecutor taskExecutor;
48
49     private BeanFactory beanFactory;
50
51     public SimpleStorageProtocolResolver() {
52     }
53
54     public SimpleStorageProtocolResolver(AmazonS3 amazonS3) {
55         this.amazonS3 = AmazonS3ProxyFactory.createProxy(amazonS3);
56     }
57
58     @RuntimeUse
59     public void setTaskExecutor(TaskExecutor taskExecutor) {
60         this.taskExecutor = taskExecutor;
61     }
62
63     @Override
64     public void afterPropertiesSet() {
65         if (this.taskExecutor == null) {
66             this.taskExecutor = new SyncTaskExecutor();
67         }
68     }
69
70     @Override
71     public Resource resolve(String location, ResourceLoader resourceLoader) {
72         if (SimpleStorageNameUtils.isSimpleStorageResource(location)) {
73             return new SimpleStorageResource(this.getAmazonS3(),
74                     SimpleStorageNameUtils.getBucketNameFromLocation(location),
75                     SimpleStorageNameUtils.getObjectNameFromLocation(location),
76                     this.taskExecutor,
77                     SimpleStorageNameUtils.getVersionIdFromLocation(location));
78         }
79         else {
80             return null;
81         }
82     }
83
84     public AmazonS3 getAmazonS3() {
85         if (this.amazonS3 == null) {
86             this.amazonS3 = AmazonS3ProxyFactory
87                     .createProxy(this.beanFactory.getBean(AmazonS3.class));
88         }
89         return this.amazonS3;
90     }
91
92     @Override
93     public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
94         this.beanFactory = beanFactory;
95     }
96
97 }
98