use of org.kohsuke.stapler.WebMethod in project blueocean-plugin by jenkinsci.
the class BluePipeline method update.
/**
* Updates this pipeline using {@link BluePipelineUpdateRequest}
* @param staplerRequest stapler request
* @return Updated BluePipeline instance
* @throws IOException throws IOException in certain cases
*/
@PUT
@WebMethod(name = "")
@TreeResponse
public BluePipeline update(StaplerRequest staplerRequest) throws IOException {
JSONObject body = JSONObject.fromObject(IOUtils.toString(staplerRequest.getReader()));
if (body.get("$class") == null) {
throw new ServiceException.BadRequestExpception("$class is required element");
}
BluePipelineUpdateRequest request = staplerRequest.bindJSON(BluePipelineUpdateRequest.class, body);
return update(request);
}
use of org.kohsuke.stapler.WebMethod in project blueocean-plugin by jenkinsci.
the class GithubScm method getRepository.
/**
* @param jobName the job name
* @param apiUrl github api url
* @return GHRepository used by the job
*/
@GET
@WebMethod(name = "repository")
@TreeResponse
public GithubRepository getRepository(@QueryParameter String jobName, @QueryParameter String apiUrl) {
Item item = Jenkins.get().getItem(jobName);
if (item == null) {
throw new ServiceException.NotFoundException(String.format("Job %s not found", jobName));
}
GitHubSCMSource gitHubSCMSource = ((GitHubSCMSource) ((WorkflowMultiBranchProject) item).getSCMSource("blueocean"));
if (gitHubSCMSource == null) {
throw new ServiceException.NotFoundException(String.format("GitHubSCMSource for Job %s not found", jobName));
}
String repoOwner = gitHubSCMSource.getRepoOwner();
String repoName = gitHubSCMSource.getRepository();
StandardUsernamePasswordCredentials credential = getCredential();
String accessToken = credential.getPassword().getPlainText();
try {
String url = String.format("%s/repos/%s/%s", apiUrl, repoOwner, repoName);
GHRepository ghRepository = HttpRequest.get(url).withAuthorizationToken(accessToken).to(GHRepository.class);
return new GithubRepository(ghRepository, credential, this);
} catch (IOException e) {
throw new ServiceException.UnexpectedErrorException(e.getMessage(), e);
}
}
use of org.kohsuke.stapler.WebMethod in project blueocean-plugin by jenkinsci.
the class AnalyticsRoute method track.
@POST
@WebMethod(name = "track")
public void track(StaplerRequest staplerRequest) throws IOException {
Analytics analytics = Analytics.get();
if (analytics == null) {
return;
}
TrackRequest req;
try (InputStream is = staplerRequest.getInputStream()) {
req = JsonConverter.toJava(is, TrackRequest.class);
}
analytics.track(req);
}
use of org.kohsuke.stapler.WebMethod in project blueocean-plugin by jenkinsci.
the class BluePipelineContainer method create.
/**
* Create new pipeline.
*
* @param body {@link BluePipelineCreateRequest} request object
* @return {@link CreateResponse} response
*/
@POST
@WebMethod(name = "")
public CreateResponse create(@JsonBody JSONObject body, StaplerRequest staplerRequest) throws IOException {
ErrorMessage err = new ErrorMessage(400, "Failed to create Git pipeline");
if (body.get("name") == null) {
err.add(new ErrorMessage.Error("name", ErrorMessage.Error.ErrorCodes.MISSING.toString(), "name is required"));
}
if (body.get("$class") == null) {
err.add(new ErrorMessage.Error("$class", ErrorMessage.Error.ErrorCodes.MISSING.toString(), "$class is required"));
}
if (!err.getErrors().isEmpty()) {
throw new ServiceException.BadRequestException(err);
}
BluePipelineCreateRequest request = staplerRequest.bindJSON(BluePipelineCreateRequest.class, body);
return create(request);
}
use of org.kohsuke.stapler.WebMethod in project hudson-2.x by hudson.
the class AbstractItem method doConfigDotXml.
/**
* Accepts <tt>config.xml</tt> submission, as well as serve it.
*/
@WebMethod(name = "config.xml")
public void doConfigDotXml(StaplerRequest req, StaplerResponse rsp) throws IOException {
if (req.getMethod().equals("GET")) {
// read
checkPermission(EXTENDED_READ);
rsp.setContentType("application/xml");
getConfigFile().writeRawTo(rsp.getOutputStream());
return;
}
if (req.getMethod().equals("POST")) {
// submission
checkPermission(CONFIGURE);
XmlFile configXmlFile = getConfigFile();
AtomicFileWriter out = new AtomicFileWriter(configXmlFile.getFile());
try {
try {
// this allows us to use UTF-8 for storing data,
// plus it checks any well-formedness issue in the submitted
// data
Transformer t = TransformerFactory.newInstance().newTransformer();
t.transform(new StreamSource(req.getReader()), new StreamResult(out));
out.close();
} catch (TransformerException e) {
throw new IOException2("Failed to persist configuration.xml", e);
}
// try to reflect the changes by reloading
new XmlFile(Items.XSTREAM, out.getTemporaryFile()).unmarshal(this);
onLoad(getParent(), getRootDir().getName());
// if everything went well, commit this new version
out.commit();
} finally {
// don't leave anything behind
out.abort();
}
return;
}
// huh?
rsp.sendError(SC_BAD_REQUEST);
}
Aggregations