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.
-
Greg Messner authoreda28210cc
package org.gitlab4j.api;
import javax.ws.rs.NotAuthorizedException;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import org.gitlab4j.api.GitLabApi.ApiVersion;
import java.net.URL;
import java.net.URLEncoder;
/**
* This class is the base class for all the sub API classes. It provides implementations of
* delete(), get(), post() and put() that are re-used by all the sub-classes.
*/
public abstract class AbstractApi {
private GitLabApi gitLabApi;
public AbstractApi(GitLabApi gitLabApi) {
this.gitLabApi = gitLabApi;
}
protected ApiVersion getApiVersion() {
return (gitLabApi.getApiVersion());
}
protected boolean isApiVersion(ApiVersion apiVersion) {
return (gitLabApi.getApiVersion() == apiVersion);
}
protected int getDefaultPerPage() {
return (gitLabApi.getDefaultPerPage());
}
protected GitLabApiClient getApiClient() {
return (gitLabApi.getApiClient());
}
protected String urlEncode(String s) throws GitLabApiException {
try {
return (URLEncoder.encode(s, "UTF-8"));
} catch (Exception e) {
throw new GitLabApiException(e);
}
}
/**
* 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 expectedStatus the HTTP status that should be returned from the server
* @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 GitLabApiException if any exception occurs during execution
*/
protected Response get(Response.Status expectedStatus, MultivaluedMap<String, String> queryParams, Object... pathArgs) throws GitLabApiException {
try {
return validate(getApiClient().get(queryParams, pathArgs), expectedStatus);
} catch (Exception e) {
throw handle(e);
}
}
/**
* Perform an HTTP GET call with the specified query parameters and URL, returning
* a ClientResponse instance with the data returned from the endpoint.
*
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
* @param expectedStatus the HTTP status that should be returned from the server
* @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
* @throws GitLabApiException if any exception occurs during execution
*/
protected Response get(Response.Status expectedStatus, MultivaluedMap<String, String> queryParams, URL url) throws GitLabApiException {
try {
return validate(getApiClient().get(queryParams, url), expectedStatus);
} catch (Exception e) {
throw handle(e);
}
}
/**
* 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 expectedStatus the HTTP status that should be returned from the server
* @param formData the Form containing the name/value pairs for 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 GitLabApiException if any exception occurs during execution
*/
protected Response post(Response.Status expectedStatus, Form formData, Object... pathArgs) throws GitLabApiException {
try {
return validate(getApiClient().post(formData, pathArgs), expectedStatus);
} catch (Exception e) {
throw handle(e);
}
}
/**
* 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 expectedStatus the HTTP status that should be returned from the server
* @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 GitLabApiException if any exception occurs during execution
*/
protected Response post(Response.Status expectedStatus, MultivaluedMap<String, String> queryParams, Object... pathArgs) throws GitLabApiException {
try {
return validate(getApiClient().post(queryParams, pathArgs), expectedStatus);
} catch (Exception e) {
throw handle(e);
}
}
/**
* Perform an HTTP POST call with the specified form data and URL, returning
* a ClientResponse instance with the data returned from the endpoint.
*
* @param expectedStatus the HTTP status that should be returned from the server
* @param formData the Form containing the name/value pairs for the POST data
* @param url the fully formed path to the GitLab API endpoint
* @return a ClientResponse instance with the data returned from the endpoint
* @throws GitLabApiException if any exception occurs during execution
*/
protected Response post(Response.Status expectedStatus, Form formData, URL url) throws GitLabApiException {
try {
return validate(getApiClient().post(formData, url), expectedStatus);
} catch (Exception e) {
throw handle(e);
}
}
/**
* Perform an HTTP PUT call with the specified form data and path objects, returning
141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
* a ClientResponse instance with the data returned from the endpoint.
*
* @param expectedStatus the HTTP status that should be returned from the server
* @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 GitLabApiException if any exception occurs during execution
*/
protected Response put(Response.Status expectedStatus, MultivaluedMap<String, String> queryParams, Object... pathArgs) throws GitLabApiException {
try {
return validate(getApiClient().put(queryParams, pathArgs), expectedStatus);
} catch (Exception e) {
throw handle(e);
}
}
/**
* Perform an HTTP PUT call with the specified form data and URL, returning
* a ClientResponse instance with the data returned from the endpoint.
*
* @param expectedStatus the HTTP status that should be returned from the server
* @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
* @throws GitLabApiException if any exception occurs during execution
*/
protected Response put(Response.Status expectedStatus, MultivaluedMap<String, String> queryParams, URL url) throws GitLabApiException {
try {
return validate(getApiClient().put(queryParams, url), expectedStatus);
} catch (Exception e) {
throw handle(e);
}
}
/**
* Perform an HTTP DELETE call with the specified form data and path objects, returning
* a ClientResponse instance with the data returned from the endpoint.
*
* @param expectedStatus the HTTP status that should be returned from the server
* @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 GitLabApiException if any exception occurs during execution
*/
protected Response delete(Response.Status expectedStatus, MultivaluedMap<String, String> queryParams, Object... pathArgs) throws GitLabApiException {
try {
return validate(getApiClient().delete(queryParams, pathArgs), expectedStatus);
} catch (Exception e) {
throw handle(e);
}
}
/**
* Perform an HTTP DELETE call with the specified form data and URL, returning
* a ClientResponse instance with the data returned from the endpoint.
*
* @param expectedStatus the HTTP status that should be returned from the server
* @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
* @throws GitLabApiException if any exception occurs during execution
*/
protected Response delete(Response.Status expectedStatus, MultivaluedMap<String, String> queryParams, URL url) throws GitLabApiException {
try {
return validate(getApiClient().delete(queryParams, url), expectedStatus);
} catch (Exception e) {
throw handle(e);
}
}
211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
/**
* Convenience method for adding query and form parameters to a get() or post() call.
*
* @param formData the Form containing the name/value pairs
* @param name the name of the field/attribute to add
* @param value the value of the field/attribute to add
*/
protected void addFormParam(Form formData, String name, Object value) throws IllegalArgumentException {
addFormParam(formData, name, value, false);
}
/**
* Convenience method for adding query and form parameters to a get() or post() call.
* If required is true and value is null, will throw an IllegalArgumentException.
*
* @param formData the Form containing the name/value pairs
* @param name the name of the field/attribute to add
* @param value the value of the field/attribute to add
* @param required the field is required flag
* @throws IllegalArgumentException if a required parameter is null or empty
*/
protected void addFormParam(Form formData, String name, Object value, boolean required) throws IllegalArgumentException {
if (value == null) {
if (required) {
throw new IllegalArgumentException(name + " cannot be empty or null");
}
return;
}
String stringValue = value.toString().trim();
if (required && stringValue.length() == 0) {
throw new IllegalArgumentException(name + " cannot be empty or null");
}
formData.param(name, stringValue);
}
/**
* Validates response the response from the server against the expected HTTP status and
* the returned secret token, if either is not correct will throw a GitLabApiException.
*
* @param response response
* @param expected expected response status
* @return original response if the response status is expected
* @throws GitLabApiException if HTTP status is not as expected, or the secret token doesn't match
*/
protected Response validate(Response response, Response.Status expected) throws GitLabApiException {
if (response.getStatus() != expected.getStatusCode()) {
throw new GitLabApiException(response);
}
if (!getApiClient().validateSecretToken(response)) {
throw new GitLabApiException(new NotAuthorizedException("Invalid secret token in response."));
}
return (response);
}
/**
* Wraps an exception in a GitLabApiException if needed.
*
* @param thrown the exception that should be wrapped
* @return either the untouched GitLabApiException or a new GitLabApiExceptin wrapping a non-GitLabApiException
*/
protected GitLabApiException handle(Exception thrown) {
281282283284285286287288
if (thrown instanceof GitLabApiException) {
return ((GitLabApiException) thrown);
}
return (new GitLabApiException(thrown));
}
}