AbstractApi.java 26.86 KiB
package org.gitlab4j.api;
import java.io.File;
import java.net.URL;
import java.net.URLEncoder;
import javax.ws.rs.NotAuthorizedException;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
import org.gitlab4j.api.GitLabApi.ApiVersion;
import org.gitlab4j.api.models.Group;
import org.gitlab4j.api.models.Project;
import org.gitlab4j.api.models.User;
/**
 * 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 implements Constants {
    private final GitLabApi gitLabApi;
    public AbstractApi(GitLabApi gitLabApi) {
        this.gitLabApi = gitLabApi;
    /**
     * Returns the project ID or path from the provided Integer, String, or Project instance.
     * @param obj the object to determine the ID or path from
     * @return the project ID or path from the provided Integer, String, or Project instance
     * @throws GitLabApiException if any exception occurs during execution
    public Object getProjectIdOrPath(Object obj) throws GitLabApiException {
        if (obj == null) {
            throw (new RuntimeException("Cannot determine ID or path from null object"));
        } else if (obj instanceof Integer) {
            return (obj);
        } else if (obj instanceof String) {
            return (urlEncode(((String) obj).trim()));
        } else if (obj instanceof Project) {
            Integer id = ((Project) obj).getId();
            if (id != null && id.intValue() > 0) {
                return (id);
            String path = ((Project) obj).getPathWithNamespace();
            if (path != null && path.trim().length() > 0) {
                return (urlEncode(path.trim()));
            throw (new RuntimeException("Cannot determine ID or path from provided Project instance"));
        } else {
            throw (new RuntimeException("Cannot determine ID or path from provided " + obj.getClass().getSimpleName() +
                    " instance, must be Integer, String, or a Project instance"));
    /**
     * Returns the group ID or path from the provided Integer, String, or Group instance.
     * @param obj the object to determine the ID or path from
     * @return the group ID or path from the provided Integer, String, or Group instance
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
* @throws GitLabApiException if any exception occurs during execution */ public Object getGroupIdOrPath(Object obj) throws GitLabApiException { if (obj == null) { throw (new RuntimeException("Cannot determine ID or path from null object")); } else if (obj instanceof Integer) { return (obj); } else if (obj instanceof String) { return (urlEncode(((String) obj).trim())); } else if (obj instanceof Group) { Integer id = ((Group) obj).getId(); if (id != null && id.intValue() > 0) { return (id); } String path = ((Group) obj).getFullPath(); if (path != null && path.trim().length() > 0) { return (urlEncode(path.trim())); } throw (new RuntimeException("Cannot determine ID or path from provided Group instance")); } else { throw (new RuntimeException("Cannot determine ID or path from provided " + obj.getClass().getSimpleName() + " instance, must be Integer, String, or a Group instance")); } } /** * Returns the user ID or path from the provided Integer, String, or User instance. * * @param obj the object to determine the ID or username from * @return the user ID or username from the provided Integer, String, or User instance * @throws GitLabApiException if any exception occurs during execution */ public Object getUserIdOrUsername(Object obj) throws GitLabApiException { if (obj == null) { throw (new RuntimeException("Cannot determine ID or username from null object")); } else if (obj instanceof Integer) { return (obj); } else if (obj instanceof String) { return (urlEncode(((String) obj).trim())); } else if (obj instanceof User) { Integer id = ((User) obj).getId(); if (id != null && id.intValue() > 0) { return (id); } String username = ((User) obj).getUsername(); if (username != null && username.trim().length() > 0) { return (urlEncode(username.trim())); } throw (new RuntimeException("Cannot determine ID or username from provided User instance")); } else { throw (new RuntimeException("Cannot determine ID or username from provided " + obj.getClass().getSimpleName() + " instance, must be Integer, String, or a User instance")); } } protected ApiVersion getApiVersion() { return (gitLabApi.getApiVersion()); }
141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
protected boolean isApiVersion(ApiVersion apiVersion) { return (gitLabApi.getApiVersion() == apiVersion); } protected int getDefaultPerPage() { return (gitLabApi.getDefaultPerPage()); } protected GitLabApiClient getApiClient() { return (gitLabApi.getApiClient()); } /** * Encode a string to be used as in-path argument for a gitlab api request. * * Standard URL encoding changes spaces to plus signs, but for arguments that are part of the path, * like the :file_path in a "Get raw file" request, gitlab expects spaces to be encoded with %20. * * @param s the string to encode * @return encoded version of s with spaces encoded as %2F * @throws GitLabApiException if encoding throws an exception */ protected String urlEncode(String s) throws GitLabApiException { try { String encoded = URLEncoder.encode(s, "UTF-8"); // Since the encode method encodes plus signs as %2B, // we can simply replace the encoded spaces with the correct encoding here encoded = encoded.replace("+", "%20"); encoded = encoded.replace(".", "%2E"); encoded = encoded.replace("-", "%2D"); encoded = encoded.replace("_", "%5F"); return (encoded); } 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 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 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 GitLabApiException if any exception occurs during execution */ protected Response getWithAccepts(Response.Status expectedStatus, MultivaluedMap<String, String> queryParams, String accepts, Object... pathArgs) throws GitLabApiException { try { return validate(getApiClient().getWithAccepts(queryParams, accepts, pathArgs), expectedStatus);
211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
} 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. * * @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 HEAD 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 head(Response.Status expectedStatus, MultivaluedMap<String, String> queryParams, Object... pathArgs) throws GitLabApiException { try { return validate(getApiClient().head(queryParams, 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 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 payload object 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 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 GitLabApiException if any exception occurs during execution */ protected Response post(Response.Status expectedStatus, Object payload, Object... pathArgs) throws GitLabApiException {