Commit ebe5d24b authored by Patrik Beno's avatar Patrik Beno
Browse files

upgrade to JAX-RS/Jersey 2.x API

parent f8768167
......@@ -5,7 +5,7 @@
<groupId>com.messners</groupId>
<artifactId>gitlab-api</artifactId>
<packaging>jar</packaging>
<version>1.0.16-SNAPSHOT</version>
<version>2.0-SNAPSHOT</version>
<name>gitlab-api</name>
<description>GitLab API provides a full featured Java API for working with GitLab repositories via the GitLab REST API</description>
<url>http://www.messners.com/#gitlab-api/gitlab-api.html</url>
......@@ -39,7 +39,7 @@
<properties>
<jdk.version>1.6</jdk.version>
<jersey.version>1.18</jersey.version>
<jersey.version>2.14</jersey.version>
<jackson.version>1.9.13</jackson.version>
<junit.version>4.9</junit.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
......@@ -134,15 +134,10 @@
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
......
package com.messners.gitlab.api;
import java.net.URL;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.MultivaluedMap;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.representation.Form;
import javax.ws.rs.core.Response;
import java.net.URL;
/**
* This class is the base class for all the sub API classes. It provides implementations of
......@@ -37,12 +35,12 @@ public abstract class AbstractApi {
* @return a ClientResponse instance with the data returned from the endpoint
* @throws GitLabApiException
*/
protected ClientResponse get (ClientResponse.Status expectedStatus, MultivaluedMap<String, String> queryParams, Object ... pathArgs)
protected Response get (Response.Status expectedStatus, MultivaluedMap<String, String> queryParams, Object ... pathArgs)
throws GitLabApiException {
ClientResponse response = null;
Response response = null;
try {
response = getApiClient().get(queryParams, pathArgs);
response = getApiClient().get(queryParams, pathArgs);
} catch (Exception e) {
throw (new GitLabApiException(e));
}
......@@ -65,10 +63,10 @@ public abstract class AbstractApi {
* @return a ClientResponse instance with the data returned from the endpoint
* @throws GitLabApiException
*/
protected ClientResponse get (ClientResponse.Status expectedStatus, MultivaluedMap<String, String> queryParams, URL url)
protected Response get (Response.Status expectedStatus, MultivaluedMap<String, String> queryParams, URL url)
throws GitLabApiException {
ClientResponse response = null;
Response response = null;
try {
response = getApiClient().get(queryParams, url);
} catch (Exception e) {
......@@ -93,9 +91,9 @@ public abstract class AbstractApi {
* @return a ClientResponse instance with the data returned from the endpoint
* @throws GitLabApiException
*/
protected ClientResponse post (ClientResponse.Status expectedStatus, Form formData, Object ... pathArgs) throws GitLabApiException {
protected Response post (Response.Status expectedStatus, Form formData, Object ... pathArgs) throws GitLabApiException {
ClientResponse response = null;
Response response = null;
try {
response = getApiClient().post(formData, pathArgs);
} catch (Exception e) {
......@@ -120,9 +118,9 @@ public abstract class AbstractApi {
* @return a ClientResponse instance with the data returned from the endpoint
* @throws GitLabApiException
*/
protected ClientResponse post (ClientResponse.Status expectedStatus, Form formData, URL url) throws GitLabApiException {
protected Response post (Response.Status expectedStatus, Form formData, URL url) throws GitLabApiException {
ClientResponse response = null;
Response response = null;
try {
response = getApiClient().post(formData, url);
} catch (Exception e) {
......@@ -147,9 +145,9 @@ public abstract class AbstractApi {
* @return a ClientResponse instance with the data returned from the endpoint
* @throws GitLabApiException
*/
protected ClientResponse put (ClientResponse.Status expectedStatus, MultivaluedMap<String, String> queryParams, Object ... pathArgs) throws GitLabApiException {
protected Response put (Response.Status expectedStatus, MultivaluedMap<String, String> queryParams, Object ... pathArgs) throws GitLabApiException {
ClientResponse response = null;
Response response = null;
try {
response = getApiClient().put(queryParams, pathArgs);
} catch (Exception e) {
......@@ -174,9 +172,9 @@ public abstract class AbstractApi {
* @return a ClientResponse instance with the data returned from the endpoint
* @throws GitLabApiException
*/
protected ClientResponse put (ClientResponse.Status expectedStatus, MultivaluedMap<String, String> queryParams, URL url) throws GitLabApiException {
protected Response put (Response.Status expectedStatus, MultivaluedMap<String, String> queryParams, URL url) throws GitLabApiException {
ClientResponse response = null;
Response response = null;
try {
response = getApiClient().put(queryParams, url);
} catch (Exception e) {
......@@ -201,10 +199,10 @@ public abstract class AbstractApi {
* @return a ClientResponse instance with the data returned from the endpoint
* @throws GitLabApiException
*/
protected ClientResponse delete (ClientResponse.Status expectedStatus, MultivaluedMap<String, String> queryParams, Object ... pathArgs)
protected Response delete (Response.Status expectedStatus, MultivaluedMap<String, String> queryParams, Object ... pathArgs)
throws GitLabApiException {
ClientResponse response = null;
Response response = null;
try {
response = getApiClient().delete(queryParams, pathArgs);
} catch (Exception e) {
......@@ -229,9 +227,9 @@ public abstract class AbstractApi {
* @return a ClientResponse instance with the data returned from the endpoint
* @throws GitLabApiException
*/
protected ClientResponse delete (ClientResponse.Status expectedStatus, MultivaluedMap<String, String> queryParams, URL url) throws GitLabApiException {
protected Response delete (Response.Status expectedStatus, MultivaluedMap<String, String> queryParams, URL url) throws GitLabApiException {
ClientResponse response = null;
Response response = null;
try {
response = getApiClient().delete(queryParams, url);
} catch (Exception e) {
......@@ -284,6 +282,6 @@ public abstract class AbstractApi {
throw new IllegalArgumentException(name + " cannot be empty or null");
}
formData.add(name, stringValue);
formData.param(name, stringValue);
}
}
package com.messners.gitlab.api;
import java.util.List;
import com.messners.gitlab.api.models.Commit;
import com.messners.gitlab.api.models.Diff;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.GenericType;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.Response;
import java.util.List;
/**
* This class implements the client side API for the GitLab commits calls.
......@@ -29,8 +29,8 @@ public class CommitsApi extends AbstractApi {
* @throws GitLabApiException
*/
public List<Commit> getCommits (int projectId) throws GitLabApiException {
ClientResponse response = get(ClientResponse.Status.OK, null, "projects", projectId, "repository", "commits");
return (response.getEntity(new GenericType<List<Commit>>() {}));
Response response = get(Response.Status.OK, null, "projects", projectId, "repository", "commits");
return (response.readEntity(new GenericType<List<Commit>>(){}));
}
......@@ -45,8 +45,8 @@ public class CommitsApi extends AbstractApi {
* @throws GitLabApiException
*/
public Commit getCommits (int projectId, String sha) throws GitLabApiException {
ClientResponse response = get(ClientResponse.Status.OK, null, "projects", projectId, "repository", "commits", sha);
return (response.getEntity(Commit.class));
Response response = get(Response.Status.OK, null, "projects", projectId, "repository", "commits", sha);
return (response.readEntity(Commit.class));
}
......@@ -61,8 +61,8 @@ public class CommitsApi extends AbstractApi {
* @throws GitLabApiException
*/
public Diff getDiff (int projectId, String sha) throws GitLabApiException {
ClientResponse response = get(ClientResponse.Status.OK, null,
Response response = get(Response.Status.OK, null,
"projects", projectId, "repository", "commits", sha, "diff");
return (response.getEntity(Diff.class));
return (response.readEntity(Diff.class));
}
}
\ No newline at end of file
package com.messners.gitlab.api;
import java.io.IOException;
import java.net.URL;
import java.security.GeneralSecurityException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.ClientProperties;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
......@@ -15,19 +10,24 @@ import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
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 com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientHandlerException;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.json.JSONConfiguration;
import com.sun.jersey.api.representation.Form;
import com.sun.jersey.client.urlconnection.HTTPSProperties;
import javax.ws.rs.core.Response;
import java.io.IOException;
import java.net.URL;
import java.security.GeneralSecurityException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.List;
import java.util.Map;
/**
* This class utilizes the Jersey client package to communicate with a GitLab API endpoint.
......@@ -45,7 +45,6 @@ public class GitLabApiClient {
private String privateToken;
private static boolean ignoreCertificateErrors;
private static SSLSocketFactory defaultSocketFactory;
private static HTTPSProperties defaultHttpsProperties;
/**
......@@ -60,11 +59,9 @@ public class GitLabApiClient {
// Remove the trailing "/" from the hostUrl if present
this.hostUrl = (hostUrl.endsWith("/") ? hostUrl.replaceAll("/$", "") : hostUrl) + API_NAMESPACE;
this.privateToken = privateToken;
clientConfig = new DefaultClientConfig();
clientConfig.getClasses().add(JacksonJson.class);
clientConfig.getFeatures().put(ClientConfig.PROPERTY_FOLLOW_REDIRECTS, Boolean.TRUE);
clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
clientConfig = new ClientConfig();
clientConfig.register(JacksonJson.class);
}
......@@ -92,20 +89,16 @@ public class GitLabApiClient {
}
if (ignoreCertificateErrors == false) {
GitLabApiClient.ignoreCertificateErrors = false;
HttpsURLConnection.setDefaultSSLSocketFactory(GitLabApiClient.defaultSocketFactory);
clientConfig.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, GitLabApiClient.defaultHttpsProperties);
return;
}
SSLSocketFactory defaultSocketFactory = HttpsURLConnection.getDefaultSSLSocketFactory();
HTTPSProperties defaultHttpsProperties = (HTTPSProperties)clientConfig.getProperties().get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES);
if (ignoreCertificateErrors() == true) {
GitLabApiClient.ignoreCertificateErrors = true;
GitLabApiClient.defaultSocketFactory = defaultSocketFactory;
GitLabApiClient.defaultHttpsProperties = defaultHttpsProperties;
} else {
throw new RuntimeException("Unable to ignore certificate errors.");
}
......@@ -150,23 +143,6 @@ public class GitLabApiClient {
return (false);
}
// Set up a HostnameVerifier for Jersey to verify all hostnames as valid
try {
clientConfig.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, new HTTPSProperties(
new HostnameVerifier() {
@Override
public boolean verify(String s, SSLSession sslSession) {
return (true);
}
}
));
} catch (NoSuchAlgorithmException e) {
HttpsURLConnection.setDefaultSSLSocketFactory(defaultSocketFactory);
return (false);
}
return (true);
}
......@@ -198,12 +174,10 @@ public class GitLabApiClient {
* @param queryParams
* @param pathArgs
* @return a ClientResponse instance with the data returned from the endpoint
* @throws UniformInterfaceException
* @throws ClientHandlerException
* @throws IOException
*/
protected ClientResponse get (MultivaluedMap<String, String> queryParams, Object ... pathArgs)
throws UniformInterfaceException, ClientHandlerException, IOException {
protected Response get (MultivaluedMap<String, String> queryParams, Object ... pathArgs)
throws IOException {
URL url = getApiUrl(pathArgs);
return (get(queryParams, url));
}
......@@ -216,25 +190,10 @@ public class GitLabApiClient {
* @param queryParams
* @param url
* @return a ClientResponse instance with the data returned from the endpoint
* @throws UniformInterfaceException
* @throws ClientHandlerException
*/
protected ClientResponse get (MultivaluedMap<String, String> queryParams, URL url)
throws UniformInterfaceException, ClientHandlerException {
if (apiClient == null) {
apiClient = Client.create(clientConfig);
}
WebResource resource = apiClient.resource(url.toString());
if (queryParams != null) {
resource.queryParams(queryParams);
}
return (resource.header(PRIVATE_TOKEN_HEADER, privateToken)
.accept(MediaType.APPLICATION_JSON)
.get(ClientResponse.class));
}
protected Response get (MultivaluedMap<String, String> queryParams, URL url) {
return invocation(url, queryParams).get();
}
/**
......@@ -244,17 +203,15 @@ public class GitLabApiClient {
* @param formData
* @param pathArgs
* @return a ClientResponse instance with the data returned from the endpoint
* @throws UniformInterfaceException
* @throws ClientHandlerException
* @throws IOException
*/
protected ClientResponse post (Form formData, Object ... pathArgs)
throws UniformInterfaceException, ClientHandlerException, IOException {
protected Response post (Form formData, Object ... pathArgs)
throws IOException {
URL url = getApiUrl(pathArgs);
return (post(formData, url));
return post(formData, url);
}
/**
* Perform an HTTP POST call with the specified form data and URL, returning
* a ClientResponse instance with the data returned from the endpoint.
......@@ -262,20 +219,9 @@ public class GitLabApiClient {
* @param formData
* @param url
* @return a ClientResponse instance with the data returned from the endpoint
* @throws UniformInterfaceException
* @throws ClientHandlerException
*/
protected ClientResponse post (Form formData, URL url) throws UniformInterfaceException, ClientHandlerException {
if (apiClient == null) {
apiClient = Client.create(clientConfig);
}
return (apiClient.resource(url.toString())
.header(PRIVATE_TOKEN_HEADER, privateToken)
.accept(MediaType.APPLICATION_JSON)
.type(MediaType.APPLICATION_FORM_URLENCODED_TYPE)
.post(ClientResponse.class, formData));
protected Response post (Form formData, URL url) {
return invocation(url, null).post(Entity.entity(formData, MediaType.APPLICATION_FORM_URLENCODED_TYPE));
}
......@@ -286,12 +232,10 @@ public class GitLabApiClient {
* @param queryParams
* @param pathArgs
* @return a ClientResponse instance with the data returned from the endpoint
* @throws UniformInterfaceException
* @throws ClientHandlerException
* @throws IOException
*/
protected ClientResponse put (MultivaluedMap<String, String> queryParams, Object ... pathArgs)
throws UniformInterfaceException, ClientHandlerException, IOException {
protected Response put (MultivaluedMap<String, String> queryParams, Object ... pathArgs)
throws IOException {
URL url = getApiUrl(pathArgs);
return (put(queryParams, url));
}
......@@ -304,69 +248,70 @@ public class GitLabApiClient {
* @param queryParams
* @param url
* @return a ClientResponse instance with the data returned from the endpoint
* @throws UniformInterfaceException
* @throws ClientHandlerException
*/
protected ClientResponse put (MultivaluedMap<String, String> queryParams, URL url)
throws UniformInterfaceException, ClientHandlerException {
if (apiClient == null) {
apiClient = Client.create(clientConfig);
}
WebResource resource = apiClient.resource(url.toString());
if (queryParams != null) {
resource.queryParams(queryParams);
}
return (resource.header(PRIVATE_TOKEN_HEADER, privateToken)
.accept(MediaType.APPLICATION_JSON)
.put(ClientResponse.class));
}
protected Response put (MultivaluedMap<String, String> queryParams, URL url) {
return invocation(url, queryParams).put(null);
}
/**
* Perform an HTTP DELETE call with the specified form data and path objects, returning
* a ClientResponse instance with the data returned from the endpoint.
* a Response instance with the data returned from the endpoint.
*
* @param queryParams
* @param pathArgs
* @return a ClientResponse instance with the data returned from the endpoint
* @throws UniformInterfaceException
* @throws ClientHandlerException
* @return a Response instance with the data returned from the endpoint
* @throws IOException
*/
protected ClientResponse delete (MultivaluedMap<String, String> queryParams, Object ... pathArgs)
throws UniformInterfaceException, ClientHandlerException, IOException {
URL url = getApiUrl(pathArgs);
return (delete(queryParams, 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 ClientResponse instance with the data returned from the endpoint.
* a Response instance with the data returned from the endpoint.
*
* @param queryParams
* @param url
* @return a ClientResponse instance with the data returned from the endpoint
* @throws UniformInterfaceException
* @throws ClientHandlerException
* @return a Response instance with the data returned from the endpoint
*/
protected ClientResponse delete (MultivaluedMap<String, String> queryParams, URL url)
throws UniformInterfaceException, ClientHandlerException {
protected Response delete (MultivaluedMap<String, String> queryParams, URL url) {
return invocation(url, queryParams).delete();
}
protected class AcceptAllHostnameVerifier implements HostnameVerifier {
@Override
public boolean verify(String s, SSLSession sslSession) {
return true;
}
}
protected Invocation.Builder invocation(URL url, MultivaluedMap<String, String> queryParams) {
if (apiClient == null) {
apiClient = Client.create(clientConfig);
apiClient = ClientBuilder.newBuilder()
.withConfig(clientConfig)
.sslContext(getSslContext())
.hostnameVerifier(new AcceptAllHostnameVerifier())
.build();
}
WebResource resource = apiClient.resource(url.toString());
WebTarget target = apiClient.target(url.toExternalForm()).property(ClientProperties.FOLLOW_REDIRECTS, true);
if (queryParams != null) {
resource.queryParams(queryParams);
for (Map.Entry<String, List<String>> param : queryParams.entrySet()) {
target = target.queryParam(param.getKey(), param.getValue().toArray());
}
}
return (resource.header(PRIVATE_TOKEN_HEADER, privateToken)
.accept(MediaType.APPLICATION_JSON)
.delete(ClientResponse.class));
}
return target.request().header(PRIVATE_TOKEN_HEADER, privateToken).accept(MediaType.APPLICATION_JSON);
}
private SSLContext getSslContext() {
try {
return SSLContext.getDefault();
} catch (NoSuchAlgorithmException e) {
throw new UnsupportedOperationException(e);
}
}
}
package com.messners.gitlab.api;
import javax.ws.rs.core.Response.StatusType;
import com.messners.gitlab.api.models.ErrorMessage;
import com.sun.jersey.api.client.ClientResponse;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.StatusType;
/**
......@@ -26,7 +26,7 @@ public class GitLabApiException extends Exception {
*
* @param response
*/
public GitLabApiException (ClientResponse response) {
public GitLabApiException (Response response) {
super();
statusInfo = response.getStatusInfo();
......@@ -35,7 +35,7 @@ public class GitLabApiException extends Exception {
if (response.hasEntity()) {
try {
ErrorMessage errorMessage = response.getEntity(ErrorMessage.class);
ErrorMessage errorMessage = response.readEntity(ErrorMessage.class);
message = errorMessage.getMessage();
} catch (Exception ignore) {}
......
package com.messners.gitlab.api;
import java.util.List;
import com.messners.gitlab.api.models.Group;
import com.messners.gitlab.api.models.Member;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.GenericType;
import com.sun.jersey.api.representation.Form;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.Response;
import java.util.List;
/**
* This class implements the client side API for the GitLab groups calls.
......@@ -29,8 +29,8 @@ public class GroupApi extends AbstractApi {
* @throws GitLabApiException
*/
public List<Group> getGroups () throws GitLabApiException {
ClientResponse response = get(ClientResponse.Status.OK, null, "groups");
return (response.getEntity(new GenericType<List<Group>>() {}));
Response response = get(Response.Status.OK, null, "groups");
return (response.readEntity(new GenericType<List<Group>>() {}));
}
......@@ -44,8 +44,8 @@ public class GroupApi extends AbstractApi {
* @throws GitLabApiException
*/
public Group getGroup (int groupId) throws GitLabApiException {
ClientResponse response = get(ClientResponse.Status.OK, null, "groups", groupId);
return (response.getEntity(Group.class));
Response response = get(Response.Status.OK, null, "groups", groupId);
return (response.readEntity(Group.class));
}
......@@ -60,9 +60,9 @@ public class GroupApi extends AbstractApi {
public void addGroup (String name, String path) throws GitLabApiException {
Form formData = new Form();
formData.add("name", name);
formData.add("path", path);
post(ClientResponse.Status.OK, formData, "groups");
formData.param("name", name);
formData.param("path", path);
post(Response.Status.OK, formData, "groups");
}
......@@ -80,7 +80,7 @@ public class GroupApi extends AbstractApi {
throw new RuntimeException("groupId cannot be null");
}
delete(ClientResponse.Status.OK, null, "groups", groupId);
delete(Response.Status.OK, null, "groups", groupId);
}
......@@ -106,8 +106,8 @@ public class GroupApi extends AbstractApi {
* @throws GitLabApiException
*/
public List<Member> getMembers (int groupId) throws GitLabApiException {
ClientResponse response = get(ClientResponse.Status.OK, null, "groups", groupId, "members");
return (response.getEntity(new GenericType<List<Member>>() {}));
Response response = get(Response.Status.OK, null, "groups", groupId, "members");
return (response.readEntity(new GenericType<List<Member>>() {}));
}
/**
......@@ -124,10 +124,10 @@ public class GroupApi extends AbstractApi {
public Member addMember (Integer groupId, Integer userId, Integer accessLevel) throws GitLabApiException {
Form formData = new Form();
formData.add("user_id", userId);
formData.add("access_level", accessLevel);
ClientResponse response = post(ClientResponse.Status.OK, formData, "groups", groupId, "members");
return (response.getEntity(Member.class));
formData.param("user_id", userId.toString());
formData.param("access_level", accessLevel.toString());
Response response = post(Response.Status.OK, formData, "groups", groupId, "members");
return (response.readEntity(Member.class));
}
......@@ -141,6 +141,6 @@ public class GroupApi extends AbstractApi {
* @throws GitLabApiException
*/
public void removeMember (Integer projectId, Integer userId) throws GitLabApiException {
delete(ClientResponse.Status.OK, null, "groups", projectId, "members", userId);
delete(Response.Status.OK, null, "groups", projectId, "members", userId);
}
}
package com.messners.gitlab.api;
import java.util.List;
import com.messners.gitlab.api.models.MergeRequest;
import com.messners.gitlab.api.models.MergeRequestComment;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.GenericType;
import com.sun.jersey.api.representation.Form;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.Response;
import java.util.List;
/**
* This class implements the client side API for the GitLab merge request calls.
*
*
* @author Greg Messner <greg@messners.com>
*/
public class MergeRequestApi extends AbstractApi {
......@@ -18,44 +18,44 @@ public class MergeRequestApi extends AbstractApi {
MergeRequestApi (GitLabApi gitLabApi) {
super(gitLabApi);
}
/**
* Get all merge requests for the specified project.
*
* GET /projects/:id/merge_requests
*
*
* @param projectId the project ID to get the merge requests for
* @return all merge requests for the specified project
* @throws GitLabApiException
* @throws GitLabApiException
*/
public List<MergeRequest> getMergeRequests (Integer projectId) throws GitLabApiException {
ClientResponse response = get(ClientResponse.Status.OK, null, "projects", projectId, "merge_requests");
return (response.getEntity(new GenericType<List<MergeRequest>>() {}));
public List<MergeRequest> getMergeRequests (Integer projectId) throws GitLabApiException {
Response response = get(Response.Status.OK, null, "projects", projectId, "merge_requests");
return (response.readEntity(new GenericType<List<MergeRequest>>() {}));
}
/**
* Get information about a single merge request.
*
*
* GET /projects/:id/merge_request/:merge_request_id
*
*
* @param projectId
* @param mergeRequestId
* @return the specified MergeRequest instance
* @throws GitLabApiException
* @throws GitLabApiException
*/
public MergeRequest getMergeRequest (Integer projectId, Integer mergeRequestId) throws GitLabApiException {
ClientResponse response = get(ClientResponse.Status.OK, null, "projects", projectId, "merge_request", mergeRequestId);
return (response.getEntity(MergeRequest.class));
public MergeRequest getMergeRequest (Integer projectId, Integer mergeRequestId) throws GitLabApiException {
Response response = get(Response.Status.OK, null, "projects", projectId, "merge_request", mergeRequestId);
return (response.readEntity(MergeRequest.class));
}
/**
* Creates a merge request and optionally assigns a reviewer to it.
*
*
* POST /projects/:id/merge_requests
*
*
* @param projectId the ID of a project, required
* @param sourceBranch the source branch, required
* @param targetBranch the target branch, required
......@@ -63,32 +63,32 @@ public class MergeRequestApi extends AbstractApi {
* @param description the description of the merge request
* @param assigneeId the Assignee user ID, optional
* @return the created MergeRequest instance
* @throws GitLabApiException
* @throws GitLabApiException
*/
public MergeRequest createMergeRequest (Integer projectId, String sourceBranch, String targetBranch, String title, String description, Integer assigneeId)
public MergeRequest createMergeRequest (Integer projectId, String sourceBranch, String targetBranch, String title, String description, Integer assigneeId)
throws GitLabApiException {
if (projectId == null) {
throw new RuntimeException("projectId cannot be null");
}
Form formData = new Form();
addFormParam(formData, "source_branch", sourceBranch, true);
addFormParam(formData, "source_branch", sourceBranch, true);
addFormParam(formData, "target_branch", targetBranch, true);
addFormParam(formData, "title", title, true);
addFormParam(formData, "description", description, false);
addFormParam(formData, "assignee_id", assigneeId, false);
ClientResponse response = post(ClientResponse.Status.CREATED, formData, "projects", projectId, "merge_requests");
return (response.getEntity(MergeRequest.class));
Response response = post(Response.Status.CREATED, formData, "projects", projectId, "merge_requests");
return (response.readEntity(MergeRequest.class));
}
/**
* Updates an existing merge request. You can change branches, title, or even close the MR.
*
*
* PUT /projects/:id/merge_request/:merge_request_id
*
*
* @param projectId
* @param mergeRequestId
* @param sourceBranch
......@@ -97,47 +97,47 @@ public class MergeRequestApi extends AbstractApi {
* @param description
* @param assigneeId
* @return the updated merge request
* @throws GitLabApiException
* @throws GitLabApiException
*/
public MergeRequest updateMergeRequest (Integer projectId, Integer mergeRequestId,
public MergeRequest updateMergeRequest (Integer projectId, Integer mergeRequestId,
String sourceBranch, String targetBranch, String title, String description, Integer assigneeId) throws GitLabApiException {
if (projectId == null) {
throw new RuntimeException("mergeRequestId cannot be null");
}
if (mergeRequestId == null) {
throw new RuntimeException("projectId cannot be null");
}
Form formData = new Form();
addFormParam(formData, "source_branch", sourceBranch, false);
addFormParam(formData, "source_branch", sourceBranch, false);
addFormParam(formData, "target_branch", targetBranch, false);
addFormParam(formData, "title", title, false);
addFormParam(formData, "description", description, false);
addFormParam(formData, "assignee_id", assigneeId, false);
ClientResponse response = put(ClientResponse.Status.OK, formData, "projects", projectId, "merge_request", mergeRequestId);
return (response.getEntity(MergeRequest.class));
Response response = put(Response.Status.OK, formData.asMap(), "projects", projectId, "merge_request", mergeRequestId);
return (response.readEntity(MergeRequest.class));
}
/**
* Adds a comment to a merge request.
*
*
* POST /projects/:id/merge_request/:merge_request_id/comments
*
*
* @param projectId
* @param mergeRequestId
* @param comments
* @return the added merge request comment
* @throws GitLabApiException
* @throws GitLabApiException
*/
public MergeRequestComment addMergeRequestComment (Integer projectId, Integer mergeRequestId, String comments) throws GitLabApiException {
Form formData = new Form();
formData.add("note", comments);
ClientResponse response = post(ClientResponse.Status.OK, formData, "projects", projectId, "merge_request", mergeRequestId, "comments");
return (response.getEntity(MergeRequestComment.class));
formData.param("note", comments);
Response response = post(Response.Status.OK, formData, "projects", projectId, "merge_request", mergeRequestId, "comments");
return (response.readEntity(MergeRequestComment.class));
}
}
package com.messners.gitlab.api;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import com.messners.gitlab.api.models.Event;
import com.messners.gitlab.api.models.Member;
import com.messners.gitlab.api.models.Project;
import com.messners.gitlab.api.models.ProjectHook;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.GenericType;
import com.sun.jersey.api.representation.Form;
import org.glassfish.jersey.uri.UriComponent;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.Response;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
public class ProjectApi extends AbstractApi {
ProjectApi (GitLabApi gitLabApi) {
super(gitLabApi);
}
......@@ -28,8 +29,8 @@ public class ProjectApi extends AbstractApi {
* @throws GitLabApiException
*/
public List<Project> getProjects () throws GitLabApiException {
ClientResponse response = get(ClientResponse.Status.OK, null, "projects");
return (response.getEntity(new GenericType<List<Project>>() {}));
Response response = get(Response.Status.OK, null, "projects");
return (response.readEntity(new GenericType<List<Project>>(){}));
}
......@@ -42,8 +43,8 @@ public class ProjectApi extends AbstractApi {
* @throws GitLabApiException
*/
public List<Project> getAllProjects () throws GitLabApiException {
ClientResponse response = get(ClientResponse.Status.OK, null, "projects", "all");
return (response.getEntity(new GenericType<List<Project>>() {}));
Response response = get(Response.Status.OK, UriComponent.decodeQuery("per_page=9999", true), "projects", "all");
return (response.readEntity(new GenericType<List<Project>>(){}));
}
......@@ -56,8 +57,8 @@ public class ProjectApi extends AbstractApi {
* @throws GitLabApiException
*/
public List<Project> getOwnedProjects () throws GitLabApiException {
ClientResponse response = get(ClientResponse.Status.OK, null, "projects", "owned");
return (response.getEntity(new GenericType<List<Project>>() {}));
Response response = get(Response.Status.OK, null, "projects", "owned");
return (response.readEntity(new GenericType<List<Project>>(){}));
}
......@@ -71,8 +72,8 @@ public class ProjectApi extends AbstractApi {
* @throws GitLabApiException
*/
public Project getProject (Integer projectId) throws GitLabApiException {
ClientResponse response = get(ClientResponse.Status.OK, null, "projects", projectId);
return (response.getEntity(Project.class));
Response response = get(Response.Status.OK, null, "projects", projectId);
return (response.readEntity(Project.class));
}
......@@ -95,9 +96,9 @@ public class ProjectApi extends AbstractApi {
throw (new GitLabApiException(uee));
}
ClientResponse response = get(ClientResponse.Status.OK, null, "projects", pid);
return (response.getEntity(Project.class));
}
Response response = get(Response.Status.OK, null, "projects", pid);
return (response.readEntity(Project.class));
}
/**
......@@ -114,8 +115,8 @@ public class ProjectApi extends AbstractApi {
addFormParam(formData, "namespace_id", groupId);
addFormParam(formData, "name", projectName, true);
ClientResponse response = post(ClientResponse.Status.CREATED, formData, "projects");
return (response.getEntity(Project.class));
Response response = post(Response.Status.CREATED, formData, "projects");
return (response.readEntity(Project.class));
}
......@@ -124,8 +125,7 @@ public class ProjectApi extends AbstractApi {
*
* @param project the Project instance with the configuration for the new project
* @return a Project instance with the newly created project info
* @throws IOException
* @throws GitLabApiException
* @throws GitLabApiException
*/
public Project createProject (Project project) throws GitLabApiException {
return (createProject(project, null));
......@@ -177,8 +177,8 @@ public class ProjectApi extends AbstractApi {
addFormParam(formData, "visibility_level", project.getVisibilityLevel());
addFormParam(formData, "import_url", importUrl);
ClientResponse response = post(ClientResponse.Status.CREATED, formData, "projects");
return (response.getEntity(Project.class));
Response response = post(Response.Status.CREATED, formData, "projects");
return (response.readEntity(Project.class));
}
......@@ -196,7 +196,7 @@ public class ProjectApi extends AbstractApi {
throw new RuntimeException("projectId cannot be null");
}
delete(ClientResponse.Status.OK, null, "projects", projectId);
delete(Response.Status.OK, null, "projects", projectId);
}
......@@ -223,8 +223,8 @@ public class ProjectApi extends AbstractApi {
* @throws GitLabApiException
*/
public List<Member> getMembers (Integer projectId) throws GitLabApiException {
ClientResponse response = get(ClientResponse.Status.OK, null, "projects", projectId, "members");
return (response.getEntity(new GenericType<List<Member>>() {}));
Response response = get(Response.Status.OK, null, "projects", projectId, "members");
return (response.readEntity(new GenericType<List<Member>>() {}));
}
......@@ -239,8 +239,8 @@ public class ProjectApi extends AbstractApi {
* @throws GitLabApiException
*/
public Member getMember (Integer projectId, Integer userId) throws GitLabApiException {
ClientResponse response = get(ClientResponse.Status.OK, null, "projects", projectId, "members", userId);
return (response.getEntity(Member.class));
Response response = get(Response.Status.OK, null, "projects", projectId, "members", userId);
return (response.readEntity(Member.class));
}
......@@ -260,10 +260,10 @@ public class ProjectApi extends AbstractApi {
public Member addMember (Integer projectId, Integer userId, Integer accessLevel) throws GitLabApiException {
Form formData = new Form();
formData.add("user_id", userId);
formData.add("access_level", accessLevel);
ClientResponse response = post(ClientResponse.Status.OK, formData, "projects", projectId, "members");
return (response.getEntity(Member.class));
formData.param("user_id", userId.toString());
formData.param("access_level", accessLevel.toString());
Response response = post(Response.Status.OK, formData, "projects", projectId, "members");
return (response.readEntity(Member.class));
}
......@@ -277,7 +277,7 @@ public class ProjectApi extends AbstractApi {
* @throws GitLabApiException
*/
public void removeMember (Integer projectId, Integer userId) throws GitLabApiException {
delete(ClientResponse.Status.OK, null, "projects", projectId, "members", userId);
delete(Response.Status.OK, null, "projects", projectId, "members", userId);
}
......@@ -291,8 +291,8 @@ public class ProjectApi extends AbstractApi {
* @throws GitLabApiException
*/
public List<Event> getProjectEvents (Integer projectId) throws GitLabApiException {
ClientResponse response = get(ClientResponse.Status.OK, null, "projects", projectId, "events");
return (response.getEntity(new GenericType<List<Event>>() {}));
Response response = get(Response.Status.OK, null, "projects", projectId, "events");
return (response.readEntity(new GenericType<List<Event>>() {}));
}
......@@ -306,8 +306,8 @@ public class ProjectApi extends AbstractApi {
* @throws GitLabApiException
*/
public List<ProjectHook> getHooks (Integer projectId) throws GitLabApiException {
ClientResponse response = get(ClientResponse.Status.OK, null, "projects", projectId, "hooks");
return (response.getEntity(new GenericType<List<ProjectHook>>() {} ));
Response response = get(Response.Status.OK, null, "projects", projectId, "hooks");
return (response.readEntity(new GenericType<List<ProjectHook>>() {}));
}
......@@ -322,8 +322,8 @@ public class ProjectApi extends AbstractApi {
* @throws GitLabApiException
*/
public ProjectHook getHook (Integer projectId, Integer hookId) throws GitLabApiException {
ClientResponse response = get(ClientResponse.Status.OK, null, "projects", projectId, "hooks", hookId);
return (response.getEntity(ProjectHook.class));
Response response = get(Response.Status.OK, null, "projects", projectId, "hooks", hookId);
return (response.readEntity(ProjectHook.class));
}
......@@ -370,13 +370,13 @@ public class ProjectApi extends AbstractApi {
throws GitLabApiException {
Form formData = new Form();
formData.add("url", url);
formData.add("push_events", doPushEvents);
formData.add("issues_enabled", doIssuesEvents);
formData.add("merge_requests_events", doMergeRequestsEvents);
formData.param("url", url);
formData.param("push_events", Boolean.toString(doPushEvents));
formData.param("issues_enabled", Boolean.toString(doIssuesEvents));
formData.param("merge_requests_events", Boolean.toString(doMergeRequestsEvents));
ClientResponse response = post(ClientResponse.Status.CREATED, formData, "projects", projectId, "hooks");
return (response.getEntity(ProjectHook.class));
Response response = post(Response.Status.CREATED, formData, "projects", projectId, "hooks");
return (response.readEntity(ProjectHook.class));
}
......@@ -390,7 +390,7 @@ public class ProjectApi extends AbstractApi {
* @throws GitLabApiException
*/
public void deleteHook (Integer projectId, Integer hookId) throws GitLabApiException {
delete(ClientResponse.Status.OK, null, "projects", projectId, "hooks", hookId);
delete(Response.Status.OK, null, "projects", projectId, "hooks", hookId);
}
......@@ -419,12 +419,12 @@ public class ProjectApi extends AbstractApi {
public ProjectHook modifyHook (ProjectHook hook) throws GitLabApiException {
Form formData = new Form();
formData.add("url", hook.getUrl());
formData.add("push_events", hook.getPushEvents());
formData.add("issues_enabled", hook.getIssuesEvents());
formData.add("merge_requests_events", hook.getMergeRequestsEvents());
formData.param("url", hook.getUrl());
formData.param("push_events", hook.getPushEvents().toString());
formData.param("issues_enabled", hook.getIssuesEvents().toString());
formData.param("merge_requests_events", hook.getMergeRequestsEvents().toString());
ClientResponse response = put(ClientResponse.Status.OK, formData, "projects", hook.getProjectId(), "hooks", hook.getId());
return (response.getEntity(ProjectHook.class));
Response response = put(Response.Status.OK, formData.asMap(), "projects", hook.getProjectId(), "hooks", hook.getId());
return (response.readEntity(ProjectHook.class));
}
}
package com.messners.gitlab.api;
import java.util.List;
import com.messners.gitlab.api.models.Branch;
import com.messners.gitlab.api.models.Tag;
import com.messners.gitlab.api.models.TreeItem;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.GenericType;
import com.sun.jersey.api.representation.Form;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.Response;
import java.util.List;
/**
......@@ -32,8 +32,8 @@ public class RepositoryApi extends AbstractApi {
* @throws GitLabApiException
*/
public List<Branch> getBranches (Integer projectId) throws GitLabApiException {
ClientResponse response = get(ClientResponse.Status.OK, null, "projects", projectId, "repository", "branches");
return (response.getEntity(new GenericType<List<Branch>>() {}));
Response response = get(Response.Status.OK, null, "projects", projectId, "repository", "branches");
return (response.readEntity(new GenericType<List<Branch>>() {}));
}
......@@ -48,8 +48,8 @@ public class RepositoryApi extends AbstractApi {
* @throws GitLabApiException
*/
public Branch getBranch (Integer projectId, String branchName) throws GitLabApiException {
ClientResponse response = get(ClientResponse.Status.OK, null, "projects", projectId, "repository", "branches", branchName);
return (response.getEntity(Branch.class));
Response response = get(Response.Status.OK, null, "projects", projectId, "repository", "branches", branchName);
return (response.readEntity(Branch.class));
}
......@@ -66,10 +66,10 @@ public class RepositoryApi extends AbstractApi {
*/
public Branch createBranch (Integer projectId, String branchName, String ref) throws GitLabApiException {
Form formData = new Form();
formData.add("branch_name ", branchName);
formData.add("ref ", ref);
ClientResponse response = post(ClientResponse.Status.OK, formData, "projects", projectId, "repository", "branches");
return (response.getEntity(Branch.class));
formData.param("branch_name ", branchName);
formData.param("ref ", ref);
Response response = post(Response.Status.OK, formData, "projects", projectId, "repository", "branches");
return (response.readEntity(Branch.class));
}
......@@ -85,8 +85,8 @@ public class RepositoryApi extends AbstractApi {
* @throws GitLabApiException
*/
public Branch protectBranch (Integer projectId, String branchName) throws GitLabApiException {
ClientResponse response = put(ClientResponse.Status.OK, null, "projects", projectId, "repository", "branches", branchName, "protect");
return (response.getEntity(Branch.class));
Response response = put(Response.Status.OK, null, "projects", projectId, "repository", "branches", branchName, "protect");
return (response.readEntity(Branch.class));
}
......@@ -102,8 +102,8 @@ public class RepositoryApi extends AbstractApi {
* @throws GitLabApiException
*/
public Branch unprotectBranch (Integer projectId, String branchName) throws GitLabApiException {
ClientResponse response = put(ClientResponse.Status.OK, null, "projects", projectId, "repository", "branches", branchName, "unprotect");
return (response.getEntity(Branch.class));
Response response = put(Response.Status.OK, null, "projects", projectId, "repository", "branches", branchName, "unprotect");
return (response.readEntity(Branch.class));
}
......@@ -117,8 +117,8 @@ public class RepositoryApi extends AbstractApi {
* @throws GitLabApiException
*/
public List<Tag> getTags (Integer projectId) throws GitLabApiException {
ClientResponse response = put(ClientResponse.Status.OK, null, "projects", projectId, "repository", "tags");
return (response.getEntity(new GenericType<List<Tag>>() {}));
Response response = put(Response.Status.OK, null, "projects", projectId, "repository", "tags");
return (response.readEntity(new GenericType<List<Tag>>() {}));
}
......@@ -132,8 +132,8 @@ public class RepositoryApi extends AbstractApi {
* @throws GitLabApiException
*/
public List<TreeItem> getTree (Integer projectId) throws GitLabApiException {
ClientResponse response = put(ClientResponse.Status.OK, null, "projects", projectId, "repository", "tree");
return (response.getEntity(new GenericType<List<TreeItem>>() {}));
Response response = put(Response.Status.OK, null, "projects", projectId, "repository", "tree");
return (response.readEntity(new GenericType<List<TreeItem>>() {}));
}
......@@ -151,7 +151,7 @@ public class RepositoryApi extends AbstractApi {
Form formData = new Form();
addFormParam(formData, "filepath", filepath, true);
ClientResponse response = get(ClientResponse.Status.OK, formData, "projects", projectId, "repository", "blobs", commitOrBranchName);
return (response.getEntity(String.class));
Response response = get(Response.Status.OK, formData.asMap(), "projects", projectId, "repository", "blobs", commitOrBranchName);
return (response.readEntity(String.class));
}
}
package com.messners.gitlab.api;
import com.messners.gitlab.api.models.Session;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.representation.Form;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.Response;
/**
......@@ -41,7 +42,7 @@ public class SessionApi extends AbstractApi {
addFormParam(formData, "password", password, true);
addFormParam(formData, "login", username, false);
ClientResponse response = post(ClientResponse.Status.CREATED, formData, "session");
return (response.getEntity(Session.class));
Response response = post(Response.Status.CREATED, formData, "session");
return (response.readEntity(Session.class));
}
}
package com.messners.gitlab.api;
import java.util.List;
import com.messners.gitlab.api.models.User;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.GenericType;
import com.sun.jersey.api.representation.Form;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.Response;
import java.util.List;
public class UserApi extends AbstractApi {
......@@ -23,8 +23,8 @@ public class UserApi extends AbstractApi {
* @throws GitLabApiException
*/
public List<User> getUsers () throws GitLabApiException {
ClientResponse response = get(ClientResponse.Status.OK, null, "users");
return (response.getEntity(new GenericType<List<User>>() {}));
Response response = get(Response.Status.OK, null, "users");
return (response.readEntity(new GenericType<List<User>>() {}));
}
......@@ -43,8 +43,8 @@ public class UserApi extends AbstractApi {
Form formData = new Form();
addFormParam(formData, "page", page, false);
addFormParam(formData, "per_page", perPage, false);
ClientResponse response = get(ClientResponse.Status.OK, formData, "users");
return (response.getEntity(new GenericType<List<User>>() {}));
Response response = get(Response.Status.OK, formData.asMap(), "users");
return (response.readEntity(new GenericType<List<User>>() {}));
}
......@@ -58,8 +58,8 @@ public class UserApi extends AbstractApi {
* @throws GitLabApiException
*/
public User getUser (int userId) throws GitLabApiException {
ClientResponse response = get(ClientResponse.Status.OK, null, "users", userId);
return (response.getEntity(User.class));
Response response = get(Response.Status.OK, null, "users", userId);
return (response.readEntity(User.class));
}
......@@ -105,8 +105,8 @@ public class UserApi extends AbstractApi {
addFormParam(formData, "admin", user.getIsAdmin(), false);
addFormParam(formData, "can_create_group", user.getCanCreateGroup(), false);
ClientResponse response = post(ClientResponse.Status.CREATED, formData, "users");
return (response.getEntity(User.class));
Response response = post(Response.Status.CREATED, formData, "users");
return (response.readEntity(User.class));
}
......@@ -152,8 +152,8 @@ public class UserApi extends AbstractApi {
addFormParam(formData, "admin", user.getIsAdmin(), false);
addFormParam(formData, "can_create_group", user.getCanCreateGroup(), false);
ClientResponse response = put(ClientResponse.Status.OK, formData, "users", user.getId());
return (response.getEntity(User.class));
Response response = put(Response.Status.OK, formData.asMap(), "users", user.getId());
return (response.readEntity(User.class));
}
......@@ -171,7 +171,7 @@ public class UserApi extends AbstractApi {
throw new RuntimeException("userId cannot be null");
}
delete(ClientResponse.Status.OK, null, "users", userId);
delete(Response.Status.OK, null, "users", userId);
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment