An error occurred while loading the file. Please try again.
An error occurred while loading the file. Please try again.
An error occurred while loading the file. Please try again.
-
David Lam authored
* Added Health Check API support
900c1a91
package org.gitlab4j.api;
import static javax.ws.rs.core.MediaType.MULTIPART_FORM_DATA_TYPE;
import java.io.File;
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 javax.ws.rs.core.StreamingOutput;
import org.gitlab4j.api.Constants.TokenType;
import org.gitlab4j.api.GitLabApi.ApiVersion;
import org.gitlab4j.api.utils.JacksonJson;
import org.glassfish.jersey.apache.connector.ApacheConnectorProvider;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.ClientProperties;
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;
/**
* 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 baseUrl;
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.
*
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
* @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
* @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);
}
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 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);
}
/**
* 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);
this.baseUrl = this.hostUrl;
if (ApiVersion.OAUTH2_CLIENT != apiVersion) {
211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
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;
clientConfig = new ClientConfig();
if (clientConfigProperties != null) {
if (clientConfigProperties.containsKey(ClientProperties.PROXY_URI)) {
clientConfig.connectorProvider(new ApacheConnectorProvider());
}
for (Map.Entry<String, Object> propertyEntry : clientConfigProperties.entrySet()) {
clientConfig.property(propertyEntry.getKey(), propertyEntry.getValue());
}
}
clientConfig.register(JacksonJson.class);
clientConfig.register(MultiPartFeature.class);
}
/**
* Get the auth token being used by this client.
*
* @return the auth token being used by this client
*/
String getAuthToken() {
return (authToken);
}
/**
* Get the secret token.
*
* @return the secret token
*/
String getSecretToken() {
return (secretToken);
}
/**
* Get the TokenType this client is using.
*
* @return the TokenType this client is using
*/
TokenType getTokenType() {
return (tokenType);
}
/**
* Set the ID of the user to sudo as.
*
*/
Integer getSudoAsId() {
return (sudoAsId);
}
/**
* Set the ID of the user to sudo as.
*
* @param sudoAsId the ID of the user to sudo as
*/
void setSudoAsId(Integer sudoAsId) {
281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
this.sudoAsId = sudoAsId;
}
/**
* Construct a REST URL with the specified path arguments.
*
* @param pathArgs variable list of arguments used to build the URI
* @return a REST URL with the specified path arguments
* @throws IOException if an error occurs while constructing the URL
*/
protected URL getApiUrl(Object... pathArgs) throws IOException {
String url = appendPathArgs(this.hostUrl, pathArgs);
return (new URL(url));
}
/**
* Construct a REST URL with the specified path arguments using
* Gitlab base url.
*
* @param pathArgs variable list of arguments used to build the URI
* @return a REST URL with the specified path arguments
* @throws IOException if an error occurs while constructing the URL
*/
protected URL getUrlWithBase(Object... pathArgs) throws IOException {
String url = appendPathArgs(this.baseUrl, pathArgs);
return (new URL(url));
}
private String appendPathArgs(String url, Object... pathArgs) {
StringBuilder urlBuilder = new StringBuilder(url);
for (Object pathArg : pathArgs) {
if (pathArg != null) {
urlBuilder.append("/");
urlBuilder.append(pathArg.toString());
}
}
return urlBuilder.toString();
}
/**
* Validates the secret token (X-GitLab-Token) header against the expected secret token, returns true if valid,
* otherwise returns false.
*
* @param response the Response instance sent from the GitLab server
* @return true if the response's secret token is valid, otherwise returns false
*/
protected boolean validateSecretToken(Response response) {
if (this.secretToken == null)
return (true);
String secretToken = response.getHeaderString(X_GITLAB_TOKEN_HEADER);
if (secretToken == null)
return (false);
return (this.secretToken.equals(secretToken));
}
/**
* Perform an HTTP GET call with the specified query parameters and path objects, returning
* a ClientResponse instance with the data returned from the endpoint.
*
* @param queryParams multivalue map of request parameters
* @param pathArgs variable list of arguments used to build the URI
* @return a ClientResponse instance with the data returned from the endpoint
* @throws IOException if an error occurs while constructing the URL
*/
protected Response get(MultivaluedMap<String, String> queryParams, Object... pathArgs) throws IOException {
URL url = getApiUrl(pathArgs);
return (get(queryParams, url));
351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
}
/**
* Perform an HTTP GET call with the specified query parameters and URL, returning
* a ClientResponse instance with the data returned from the endpoint.
*
* @param queryParams multivalue map of request parameters
* @param url the fully formed path to the GitLab API endpoint
* @return a ClientResponse instance with the data returned from the endpoint
*/
protected Response get(MultivaluedMap<String, String> queryParams, URL url) {
return (invocation(url, queryParams).get());
}
/**
* Perform an HTTP GET call with the specified query parameters and path objects, returning
* a ClientResponse instance with the data returned from the endpoint.
*
* @param queryParams multivalue map of request parameters
* @param accepts if non-empty will set the Accepts header to this value
* @param pathArgs variable list of arguments used to build the URI
* @return a ClientResponse instance with the data returned from the endpoint
* @throws IOException if an error occurs while constructing the URL
*/
protected Response getWithAccepts(MultivaluedMap<String, String> queryParams, String accepts, Object... pathArgs) throws IOException {
URL url = getApiUrl(pathArgs);
return (getWithAccepts(queryParams, url, accepts));
}
/**
* Perform an HTTP GET call with the specified query parameters and URL, returning
* a ClientResponse instance with the data returned from the endpoint.
*
* @param queryParams multivalue map of request parameters
* @param url the fully formed path to the GitLab API endpoint
* @param accepts if non-empty will set the Accepts header to this value
* @return a ClientResponse instance with the data returned from the endpoint
*/
protected Response getWithAccepts(MultivaluedMap<String, String> queryParams, URL url, String accepts) {
return (invocation(url, queryParams, accepts).get());
}
/**
* Perform an HTTP POST call with the specified form data and path objects, returning
* a ClientResponse instance with the data returned from the endpoint.
*
* @param formData the Form containing the name/value pairs
* @param pathArgs variable list of arguments used to build the URI
* @return a ClientResponse instance with the data returned from the endpoint
* @throws IOException if an error occurs while constructing the URL
*/
protected Response post(Form formData, Object... pathArgs) throws IOException {
URL url = getApiUrl(pathArgs);
return post(formData, url);
}
/**
* Perform an HTTP POST call with the specified form data and path objects, returning
* a ClientResponse instance with the data returned from the endpoint.
*
* @param queryParams multivalue map of request parameters
* @param pathArgs variable list of arguments used to build the URI
* @return a Response instance with the data returned from the endpoint
* @throws IOException if an error occurs while constructing the URL
*/
protected Response post(MultivaluedMap<String, String> queryParams, Object... pathArgs) throws IOException {
URL url = getApiUrl(pathArgs);
return post(queryParams, url);
}
421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
/**
* Perform an HTTP POST call with the specified form data and URL, returning
* a ClientResponse instance with the data returned from the endpoint.
*
* @param formData the Form containing the name/value pairs
* @param url the fully formed path to the GitLab API endpoint
* @return a ClientResponse instance with the data returned from the endpoint
*/
protected Response post(Form formData, URL url) {
if (formData instanceof GitLabApiForm)
return (invocation(url, null).post(Entity.entity(formData.asMap(), MediaType.APPLICATION_FORM_URLENCODED_TYPE)));
else
return (invocation(url, null).post(Entity.entity(formData, MediaType.APPLICATION_FORM_URLENCODED_TYPE)));
}
/**
* Perform an HTTP POST call with the specified form data and URL, returning
* a ClientResponse instance with the data returned from the endpoint.
*
* @param queryParams multivalue map of request parametersformData the Form containing the name/value pairs
* @param url the fully formed path to the GitLab API endpoint
* @return a ClientResponse instance with the data returned from the endpoint
*/
protected Response post(MultivaluedMap<String, String> queryParams, URL url) {
return (invocation(url, queryParams).post(null));
}
/**
* Perform an HTTP POST call with the specified payload object and URL, returning
* a ClientResponse instance with the data returned from the endpoint.
*
* @param payload the object instance that will be serialized to JSON and used as the POST data
* @param pathArgs variable list of arguments used to build the URI
* @return a ClientResponse instance with the data returned from the endpoint
* @throws IOException if an error occurs while constructing the URL
*/
protected Response post(Object payload, Object... pathArgs) throws IOException {
URL url = getApiUrl(pathArgs);
Entity<?> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
return (invocation(url, null).post(entity));
}
/**
* Perform an HTTP POST call with the specified StreamingOutput, MediaType, and path objects, returning
* a ClientResponse instance with the data returned from the endpoint.
*
* @param stream the StreamingOutput instance that contains the POST data
* @param mediaType the content-type of the POST data
* @param pathArgs variable list of arguments used to build the URI
* @return a ClientResponse instance with the data returned from the endpoint
* @throws IOException if an error occurs while constructing the URL
*/
protected Response post(StreamingOutput stream, String mediaType, Object... pathArgs) throws IOException {
URL url = getApiUrl(pathArgs);
return (invocation(url, null).post(Entity.entity(stream, mediaType)));
}
/**
* Perform a file upload as part of the , returning
* a ClientResponse instance with the data returned from the endpoint.
*
* @param name the name for the form field that contains the file name
* @param fileToUpload a File instance pointing to the file to upload
* @param mediaTypeString the content-type of the uploaded file, if null will be determined from fileToUpload
* @param pathArgs variable list of arguments used to build the URI
* @return a ClientResponse instance with the data returned from the endpoint
* @throws IOException if an error occurs while constructing the URL
*/
protected Response upload(String name, File fileToUpload, String mediaTypeString, Object... pathArgs) throws IOException {
URL url = getApiUrl(pathArgs);
491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
return (upload(name, fileToUpload, mediaTypeString, url));
}
/**
* Perform a file upload using multipart/form-data, returning
* a ClientResponse instance with the data returned from the endpoint.
*
* @param name the name for the form field that contains the file name
* @param fileToUpload a File instance pointing to the file to upload
* @param mediaTypeString the content-type of the uploaded file, if null will be determined from fileToUpload
* @param url the fully formed path to the GitLab API endpoint
* @return a ClientResponse instance with the data returned from the endpoint
* @throws IOException if an error occurs while constructing the URL
*/
protected Response upload(String name, File fileToUpload, String mediaTypeString, URL url) throws IOException {
MediaType mediaType = (mediaTypeString != null ? MediaType.valueOf(mediaTypeString) : null);
try (MultiPart multiPart = new FormDataMultiPart()) {
FileDataBodyPart filePart = mediaType != null ?
new FileDataBodyPart(name, fileToUpload, mediaType) :
new FileDataBodyPart(name, fileToUpload);
multiPart.bodyPart(filePart);
return (invocation(url, null).post(Entity.entity(multiPart, MULTIPART_FORM_DATA_TYPE)));
}
}
/**
* Perform an HTTP PUT call with the specified form data and path objects, returning
* a ClientResponse instance with the data returned from the endpoint.
*
* @param queryParams multivalue map of request parameters
* @param pathArgs variable list of arguments used to build the URI
* @return a ClientResponse instance with the data returned from the endpoint
* @throws IOException if an error occurs while constructing the URL
*/
protected Response put(MultivaluedMap<String, String> queryParams, Object... pathArgs) throws IOException {
URL url = getApiUrl(pathArgs);
return (put(queryParams, url));
}
/**
* Perform an HTTP PUT call with the specified form data and URL, returning
* a ClientResponse instance with the data returned from the endpoint.
*
* @param queryParams multivalue map of request parameters
* @param url the fully formed path to the GitLab API endpoint
* @return a ClientResponse instance with the data returned from the endpoint
*/
protected Response put(MultivaluedMap<String, String> queryParams, URL url) {
if (queryParams == null || queryParams.isEmpty()) {
Entity<?> empty = Entity.text("");
return (invocation(url, null).put(empty));
} else {
return (invocation(url, null).put(Entity.entity(queryParams, MediaType.APPLICATION_FORM_URLENCODED_TYPE)));
}
}
/**
* Perform an HTTP PUT call with the specified form data and path objects, returning
* a ClientResponse instance with the data returned from the endpoint.
*
* @param formData the Form containing the name/value pairs
* @param pathArgs variable list of arguments used to build the URI
* @return a ClientResponse instance with the data returned from the endpoint
* @throws IOException if an error occurs while constructing the URL
*/
protected Response put(Form formData, Object... pathArgs) throws IOException {
URL url = getApiUrl(pathArgs);
return put(formData, url);
}
561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
/**
* Perform an HTTP PUT call with the specified form data and URL, returning
* a ClientResponse instance with the data returned from the endpoint.
*
* @param formData the Form containing the name/value pairs
* @param url the fully formed path to the GitLab API endpoint
* @return a ClientResponse instance with the data returned from the endpoint
*/
protected Response put(Form formData, URL url) {
if (formData instanceof GitLabApiForm)
return (invocation(url, null).put(Entity.entity(formData.asMap(), MediaType.APPLICATION_FORM_URLENCODED_TYPE)));
else
return (invocation(url, null).put(Entity.entity(formData, MediaType.APPLICATION_FORM_URLENCODED_TYPE)));
}
/**
* Perform an HTTP DELETE call with the specified form data and path objects, returning
* a Response instance with the data returned from the endpoint.
*
* @param queryParams multivalue map of request parameters
* @param pathArgs variable list of arguments used to build the URI
* @return a Response instance with the data returned from the endpoint
* @throws IOException if an error occurs while constructing the URL
*/
protected Response delete(MultivaluedMap<String, String> queryParams, Object... pathArgs) throws IOException {
return (delete(queryParams, getApiUrl(pathArgs)));
}
/**
* Perform an HTTP DELETE call with the specified form data and URL, returning
* a Response instance with the data returned from the endpoint.
*
* @param queryParams multivalue map of request parameters
* @param url the fully formed path to the GitLab API endpoint
* @return a Response instance with the data returned from the endpoint
*/
protected Response delete(MultivaluedMap<String, String> queryParams, URL url) {
return (invocation(url, queryParams).delete());
}
protected Invocation.Builder invocation(URL url, MultivaluedMap<String, String> queryParams) {
return (invocation(url, queryParams, MediaType.APPLICATION_JSON));
}
protected Invocation.Builder invocation(URL url, MultivaluedMap<String, String> queryParams, String accept) {
if (apiClient == null) {
if (ignoreCertificateErrors) {
apiClient = ClientBuilder.newBuilder()
.withConfig(clientConfig)
.sslContext(openSslContext)
.hostnameVerifier(openHostnameVerifier)
.build();
} else {
apiClient = ClientBuilder.newBuilder().withConfig(clientConfig).build();
}
}
WebTarget target = apiClient.target(url.toExternalForm()).property(ClientProperties.FOLLOW_REDIRECTS, true);
if (queryParams != null) {
for (Map.Entry<String, List<String>> param : queryParams.entrySet()) {
target = target.queryParam(param.getKey(), param.getValue().toArray());
}
}
String authHeader = (tokenType == TokenType.ACCESS ? AUTHORIZATION_HEADER : PRIVATE_TOKEN_HEADER);
String authValue = (tokenType == TokenType.ACCESS ? "Bearer " + authToken : authToken);
Invocation.Builder builder = target.request();
if (accept == null || accept.trim().length() == 0) {
631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
builder = builder.header(authHeader, authValue);
} else {
builder = builder.header(authHeader, authValue).accept(accept);
}
// If sudo as ID is set add the Sudo header
if (sudoAsId != null && sudoAsId.intValue() > 0)
builder = builder.header(SUDO_HEADER, sudoAsId);
return (builder);
}
/**
* Returns true if the API is setup to ignore SSL certificate errors, otherwise returns false.
*
* @return true if the API is setup to ignore SSL certificate errors, otherwise returns false
*/
public boolean getIgnoreCertificateErrors() {
return (ignoreCertificateErrors);
}
/**
* Sets up the Jersey system ignore SSL certificate errors or not.
*
* @param ignoreCertificateErrors if true will set up the Jersey system ignore SSL certificate errors
*/
public void setIgnoreCertificateErrors(boolean ignoreCertificateErrors) {
if (this.ignoreCertificateErrors == ignoreCertificateErrors) {
return;
}
if (!ignoreCertificateErrors) {
this.ignoreCertificateErrors = false;
openSslContext = null;
openHostnameVerifier = null;
apiClient = null;
} else {
if (setupIgnoreCertificateErrors()) {
this.ignoreCertificateErrors = true;
apiClient = null;
} else {
this.ignoreCertificateErrors = false;
apiClient = null;
throw new RuntimeException("Unable to ignore certificate errors.");
}
}
}
/**
* Sets up Jersey client to ignore certificate errors.
*
* @return true if successful at setting up to ignore certificate errors, otherwise returns false.
*/
private boolean setupIgnoreCertificateErrors() {
// Create a TrustManager that trusts all certificates
TrustManager[] trustAllCerts = new TrustManager[] { new X509ExtendedTrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType, Socket socket) throws CertificateException {
}
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType, SSLEngine engine) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType, Socket socket) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType, SSLEngine engine) throws CertificateException {
}
}};
// Ignore differences between given hostname and certificate hostname
HostnameVerifier hostnameVerifier = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
try {
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustAllCerts, new SecureRandom());
openSslContext = sslContext;
openHostnameVerifier = hostnameVerifier;
} catch (GeneralSecurityException ex) {
openSslContext = null;
openHostnameVerifier = null;
return (false);
}
return (true);
}
}