Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.chavaillaz.client.jenkins.apache.ApacheHttpJobApi Maven / Gradle / Ivy
package com.chavaillaz.client.jenkins.apache;
import static com.chavaillaz.client.common.apache.ApacheHttpUtils.formData;
import static com.chavaillaz.client.common.utility.Utils.encodeQuery;
import static com.chavaillaz.client.jenkins.JenkinsConstant.FOLDER_MODE;
import static com.chavaillaz.client.jenkins.JenkinsConstant.LIST_VIEW;
import static org.apache.hc.client5.http.async.methods.SimpleRequestBuilder.get;
import static org.apache.hc.client5.http.async.methods.SimpleRequestBuilder.post;
import static org.apache.hc.core5.http.ContentType.APPLICATION_XML;
import java.io.InputStream;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import com.chavaillaz.client.common.utility.Utils;
import com.chavaillaz.client.jenkins.JenkinsAuthentication;
import com.chavaillaz.client.jenkins.api.JobApi;
import com.chavaillaz.client.jenkins.domain.folder.Folder;
import com.chavaillaz.client.jenkins.domain.folder.Path;
import com.chavaillaz.client.jenkins.domain.job.JobInfo;
import com.chavaillaz.client.jenkins.domain.job.build.BuildInfo;
import com.chavaillaz.client.jenkins.domain.job.test.CoverageReport;
import com.chavaillaz.client.jenkins.domain.job.test.TestReport;
import com.chavaillaz.client.jenkins.domain.view.ViewCreation;
import com.chavaillaz.client.jenkins.domain.view.ViewInfo;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
/**
* Implementation of {@link JobApi} for Apache HTTP.
*/
public class ApacheHttpJobApi extends AbstractApacheHttpClient implements JobApi {
/**
* Creates a new job client based on Apache HTTP client.
*
* @param client The Apache HTTP client to use
* @param baseUrl The URL of Jenkins
* @param authentication The authentication method
*/
public ApacheHttpJobApi(CloseableHttpAsyncClient client, String baseUrl, JenkinsAuthentication authentication) {
super(client, baseUrl, authentication);
}
@Override
public CompletableFuture getFolder(Path path) {
return sendAsync(requestBuilder(get(), URL_FOLDER, path), Folder.class);
}
@Override
public CompletableFuture getView(Path path, String viewName) {
return sendAsync(requestBuilder(get(), URL_VIEW_INFO, path, viewName), ViewInfo.class);
}
@Override
public CompletableFuture getJob(Path path, String jobName) {
return sendAsync(requestBuilder(get(), URL_JOB_INFO, path, jobName), JobInfo.class);
}
@Override
public CompletableFuture getFolderConfiguration(Path path) {
return sendAsync(requestBuilder(get(), URL_FOLDER_CONFIGURATION, path))
.thenApply(Utils::readInputStream);
}
@Override
public CompletableFuture getJobConfiguration(Path path, String jobName) {
return sendAsync(requestBuilder(get(), URL_JOB_CONFIGURATION, path, jobName))
.thenApply(Utils::readInputStream);
}
@Override
public CompletableFuture getViewConfiguration(Path path, String viewName) {
return sendAsync(requestBuilder(get(), URL_VIEW_CONFIGURATION, path, viewName))
.thenApply(Utils::readInputStream);
}
@Override
public CompletableFuture getJobDescription(Path path, String jobName) {
return sendAsync(requestBuilder(get(), URL_JOB_DESCRIPTION, path, jobName))
.thenApply(Utils::readInputStream);
}
@Override
public CompletableFuture getLastBuildNumber(Path path, String jobName) {
return sendAsync(requestBuilder(get(), URL_JOB_LAST_BUILD_NUMBER, path, jobName))
.thenApply(Utils::readInputStream)
.thenApply(NumberUtils::toInt)
.exceptionally(exception -> null);
}
@Override
public CompletableFuture getBuild(Path path, String jobName, int buildNumber) {
return sendAsync(requestBuilder(get(), URL_JOB_BUILD_INFO, path, jobName, buildNumber), BuildInfo.class);
}
@Override
public CompletableFuture getArtifact(Path path, String jobName, int buildNumber, String artifactPath) {
return sendAsync(requestBuilder(get(), URL_JOB_BUILD_ARTIFACT, path, jobName, buildNumber, artifactPath));
}
@Override
public CompletableFuture getTestReport(Path path, String jobName, int buildNumber) {
return sendAsync(requestBuilder(get(), URL_JOB_BUILD_TESTS, path, jobName, buildNumber), TestReport.class)
.exceptionally(exception -> null);
}
@Override
public CompletableFuture getTestCoverageReport(Path path, String jobName, int buildNumber) {
return sendAsync(requestBuilder(get(), URL_JOB_BUILD_COVERAGE, path, jobName, buildNumber), CoverageReport.class)
.exceptionally(exception -> null);
}
@Override
public CompletableFuture getConsoleOutput(Path path, String jobName, int bufferOffset) {
return sendAsync(requestBuilder(get(), URL_JOB_LAST_BUILD_CONSOLE, path, jobName, bufferOffset))
.thenApply(Utils::readInputStream);
}
@Override
public CompletableFuture getConsoleOutput(Path path, String jobName, int buildNumber, int bufferOffset) {
return sendAsync(requestBuilder(get(), URL_JOB_BUILD_CONSOLE, path, jobName, buildNumber, bufferOffset))
.thenApply(Utils::readInputStream);
}
@Override
public CompletableFuture createFolder(Path path, String folderName) {
return sendAsync(requestBuilder(post(), URL_FOLDER_CREATE, path, folderName, FOLDER_MODE), Void.class);
}
@Override
public CompletableFuture createJob(Path path, String jobName, String configXML) {
return sendAsync(requestBuilder(post(), URL_JOB_CREATE, path, jobName)
.setBody(configXML, APPLICATION_XML), Void.class);
}
@Override
public CompletableFuture createView(Path path, String viewName) {
return sendAsync(requestBuilder(post(), URL_VIEW_CREATE, path)
.setHeader(HEADER_CONTENT_TYPE, HEADER_CONTENT_FORM)
.addParameters(formData(Map.of(
"name", viewName,
"mode", LIST_VIEW,
"json", serialize(new ViewCreation(viewName, LIST_VIEW))
))), Void.class);
}
@Override
public CompletableFuture updateJobConfiguration(Path path, String jobName, String configXML) {
return sendAsync(requestBuilder(post(), URL_JOB_CONFIGURATION, path, jobName)
.setBody(configXML, APPLICATION_XML), Void.class);
}
@Override
public CompletableFuture updateViewConfiguration(Path path, String viewName, String configXML) {
return sendAsync(requestBuilder(post(), URL_VIEW_CONFIGURATION, path, viewName)
.setBody(configXML, APPLICATION_XML), Void.class);
}
@Override
public CompletableFuture renameFolder(Path path, String folderName, String newName) {
return sendAsync(requestBuilder(post(), URL_FOLDER_RENAME, path, folderName, newName), Void.class);
}
@Override
public CompletableFuture renameJob(Path path, String jobName, String newName) {
return sendAsync(requestBuilder(post(), URL_JOB_RENAME, path, jobName, newName), Void.class);
}
@Override
public CompletableFuture enableJob(Path path, String jobName) {
return sendAsync(requestBuilder(post(), URL_JOB_ENABLE, path, jobName), Void.class);
}
@Override
public CompletableFuture disableJob(Path path, String jobName) {
return sendAsync(requestBuilder(post(), URL_JOB_DISABLE, path, jobName), Void.class);
}
@Override
public CompletableFuture deleteFolder(Path path, String folderName) {
return sendAsync(requestBuilder(post(), URL_FOLDER_DELETE, path, folderName), Void.class);
}
@Override
public CompletableFuture deleteJob(Path path, String jobName) {
return sendAsync(requestBuilder(post(), URL_JOB_DELETE, path, jobName), Void.class);
}
@Override
public CompletableFuture deleteView(Path path, String viewName) {
return sendAsync(requestBuilder(post(), URL_VIEW_DELETE, path, viewName), Void.class);
}
@Override
public CompletableFuture buildJob(Path path, String jobName) {
return sendAsync(requestBuilder(post(), URL_JOB_BUILD, path, jobName), Void.class);
}
@Override
public CompletableFuture buildJob(Path path, String jobName, Map properties) {
return sendAsync(requestBuilder(post(), URL_JOB_BUILD_PARAMETERS, path, jobName, encodeQuery(properties)), Void.class);
}
@Override
public CompletableFuture stopBuild(Path path, String jobName, int buildNumber) {
return sendAsync(requestBuilder(post(), URL_JOB_BUILD_STOP, path, jobName, buildNumber), Void.class);
}
}