diff --git a/src/org/kar/archidata/tools/RESTApi.java b/src/org/kar/archidata/tools/RESTApi.java index 794feb9..69dbc60 100644 --- a/src/org/kar/archidata/tools/RESTApi.java +++ b/src/org/kar/archidata/tools/RESTApi.java @@ -36,7 +36,7 @@ public class RESTApi { this.token = token; } - public List gets(final Class clazz, final String urlOffset) + public List gets(final Class clazz, final String urlOffset) throws RESTErrorResponseException, IOException, InterruptedException { final HttpClient client = HttpClient.newHttpClient(); Builder requestBuilding = HttpRequest.newBuilder().version(Version.HTTP_1_1) @@ -60,58 +60,79 @@ public class RESTApi { this.mapper.getTypeFactory().constructCollectionType(List.class, clazz)); } - public T get(final Class clazz, final String urlOffset) + public TYPE_RESPONSE get(final Class clazz, final String urlOffset) throws RESTErrorResponseException, IOException, InterruptedException { return modelSendJson("GET", clazz, urlOffset, null); } - public T post(final Class clazz, final String urlOffset, final U data) - throws RESTErrorResponseException, IOException, InterruptedException { + public TYPE_RESPONSET post( + final Class clazz, + final String urlOffset, + final TYPE_BODY data) throws RESTErrorResponseException, IOException, InterruptedException { return modelSend("POST", clazz, urlOffset, data); } - public T postJson(final Class clazz, final String urlOffset, final String body) - throws RESTErrorResponseException, IOException, InterruptedException { + public TYPE_RESPONSE postJson( + final Class clazz, + final String urlOffset, + final String body) throws RESTErrorResponseException, IOException, InterruptedException { return modelSendJson("POST", clazz, urlOffset, body); } - public T postMap(final Class clazz, final String urlOffset, final Map data) - throws RESTErrorResponseException, IOException, InterruptedException { + public TYPE_RESPONSE postMap( + final Class clazz, + final String urlOffset, + final Map data) throws RESTErrorResponseException, IOException, InterruptedException { return modelSendMap("POST", clazz, urlOffset, data); } - public T put(final Class clazz, final String urlOffset, final U data) - throws RESTErrorResponseException, IOException, InterruptedException { + public TYPE_RESPONSE put( + final Class clazz, + final String urlOffset, + final TYPE_BODY data) throws RESTErrorResponseException, IOException, InterruptedException { return modelSend("PUT", clazz, urlOffset, data); } - public T putJson(final Class clazz, final String urlOffset, final String body) - throws RESTErrorResponseException, IOException, InterruptedException { + public TYPE_RESPONSE putJson( + final Class clazz, + final String urlOffset, + final String body) throws RESTErrorResponseException, IOException, InterruptedException { return modelSendJson("PUT", clazz, urlOffset, body); } - public T putMap(final Class clazz, final String urlOffset, final Map data) - throws RESTErrorResponseException, IOException, InterruptedException { + public TYPE_RESPONSE putMap( + final Class clazz, + final String urlOffset, + final Map data) throws RESTErrorResponseException, IOException, InterruptedException { return modelSendMap("PUT", clazz, urlOffset, data); } - public T patch(final Class clazz, final String urlOffset, final U data) - throws RESTErrorResponseException, IOException, InterruptedException { + public TYPE_RESPONSE patch( + final Class clazz, + final String urlOffset, + final TYPE_BODY data) throws RESTErrorResponseException, IOException, InterruptedException { return modelSend("PATCH", clazz, urlOffset, data); } - public T patchJson(final Class clazz, final String urlOffset, final String body) - throws RESTErrorResponseException, IOException, InterruptedException { + public TYPE_RESPONSE patchJson( + final Class clazz, + final String urlOffset, + final String body) throws RESTErrorResponseException, IOException, InterruptedException { return modelSendJson("PATCH", clazz, urlOffset, body); } - public T patchMap(final Class clazz, final String urlOffset, final Map data) - throws RESTErrorResponseException, IOException, InterruptedException { + public TYPE_RESPONSE patchMap( + final Class clazz, + final String urlOffset, + final Map data) throws RESTErrorResponseException, IOException, InterruptedException { return modelSendMap("PATCH", clazz, urlOffset, data); } - protected T modelSend(final String model, final Class clazz, final String urlOffset, final U data) - throws RESTErrorResponseException, IOException, InterruptedException { + protected TYPE_RESPONSE modelSend( + final String model, + final Class clazz, + final String urlOffset, + final TYPE_BODY data) throws RESTErrorResponseException, IOException, InterruptedException { if (data == null) { return modelSendJson(model, clazz, urlOffset, null); } else { @@ -121,8 +142,11 @@ public class RESTApi { } @SuppressWarnings("unchecked") - public T modelSendJson(final String model, final Class clazz, final String urlOffset, String body) - throws RESTErrorResponseException, IOException, InterruptedException { + public TYPE_RESPONSE modelSendJson( + final String model, + final Class clazz, + final String urlOffset, + String body) throws RESTErrorResponseException, IOException, InterruptedException { final HttpClient client = HttpClient.newHttpClient(); // client.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true); Builder requestBuilding = HttpRequest.newBuilder().version(Version.HTTP_1_1) @@ -160,16 +184,16 @@ public class RESTApi { return null; } if (clazz.equals(String.class)) { - return (T) httpResponse.body(); + return (TYPE_RESPONSE) httpResponse.body(); } LOGGER.trace("Receive model: {} with data: '{}'", clazz.getCanonicalName(), httpResponse.body()); return this.mapper.readValue(httpResponse.body(), clazz); } @SuppressWarnings("unchecked") - public T modelSendMap( + public TYPE_RESPONSE modelSendMap( final String model, - final Class clazz, + final Class clazz, final String urlOffset, final Map data) throws RESTErrorResponseException, IOException, InterruptedException { final HttpClient client = HttpClient.newHttpClient(); @@ -201,7 +225,7 @@ public class RESTApi { return null; } if (clazz.equals(String.class)) { - return (T) httpResponse.body(); + return (TYPE_RESPONSE) httpResponse.body(); } return this.mapper.readValue(httpResponse.body(), clazz); } @@ -216,12 +240,12 @@ public class RESTApi { /** * Call a DELETE on a REST API with retrieving some data - * @param Type of data that might be received. + * @param Type of data that might be received. * @param clazz Class model of the data that might be parsed. * @param urlOffset Offset to call the API * @return The parsed object received. */ - public T delete(final Class clazz, final String urlOffset) + public TYPE_RESPONSE delete(final Class clazz, final String urlOffset) throws RESTErrorResponseException, IOException, InterruptedException { return simpleRequest("DELETE", clazz, urlOffset); } @@ -236,12 +260,12 @@ public class RESTApi { /** * Call a ARCHIVE on a REST API with retrieving some data - * @param Type of data that might be received. + * @param Type of data that might be received. * @param clazz Class model of the data that might be parsed. * @param urlOffset Offset to call the API * @return The parsed object received. */ - public T archive(final Class clazz, final String urlOffset) + public TYPE_RESPONSE archive(final Class clazz, final String urlOffset) throws RESTErrorResponseException, IOException, InterruptedException { return simpleRequest("ARCHIVE", clazz, urlOffset); } @@ -268,14 +292,16 @@ public class RESTApi { /** * Call a key on a REST API with retrieving some data - * @param Type of data that might be received. + * @param Type of data that might be received. * @param model name of the key for the REST call * @param clazz Class model of the data that might be parsed. * @param urlOffset Offset to call the API * @return The parsed object received. */ - public T simpleRequest(final String model, final Class clazz, final String urlOffset) - throws RESTErrorResponseException, IOException, InterruptedException { + public TYPE_RESPONSE simpleRequest( + final String model, + final Class clazz, + final String urlOffset) throws RESTErrorResponseException, IOException, InterruptedException { final HttpClient client = HttpClient.newHttpClient(); Builder requestBuilding = HttpRequest.newBuilder().version(Version.HTTP_1_1) .uri(URI.create(this.baseUrl + urlOffset)); @@ -298,7 +324,7 @@ public class RESTApi { return null; } if (clazz.equals(String.class)) { - return (T) httpResponse.body(); + return (TYPE_RESPONSE) httpResponse.body(); } return this.mapper.readValue(httpResponse.body(), clazz); }