Skip to content

Commit

Permalink
GDB-6143 Changes the object used for endpoint creation from URL to URI
Browse files Browse the repository at this point in the history
- Changed the object for the creation of the full address of the command
endpoints from `URL` to `URI`.
- Updated all of the affected command implementations and tests.
- Prepare for release of version `1.3.0`.
  • Loading branch information
tonyKunchev committed Dec 13, 2021
1 parent bca79ed commit 0c564fc
Show file tree
Hide file tree
Showing 37 changed files with 105 additions and 142 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
additional information to the different commands.
- Moved the `ExportRowsCommand` in its own package in order to keep the structure of the project consistent. Updated the command to comply with the new definition of the
commands.
- Changed the object that is used for creation of the full endpoint address from `URL` to `URI`, which can be passed directly to the request builders without the need of
converting it to `String`.

### Bug fixes

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ complete given operation.
<dependency>
<groupId>com.ontotext</groupId>
<artifactId>ontorefine-client</artifactId>
<version>1.2.0</version>
<version>1.3.0</version>
</dependency>
```

Expand Down
1 change: 0 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

## Minor

- Change the building of the Refine instance address. Instead of URL, we can use URI and directly provide it to the request builders
- Refactor the project metadata retrieving command to implement the new definition.
- Think of a way to separate the commands in the `RefineCommands` interface by some common classifier. For example `project`, `reconciliation`, `preferences`, etc.
- Remove the unit tests, which are duplicating the integration tests as there are redundant. We need to use unit tests only for commands input verification or error handling.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<groupId>com.ontotext</groupId>
<artifactId>ontorefine-client</artifactId>
<version>1.3-SNAPSHOT</version>
<version>1.3.0</version>

<name>${project.groupId}:${project.artifactId}</name>
<description>An OntoRefine Client Library</description>
Expand Down
34 changes: 17 additions & 17 deletions src/main/java/com/ontotext/refine/client/RefineClient.java
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
package com.ontotext.refine.client;

import java.io.Closeable;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;


/**
* Represents the client which purpose is to provide the HTTP communication between the application
* and the Refine instance.
*/
public class RefineClient implements Closeable {
public class RefineClient implements AutoCloseable {

private final URL url;
private final URI uri;
private final CloseableHttpClient httpClient;

/**
* Creates new client instance.
*
* @param url where the Refine instance could be accessed
* @param uri where the Refine instance could be accessed
* @param httpClient used to executed the requests
*/
RefineClient(URL url, CloseableHttpClient httpClient) {
this.url = url;
RefineClient(URI uri, CloseableHttpClient httpClient) {
this.uri = uri;
this.httpClient = httpClient;
}

/**
* Creates URL from the provided path and the base URL of the current client.
* Creates URI from the provided path and the base URI of the current client.
*
* @param path to be added to the base URL
* @return new {@link URL}
* @param path to be added to the base URI
* @return new {@link URI}
*/
public URL createUrl(String path) {
public URI createUri(String path) {
try {
return new URL(url, path);
} catch (MalformedURLException e) {
throw new IllegalArgumentException(e);
return new URIBuilder(uri).setPath(path).build();
} catch (URISyntaxException uriExc) {
throw new IllegalArgumentException(uriExc);
}
}

Expand All @@ -58,12 +58,12 @@ public <T> T execute(HttpUriRequest request, ResponseHandler<? extends T> respon
}

@Override
public void close() throws IOException {
public void close() throws Exception {
httpClient.close();
}

@Override
public String toString() {
return "RefineClient{" + "url=" + url + '}';
return "RefineClient{" + "url=" + uri + '}';
}
}
8 changes: 4 additions & 4 deletions src/main/java/com/ontotext/refine/client/RefineClients.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.ontotext.refine.client;

import java.net.MalformedURLException;
import java.net.URL;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.http.impl.client.HttpClients;

public interface RefineClients {

static RefineClient create(String url) throws MalformedURLException {
static RefineClient create(String uri) throws URISyntaxException {
// TODO configure some sensible timeouts and other configurations
return new RefineClient(new URL(url), HttpClients.createDefault());
return new RefineClient(new URI(uri), HttpClients.createDefault());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import com.ontotext.refine.client.RefineClient;
import com.ontotext.refine.client.exceptions.RefineException;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -79,9 +78,9 @@ public ExpressionPreviewResponse execute(RefineClient client) throws RefineExcep
form.add(new BasicNameValuePair("repeat", String.valueOf(repeat)));
form.add(new BasicNameValuePair("repeatCount", String.valueOf(repeatCount)));

URL url = client.createUrl(endpoint() + "?" + Constants.CSRF_TOKEN_PARAM + token);
HttpUriRequest request = RequestBuilder
.post(url.toString())
.post(client.createUri(endpoint()))
.addParameter(Constants.CSRF_TOKEN, token)
.setHeader(ACCEPT, APPLICATION_JSON.getMimeType())
.setEntity(new UrlEncodedFormEntity(form, Consts.UTF_8))
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public String endpoint() {
public GetProjectMetadataResponse execute(RefineClient client) throws RefineException {
try {
HttpUriRequest request = RequestBuilder
.get(client.createUrl(endpoint()).toString())
.get(client.createUri(endpoint()))
.setHeader(ACCEPT, APPLICATION_JSON.getMimeType())
.addParameter(new BasicNameValuePair("project", projectId))
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ public interface RefineCommand<T> extends ResponseHandler<T> {
class Constants {

public static final String PROJECT = "project";
public static final String PROJECT_PARAM = PROJECT + "=";

public static final String CSRF_TOKEN = "csrf_token";
public static final String CSRF_TOKEN_PARAM = CSRF_TOKEN + "=";

private Constants() {
// utility
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.ontotext.refine.client.command.create;

import static com.ontotext.refine.client.command.RefineCommand.Constants.CSRF_TOKEN_PARAM;
import static com.ontotext.refine.client.util.HttpParser.HTTP_PARSER;
import static java.util.Collections.singletonList;
import static org.apache.commons.lang3.Validate.notBlank;
import static org.apache.commons.lang3.Validate.notNull;
import static org.apache.http.HttpHeaders.ACCEPT;
Expand All @@ -18,15 +16,12 @@
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.message.BasicNameValuePair;

/**
* A command to create a project.
Expand Down Expand Up @@ -65,14 +60,6 @@ public String endpoint() {
@Override
public CreateProjectResponse execute(RefineClient client) throws RefineException {
try {
String urlAsStr = endpoint() + "?" + CSRF_TOKEN_PARAM + token;
if (options != null) {
// https://github.com/dtap-gmbh/refine-java/issues/14
// https://github.com/OpenRefine/OpenRefine/issues/1757
// OpenRefine ignores options as form parameter, but accepts them as get parameter
urlAsStr += "&" + urlEncodedOptions();
}

MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
if (format != null) {
multipartEntityBuilder.addTextBody("format", format.getValue(), TEXT_PLAIN);
Expand All @@ -88,7 +75,8 @@ public CreateProjectResponse execute(RefineClient client) throws RefineException
.build();

HttpUriRequest request = RequestBuilder
.post(client.createUrl(urlAsStr).toString())
.post(client.createUri(endpoint()))
.addParameter(Constants.CSRF_TOKEN, token)
.setHeader(ACCEPT, APPLICATION_JSON.getMimeType())
.setEntity(entity)
.build();
Expand All @@ -100,11 +88,6 @@ public CreateProjectResponse execute(RefineClient client) throws RefineException
}
}

private String urlEncodedOptions() {
return URLEncodedUtils.format(
singletonList(new BasicNameValuePair("options", options.asJson())), StandardCharsets.UTF_8);
}

@Override
public CreateProjectResponse handleResponse(HttpResponse response) throws IOException {
// TODO: parse errors in refine are returned as HTML
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import com.ontotext.refine.client.command.RefineCommand;
import com.ontotext.refine.client.exceptions.RefineException;
import java.io.IOException;
import java.net.URL;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
Expand All @@ -37,10 +36,8 @@ public String endpoint() {
@Override
public GetCsrfTokenResponse execute(RefineClient client) throws RefineException {
try {
URL url = client.createUrl(endpoint());

HttpUriRequest request = RequestBuilder
.get(url.toString())
.get(client.createUri(endpoint()))
.setHeader(ACCEPT, APPLICATION_JSON.getMimeType())
.build();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.ontotext.refine.client.command.delete;

import static com.ontotext.refine.client.command.RefineCommand.Constants.CSRF_TOKEN_PARAM;
import static com.ontotext.refine.client.util.HttpParser.HTTP_PARSER;
import static com.ontotext.refine.client.util.JsonParser.JSON_PARSER;
import static java.util.Collections.singletonList;
Expand All @@ -14,8 +13,7 @@
import com.ontotext.refine.client.command.RefineCommand;
import com.ontotext.refine.client.exceptions.RefineException;
import java.io.IOException;
import java.net.URL;
import org.apache.http.Consts;
import java.nio.charset.StandardCharsets;
import org.apache.http.HttpResponse;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpUriRequest;
Expand Down Expand Up @@ -51,13 +49,12 @@ public String endpoint() {
@Override
public DeleteProjectResponse execute(RefineClient client) throws RefineException {
try {
URL url = client.createUrl(endpoint() + "?" + CSRF_TOKEN_PARAM + token);

UrlEncodedFormEntity entity = new UrlEncodedFormEntity(
singletonList(new BasicNameValuePair("project", projectId)), Consts.UTF_8);
singletonList(new BasicNameValuePair("project", projectId)), StandardCharsets.UTF_8);

HttpUriRequest request = RequestBuilder
.post(url.toString())
.post(client.createUri(endpoint()))
.addParameter(Constants.CSRF_TOKEN, token)
.setHeader(ACCEPT, APPLICATION_JSON.getMimeType())
.setEntity(entity)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public ExportRowsResponse execute(RefineClient client) throws RefineException {
List<NameValuePair> form = buildForm();

HttpUriRequest request = RequestBuilder
.post(client.createUrl(endpoint()).toString())
.post(client.createUri(endpoint()))
.setHeader(ACCEPT, APPLICATION_JSON.getMimeType())
.setEntity(new UrlEncodedFormEntity(form, UTF_8))
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.ontotext.refine.client.exceptions.RefineException;
import com.ontotext.refine.client.util.HttpParser;
import java.io.IOException;
import java.net.URL;
import java.net.URI;
import org.apache.commons.lang3.Validate;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
Expand Down Expand Up @@ -34,9 +34,9 @@ public String endpoint() {
@Override
public GetProjectModelsResponse execute(RefineClient client) throws RefineException {
try {
URL url = client.createUrl(endpoint());
URI uri = client.createUri(endpoint());
HttpUriRequest request =
RequestBuilder.get(url.toString()).addParameter(Constants.PROJECT, project).build();
RequestBuilder.get(uri).addParameter(Constants.PROJECT, project).build();
return client.execute(request, this);
} catch (IOException ioe) {
throw new RefineException(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.ontotext.refine.client.command.operations;

import static com.ontotext.refine.client.command.RefineCommand.Constants.CSRF_TOKEN_PARAM;
import static com.ontotext.refine.client.util.HttpParser.HTTP_PARSER;
import static com.ontotext.refine.client.util.JsonParser.JSON_PARSER;
import static org.apache.commons.lang3.StringUtils.appendIfMissing;
Expand All @@ -19,7 +18,6 @@
import com.ontotext.refine.client.command.RefineCommand;
import com.ontotext.refine.client.exceptions.RefineException;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -64,8 +62,6 @@ public String endpoint() {
@Override
public ApplyOperationsResponse execute(RefineClient client) throws RefineException {
try {
URL url = client.createUrl(endpoint() + "?" + CSRF_TOKEN_PARAM + token);

List<NameValuePair> form = new ArrayList<>(2);
form.add(new BasicNameValuePair(Constants.PROJECT, projectId));

Expand All @@ -77,7 +73,8 @@ public ApplyOperationsResponse execute(RefineClient client) throws RefineExcepti
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(form, Consts.UTF_8);

HttpUriRequest request = RequestBuilder
.post(url.toString())
.post(client.createUri(endpoint()))
.addParameter(Constants.CSRF_TOKEN, token)
.setHeader(ACCEPT, APPLICATION_JSON.getMimeType())
.setEntity(entity)
.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package com.ontotext.refine.client.command.operations;

import static com.ontotext.refine.client.command.RefineCommand.Constants.PROJECT;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.ontotext.refine.client.RefineClient;
import com.ontotext.refine.client.command.RefineCommand;
import com.ontotext.refine.client.exceptions.RefineException;
import com.ontotext.refine.client.util.HttpParser;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import org.apache.commons.lang3.Validate;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;


Expand All @@ -36,8 +38,9 @@ public String endpoint() {
@Override
public GetOperationsResponse execute(RefineClient client) throws RefineException {
try {
URL url = client.createUrl(endpoint() + "?" + Constants.PROJECT_PARAM + project);
return client.execute(RequestBuilder.get(url.toString()).build(), this);
HttpUriRequest request =
RequestBuilder.get(client.createUri(endpoint())).addParameter(PROJECT, project).build();
return client.execute(request, this);
} catch (IOException ioe) {
String error = String.format(
"Failed to retrieve the operations for project: '%s' due to: %s",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public String endpoint() {
public GetPreferenceCommandResponse execute(RefineClient client) throws RefineException {
try {
HttpUriRequest request = RequestBuilder
.get(client.createUrl(endpoint()).toString())
.get(client.createUri(endpoint()))
.addParameter("name", property)
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public SetPreferenceCommandResponse execute(RefineClient client) throws RefineEx
form.add(new BasicNameValuePair("value", value.toString()));

HttpUriRequest request = RequestBuilder
.post(client.createUrl(endpoint()).toString())
.post(client.createUri(endpoint()))
.addParameter("name", property)
.setEntity(new UrlEncodedFormEntity(form, StandardCharsets.UTF_8))
.build();
Expand Down
Loading

0 comments on commit 0c564fc

Please sign in to comment.