package hudson.plugins.s3;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;

import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;

import hudson.model.RunAction;
import hudson.model.Run;

public class S3ArtifactsAction implements RunAction {
  private final Run run;
  private final String profile;
  private final List<FingerprintRecord> artifacts;

  public S3ArtifactsAction(Run<?,?> run, S3Profile profile, List<FingerprintRecord> artifacts) {
      this.run = run;
      this.profile = profile.getName();
      this.artifacts = artifacts;
      onLoad();   // make compact
  }

  public Run<?,?> getRun() {
      return run;
  }

  public String getIconFileName() {
    return "fingerprint.png";
  }

  public String getDisplayName() {
    return "S3 Artifacts";
  }

  public String getUrlName() {
    return "s3";
  }

  public void onLoad() {
  }

  public void onAttached(Run r) {
  }

  public void onBuildComplete() {
  }

  public String getProfile() {
    return profile;
  }

  public List<FingerprintRecord> getArtifacts() {
    return artifacts;
  }

  public void doDownload(final StaplerRequest request, final StaplerResponse response) throws IOException, ServletException {

      String restOfPath = request.getRestOfPath();
      if (restOfPath == null) {
          return;
      }

      // skip the leading /
      String artifact = restOfPath.substring(1);
      for (FingerprintRecord record : getArtifacts()) {
          if (record.artifact.getName().equals(artifact)) {
              S3Profile s3 = S3BucketPublisher.getProfile(profile);
              String url = s3.getDownloadURL(run, record);
              response.sendRedirect2(url);
              return;
          }
      }
      response.sendError(404, "This artifact is not available");
  }

}
@@ -113,16 +113,49 @@
 ption;%0A%0A
+import jenkins.model.RunAction2;%0A
 import o
@@ -238,39 +238,8 @@
 e;%0A%0A
-import hudson.model.RunAction;%0A
 impo
@@ -311,16 +311,17 @@
 unAction
+2
  %7B%0A  pri
@@ -335,20 +335,56 @@
 nal Run 
-run;
+build; // Compatibility for old versions
 %0A  priva
@@ -568,19 +568,21 @@
    this.
-run
+build
  = run;%0A
@@ -668,16 +668,19 @@
  onLoad(
+run
 );   // 
@@ -718,19 +718,21 @@
 ?,?%3E get
-Run
+Build
 () %7B%0A   
@@ -741,19 +741,21 @@
  return 
-run
+build
 ;%0A  %7D%0A%0A 
@@ -936,32 +936,44 @@
 turn %22s3%22;%0A  %7D%0A%0A
+  @Override%0A
   public void on
@@ -977,16 +977,27 @@
  onLoad(
+Run%3C?, ?%3E r
 ) %7B%0A  %7D%0A
@@ -1040,47 +1040,8 @@
  %7D%0A%0A
-  public void onBuildComplete() %7B%0A  %7D%0A%0A
   pu
@@ -1712,19 +1712,21 @@
 loadURL(
-run
+build
 , record

Add compatibility for old versions of S3 plugin