Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.messners.gitlab.api;
  
  import java.net.URL;
 
 import  javax.ws.rs.core.MediaType;
 import  javax.ws.rs.core.MultivaluedMap;
 
 import  com.sun.jersey.api.representation.Form;
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
 
 	protected 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
 
 	protected void setIgnoreCerificateErrors (boolean ignoreCertificateErrors) {
 		
 		if (. == ignoreCertificateErrors) {
 			return;
 		}
 		
 		if (ignoreCertificateErrors == false) {	
 		
 			return;
		}
		SSLSocketFactory defaultSocketFactory = HttpsURLConnection.getDefaultSSLSocketFactory();
		if (ignoreCertificateErrors() == true) {
			. = defaultSocketFactory;
			. = defaultHttpsProperties;
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);
		}
    
		// Set up a HostnameVerifier for Jersey to verify all hostnames as valid
		try {
			     new HostnameVerifier() {
			         @Override
			         public boolean verify(String sSSLSession sslSession) {
			             return (true);
			         }
			     }
			 ));
catch (NoSuchAlgorithmException e) {
			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:
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:
UniformInterfaceException
ClientHandlerException
IOException
	protected  ClientResponse get (MultivaluedMap<StringStringqueryParamsObject ... pathArgs
		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
Throws:
UniformInterfaceException
ClientHandlerException
	protected ClientResponse get (MultivaluedMap<StringStringqueryParamsURL url
		if ( == null) {
		}
		WebResource resource = .resource(url.toString());
		if (queryParams != null) {
			resource.queryParams(queryParams);
		}
			.accept(MediaType.APPLICATION_JSON)
			.get(ClientResponse.class));
	}		


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:
UniformInterfaceException
ClientHandlerException
IOException
	protected ClientResponse post (Form formDataObject ... pathArgs
		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
Throws:
UniformInterfaceException
ClientHandlerException
	protected ClientResponse post (Form formDataURL urlthrows UniformInterfaceExceptionClientHandlerException {
		if ( == null) {
		}
		return (.resource(url.toString())
			.accept(MediaType.APPLICATION_JSON)
			.type(MediaType.APPLICATION_FORM_URLENCODED_TYPE)
			.post(ClientResponse.classformData));		
	}


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:
UniformInterfaceException
ClientHandlerException
IOException
	protected  ClientResponse put (MultivaluedMap<StringStringqueryParamsObject ... pathArgs
		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
Throws:
UniformInterfaceException
ClientHandlerException
	protected ClientResponse put (MultivaluedMap<StringStringqueryParamsURL url
		if ( == null) {
		}
		WebResource resource = .resource(url.toString());
		if (queryParams != null) {
			resource.queryParams(queryParams);
		}
			.accept(MediaType.APPLICATION_JSON)
			.put(ClientResponse.class));
	}	


Perform an HTTP DELETE 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:
UniformInterfaceException
ClientHandlerException
IOException
	protected  ClientResponse delete (MultivaluedMap<StringStringqueryParamsObject ... pathArgs
		URL url = getApiUrl(pathArgs);
		return (delete(queryParamsurl));	
	}	


Perform an HTTP DELETE 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
Throws:
UniformInterfaceException
ClientHandlerException
	protected ClientResponse delete (MultivaluedMap<StringStringqueryParamsURL url
		if ( == null) {
		}
		WebResource resource = .resource(url.toString());
		if (queryParams != null) {
			resource.queryParams(queryParams);
		}
			.accept(MediaType.APPLICATION_JSON)
			.delete(ClientResponse.class));
	}	
New to GrepCode? Check out our FAQ X