Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.messners.gitlab.api;
  
  
 import java.net.URL;
 import java.util.List;
 import java.util.Map;

This class utilizes the Jersey client package to communicate with a GitLab API endpoint.

Author(s):
Greg Messner <greg@messners.com>
 
 public class GitLabApiClient {
 	
 	protected static final String PRIVATE_TOKEN_HEADER = "PRIVATE-TOKEN";
 	protected static final String API_NAMESPACE = "/api/v3";
 	
 	private Client apiClient;
 	private String hostUrl;
 	private String privateToken;
 	private static boolean ignoreCertificateErrors;
Construct an instance to communicate with a GitLab API server using the specified server URL and private token.

Parameters:
hostUrl the URL to the GitLab API server
privateToken the private token to authenticate with
 
 	public GitLabApiClient (String hostUrlString privateToken) {	
 		
 		// Remove the trailing "/" from the hostUrl if present
 		this. = (hostUrl.endsWith("/") ? hostUrl.replaceAll("/$""") : hostUrl) + ;
 		this. = privateToken;
 
 	}


Returns true if the API is setup to ignore SSL certificate errors, otherwise returns false.

Returns:
true if the API is setup to ignore SSL certificate errors, otherwise returns false
 
 	public boolean getIgnoreCertificateErrors () {
 	}


Sets up the Jersey system ignore SSL certificate errors or not.

WARNING: Setting this to true will affect ALL uses of HttpsURLConnection and Jersey.

Parameters:
ignoreCertificateErrors
 
 	public void setIgnoreCerificateErrors (boolean ignoreCertificateErrors) {
 		
 		if (. == ignoreCertificateErrors) {
 			return;
 		}
 		
 		if (ignoreCertificateErrors == false) {	
 			return;
 		}
 		
 		SSLSocketFactory defaultSocketFactory = HttpsURLConnection.getDefaultSSLSocketFactory();
 
 		if (ignoreCertificateErrors() == true) {
			. = defaultSocketFactory;
else {
			throw new RuntimeException("Unable to ignore certificate errors.");
		}
	}


Sets up Jersey client to ignore certificate errors.

Returns:
true if successful at setting up to ignore certificate errors, otherwise returns false.
	private boolean ignoreCertificateErrors () {
		// Create a TrustManager that trusts all certificates
		TrustManager[ ] certs = new TrustManager[ ] {
            new X509TrustManager() {
					return null;
				}
				public void checkServerTrusted(X509Certificate[] chainString authType)
				}
				public void checkClientTrusted(X509Certificate[] chainString authType)
				}
			}
		};
    
		// Now set the default SSLSocketFactory to use the just created TrustManager
		SSLSocketFactory defaultSocketFactory = HttpsURLConnection.getDefaultSSLSocketFactory();
		SSLContext sslContext = null;
		try {
			sslContext = SSLContext.getInstance("TLS");
			sslContext.init(nullcertsnew SecureRandom());
			HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
			HttpsURLConnection.getDefaultSSLSocketFactory();
catch (GeneralSecurityException ex) {
			HttpsURLConnection.setDefaultSSLSocketFactory(defaultSocketFactory);
			return (false);
		}
    
		return (true);
	}
    

Construct a REST URL with the specified path arguments.

Parameters:
pathArgs
Returns:
a REST URL with the specified path arguments
Throws:
java.io.IOException
	protected URL getApiUrl (Object ... pathArgsthrows IOException {
		for (Object pathArg : pathArgs) {
			url.append("/");
			url.append(pathArg.toString());
		}
		return (new URL(url.toString()));
    }


Perform an HTTP GET call with the specified query parameters and path objects, returning a ClientResponse instance with the data returned from the endpoint.

Parameters:
queryParams
pathArgs
Returns:
a ClientResponse instance with the data returned from the endpoint
Throws:
java.io.IOException
	protected Response get (MultivaluedMap<StringStringqueryParamsObject ... pathArgs)
			throws IOException {
		URL url = getApiUrl(pathArgs);
		return (get(queryParamsurl));	
	}	


Perform an HTTP GET call with the specified query parameters and URL, returning a ClientResponse instance with the data returned from the endpoint.

Parameters:
queryParams
url
Returns:
a ClientResponse instance with the data returned from the endpoint
	protected Response get (MultivaluedMap<StringStringqueryParamsURL url) {
		return invocation(urlqueryParams).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.

Parameters:
formData
pathArgs
Returns:
a ClientResponse instance with the data returned from the endpoint
Throws:
java.io.IOException
	protected Response post (Form formDataObject ... pathArgs)
			throws IOException {
		URL url = getApiUrl(pathArgs);
		return post(formDataurl);
	}


Perform an HTTP POST call with the specified form data and URL, returning a ClientResponse instance with the data returned from the endpoint.

Parameters:
formData
url
Returns:
a ClientResponse instance with the data returned from the endpoint
	protected Response post (Form formDataURL url) {
		return invocation(urlnull).post(Entity.entity(formData.));
	}


Perform an HTTP PUT call with the specified form data and path objects, returning a ClientResponse instance with the data returned from the endpoint.

Parameters:
queryParams
pathArgs
Returns:
a ClientResponse instance with the data returned from the endpoint
Throws:
java.io.IOException
	protected  Response put (MultivaluedMap<StringStringqueryParamsObject ... pathArgs)
			throws IOException {
		URL url = getApiUrl(pathArgs);
		return (put(queryParamsurl));	
	}	


Perform an HTTP PUT call with the specified form data and URL, returning a ClientResponse instance with the data returned from the endpoint.

Parameters:
queryParams
url
Returns:
a ClientResponse instance with the data returned from the endpoint
	protected Response put (MultivaluedMap<StringStringqueryParamsURL url) {
		return invocation(urlqueryParams).put(null);
	}


Perform an HTTP DELETE call with the specified form data and path objects, returning a Response instance with the data returned from the endpoint.

Parameters:
queryParams
pathArgs
Returns:
a Response instance with the data returned from the endpoint
Throws:
java.io.IOException
	protected Response delete (MultivaluedMap<StringStringqueryParamsObject ... pathArgsthrows IOException {
		return delete(queryParamsgetApiUrl(pathArgs));
	}	


Perform an HTTP DELETE call with the specified form data and URL, returning a Response instance with the data returned from the endpoint.

Parameters:
queryParams
url
Returns:
a Response instance with the data returned from the endpoint
	protected Response delete (MultivaluedMap<StringStringqueryParamsURL url) {
		return invocation(urlqueryParams).delete();
	}
	protected class AcceptAllHostnameVerifier implements HostnameVerifier {
		public boolean verify(String sSSLSession sslSession) {
			return true;
		}
	}
	protected Invocation.Builder invocation(URL urlMultivaluedMap<StringStringqueryParams) {
		if ( == null) {
			 = ClientBuilder.newBuilder()
		}
		if (queryParams != null) {
			for (Map.Entry<StringList<String>> param : queryParams.entrySet()) {
				target = target.queryParam(param.getKey(), param.getValue().toArray());
			}
		}
	}
		try {
			return SSLContext.getDefault();
catch (NoSuchAlgorithmException e) {
		}
	}
New to GrepCode? Check out our FAQ X