use of org.kohsuke.stapler.StaplerRequest in project blueocean-plugin by jenkinsci.
the class ApiHead method getDynamic.
/**
* Exposes all {@link ApiRoutable}s to URL space.
*
* @param route current URL route handled by ApiHead
* @return {@link ApiRoutable} object
*/
public ApiRoutable getDynamic(String route) {
setApis();
StaplerRequest request = Stapler.getCurrentRequest();
String m = request.getMethod();
if (m.equalsIgnoreCase("POST") || m.equalsIgnoreCase("PUT") || m.equalsIgnoreCase("PATCH")) {
String header = request.getHeader("Content-Type");
if (header == null || !header.contains("application/json")) {
throw new ServiceException(415, "Content-Type: application/json required");
}
}
ApiRoutable apiRoutable = apis.get(route);
// JENKINS-46025 - Avoid caching REST API responses for IE
StaplerResponse response = Stapler.getCurrentResponse();
if (response != null && !response.containsHeader("Cache-Control")) {
response.setHeader("Cache-Control", "no-cache, no-store, no-transform");
}
return apiRoutable;
}
use of org.kohsuke.stapler.StaplerRequest in project blueocean-plugin by jenkinsci.
the class GithubScmContentProviderTest method saveContentToMbpGHE.
@Test
public void saveContentToMbpGHE() throws UnirestException, IOException {
String credentialId = createGithubEnterpriseCredential();
StaplerRequest staplerRequest = mockStapler(GithubEnterpriseScm.ID);
GitContent content = new GitContent.Builder().autoCreateBranch(true).base64Data("c2xlZXAgMTUKbm9kZSB7CiAgY2hlY2tvdXQgc2NtCiAgc2ggJ2xzIC1sJwp9\\nCnNsZWVwIDE1Cg==\\n").branch("test1").message("another commit").owner("cloudbeers").path("Jankinsfile").repo("PR-demo").sha("e23b8ef5c2c4244889bf94db6c05cc08ea138aef").build();
when(staplerRequest.bindJSON(Mockito.eq(GithubScmSaveFileRequest.class), Mockito.any(JSONObject.class))).thenReturn(new GithubScmSaveFileRequest(content));
MultiBranchProject mbp = mockMbp(credentialId, user, GithubEnterpriseScm.DOMAIN_NAME);
String request = "{\n" + " \"content\" : {\n" + " \"message\" : \"first commit\",\n" + " \"path\" : \"Jenkinsfile\",\n" + " \"branch\" : \"test1\",\n" + " \"repo\" : \"PR-demo\",\n" + " \"sha\" : \"e23b8ef5c2c4244889bf94db6c05cc08ea138aef\",\n" + " \"base64Data\" : " + "\"c2xlZXAgMTUKbm9kZSB7CiAgY2hlY2tvdXQgc2NtCiAgc2ggJ2xzIC1sJwp9\\nCnNsZWVwIDE1Cg==\\n\"" + " }\n" + "}";
when(staplerRequest.getReader()).thenReturn(new BufferedReader(new StringReader(request), request.length()));
GithubFile file = (GithubFile) new GithubScmContentProvider().saveContent(staplerRequest, mbp);
assertEquals("Jenkinsfile", file.getContent().getName());
assertEquals("e23b8ef5c2c4244889bf94db6c05cc08ea138aef", file.getContent().getSha());
assertEquals("PR-demo", file.getContent().getRepo());
assertEquals("cloudbeers", file.getContent().getOwner());
}
use of org.kohsuke.stapler.StaplerRequest in project blueocean-plugin by jenkinsci.
the class GithubScmContentProviderTest method unauthorizedAccessToContentForOrgFolderShouldFail.
@Test
public void unauthorizedAccessToContentForOrgFolderShouldFail() throws UnirestException, IOException {
User alice = User.get("alice");
alice.setFullName("Alice Cooper");
alice.addProperty(new Mailer.UserProperty("alice@jenkins-ci.org"));
String aliceCredentialId = createGithubCredential(alice);
StaplerRequest staplerRequest = mockStapler();
MultiBranchProject mbp = mockMbp(aliceCredentialId, user, GithubScm.DOMAIN_NAME);
try {
// Bob trying to access content but his credential is not setup so should fail
new GithubScmContentProvider().getContent(staplerRequest, mbp);
} catch (ServiceException.PreconditionRequired e) {
assertEquals("Can't access content from github: no credential found", e.getMessage());
return;
}
fail("Should have failed with PreConditionException");
}
use of org.kohsuke.stapler.StaplerRequest in project blueocean-plugin by jenkinsci.
the class BlueUrlTokenizer method parseCurrentRequest.
/**
* Parse the {@link Stapler#getCurrentRequest() current Stapler request} and return a {@link BlueUrlTokenizer} instance
* iff the URL is a Blue Ocean UI URL.
*
* @return A {@link BlueUrlTokenizer} instance iff the URL is a Blue Ocean UI URL, otherwise {@code null}.
* @throws IllegalStateException Called outside the scope of an active {@link StaplerRequest}.
*/
@CheckForNull
public static BlueUrlTokenizer parseCurrentRequest() throws IllegalStateException {
StaplerRequest currentRequest = Stapler.getCurrentRequest();
if (currentRequest == null) {
throw new IllegalStateException("Illegal call to BlueoceanUrl.parseCurrentRequest outside the scope of an active StaplerRequest.");
}
String path = currentRequest.getOriginalRequestURI();
String contextPath = currentRequest.getContextPath();
path = path.substring(contextPath.length());
return parse(path);
}
use of org.kohsuke.stapler.StaplerRequest in project blueocean-plugin by jenkinsci.
the class PipelineNodeImpl method restart.
public HttpResponse restart(StaplerRequest request) {
try {
JSONObject body = JSONObject.fromObject(IOUtils.toString(request.getReader()));
boolean restart = body.getBoolean("restart");
if (restart && isRestartable()) {
LOGGER.debug("submitInputStep, restart: {}, step: {}", restart, this.getDisplayName());
RestartDeclarativePipelineAction restartDeclarativePipelineAction = this.run.getAction(RestartDeclarativePipelineAction.class);
Queue.Item item = restartDeclarativePipelineAction.run(this.getDisplayName());
BluePipeline bluePipeline = BluePipelineFactory.getPipelineInstance(this.run.getParent(), this.parent);
BlueQueueItem queueItem = QueueUtil.getQueuedItem(bluePipeline.getOrganization(), item, run.getParent());
if (queueItem != null) {
// If the item is still queued
return (req, rsp, node1) -> {
rsp.setStatus(HttpServletResponse.SC_OK);
rsp.getOutputStream().print(Export.toJson(queueItem.toRun()));
};
}
final WorkflowRun restartRun = getRun(run.getParent(), item.getId());
if (restartRun != null) {
return (req, rsp, node1) -> {
rsp.setStatus(HttpServletResponse.SC_OK);
rsp.getOutputStream().print(Export.toJson(new PipelineRunImpl(restartRun, parent, bluePipeline.getOrganization())));
};
} else {
// For some reason could not be added to the queue
throw new ServiceException.UnexpectedErrorException("Run was not added to queue.");
}
}
// ISE cant happen if stage not restartable or anything else :)
} catch (Exception e) {
LOGGER.warn("error restarting stage: " + e.getMessage(), e);
throw new ServiceException.UnexpectedErrorException(e.getMessage());
}
return null;
}
Aggregations