Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch '4.x'

  • Loading branch information...
commit d539db9ff115d4a857fe50c9af20efb438194a9d 2 parents d2cfb6a + 252bcb3
@nebhale nebhale authored
View
17 src/main/java/org/springframework/build/aws/maven/SimpleStorageServiceWagon.java
@@ -40,6 +40,7 @@
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
+import com.amazonaws.services.s3.internal.Mimetypes;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.ListObjectsRequest;
import com.amazonaws.services.s3.model.ObjectListing;
@@ -176,15 +177,27 @@ protected void getResource(String resourceName, File destination, TransferProgre
}
@Override
- protected void putResource(File source, String destination, TransferProgress transferProgress) throws TransferFailedException {
+ protected void putResource(File source, String destination, TransferProgress transferProgress) throws TransferFailedException,
+ ResourceDoesNotExistException {
String key = getKey(destination);
mkdirs(key, 0);
+ InputStream in = null;
try {
- this.amazonS3.putObject(new PutObjectRequest(this.bucketName, key, source).withCannedAcl(CannedAccessControlList.PublicRead));
+ ObjectMetadata objectMetadata = new ObjectMetadata();
+ objectMetadata.setContentLength(source.length());
+ objectMetadata.setContentType(Mimetypes.getInstance().getMimetype(source));
+
+ in = new TransferProgressFileInputStream(source, transferProgress);
+
+ this.amazonS3.putObject(new PutObjectRequest(this.bucketName, key, in, objectMetadata).withCannedAcl(CannedAccessControlList.PublicRead));
} catch (AmazonServiceException e) {
throw new TransferFailedException(String.format("Cannot write file to '%s'", destination), e);
+ } catch (FileNotFoundException e) {
+ throw new ResourceDoesNotExistException(String.format("Cannot read file from '%s'", source), e);
+ } finally {
+ IoUtils.closeQuietly(in);
}
}
View
19 ...t/java/org/springframework/build/aws/maven/SimpleStorageServiceWagonIntegrationTests.java
@@ -49,6 +49,7 @@
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
+import com.amazonaws.services.s3.model.PutObjectResult;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.S3ObjectSummary;
@@ -200,7 +201,7 @@ public void getResourceSourceDoesNotExist() throws TransferFailedException, Reso
}
@Test
- public void putResource() throws TransferFailedException {
+ public void putResource() throws TransferFailedException, ResourceDoesNotExistException {
File file = new File("src/test/resources/test.txt");
this.wagon.putResource(file, FILE_NAME, this.transferProgress);
@@ -221,23 +222,27 @@ public void putResource() throws TransferFailedException {
PutObjectRequest fileRequest = putObjectRequests.get(2);
assertEquals(BUCKET_NAME, fileRequest.getBucketName());
assertEquals(BASE_DIRECTORY + FILE_NAME, fileRequest.getKey());
- assertEquals(file, fileRequest.getFile());
+ assertNotNull(fileRequest.getInputStream());
assertEquals(CannedAccessControlList.PublicRead, fileRequest.getCannedAcl());
+
+ ObjectMetadata objectMetadata = fileRequest.getMetadata();
+ assertNotNull(objectMetadata);
+ assertEquals(file.length(), objectMetadata.getContentLength());
+ assertEquals("text/plain", objectMetadata.getContentType());
}
@Test(expected = TransferFailedException.class)
- public void putResourceMkdirException() throws TransferFailedException {
+ public void putResourceMkdirException() throws TransferFailedException, ResourceDoesNotExistException {
when(this.amazonS3.putObject(any(PutObjectRequest.class))).thenThrow(new AmazonServiceException(""));
File file = new File("src/test/resources/test.txt");
this.wagon.putResource(file, FILE_NAME, this.transferProgress);
}
@Test(expected = TransferFailedException.class)
- public void putResourcePutException() throws TransferFailedException {
+ public void putResourcePutException() throws TransferFailedException, ResourceDoesNotExistException {
+ when(this.amazonS3.putObject(any(PutObjectRequest.class))).thenReturn((PutObjectResult) null, (PutObjectResult) null).thenThrow(
+ new AmazonServiceException(""));
File file = new File("src/test/resources/test.txt");
- PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKET_NAME, BASE_DIRECTORY + FILE_NAME, file).withCannedAcl(CannedAccessControlList.PublicRead);
- when(this.amazonS3.putObject(eq(putObjectRequest))).thenThrow(new AmazonServiceException(""));
-
this.wagon.putResource(file, FILE_NAME, this.transferProgress);
}
}
View
6 src/test/java/org/springframework/build/aws/maven/matchers/Matchers.java
@@ -20,7 +20,6 @@
import org.apache.maven.wagon.events.TransferEvent;
import com.amazonaws.services.s3.model.ListObjectsRequest;
-import com.amazonaws.services.s3.model.PutObjectRequest;
public final class Matchers {
@@ -31,10 +30,6 @@ public static ListObjectsRequest eq(ListObjectsRequest listObjectsRequest) {
return org.mockito.Matchers.argThat(new ListObjectsRequestMatcher(listObjectsRequest));
}
- public static PutObjectRequest eq(PutObjectRequest putObjectRequest) {
- return org.mockito.Matchers.argThat(new PutObjectRequestMatcher(putObjectRequest));
- }
-
public static SessionEvent eq(SessionEvent sessionEvent) {
return org.mockito.Matchers.argThat(new SessionEventMatcher(sessionEvent));
}
@@ -42,4 +37,5 @@ public static SessionEvent eq(SessionEvent sessionEvent) {
public static TransferEvent eq(TransferEvent transferEvent) {
return org.mockito.Matchers.argThat(new TransferEventMatcher(transferEvent));
}
+
}
View
112 src/test/java/org/springframework/build/aws/maven/matchers/PutObjectRequestMatcher.java
@@ -1,112 +0,0 @@
-/*
- * Copyright 2011 the original author or authors.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.springframework.build.aws.maven.matchers;
-
-import org.hamcrest.BaseMatcher;
-import org.hamcrest.Description;
-
-import com.amazonaws.services.s3.model.PutObjectRequest;
-
-final class PutObjectRequestMatcher extends BaseMatcher<PutObjectRequest> {
-
- private final PutObjectRequest putObjectRequest;
-
- PutObjectRequestMatcher(PutObjectRequest putObjectRequest) {
- this.putObjectRequest = putObjectRequest;
- }
-
- // CHECKSTYLE:OFF
-
- @Override
- public boolean matches(Object obj) {
- if (this.putObjectRequest == obj) {
- return true;
- }
- if (obj == null) {
- return false;
- }
- if (PutObjectRequest.class != obj.getClass()) {
- return false;
- }
- PutObjectRequest other = (PutObjectRequest) obj;
- if (this.putObjectRequest.getBucketName() == null) {
- if (other.getBucketName() != null) {
- return false;
- }
- } else if (!this.putObjectRequest.getBucketName().equals(other.getBucketName())) {
- return false;
- }
- if (this.putObjectRequest.getKey() == null) {
- if (other.getKey() != null) {
- return false;
- }
- } else if (!this.putObjectRequest.getKey().equals(other.getKey())) {
- return false;
- }
- if (this.putObjectRequest.getFile() == null) {
- if (other.getFile() != null) {
- return false;
- }
- } else if (!this.putObjectRequest.getFile().equals(other.getFile())) {
- return false;
- }
- if (this.putObjectRequest.getInputStream() == null) {
- if (other.getInputStream() != null) {
- return false;
- }
- } else if (!this.putObjectRequest.getInputStream().equals(other.getInputStream())) {
- return false;
- }
- if (this.putObjectRequest.getMetadata() == null) {
- if (other.getMetadata() != null) {
- return false;
- }
- } else if (!this.putObjectRequest.getMetadata().equals(other.getMetadata())) {
- return false;
- }
- if (this.putObjectRequest.getCannedAcl() == null) {
- if (other.getCannedAcl() != null) {
- return false;
- }
- } else if (!this.putObjectRequest.getCannedAcl().equals(other.getCannedAcl())) {
- return false;
- }
- if (this.putObjectRequest.getStorageClass() == null) {
- if (other.getStorageClass() != null) {
- return false;
- }
- } else if (!this.putObjectRequest.getStorageClass().equals(other.getStorageClass())) {
- return false;
- }
- if (this.putObjectRequest.getProgressListener() == null) {
- if (other.getProgressListener() != null) {
- return false;
- }
- } else if (!this.putObjectRequest.getProgressListener().equals(other.getProgressListener())) {
- return false;
- }
- return true;
- }
-
- // CHECKSTYLE:ON
-
- @Override
- public void describeTo(Description description) {
- description.appendValue(this.putObjectRequest);
- }
-
-}
Please sign in to comment.
Something went wrong with that request. Please try again.