Skip to content

Instantly share code, notes, and snippets.

@holyjak
Created September 9, 2012 12:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save holyjak/3684066 to your computer and use it in GitHub Desktop.
Save holyjak/3684066 to your computer and use it in GitHub Desktop.
S3FilesResource - refactored
public interface S3Facade {
List<S3File> listObjects(String bucketName);
}
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;
}
}
public class S3File {
public final String displayName;
public final String path;
public S3File(String displayName, String path) {
this.displayName = displayName;
this.path = path;
}
}
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