Skip to content

Commit

Permalink
Merge pull request gitlab4j#4 from patrikbeno/jersey2
Browse files Browse the repository at this point in the history
Upgrade to JAX-RS/Jersey 2.x API
  • Loading branch information
gmessner committed May 13, 2015
2 parents f876816 + 1fdccb1 commit 420712e
Show file tree
Hide file tree
Showing 11 changed files with 357 additions and 471 deletions.
11 changes: 3 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down
176 changes: 67 additions & 109 deletions src/main/java/com/messners/gitlab/api/AbstractApi.java
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
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
Expand Down Expand Up @@ -37,23 +35,14 @@ protected GitLabApiClient getApiClient () {
* @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;
try {
response = getApiClient().get(queryParams, pathArgs);
try {
return validate(getApiClient().get(queryParams, pathArgs), expectedStatus);
} catch (Exception e) {
throw (new GitLabApiException(e));
throw handle(e);
}

if (response.getStatus() != expectedStatus.getStatusCode()) {
throw (new GitLabApiException(response));
}

return (response);
}

}

/**
* Perform an HTTP GET call with the specified query parameters and URL, returning
Expand All @@ -65,22 +54,14 @@ protected ClientResponse get (ClientResponse.Status expectedStatus, Multivalued
* @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;
try {
response = getApiClient().get(queryParams, url);
try {
return validate(getApiClient().get(queryParams, url), expectedStatus);
} catch (Exception e) {
throw (new GitLabApiException(e));
throw handle(e);
}

if (response.getStatus() != expectedStatus.getStatusCode()) {
throw (new GitLabApiException(response));
}

return (response);
}
}


/**
Expand All @@ -93,20 +74,12 @@ protected ClientResponse get (ClientResponse.Status expectedStatus, MultivaluedM
* @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 {

ClientResponse response = null;
try {
response = getApiClient().post(formData, pathArgs);
protected Response post (Response.Status expectedStatus, Form formData, Object ... pathArgs) throws GitLabApiException {
try {
return validate(getApiClient().post(formData, pathArgs), expectedStatus);
} catch (Exception e) {
throw (new GitLabApiException(e));
}

if (response.getStatus() != expectedStatus.getStatusCode()) {
throw (new GitLabApiException(response));
throw handle(e);
}

return (response);
}


Expand All @@ -120,20 +93,12 @@ protected ClientResponse post (ClientResponse.Status expectedStatus, Form formDa
* @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 {

ClientResponse response = null;
try {
response = getApiClient().post(formData, url);
protected Response post (Response.Status expectedStatus, Form formData, URL url) throws GitLabApiException {
try {
return validate(getApiClient().post(formData, url), expectedStatus);
} catch (Exception e) {
throw (new GitLabApiException(e));
}

if (response.getStatus() != expectedStatus.getStatusCode()) {
throw (new GitLabApiException(response));
throw handle(e);
}

return (response);
}


Expand All @@ -147,21 +112,13 @@ protected ClientResponse post (ClientResponse.Status expectedStatus, Form formDa
* @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 {

ClientResponse response = null;
try {
response = getApiClient().put(queryParams, pathArgs);
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 (new GitLabApiException(e));
throw handle(e);
}

if (response.getStatus() != expectedStatus.getStatusCode()) {
throw (new GitLabApiException(response));
}

return (response);
}
}


/**
Expand All @@ -174,21 +131,13 @@ protected ClientResponse put (ClientResponse.Status expectedStatus, Multivalued
* @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 {

ClientResponse response = null;
try {
response = getApiClient().put(queryParams, url);
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 (new GitLabApiException(e));
throw handle(e);
}

if (response.getStatus() != expectedStatus.getStatusCode()) {
throw (new GitLabApiException(response));
}

return (response);
}
}


/**
Expand All @@ -201,22 +150,14 @@ protected ClientResponse put (ClientResponse.Status expectedStatus, MultivaluedM
* @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;
try {
response = getApiClient().delete(queryParams, pathArgs);
try {
return validate(getApiClient().delete(queryParams, pathArgs), expectedStatus);
} catch (Exception e) {
throw (new GitLabApiException(e));
throw handle(e);
}

if (response.getStatus() != expectedStatus.getStatusCode()) {
throw (new GitLabApiException(response));
}

return (response);
}
}


/**
Expand All @@ -229,20 +170,12 @@ protected ClientResponse delete (ClientResponse.Status expectedStatus, Multival
* @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 {

ClientResponse response = null;
try {
response = getApiClient().delete(queryParams, url);
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 (new GitLabApiException(e));
}

if (response.getStatus() != expectedStatus.getStatusCode()) {
throw (new GitLabApiException(response));
throw handle(e);
}

return (response);
}


Expand All @@ -269,7 +202,7 @@ protected void addFormParam(Form formData, String name, Object value) throws Ill
* @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) {
Expand All @@ -284,6 +217,31 @@ protected void addFormParam(Form formData, String name, Object value, boolean re
throw new IllegalArgumentException(name + " cannot be empty or null");
}

formData.add(name, stringValue);
formData.param(name, stringValue);
}

/**
* Validates response.
* @param response response
* @param expected expected respone status
* @return original response if the response status is expected
* @throws GitLabApiException in case of unexpected response status
*/
protected Response validate(Response response, Response.Status expected) throws GitLabApiException {
if (response.getStatus() != expected.getStatusCode()) {
throw new GitLabApiException(response);
}
return response;
}

/**
* Wraps exception if needed
* @param thrown exception
* @return never returns
* @throws GitLabApiException always
*/
protected GitLabApiException handle(Exception thrown) throws GitLabApiException {
if (thrown instanceof GitLabApiException) { throw (GitLabApiException) thrown; }
throw new GitLabApiException(thrown);
}
}
20 changes: 10 additions & 10 deletions src/main/java/com/messners/gitlab/api/CommitsApi.java
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
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.
Expand All @@ -29,8 +29,8 @@ public CommitsApi (GitLabApi gitLabApi) {
* @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>>(){}));
}


Expand All @@ -45,8 +45,8 @@ public List<Commit> getCommits (int projectId) throws GitLabApiException {
* @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));
}


Expand All @@ -61,8 +61,8 @@ public Commit getCommits (int projectId, String sha) throws GitLabApiException {
* @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));
}
}
Loading

0 comments on commit 420712e

Please sign in to comment.