Skip to content

Commit

Permalink
Using HTTP error codes #146
Browse files Browse the repository at this point in the history
* New datastructure in JSON responses.
* Lowering log-level i Jenkins server log.
  • Loading branch information
tomasbjerre committed Nov 21, 2019
1 parent 69703bf commit 898775b
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 14 deletions.
20 changes: 20 additions & 0 deletions src/main/java/org/jenkinsci/plugins/gwt/GenericResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.jenkinsci.plugins.gwt;

import java.util.Map;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.HttpResponse;
import org.kohsuke.stapler.json.JsonHttpResponse;

public class GenericResponse {

static HttpResponse jsonResponse(
final int httpStatusCode, final String message, final Map<String, Object> jobs) {
final GenericWebhookResponse genericResponse = new GenericWebhookResponse(message, jobs);
return new JsonHttpResponse(JSONObject.fromObject(genericResponse), httpStatusCode);
}

static HttpResponse jsonResponse(final int httpStatusCode, final String message) {
final GenericWebhookResponse genericResponse = new GenericWebhookResponse(message);
return new JsonHttpResponse(JSONObject.fromObject(genericResponse), httpStatusCode);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package org.jenkinsci.plugins.gwt;

import static com.google.common.base.Charsets.UTF_8;
import static hudson.util.HttpResponses.okJSON;
import static java.util.logging.Level.FINE;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.SEVERE;
import static org.jenkinsci.plugins.gwt.GenericResponse.jsonResponse;
import static org.kohsuke.stapler.HttpResponses.ok;

import com.google.common.annotations.VisibleForTesting;
Expand All @@ -17,6 +16,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
Expand Down Expand Up @@ -52,21 +52,20 @@ public HttpResponse doInvoke(final StaplerRequest request) {
postContent = IOUtils.toString(request.getInputStream(), UTF_8.name());
} catch (final IOException e) {
LOGGER.log(SEVERE, "", e);
return jsonResponse(500, "Unable to read inputstream: " + e.getMessage());
}

try {
WhitelistVerifier.verifyWhitelist(request.getRemoteAddr(), headers, postContent);
} catch (final WhitelistException e) {
final Map<String, Object> response = new HashMap<>();
response.put(
"triggerResults",
return jsonResponse(
403,
"Sender, "
+ request.getRemoteHost()
+ ", with headers "
+ headers
+ " did not pass whitelist.\n"
+ e.getMessage());
return okJSON(response);
}

final String givenToken = getGivenToken(headers, parameterMap);
Expand Down Expand Up @@ -112,14 +111,11 @@ HttpResponse doInvoke(

final List<FoundJob> foundJobs = JobFinder.findAllJobsWithTrigger(givenToken);
final Map<String, Object> triggerResultsMap = new HashMap<>();
if (foundJobs.isEmpty()) {
LOGGER.log(INFO, NO_JOBS_MSG);
triggerResultsMap.put("ANY", NO_JOBS_MSG);
}
boolean allSilent = true;
boolean errors = false;
for (final FoundJob foundJob : foundJobs) {
try {
LOGGER.log(INFO, "Triggering " + foundJob.getFullName());
LOGGER.log(FINE, "Triggering " + foundJob.getFullName());
LOGGER.log(FINE, " with:\n\n" + postContent + "\n\n");
final GenericTrigger genericTrigger = foundJob.getGenericTrigger();
final GenericTriggerResults triggerResults =
Expand All @@ -132,14 +128,22 @@ HttpResponse doInvoke(
LOGGER.log(SEVERE, foundJob.getFullName(), t);
final String msg = createMessageFromException(t);
triggerResultsMap.put(foundJob.getFullName(), msg);
errors = true;
}
}
if (allSilent && foundJobs.size() > 0) {
return ok();
}
final Map<String, Object> response = new HashMap<>();
response.put("triggerResults", triggerResultsMap);
return okJSON(response);
if (errors) {
return jsonResponse(500, "There were errors when triggering jobs.", triggerResultsMap);
} else {
if (foundJobs.isEmpty()) {
LOGGER.log(Level.FINE, NO_JOBS_MSG);
return jsonResponse(404, NO_JOBS_MSG);
} else {
return jsonResponse(200, "Triggered jobs.", triggerResultsMap);
}
}
}

String createMessageFromException(final Throwable t) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.jenkinsci.plugins.gwt;

import java.util.Map;

public class GenericWebhookResponse {

private final String message;
private Map<String, Object> jobs;

public GenericWebhookResponse(final String message) {
this.message = message;
}

public GenericWebhookResponse(final String message, final Map<String, Object> jobs) {
this.message = message;
this.jobs = jobs;
}

public String getMessage() {
return message;
}

public Map<String, Object> getJobs() {
return jobs;
}
}

0 comments on commit 898775b

Please sign in to comment.