Search in sources :

Example 1 with WebMethod

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);
}
Also used : JSONObject(net.sf.json.JSONObject) WebMethod(org.kohsuke.stapler.WebMethod) TreeResponse(io.jenkins.blueocean.commons.stapler.TreeResponse) PUT(org.kohsuke.stapler.verb.PUT)

Example 2 with WebMethod

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);
    }
}
Also used : WorkflowMultiBranchProject(org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject) StandardUsernamePasswordCredentials(com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials) Item(hudson.model.Item) GHRepository(org.kohsuke.github.GHRepository) ServiceException(io.jenkins.blueocean.commons.ServiceException) GitHubSCMSource(org.jenkinsci.plugins.github_branch_source.GitHubSCMSource) IOException(java.io.IOException) WebMethod(org.kohsuke.stapler.WebMethod) TreeResponse(io.jenkins.blueocean.commons.stapler.TreeResponse) GET(org.kohsuke.stapler.verb.GET)

Example 3 with WebMethod

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);
}
Also used : TrackRequest(io.jenkins.blueocean.analytics.Analytics.TrackRequest) InputStream(java.io.InputStream) WebMethod(org.kohsuke.stapler.WebMethod) POST(org.kohsuke.stapler.verb.POST)

Example 4 with WebMethod

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);
}
Also used : ErrorMessage(io.jenkins.blueocean.commons.ErrorMessage) WebMethod(org.kohsuke.stapler.WebMethod) POST(org.kohsuke.stapler.verb.POST)

Example 5 with WebMethod

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);
}
Also used : Transformer(javax.xml.transform.Transformer) XmlFile(hudson.XmlFile) StreamResult(javax.xml.transform.stream.StreamResult) AtomicFileWriter(hudson.util.AtomicFileWriter) StreamSource(javax.xml.transform.stream.StreamSource) TransformerException(javax.xml.transform.TransformerException) IOException2(hudson.util.IOException2) WebMethod(org.kohsuke.stapler.WebMethod)

Aggregations

WebMethod (org.kohsuke.stapler.WebMethod)9 JSONObject (net.sf.json.JSONObject)3 POST (org.kohsuke.stapler.verb.POST)3 TreeResponse (io.jenkins.blueocean.commons.stapler.TreeResponse)2 IOException (java.io.IOException)2 BitbucketEndpointConfiguration (com.cloudbees.jenkins.plugins.bitbucket.endpoints.BitbucketEndpointConfiguration)1 CredentialsStoreAction (com.cloudbees.plugins.credentials.CredentialsStoreAction)1 IdCredentials (com.cloudbees.plugins.credentials.common.IdCredentials)1 StandardUsernamePasswordCredentials (com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials)1 XmlFile (hudson.XmlFile)1 Item (hudson.model.Item)1 User (hudson.model.User)1 AtomicFileWriter (hudson.util.AtomicFileWriter)1 IOException2 (hudson.util.IOException2)1 TrackRequest (io.jenkins.blueocean.analytics.Analytics.TrackRequest)1 ErrorMessage (io.jenkins.blueocean.commons.ErrorMessage)1 ServiceException (io.jenkins.blueocean.commons.ServiceException)1 CreateResponse (io.jenkins.blueocean.rest.model.CreateResponse)1 InputStream (java.io.InputStream)1 PrintWriter (java.io.PrintWriter)1