forked from riboseinc/aws-codecommit-trigger-plugin
/
SQSActivityAction.java
120 lines (102 loc) · 3.88 KB
/
SQSActivityAction.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package com.ribose.jenkins.plugin.awscodecommittrigger;
import com.ribose.jenkins.plugin.awscodecommittrigger.exception.UnexpectedException;
import com.ribose.jenkins.plugin.awscodecommittrigger.logging.Log;
import hudson.model.Action;
import hudson.model.Job;
import hudson.util.FormValidation;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.comparator.NameFileComparator;
import org.apache.commons.lang.time.FastDateFormat;
import org.apache.http.HttpStatus;
import org.kohsuke.stapler.Stapler;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.kohsuke.stapler.interceptor.RespondSuccess;
import javax.annotation.Nonnull;
import javax.servlet.ServletException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
public class SQSActivityAction implements Action {
private static final Log log = Log.get(SQSActivityAction.class);
private static final FastDateFormat df = FastDateFormat.getInstance("yyyyMMdd");
private final transient Job job;
private final transient File activityDir;
public SQSActivityAction(Job job) {
this.job = job;
this.activityDir = new File(this.job.getRootDir(), ".activity");
if (!this.activityDir.exists() && !this.activityDir.mkdirs()) {
log.error("Unable to create trigger activity dir %s", this.activityDir.getPath());
}
log.debug("Activity dir %s is writeable? %s", this.activityDir.getPath(), this.activityDir.canWrite());
}
@Override
public String getIconFileName() {
return "clipboard.png";
}
@Nonnull
@Override
public String getDisplayName() {
return "SQS Activity";
}
@Override
public String getUrlName() {
return getDisplayName().toLowerCase().replace(" ", "-");
}
public File getActivityDir() {
return activityDir;
}
public List<String> getLogNames() {
List<String> names = new ArrayList<>();
File[] files = this.activityDir.listFiles();
if (files != null) {
Arrays.sort(files, NameFileComparator.NAME_REVERSE);
for (File file : files) {
names.add(file.getName());
}
}
return names;
}
@RespondSuccess
public void doDownload() throws IOException, ServletException {
StaplerRequest request = Stapler.getCurrentRequest();
StaplerResponse response = Stapler.getCurrentResponse();
String name = request.getRestOfPath();
File file = new File(this.activityDir.getPath() + "/" + name);
if (file.exists()) {
FileInputStream is = FileUtils.openInputStream(file);
response.serveFile(request, is, 0L, 60_000L, file.length(), name);
} else {
response.setStatus(HttpStatus.SC_NOT_FOUND);
response.getOutputStream().println("sorry, we not found it " + name.replace("/", ""));
}
}
public Job getJob() {
return job;
}
public FormValidation doClear() {
try {
FileUtils.cleanDirectory(this.activityDir);
} catch (IOException e) {
return FormValidation.error(e, "Unable clear Activity");
}
return FormValidation.ok("Done. Please refresh the page.");
}
public File getActivityLogFile() {
String date = df.format(new Date());
String logPath = String.format("%s/activities-on-%s.log", this.getActivityDir().getPath(), date);
File logFile = new File(logPath);
if (!logFile.exists()) {
try {
FileUtils.write(logFile, "", "UTF-8");
} catch (IOException e) {
throw new UnexpectedException("Unable to create activity file: " + logPath, e);
}
}
return logFile;
}
}