diff --git a/src/main/java/com/dabsquared/gitlabjenkins/webhook/status/StatusPngAction.java b/src/main/java/com/dabsquared/gitlabjenkins/webhook/status/StatusPngAction.java index bb344a7fe..daefe9855 100644 --- a/src/main/java/com/dabsquared/gitlabjenkins/webhook/status/StatusPngAction.java +++ b/src/main/java/com/dabsquared/gitlabjenkins/webhook/status/StatusPngAction.java @@ -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; /** @@ -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"); } } } diff --git a/src/main/webapp/images/failed.png b/src/main/resources/com/dabsquared/gitlabjenkins/webhook/status/failed.png similarity index 100% rename from src/main/webapp/images/failed.png rename to src/main/resources/com/dabsquared/gitlabjenkins/webhook/status/failed.png diff --git a/src/main/webapp/images/running.png b/src/main/resources/com/dabsquared/gitlabjenkins/webhook/status/running.png similarity index 100% rename from src/main/webapp/images/running.png rename to src/main/resources/com/dabsquared/gitlabjenkins/webhook/status/running.png diff --git a/src/main/webapp/images/success.png b/src/main/resources/com/dabsquared/gitlabjenkins/webhook/status/success.png similarity index 100% rename from src/main/webapp/images/success.png rename to src/main/resources/com/dabsquared/gitlabjenkins/webhook/status/success.png diff --git a/src/main/webapp/images/unknown.png b/src/main/resources/com/dabsquared/gitlabjenkins/webhook/status/unknown.png similarity index 100% rename from src/main/webapp/images/unknown.png rename to src/main/resources/com/dabsquared/gitlabjenkins/webhook/status/unknown.png diff --git a/src/main/webapp/images/unstable.png b/src/main/resources/com/dabsquared/gitlabjenkins/webhook/status/unstable.png similarity index 100% rename from src/main/webapp/images/unstable.png rename to src/main/resources/com/dabsquared/gitlabjenkins/webhook/status/unstable.png