View Javadoc

1   package org.kuali.maven.mojo.s3;
2   
3   import java.io.IOException;
4   import java.io.InputStream;
5   
6   import org.apache.maven.plugin.MojoExecutionException;
7   import org.apache.maven.settings.Server;
8   import org.springframework.core.io.DefaultResourceLoader;
9   import org.springframework.core.io.Resource;
10  import org.springframework.core.io.ResourceLoader;
11  
12  import com.amazonaws.auth.AWSCredentials;
13  import com.amazonaws.auth.BasicAWSCredentials;
14  import com.amazonaws.services.s3.internal.Mimetypes;
15  import com.amazonaws.services.s3.model.CannedAccessControlList;
16  import com.amazonaws.services.s3.model.ObjectMetadata;
17  import com.amazonaws.services.s3.model.PutObjectRequest;
18  
19  /**
20   * Abstract mojo containing S3 mojo essentials.
21   */
22  public abstract class S3Mojo extends BaseMojo {
23      /**
24       * Helper class for determining the mime types of objects.
25       */
26      Mimetypes mimeTypes = Mimetypes.getInstance();
27  
28      /**
29       * This is the id of the server from settings.xml containing Amazon AWS
30       * credentials.
31       *
32       * @parameter expression="${serverId}"
33       */
34      private String serverId;
35  
36      /**
37       * Only update the bucket hierarchy underneath this prefix.
38       *
39       * @parameter expression="${prefix}"
40       */
41      private String prefix;
42  
43      /**
44       * The delimiter used to organize keys into a hierarchy.
45       *
46       * @parameter expression="${delimiter}" default-value="/"
47       */
48      private String delimiter;
49  
50      /**
51       * Maximum number of keys to return per query.
52       *
53       * @parameter expression="${maxKeys}"
54       */
55      private Integer maxKeys;
56  
57      /**
58       * Amazon AWS Access Key Id. See also <code>serverId</code>.
59       *
60       * @parameter expression="${accessKeyId}"
61       */
62      private String accessKeyId;
63  
64      /**
65       * Amazon AWS Secret Access Key. See also <code>serverId</code>.
66       *
67       * @parameter expression="${secretAccessKey}"
68       */
69      private String secretAccessKey;
70  
71      /**
72       * The name of the bucket to update.
73       *
74       * @parameter expression="${bucket}"
75       * @required
76       */
77      private String bucket;
78  
79      protected ObjectMetadata getObjectMetadata(final String location,
80              final Resource resource) throws IOException {
81          ObjectMetadata om = new ObjectMetadata();
82          String contentType = mimeTypes.getMimetype(location);
83          om.setContentType(contentType);
84          om.setContentLength(resource.contentLength());
85          return om;
86      }
87  
88      protected PutObjectRequest getPutObjectRequest(final String location, final String key)
89              throws IOException {
90          ResourceLoader loader = new DefaultResourceLoader();
91          Resource resource = loader.getResource(location);
92          InputStream in = resource.getInputStream();
93          ObjectMetadata objectMetadata = getObjectMetadata(location, resource);
94          PutObjectRequest request = new PutObjectRequest(getBucket(), key, in,
95                  objectMetadata);
96          request.setCannedAcl(CannedAccessControlList.PublicRead);
97          return request;
98      }
99  
100     protected PutObjectRequest getPutObjectRequest(final String location)
101             throws IOException {
102         String key = location.substring(1);
103         return getPutObjectRequest(location, key);
104     }
105 
106     protected String getAuthenticationErrorMessage() {
107         StringBuffer sb = new StringBuffer();
108         sb.append("\n\nError: accessKeyId and secretAccessKey must be provided.\n");
109         sb.append("Provide them in the plugin configuration or specify them on the command line:\n\n");
110         sb.append("-DaccessKeyId=XXXX\n");
111         sb.append("-DsecretAccessKey=XXXX\n");
112         sb.append("\n");
113         sb.append("You can also provide them in settings.xml as username and password:\n\n");
114         sb.append("<server>\n");
115         sb.append("  <id>[serverId]</id>\n");
116         sb.append("  <username>[AWS Access Key ID]</username>\n");
117         sb.append("  <password>[AWS Secret Access Key]</password>\n");
118         sb.append("</server>\n\n.\n");
119         return sb.toString();
120     }
121 
122     protected void updateCredentials() {
123         if (getServerId() == null) {
124             return;
125         }
126         Server server = getSettings().getServer(getServerId());
127         if (getAccessKeyId() == null) {
128             setAccessKeyId(server.getUsername());
129         }
130         if (getSecretAccessKey() == null) {
131             setSecretAccessKey(server.getPassword());
132         }
133     }
134 
135     protected void validateCredentials() throws MojoExecutionException {
136         if (getAccessKeyId() == null || getSecretAccessKey() == null) {
137             throw new MojoExecutionException(getAuthenticationErrorMessage());
138         }
139     }
140 
141     protected AWSCredentials getCredentials() throws MojoExecutionException {
142         return new BasicAWSCredentials(getAccessKeyId(), getSecretAccessKey());
143     }
144 
145     public String getAccessKeyId() {
146         return accessKeyId;
147     }
148 
149     public void setAccessKeyId(final String accessKeyId) {
150         this.accessKeyId = accessKeyId;
151     }
152 
153     public String getSecretAccessKey() {
154         return secretAccessKey;
155     }
156 
157     public void setSecretAccessKey(final String secretAccessKey) {
158         this.secretAccessKey = secretAccessKey;
159     }
160 
161     public String getBucket() {
162         return bucket;
163     }
164 
165     public void setBucket(final String bucket) {
166         this.bucket = bucket;
167     }
168 
169     public String getPrefix() {
170         return prefix;
171     }
172 
173     public void setPrefix(final String prefix) {
174         this.prefix = prefix;
175     }
176 
177     public String getDelimiter() {
178         return delimiter;
179     }
180 
181     public void setDelimiter(final String delimiter) {
182         this.delimiter = delimiter;
183     }
184 
185     public Integer getMaxKeys() {
186         return maxKeys;
187     }
188 
189     public void setMaxKeys(final Integer maxKeys) {
190         this.maxKeys = maxKeys;
191     }
192 
193     public String getServerId() {
194         return serverId;
195     }
196 
197     public void setServerId(final String serverId) {
198         this.serverId = serverId;
199     }
200 }