| 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 | |
|
| 21 | |
|
| 22 | 0 | public abstract class S3Mojo extends BaseMojo { |
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | 0 | Mimetypes mimeTypes = Mimetypes.getInstance(); |
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
private String serverId; |
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
private String prefix; |
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
private String delimiter; |
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
private Integer maxKeys; |
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
private String accessKeyId; |
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
private String secretAccessKey; |
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
private String bucket; |
| 78 | |
|
| 79 | |
protected ObjectMetadata getObjectMetadata(final String location, |
| 80 | |
final Resource resource) throws IOException { |
| 81 | 0 | ObjectMetadata om = new ObjectMetadata(); |
| 82 | 0 | String contentType = mimeTypes.getMimetype(location); |
| 83 | 0 | om.setContentType(contentType); |
| 84 | 0 | om.setContentLength(resource.contentLength()); |
| 85 | 0 | return om; |
| 86 | |
} |
| 87 | |
|
| 88 | |
protected PutObjectRequest getPutObjectRequest(final String location, final String key) |
| 89 | |
throws IOException { |
| 90 | 0 | ResourceLoader loader = new DefaultResourceLoader(); |
| 91 | 0 | Resource resource = loader.getResource(location); |
| 92 | 0 | InputStream in = resource.getInputStream(); |
| 93 | 0 | ObjectMetadata objectMetadata = getObjectMetadata(location, resource); |
| 94 | 0 | PutObjectRequest request = new PutObjectRequest(getBucket(), key, in, |
| 95 | |
objectMetadata); |
| 96 | 0 | request.setCannedAcl(CannedAccessControlList.PublicRead); |
| 97 | 0 | return request; |
| 98 | |
} |
| 99 | |
|
| 100 | |
protected PutObjectRequest getPutObjectRequest(final String location) |
| 101 | |
throws IOException { |
| 102 | 0 | String key = location.substring(1); |
| 103 | 0 | return getPutObjectRequest(location, key); |
| 104 | |
} |
| 105 | |
|
| 106 | |
protected String getAuthenticationErrorMessage() { |
| 107 | 0 | StringBuffer sb = new StringBuffer(); |
| 108 | 0 | sb.append("\n\nError: accessKeyId and secretAccessKey must be provided.\n"); |
| 109 | 0 | sb.append("Provide them in the plugin configuration or specify them on the command line:\n\n"); |
| 110 | 0 | sb.append("-DaccessKeyId=XXXX\n"); |
| 111 | 0 | sb.append("-DsecretAccessKey=XXXX\n"); |
| 112 | 0 | sb.append("\n"); |
| 113 | 0 | sb.append("You can also provide them in settings.xml as username and password:\n\n"); |
| 114 | 0 | sb.append("<server>\n"); |
| 115 | 0 | sb.append(" <id>[serverId]</id>\n"); |
| 116 | 0 | sb.append(" <username>[AWS Access Key ID]</username>\n"); |
| 117 | 0 | sb.append(" <password>[AWS Secret Access Key]</password>\n"); |
| 118 | 0 | sb.append("</server>\n\n.\n"); |
| 119 | 0 | return sb.toString(); |
| 120 | |
} |
| 121 | |
|
| 122 | |
protected void updateCredentials() { |
| 123 | 0 | if (getServerId() == null) { |
| 124 | 0 | return; |
| 125 | |
} |
| 126 | 0 | Server server = getSettings().getServer(getServerId()); |
| 127 | 0 | if (getAccessKeyId() == null) { |
| 128 | 0 | setAccessKeyId(server.getUsername()); |
| 129 | |
} |
| 130 | 0 | if (getSecretAccessKey() == null) { |
| 131 | 0 | setSecretAccessKey(server.getPassword()); |
| 132 | |
} |
| 133 | 0 | } |
| 134 | |
|
| 135 | |
protected void validateCredentials() throws MojoExecutionException { |
| 136 | 0 | if (getAccessKeyId() == null || getSecretAccessKey() == null) { |
| 137 | 0 | throw new MojoExecutionException(getAuthenticationErrorMessage()); |
| 138 | |
} |
| 139 | 0 | } |
| 140 | |
|
| 141 | |
protected AWSCredentials getCredentials() throws MojoExecutionException { |
| 142 | 0 | return new BasicAWSCredentials(getAccessKeyId(), getSecretAccessKey()); |
| 143 | |
} |
| 144 | |
|
| 145 | |
public String getAccessKeyId() { |
| 146 | 0 | return accessKeyId; |
| 147 | |
} |
| 148 | |
|
| 149 | |
public void setAccessKeyId(final String accessKeyId) { |
| 150 | 0 | this.accessKeyId = accessKeyId; |
| 151 | 0 | } |
| 152 | |
|
| 153 | |
public String getSecretAccessKey() { |
| 154 | 0 | return secretAccessKey; |
| 155 | |
} |
| 156 | |
|
| 157 | |
public void setSecretAccessKey(final String secretAccessKey) { |
| 158 | 0 | this.secretAccessKey = secretAccessKey; |
| 159 | 0 | } |
| 160 | |
|
| 161 | |
public String getBucket() { |
| 162 | 0 | return bucket; |
| 163 | |
} |
| 164 | |
|
| 165 | |
public void setBucket(final String bucket) { |
| 166 | 0 | this.bucket = bucket; |
| 167 | 0 | } |
| 168 | |
|
| 169 | |
public String getPrefix() { |
| 170 | 0 | return prefix; |
| 171 | |
} |
| 172 | |
|
| 173 | |
public void setPrefix(final String prefix) { |
| 174 | 0 | this.prefix = prefix; |
| 175 | 0 | } |
| 176 | |
|
| 177 | |
public String getDelimiter() { |
| 178 | 0 | return delimiter; |
| 179 | |
} |
| 180 | |
|
| 181 | |
public void setDelimiter(final String delimiter) { |
| 182 | 0 | this.delimiter = delimiter; |
| 183 | 0 | } |
| 184 | |
|
| 185 | |
public Integer getMaxKeys() { |
| 186 | 0 | return maxKeys; |
| 187 | |
} |
| 188 | |
|
| 189 | |
public void setMaxKeys(final Integer maxKeys) { |
| 190 | 0 | this.maxKeys = maxKeys; |
| 191 | 0 | } |
| 192 | |
|
| 193 | |
public String getServerId() { |
| 194 | 0 | return serverId; |
| 195 | |
} |
| 196 | |
|
| 197 | |
public void setServerId(final String serverId) { |
| 198 | 0 | this.serverId = serverId; |
| 199 | 0 | } |
| 200 | |
} |