GitLabApiClient.java 26.19 KiB
package org.gitlab4j.api;
import java.io.IOException;
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 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 javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import org.gitlab4j.api.Constants.TokenType;
import org.gitlab4j.api.GitLabApi.ApiVersion;
import org.gitlab4j.api.utils.JacksonJson;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.ClientProperties;
/**
 * This class utilizes the Jersey client package to communicate with a GitLab API endpoint.
public class GitLabApiClient {
    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 hostUrl;
    private TokenType tokenType = TokenType.PRIVATE;
    private String authToken;
    private String secretToken;
    private boolean ignoreCertificateErrors;
    private SSLContext openSslContext;
    private HostnameVerifier openHostnameVerifier;
    private Integer sudoAsId;
    /**
     * 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.
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
* * @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 * @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 * @param secretToken use this token to validate received payloads */ public GitLabApiClient(ApiVersion apiVersion, String hostUrl, TokenType tokenType, String authToken, String secretToken) { this(apiVersion, hostUrl, tokenType, authToken, secretToken, 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 * @param secretToken use this token to validate received payloads */ public GitLabApiClient(String hostUrl, String privateToken, String secretToken) { this(ApiVersion.V4, hostUrl, TokenType.PRIVATE, privateToken, secretToken, null);
141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
} /** * 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 * @param secretToken use this token to validate received payloads */ public GitLabApiClient(String hostUrl, TokenType tokenType, String authToken, String secretToken) { this(ApiVersion.V4, hostUrl, tokenType, authToken, secretToken, null); } /** * Construct an instance to communicate with a GitLab API server using GitLab API version 4, and the specified * server URL and private token. * * @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 * @param clientConfigProperties the properties given to Jersey's clientconfig */ public GitLabApiClient(String hostUrl, String privateToken, String secretToken, Map<String, Object> clientConfigProperties) { this(ApiVersion.V4, hostUrl, TokenType.PRIVATE, privateToken, secretToken, clientConfigProperties); } /** * Construct an instance to communicate with a GitLab API server using the specified GitLab API version, * server URL and private 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 * @param clientConfigProperties the properties given to Jersey's clientconfig */ public GitLabApiClient(ApiVersion apiVersion, String hostUrl, String privateToken, String secretToken, Map<String, Object> clientConfigProperties) { this(apiVersion, hostUrl, TokenType.PRIVATE, privateToken, secretToken, clientConfigProperties); } /** * Construct an instance to communicate with a GitLab API server using the specified GitLab API version, * server URL and private 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 private token to authenticate with * @param secretToken use this token to validate received payloads * @param clientConfigProperties the properties given to Jersey's clientconfig */ public GitLabApiClient(ApiVersion apiVersion, String hostUrl, TokenType tokenType, String authToken, String secretToken, Map<String, Object> clientConfigProperties) { // Remove the trailing "/" from the hostUrl if present this.hostUrl = (hostUrl.endsWith("/") ? hostUrl.replaceAll("/$", "") : hostUrl); if (ApiVersion.OAUTH2_CLIENT != apiVersion) { this.hostUrl += apiVersion.getApiNamespace(); } this.tokenType = tokenType; this.authToken = authToken; if (secretToken != null) { secretToken = secretToken.trim(); secretToken = (secretToken.length() > 0 ? secretToken : null); } this.secretToken = secretToken;