use of org.kohsuke.stapler.interceptor.RequirePOST in project sonar-scanner-jenkins by SonarSource.
the class SonarQubeWebHook method doIndex.
@RequirePOST
public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException {
String payload = IOUtils.toString(req.getReader());
LOGGER.info("Received POST from " + req.getRemoteHost());
try {
JSONObject o = JSONObject.fromObject(payload);
LOGGER.fine(() -> "Full details of the POST was " + o.toString());
String taskId = o.getString("taskId");
String status = o.getString("status");
String qgStatus = null;
if (CETask.STATUS_SUCCESS.equals(status)) {
qgStatus = o.has("qualityGate") ? o.getJSONObject("qualityGate").getString("status") : "NONE";
}
for (Listener listener : listeners) {
listener.onTaskCompleted(taskId, status, qgStatus);
}
} catch (JSONException e) {
LOGGER.log(Level.WARNING, e, () -> "Invalid payload " + payload);
rsp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid JSON Payload");
}
rsp.setStatus(HttpServletResponse.SC_OK);
}
use of org.kohsuke.stapler.interceptor.RequirePOST in project configuration-as-code-plugin by jenkinsci.
the class ConfigurationAsCode method doReplace.
@RequirePOST
@Restricted(NoExternalUse.class)
public void doReplace(StaplerRequest request, StaplerResponse response) throws Exception {
if (!Jenkins.get().hasPermission(Jenkins.ADMINISTER)) {
response.sendError(HttpServletResponse.SC_FORBIDDEN);
return;
}
String newSource = request.getParameter("_.newSource");
String normalizedSource = Util.fixEmptyAndTrim(newSource);
List<String> candidateSources = new ArrayList<>();
for (String candidateSource : inputToCandidateSources(normalizedSource)) {
File file = new File(candidateSource);
if (file.exists() || ConfigurationAsCode.isSupportedURI(candidateSource)) {
candidateSources.add(candidateSource);
} else {
LOGGER.log(Level.WARNING, "Source {0} could not be applied", candidateSource);
// todo: show message in UI
}
}
if (!candidateSources.isEmpty()) {
List<YamlSource> candidates = getConfigFromSources(candidateSources);
if (canApplyFrom(candidates)) {
sources = candidateSources;
configureWith(getConfigFromSources(getSources()));
CasCGlobalConfig config = GlobalConfiguration.all().get(CasCGlobalConfig.class);
if (config != null) {
config.setConfigurationPath(normalizedSource);
config.save();
}
LOGGER.log(Level.FINE, "Replace configuration with: " + normalizedSource);
} else {
LOGGER.log(Level.WARNING, "Provided sources could not be applied");
// todo: show message in UI
}
} else {
LOGGER.log(Level.FINE, "No such source exists, applying default");
// May be do nothing instead?
configure();
}
response.sendRedirect("");
}
use of org.kohsuke.stapler.interceptor.RequirePOST in project configuration-as-code-plugin by jenkinsci.
the class TokenReloadAction method doIndex.
@RequirePOST
public void doIndex(StaplerRequest request, StaplerResponse response) throws IOException {
String token = getReloadToken();
if (token == null || token.isEmpty()) {
response.sendError(404);
LOGGER.warning("Configuration reload via token is not enabled");
} else {
String requestToken = getRequestToken(request);
if (requestToken != null && MessageDigest.isEqual(token.getBytes(StandardCharsets.UTF_8), requestToken.getBytes(StandardCharsets.UTF_8))) {
LOGGER.info("Configuration reload triggered via token");
try (ACLContext ignored = ACL.as2(ACL.SYSTEM2)) {
ConfigurationAsCode.get().configure();
}
} else {
response.sendError(401);
LOGGER.warning("Invalid token received, not reloading configuration");
}
}
}
use of org.kohsuke.stapler.interceptor.RequirePOST in project workflow-job-plugin by jenkinsci.
the class WorkflowRun method doKill.
/**
* Immediately kills the build.
*/
@RequirePOST
public void doKill() {
checkPermission(Item.CANCEL);
if (!isBuilding() || /* probably redundant, but just to be sure */
execution == null) {
return;
}
if (listener != null) {
listener.getLogger().println("Hard kill!");
}
// ensures isInProgress returns false
execution = null;
FlowInterruptedException suddenDeath = new FlowInterruptedException(Result.ABORTED);
finish(Result.ABORTED, suddenDeath);
executionPromise.setException(suddenDeath);
// TODO CpsFlowExecution.onProgramEnd does some cleanup which we cannot access here; perhaps need a FlowExecution.halt(Throwable) API?
}
use of org.kohsuke.stapler.interceptor.RequirePOST in project workflow-cps-plugin by jenkinsci.
the class ReplayAction method doRun.
@Restricted(DoNotUse.class)
@RequirePOST
public void doRun(StaplerRequest req, StaplerResponse rsp) throws ServletException, IOException {
if (!isEnabled() || !(isReplayableSandboxTest())) {
// AccessDeniedException2 requires us to look up the specific Permission
throw new AccessDeniedException("not allowed to replay");
}
JSONObject form = req.getSubmittedForm();
// Copy originalLoadedScripts, replacing values with those from the form wherever defined.
Map<String, String> replacementLoadedScripts = new HashMap<>();
for (Map.Entry<String, String> entry : getOriginalLoadedScripts().entrySet()) {
// optString since you might be replaying a running build, which might have loaded a script after the page load but before submission.
replacementLoadedScripts.put(entry.getKey(), form.optString(entry.getKey().replace('.', '_'), entry.getValue()));
}
if (run(form.getString("mainScript"), replacementLoadedScripts) == null) {
throw HttpResponses.error(SC_CONFLICT, new IOException(run.getParent().getFullName() + " is not buildable"));
}
// back to WorkflowJob; new build might not start instantly so cannot redirect to it
rsp.sendRedirect("../..");
}
Aggregations