| 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 |
|
|
|
|
|
| 0% |
Uncovered Elements: 80 (80) |
Complexity: 26 |
Complexity Density: 0.51 |
|
| 22 |
|
public abstract class S3Mojo extends BaseMojo { |
| 23 |
|
|
| 24 |
|
|
| 25 |
|
|
| 26 |
|
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 |
|
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
| 79 |
0
|
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 |
|
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
|
| 88 |
0
|
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 |
|
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 100 |
0
|
protected PutObjectRequest getPutObjectRequest(final String location)... |
| 101 |
|
throws IOException { |
| 102 |
0
|
String key = location.substring(1); |
| 103 |
0
|
return getPutObjectRequest(location, key); |
| 104 |
|
} |
| 105 |
|
|
|
|
|
| 0% |
Uncovered Elements: 13 (13) |
Complexity: 1 |
Complexity Density: 0.08 |
|
| 106 |
0
|
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 |
|
|
|
|
|
| 0% |
Uncovered Elements: 13 (13) |
Complexity: 4 |
Complexity Density: 0.57 |
|
| 122 |
0
|
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 |
|
} |
| 134 |
|
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 3 |
Complexity Density: 1.5 |
|
| 135 |
0
|
protected void validateCredentials() throws MojoExecutionException {... |
| 136 |
0
|
if (getAccessKeyId() == null || getSecretAccessKey() == null) { |
| 137 |
0
|
throw new MojoExecutionException(getAuthenticationErrorMessage()); |
| 138 |
|
} |
| 139 |
|
} |
| 140 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 141 |
0
|
protected AWSCredentials getCredentials() throws MojoExecutionException {... |
| 142 |
0
|
return new BasicAWSCredentials(getAccessKeyId(), getSecretAccessKey()); |
| 143 |
|
} |
| 144 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 145 |
0
|
public String getAccessKeyId() {... |
| 146 |
0
|
return accessKeyId; |
| 147 |
|
} |
| 148 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 149 |
0
|
public void setAccessKeyId(final String accessKeyId) {... |
| 150 |
0
|
this.accessKeyId = accessKeyId; |
| 151 |
|
} |
| 152 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 153 |
0
|
public String getSecretAccessKey() {... |
| 154 |
0
|
return secretAccessKey; |
| 155 |
|
} |
| 156 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 157 |
0
|
public void setSecretAccessKey(final String secretAccessKey) {... |
| 158 |
0
|
this.secretAccessKey = secretAccessKey; |
| 159 |
|
} |
| 160 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 161 |
0
|
public String getBucket() {... |
| 162 |
0
|
return bucket; |
| 163 |
|
} |
| 164 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 165 |
0
|
public void setBucket(final String bucket) {... |
| 166 |
0
|
this.bucket = bucket; |
| 167 |
|
} |
| 168 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 169 |
0
|
public String getPrefix() {... |
| 170 |
0
|
return prefix; |
| 171 |
|
} |
| 172 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 173 |
0
|
public void setPrefix(final String prefix) {... |
| 174 |
0
|
this.prefix = prefix; |
| 175 |
|
} |
| 176 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 177 |
0
|
public String getDelimiter() {... |
| 178 |
0
|
return delimiter; |
| 179 |
|
} |
| 180 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 181 |
0
|
public void setDelimiter(final String delimiter) {... |
| 182 |
0
|
this.delimiter = delimiter; |
| 183 |
|
} |
| 184 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 185 |
0
|
public Integer getMaxKeys() {... |
| 186 |
0
|
return maxKeys; |
| 187 |
|
} |
| 188 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 189 |
0
|
public void setMaxKeys(final Integer maxKeys) {... |
| 190 |
0
|
this.maxKeys = maxKeys; |
| 191 |
|
} |
| 192 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 193 |
0
|
public String getServerId() {... |
| 194 |
0
|
return serverId; |
| 195 |
|
} |
| 196 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 197 |
0
|
public void setServerId(final String serverId) {... |
| 198 |
0
|
this.serverId = serverId; |
| 199 |
|
} |
| 200 |
|
} |