GitLabApiClient.java 40.45 KiB
package org.gitlab4j.api;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.net.URL;
import java.security.GeneralSecurityException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509ExtendedTrustManager;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.client.Invocation;
import jakarta.ws.rs.client.WebTarget;
import jakarta.ws.rs.core.Form;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.StreamingOutput;
import org.gitlab4j.api.Constants.TokenType;
import org.gitlab4j.api.GitLabApi.ApiVersion;
import org.gitlab4j.api.utils.JacksonJson;
import org.gitlab4j.api.utils.MaskingLoggingFilter;
import org.glassfish.jersey.apache.connector.ApacheConnectorProvider;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.client.JerseyClientBuilder;
import org.glassfish.jersey.jackson.JacksonFeature;
import org.glassfish.jersey.media.multipart.BodyPart;
import org.glassfish.jersey.media.multipart.Boundary;
import org.glassfish.jersey.media.multipart.FormDataMultiPart;
import org.glassfish.jersey.media.multipart.MultiPart;
import org.glassfish.jersey.media.multipart.MultiPartFeature;
import org.glassfish.jersey.media.multipart.file.FileDataBodyPart;
import org.glassfish.jersey.media.multipart.file.StreamDataBodyPart;
/**
 * This class utilizes the Jersey client package to communicate with a GitLab API endpoint.
public class GitLabApiClient implements AutoCloseable {
    protected static final String PRIVATE_TOKEN_HEADER  = "PRIVATE-TOKEN";
    protected static final String SUDO_HEADER           = "Sudo";
    protected static final String AUTHORIZATION_HEADER  = "Authorization";
    protected static final String X_GITLAB_TOKEN_HEADER = "X-Gitlab-Token";
    private ClientConfig clientConfig;
    private Client apiClient;
    private String baseUrl;
    private String hostUrl;
    private TokenType tokenType = TokenType.PRIVATE;
    private Supplier<String> authToken;
    private String secretToken;
    private boolean ignoreCertificateErrors;
    private SSLContext openSslContext;
    private HostnameVerifier openHostnameVerifier;
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
private Long sudoAsId; private Integer connectTimeout; private Integer readTimeout; /** * Construct an instance to communicate with a GitLab API server using the specified GitLab API version, * server URL, private token, and secret token. * * @param apiVersion the ApiVersion specifying which version of the API to use * @param hostUrl the URL to the GitLab API server * @param privateToken the private token to authenticate with */ public GitLabApiClient(ApiVersion apiVersion, String hostUrl, String privateToken) { this(apiVersion, hostUrl, TokenType.PRIVATE, privateToken, null); } /** * Construct an instance to communicate with a GitLab API server using the specified GitLab API version, * server URL, auth token type, private or access token, and secret token. * * @param apiVersion the ApiVersion specifying which version of the API to use * @param hostUrl the URL to the GitLab API server * @param tokenType the type of auth the token is for, PRIVATE or ACCESS * @param authToken the token to authenticate with */ public GitLabApiClient(ApiVersion apiVersion, String hostUrl, TokenType tokenType, String authToken) { this(apiVersion, hostUrl, tokenType, authToken, null); } /** * Construct an instance to communicate with a GitLab API server using GitLab API version 4, and the specified * server URL, private token, and secret token. * * @param hostUrl the URL to the GitLab API server * @param privateToken the private token to authenticate with */ public GitLabApiClient(String hostUrl, String privateToken) { this(ApiVersion.V4, hostUrl, TokenType.PRIVATE, privateToken, null); } /** * Construct an instance to communicate with a GitLab API server using GitLab API version 4, and the specified * server URL, private token, and secret token. * * @param hostUrl the URL to the GitLab API server * @param tokenType the type of auth the token is for, PRIVATE or ACCESS * @param authToken the token to authenticate with */ public GitLabApiClient(String hostUrl, TokenType tokenType, String authToken) { this(ApiVersion.V4, hostUrl, tokenType, authToken, null); } /** * Construct an instance to communicate with a GitLab API server using the specified GitLab API version, * server URL, private token, and secret token. * * @param apiVersion the ApiVersion specifying which version of the API to use * @param hostUrl the URL to the GitLab API server * @param privateToken the private token to authenticate with * @param secretToken use this token to validate received payloads */ public GitLabApiClient(ApiVersion apiVersion, String hostUrl, String privateToken, String secretToken) { this(apiVersion, hostUrl, TokenType.PRIVATE, privateToken, secretToken, null); } /** * Construct an instance to communicate with a GitLab API server using the specified GitLab API version, * server URL, private token, and secret token. * * @param apiVersion the ApiVersion specifying which version of the API to use