Created
September 9, 2012 12:22
-
-
Save holyjak/3684066 to your computer and use it in GitHub Desktop.
S3FilesResource - refactored
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface S3Facade { | |
List<S3File> listObjects(String bucketName); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class S3FacadeImpl implements S3Facade { | |
AmazonS3Client amazonS3Client; | |
@Override | |
public List<S3File> listObjects(String bucketName) { | |
List<S3File> result = new ArrayList<S3File>(); | |
List<S3ObjectSummary> files = this.amazonS3Client.listObjects(bucketName).getObjectSummaries(); | |
for (S3ObjectSummary file : files) { | |
result.add(new S3File(file.getKey(), file.getKey())); // later we can use st. else for the display name | |
} | |
return result; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class S3File { | |
public final String displayName; | |
public final String path; | |
public S3File(String displayName, String path) { | |
this.displayName = displayName; | |
this.path = path; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class S3FilesResource { | |
S3Facade amazonS3Client = new S3FacadeImpl(); | |
// ... | |
@Path("/files") | |
public String listS3Files() { | |
StringBuilder html = new StringBuilder("<html><body>"); | |
List<S3File> files = fetchS3Files(); | |
for (S3File file : files) { | |
html.append("<a href='/content?fileName=").append(file.path).append("'>").append(file.displayName) | |
.append("</a><br>"); | |
} | |
return html.append("</body></html>").toString(); | |
} | |
List<S3File> fetchS3Files() { | |
List<S3File> files = this.amazonS3Client.listObjects("myBucket"); | |
List<S3File> result = new ArrayList<S3File>(files.size()); | |
for (S3File file : files) { | |
if (!file.path.endsWith("/")) { | |
result.add(file); | |
} | |
} | |
return result; | |
} | |
@Path("/content") | |
public String getContent(@QueryParam("fileName") String fileName) { | |
throw new UnsupportedOperationException("Not implemented yet"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment