Skip to content

Commit

Permalink
Move status images to the resources directory so they are easier to a…
Browse files Browse the repository at this point in the history
…ccess
  • Loading branch information
coder-hugo committed Mar 10, 2016
1 parent cf1f0ed commit d290997
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
import jenkins.model.Jenkins;
import org.acegisecurity.Authentication;
import org.acegisecurity.context.SecurityContextHolder;
import org.apache.commons.io.IOUtils;
import org.kohsuke.stapler.StaplerResponse;

import java.io.File;
import java.io.InputStream;
import java.net.URL;

/**
Expand All @@ -22,33 +24,29 @@ protected StatusPngAction(AbstractProject<?, ?> project, AbstractBuild<?, ?> bui

@Override
protected void writeStatusBody(StaplerResponse response, AbstractBuild<?, ?> build, BuildStatus status) {
Authentication old = SecurityContextHolder.getContext().getAuthentication();
SecurityContextHolder.getContext().setAuthentication(ACL.SYSTEM);
try {
URL resourceUrl = new URL(Jenkins.getInstance().getPlugin("gitlab-plugin").getWrapper().baseResourceURL + getStatusImageUrl(status));
response.setHeader("Expires","Fri, 01 Jan 1984 00:00:00 GMT");
response.setHeader("Cache-Control", "no-cache, private");
response.setHeader("Content-Type", "image/png");
hudson.util.IOUtils.copy(new File(resourceUrl.toURI()), response.getOutputStream());
IOUtils.copy(getStatusImage(status), response.getOutputStream());
response.flushBuffer();
} catch (Exception e) {
throw HttpResponses.error(500, "Could not generate response.");
} finally {
SecurityContextHolder.getContext().setAuthentication(old);
}
}

private String getStatusImageUrl(BuildStatus status) {
if(status == BuildStatus.RUNNING) {
return "images/running.png";
} else if (status == BuildStatus.SUCCESS) {
return "images/success.png";
} else if (status == BuildStatus.FAILED) {
return "images/failed.png";
} else if (status == BuildStatus.UNSTABLE) {
return "images/unstable.png";
} else {
return "images/unknown.png";
private InputStream getStatusImage(BuildStatus status) {
switch (status) {
case RUNNING:
return getClass().getResourceAsStream("running.png");
case SUCCESS:
return getClass().getResourceAsStream("success.png");
case FAILED:
return getClass().getResourceAsStream("failed.png");
case UNSTABLE:
return getClass().getResourceAsStream("unstable.png");
default:
return getClass().getResourceAsStream("unknown.png");
}
}
}
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes

0 comments on commit d290997

Please sign in to comment.