All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.chavaillaz.client.jenkins.apache.ApacheHttpSystemApi Maven / Gradle / Ivy

package com.chavaillaz.client.jenkins.apache;

import static org.apache.hc.client5.http.async.methods.SimpleRequestBuilder.get;
import static org.apache.hc.client5.http.async.methods.SimpleRequestBuilder.head;
import static org.apache.hc.client5.http.async.methods.SimpleRequestBuilder.post;

import java.util.Optional;
import java.util.concurrent.CompletableFuture;

import com.chavaillaz.client.jenkins.JenkinsAuthentication;
import com.chavaillaz.client.jenkins.api.SystemApi;
import com.chavaillaz.client.jenkins.domain.system.Load;
import com.chavaillaz.client.jenkins.domain.system.SystemInfo;
import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
import org.apache.hc.core5.http.NameValuePair;

/**
 * Implementation of {@link SystemApi} for Apache HTTP.
 */
public class ApacheHttpSystemApi extends AbstractApacheHttpClient implements SystemApi {

    /**
     * Creates a new system 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 ApacheHttpSystemApi(CloseableHttpAsyncClient client, String baseUrl, JenkinsAuthentication authentication) {
        super(client, baseUrl, authentication);
    }

    @Override
    public CompletableFuture getSystemInfo() {
        return sendAsyncBase(requestBuilder(head(), URL_SYSTEM))
                .thenApply(this::systemInfoFromHeader);
    }

    protected SystemInfo systemInfoFromHeader(SimpleHttpResponse response) {
        return new SystemInfo(
                extractHeader(response, "X-Hudson"),
                extractHeader(response, "X-Instance-Identity"),
                extractHeader(response, "X-Jenkins-Session"),
                extractHeader(response, "X-Jenkins"),
                extractHeader(response, "Server"),
                extractHeader(response, "X-SSH-Endpoint")
        );
    }

    protected String extractHeader(SimpleHttpResponse response, String header) {
        return Optional.of(header)
                .map(response::getFirstHeader)
                .map(NameValuePair::getValue)
                .orElse(null);
    }

    @Override
    public CompletableFuture getOverallLoad() {
        return sendAsync(requestBuilder(get(), URL_LOAD), Load.class);
    }

    @Override
    public CompletableFuture quietDown() {
        return sendAsync(requestBuilder(post(), URL_QUIET_DOWN_START), Void.class);
    }

    @Override
    public CompletableFuture cancelQuietDown() {
        return sendAsync(requestBuilder(post(), URL_QUIET_DOWN_CANCELLATION), Void.class);
    }

    @Override
    public CompletableFuture restart() {
        return sendAsync(requestBuilder(post(), URL_RESTART), Void.class);
    }

    @Override
    public CompletableFuture safeRestart() {
        return sendAsync(requestBuilder(post(), URL_RESTART_SAFE), Void.class);
    }

    @Override
    public CompletableFuture exit() {
        return sendAsync(requestBuilder(post(), URL_EXIT), Void.class);
    }

    @Override
    public CompletableFuture safeExit() {
        return sendAsync(requestBuilder(post(), URL_EXIT_SAFE), Void.class);
    }

    @Override
    public CompletableFuture reload() {
        return sendAsync(requestBuilder(post(), URL_RELOAD), Void.class);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy