com.chavaillaz.client.jenkins.java.JavaHttpSystemApi Maven / Gradle / Ivy
package com.chavaillaz.client.jenkins.java;
import static java.net.http.HttpRequest.BodyPublishers.noBody;
import static java.net.http.HttpResponse.BodyHandlers.discarding;
import java.net.http.HttpClient;
import java.net.http.HttpResponse;
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;
/**
* Implementation of {@link SystemApi} for Java HTTP.
*/
public class JavaHttpSystemApi extends AbstractJavaHttpClient implements SystemApi {
/**
* Creates a new system client based on Java HTTP client.
*
* @param client The Java HTTP client to use
* @param baseUrl The URL of Jenkins
* @param authentication The authentication method
*/
public JavaHttpSystemApi(HttpClient client, String baseUrl, JenkinsAuthentication authentication) {
super(client, baseUrl, authentication);
}
@Override
public CompletableFuture getSystemInfo() {
return client.sendAsync(requestBuilder(URL_SYSTEM).method("HEAD", noBody()).build(), discarding())
.thenApply(this::checkResponse)
.thenApply(this::systemInfoFromHeader);
}
protected SystemInfo systemInfoFromHeader(HttpResponse response) {
return new SystemInfo(
response.headers().firstValue("X-Hudson").orElse(null),
response.headers().firstValue("X-Instance-Identity").orElse(null),
response.headers().firstValue("X-Jenkins-Session").orElse(null),
response.headers().firstValue("X-Jenkins").orElse(null),
response.headers().firstValue("Server").orElse(null),
response.headers().firstValue("X-SSH-Endpoint").orElse(null)
);
}
@Override
public CompletableFuture getOverallLoad() {
return sendAsync(requestBuilder(URL_LOAD).GET(), Load.class);
}
@Override
public CompletableFuture quietDown() {
return sendAsync(requestBuilder(URL_QUIET_DOWN_START).POST(noBody()), Void.class);
}
@Override
public CompletableFuture cancelQuietDown() {
return sendAsync(requestBuilder(URL_QUIET_DOWN_CANCELLATION).POST(noBody()), Void.class);
}
@Override
public CompletableFuture restart() {
return sendAsync(requestBuilder(URL_RESTART).POST(noBody()), Void.class);
}
@Override
public CompletableFuture safeRestart() {
return sendAsync(requestBuilder(URL_RESTART_SAFE).POST(noBody()), Void.class);
}
@Override
public CompletableFuture exit() {
return sendAsync(requestBuilder(URL_EXIT).POST(noBody()), Void.class);
}
@Override
public CompletableFuture safeExit() {
return sendAsync(requestBuilder(URL_EXIT_SAFE).POST(noBody()), Void.class);
}
@Override
public CompletableFuture reload() {
return sendAsync(requestBuilder(URL_RELOAD).POST(noBody()), Void.class);
}
}