From c7137ae52b031970effb852526a4aea4c0a49d29 Mon Sep 17 00:00:00 2001 From: SristiSingh-eu Date: Thu, 21 Nov 2024 14:50:48 +0100 Subject: [PATCH 01/21] EA-4015 updated set client code --- .../europeana/set/client/BaseUserSetApi.java | 66 +++++++++--- ...rSetApiImpl.java => UserSetApiClient.java} | 25 ++--- .../client/config/ClientConfiguration.java | 102 +++++------------- .../client/connection/BaseApiConnection.java | 66 +++++------- .../connection/UserSetApiConnection.java | 53 +-------- .../exception/SetApiClientException.java | 14 +++ set-client/src/main/resources/.gitignore | 3 +- .../web/BaseAPIConnectionTest.java | 9 +- .../web/BaseWebUserSetProtocol.java | 17 ++- .../web/ClientConfigurationTest.java | 7 +- 10 files changed, 147 insertions(+), 215 deletions(-) rename set-client/src/main/java/eu/europeana/set/client/{web/WebUserSetApiImpl.java => UserSetApiClient.java} (69%) create mode 100644 set-client/src/main/java/eu/europeana/set/client/exception/SetApiClientException.java diff --git a/set-client/src/main/java/eu/europeana/set/client/BaseUserSetApi.java b/set-client/src/main/java/eu/europeana/set/client/BaseUserSetApi.java index c366b312..25b21c4b 100644 --- a/set-client/src/main/java/eu/europeana/set/client/BaseUserSetApi.java +++ b/set-client/src/main/java/eu/europeana/set/client/BaseUserSetApi.java @@ -2,6 +2,16 @@ import eu.europeana.set.client.config.ClientConfiguration; import eu.europeana.set.client.connection.UserSetApiConnection; +import eu.europeana.set.client.exception.SetApiClientException; +import eu.europeana.set.client.exception.TechnicalRuntimeException; +import eu.europeana.set.common.http.HttpConnection; +import eu.europeana.set.definitions.model.vocabulary.WebUserSetFields; +import org.codehaus.jettison.json.JSONException; +import org.codehaus.jettison.json.JSONObject; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; + +import java.io.IOException; /** * Base class for client API @@ -12,25 +22,57 @@ public class BaseUserSetApi { private final ClientConfiguration configuration; - protected final UserSetApiConnection apiConnection; - - protected BaseUserSetApi(ClientConfiguration configuration, UserSetApiConnection apiConnection) { - this.configuration = configuration; - this.apiConnection = apiConnection; + private UserSetApiConnection apiConnection; + + protected BaseUserSetApi(ClientConfiguration configuration) throws SetApiClientException { + this.configuration = configuration; + if (this.configuration.getServiceUri() == null && this.configuration.getApiKey() == null) { + throw new SetApiClientException(" Set Api Endpoint not provide !!!"); + } + + if (this.configuration.getOauthServiceUri() == null || this.configuration.getOauthRequestParams() == null) { + throw new SetApiClientException("Oauth uri and param not provided !!!"); + } + + this.apiConnection = new UserSetApiConnection( + this.configuration.getServiceUri(), + this.configuration.getApiKey(), + getOauthToken(this.configuration.getOauthServiceUri(), this.configuration.getOauthRequestParams())); } - protected BaseUserSetApi() { - this.configuration = ClientConfiguration.getInstance(); - this.apiConnection = new UserSetApiConnection(getConfiguration().getServiceUri(), - getConfiguration().getApiKey()); + public BaseUserSetApi() throws SetApiClientException { + this(new ClientConfiguration()); + } + + private String getOauthToken(String oauthServiceUri, String oauthRequestParams ) { + try { + String ACCESS_TOKEN = "access_token"; + HttpConnection connection = new HttpConnection(); + ResponseEntity response; + response = connection.post(oauthServiceUri, oauthRequestParams, "application/x-www-form-urlencoded"); + + if (HttpStatus.OK == response.getStatusCode()) { + String body = response.getBody(); + JSONObject json = new JSONObject(body); + if (json.has(ACCESS_TOKEN)) { + return "Bearer " + json.getString(ACCESS_TOKEN); + } else { + throw new TechnicalRuntimeException( + "Cannot extract authentication token from reponse:" + body); + } + } else { + throw new TechnicalRuntimeException("Error occured when calling oath service! " + response); + } + } catch (IOException | JSONException e) { + throw new TechnicalRuntimeException("Cannot retrieve authentication token!", e); + } } public UserSetApiConnection getApiConnection() { - return apiConnection; + return apiConnection; } public ClientConfiguration getConfiguration() { - return configuration; + return configuration; } - } diff --git a/set-client/src/main/java/eu/europeana/set/client/web/WebUserSetApiImpl.java b/set-client/src/main/java/eu/europeana/set/client/UserSetApiClient.java similarity index 69% rename from set-client/src/main/java/eu/europeana/set/client/web/WebUserSetApiImpl.java rename to set-client/src/main/java/eu/europeana/set/client/UserSetApiClient.java index 722c83d8..76f4e22d 100644 --- a/set-client/src/main/java/eu/europeana/set/client/web/WebUserSetApiImpl.java +++ b/set-client/src/main/java/eu/europeana/set/client/UserSetApiClient.java @@ -1,7 +1,10 @@ -package eu.europeana.set.client.web; +package eu.europeana.set.client; import java.io.IOException; +import eu.europeana.set.client.config.ClientConfiguration; +import eu.europeana.set.client.exception.SetApiClientException; +import eu.europeana.set.client.web.WebUserSetApi; import org.springframework.http.ResponseEntity; import eu.europeana.set.client.BaseUserSetApi; @@ -13,26 +16,21 @@ * */ -public class WebUserSetApiImpl extends BaseUserSetApi implements WebUserSetApi { +public class UserSetApiClient extends BaseUserSetApi implements WebUserSetApi { - /** - * Default constructor - */ - public WebUserSetApiImpl() { - super(); + public UserSetApiClient(ClientConfiguration configuration) throws SetApiClientException { + super(configuration); } @Override public ResponseEntity createUserSet(String set, String profile) { - ResponseEntity res; try { - res = apiConnection.createUserSet(set, profile); + res = getApiConnection().createUserSet(set, profile); } catch (IOException e) { throw new TechnicalRuntimeException( "Exception occured when invoking the UserSetJsonApi createUserSet method", e); } - return res; } @@ -40,7 +38,7 @@ public ResponseEntity createUserSet(String set, String profile) { public ResponseEntity deleteUserSet(String identifier) { ResponseEntity res; try { - res = apiConnection.deleteUserSet(identifier); + res = getApiConnection().deleteUserSet(identifier); } catch (IOException e) { throw new TechnicalRuntimeException( "Exception occured when invoking the UserSetJsonApi deleteUserSet method", e); @@ -51,10 +49,9 @@ public ResponseEntity deleteUserSet(String identifier) { @Override public ResponseEntity getUserSet(String identifier, String profile) { - ResponseEntity res; try { - res = apiConnection.getUserSet(identifier, profile); + res = getApiConnection().getUserSet(identifier, profile); } catch (IOException e) { throw new TechnicalRuntimeException("Exception occured when invoking the UserSetJsonApi getUserSet method", e); @@ -67,7 +64,7 @@ public ResponseEntity getUserSet(String identifier, String profile) { public ResponseEntity updateUserSet(String identifier, String set, String profile) { ResponseEntity res; try { - res = apiConnection.updateUserSet(identifier, set, profile); + res = getApiConnection().updateUserSet(identifier, set, profile); } catch (IOException e) { throw new TechnicalRuntimeException( "Exception occured when invoking the UserSetJsonApi updateUserSet method", e); diff --git a/set-client/src/main/java/eu/europeana/set/client/config/ClientConfiguration.java b/set-client/src/main/java/eu/europeana/set/client/config/ClientConfiguration.java index aef7c72d..afc18b10 100644 --- a/set-client/src/main/java/eu/europeana/set/client/config/ClientConfiguration.java +++ b/set-client/src/main/java/eu/europeana/set/client/config/ClientConfiguration.java @@ -5,6 +5,8 @@ import java.util.Properties; import eu.europeana.set.client.exception.TechnicalRuntimeException; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; /** * configuration for accessing remote api @@ -15,6 +17,8 @@ public final class ClientConfiguration { + private static final Logger LOGGER = LogManager.getLogger(ClientConfiguration.class); + protected static final String SET_CLIENT_PROPERTIES_FILE = "/set-client.user.properties"; protected static final String PROP_SET_API_KEY = "set.api.key"; protected static final String PROP_SET_SERVICE_URI = "set.service.uri"; @@ -22,105 +26,47 @@ public final class ClientConfiguration { protected static final String PROP_OAUTH_REQUEST_PARAMS = "oauth.token.request.params"; private Properties properties; - private static ClientConfiguration singleton; - /** - * Hide the default constructor - */ - private ClientConfiguration() { + public ClientConfiguration() { + loadProperties(SET_CLIENT_PROPERTIES_FILE); } - /** - * Accessor method for the singleton - * - * @return - */ - public static synchronized ClientConfiguration getInstance() { - if (singleton == null) { - singleton = new ClientConfiguration(); - singleton.loadProperties(); - } - - return singleton; + public ClientConfiguration(Properties properties) { + this.properties = properties; } - /** - * Laizy loading of configuration properties - */ - public synchronized void loadProperties() { - - try { - properties = new Properties(); - InputStream resourceAsStream = getClass().getResourceAsStream(SET_CLIENT_PROPERTIES_FILE); - if (resourceAsStream == null) { - throw new TechnicalRuntimeException( - "No properties file found in classpath! " + SET_CLIENT_PROPERTIES_FILE); - } - getProperties().load(resourceAsStream); - - } catch (RuntimeException | IOException e) { - throw new TechnicalRuntimeException("Cannot read configuration file: " + SET_CLIENT_PROPERTIES_FILE, e); - } - + private Properties loadProperties(String propertiesFile) { + try { properties = new Properties(); + properties.load(getClass().getResourceAsStream(propertiesFile)); + } catch (IOException e) { + LOGGER.error("Error loading the properties file {}", propertiesFile); + } + return properties; } - /** - * provides access to the configuration properties. It is not recommended to use - * the properties directly, but the - * - * @return - */ - Properties getProperties() { - return properties; - } - - /** - * - * @return the name of the file storing the client configuration - */ String getConfigurationFile() { return SET_CLIENT_PROPERTIES_FILE; } - /** - * This method provides access to the API key defined in the configuration file - * - * @see PROP_EUROPEANA_API_KEY - * - * @return - */ public String getApiKey() { - return getProperties().getProperty(PROP_SET_API_KEY); + return getProperty(PROP_SET_API_KEY); } - /** - * This method provides access to the search uri value defined in the - * configuration file - * - * @see PROP_EUROPEANA_SEARCH_URI - * - * @return - */ + public String getServiceUri() { - return getProperties().getProperty(PROP_SET_SERVICE_URI); + return getProperty(PROP_SET_SERVICE_URI); } - /** - * This method returns the uri of the oauth service as configured in - * - * @return - */ public String getOauthServiceUri() { - return getProperties().getProperty(PROP_OAUTH_SERVICE_URI); + return getProperty(PROP_OAUTH_SERVICE_URI); } - /** - * This method returns the request params needed to acquire a new token - * - * @return - */ public String getOauthRequestParams() { - return getProperties().getProperty(PROP_OAUTH_REQUEST_PARAMS); + return getProperty(PROP_OAUTH_REQUEST_PARAMS); + } + + private String getProperty(String propertyName) { + return properties.getProperty(propertyName); } } diff --git a/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java b/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java index 02383d75..56d662fb 100644 --- a/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java +++ b/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java @@ -17,58 +17,24 @@ public class BaseApiConnection { private static final String DELETE_URL_RESPONSE = ". Returns headers and status code."; private static final String API_ADMIN_KEY = "apiadmin"; - private String apiKey; - private String setServiceUri = ""; private HttpConnection httpConnection = new HttpConnection(); + private String apiKey; + private String setServiceUri; + String regularUserAuthorizationValue = null; - - public String getApiKey() { - return apiKey; + public BaseApiConnection(String setServiceUri, String apiKey, String regularUserAuthorizationValue) { + this.setServiceUri = setServiceUri; + this.apiKey = apiKey; + this.regularUserAuthorizationValue = regularUserAuthorizationValue; } - public String getAdminApiKey() { return API_ADMIN_KEY; } - - public void setApiKey(String apiKey) { - this.apiKey = apiKey; - } - - public StringBuilder getUserSetServiceUri() { - StringBuilder urlBuilder = new StringBuilder(); - urlBuilder.append(setServiceUri); - - if(!setServiceUri.endsWith(WebUserSetFields.SLASH)) - urlBuilder.append(WebUserSetFields.SLASH); - - return urlBuilder; - } - - public void setUserSetServiceUri(String setServiceUri) { - this.setServiceUri = setServiceUri; - } public HttpConnection getHttpConnection() { return httpConnection; } - public void setHttpConnection(HttpConnection httpConnection) { - this.httpConnection = httpConnection; - } - - - /** - * Create a new connection to the UserSet Service (REST API). - * - * @param apiKey - * API Key required to access the API - */ - public BaseApiConnection(String setServiceUri, String apiKey) { - this.apiKey = apiKey; - this.setServiceUri = setServiceUri; - } - - /** * This method makes POST request for given URL, header and JSON body parameter that returns * response body, response headers and status code. @@ -152,5 +118,21 @@ ResponseEntity deleteURL(String url, String authorizationHeaderValue) th logger.trace("Call to UserSet API (DELETE): {}. Returns headers and status code.", url); return getHttpConnection().deleteURL(url, authorizationHeaderValue); } - + + public StringBuilder getUserSetServiceUri() { + StringBuilder urlBuilder = new StringBuilder(); + urlBuilder.append(this.setServiceUri); + if(!this.setServiceUri.endsWith(WebUserSetFields.SLASH)) + urlBuilder.append(WebUserSetFields.SLASH); + + return urlBuilder; + } + + public String getApiKey() { + return apiKey; + } + + public String getSetServiceUri() { + return setServiceUri; + } } \ No newline at end of file diff --git a/set-client/src/main/java/eu/europeana/set/client/connection/UserSetApiConnection.java b/set-client/src/main/java/eu/europeana/set/client/connection/UserSetApiConnection.java index 8c09ee26..28881aa1 100644 --- a/set-client/src/main/java/eu/europeana/set/client/connection/UserSetApiConnection.java +++ b/set-client/src/main/java/eu/europeana/set/client/connection/UserSetApiConnection.java @@ -18,57 +18,10 @@ */ public class UserSetApiConnection extends BaseApiConnection { - String regularUserAuthorizationValue = null; - - /** - * Create a new connection to the UserSet Service (REST API). - * - * @param apiKey API Key required to access the API - */ - public UserSetApiConnection(String setServiceUri, String apiKey) { - super(setServiceUri, apiKey); - initConfigurations(); + public UserSetApiConnection(String setServiceUri, String apiKey, String regularUserAuthorizationValue) { + super(setServiceUri, apiKey, regularUserAuthorizationValue); } - public UserSetApiConnection() { - this(ClientConfiguration.getInstance().getServiceUri(), - ClientConfiguration.getInstance().getApiKey()); - initConfigurations(); - } - - private void initConfigurations() { - // regularUserAuthorizationValue = - // ClientConfiguration.getInstance().getAuthorizationHeaderValue(); - regularUserAuthorizationValue = getOauthToken(); - } - - public String getOauthToken() { - try { - - String ACCESS_TOKEN = "access_token"; - String oauthUri = ClientConfiguration.getInstance().getOauthServiceUri(); - String oauthParams = ClientConfiguration.getInstance().getOauthRequestParams(); - HttpConnection connection = new HttpConnection(); - ResponseEntity response; - response = connection.post(oauthUri, oauthParams, "application/x-www-form-urlencoded"); - - if (HttpStatus.OK == response.getStatusCode()) { - String body = response.getBody(); - JSONObject json = new JSONObject(body); - if (json.has(ACCESS_TOKEN)) { - return "Bearer " + json.getString(ACCESS_TOKEN); - } else { - throw new TechnicalRuntimeException( - "Cannot extract authentication token from reponse:" + body); - } - } else { - throw new TechnicalRuntimeException("Error occured when calling oath service! " + response); - } - } catch (IOException | JSONException e) { - throw new TechnicalRuntimeException("Cannot retrieve authentication token!", e); - } - - } /** * This method creates UserSet object from Json string. Example HTTP request for tag object: @@ -170,4 +123,6 @@ public ResponseEntity deleteUserSet(String identifier) throws IOExceptio return deleteURL(urlBuilder.toString(), regularUserAuthorizationValue); } + + } diff --git a/set-client/src/main/java/eu/europeana/set/client/exception/SetApiClientException.java b/set-client/src/main/java/eu/europeana/set/client/exception/SetApiClientException.java new file mode 100644 index 00000000..d742d014 --- /dev/null +++ b/set-client/src/main/java/eu/europeana/set/client/exception/SetApiClientException.java @@ -0,0 +1,14 @@ +package eu.europeana.set.client.exception; + +public class SetApiClientException extends Exception { + private static final long serialVersionUID = 8281933808897246375L; + + public SetApiClientException(String message, Exception e) { + super(message, e); + } + + public SetApiClientException(String message) { + super(message); + } +} + diff --git a/set-client/src/main/resources/.gitignore b/set-client/src/main/resources/.gitignore index 39464b50..d05ae35b 100644 --- a/set-client/src/main/resources/.gitignore +++ b/set-client/src/main/resources/.gitignore @@ -1,3 +1,4 @@ -/set-client.properties +set.client.properties +**user.properties.template /log4j.xml /log4j2.xml diff --git a/set-client/src/test/java/eu/europeana/set/client/integration/web/BaseAPIConnectionTest.java b/set-client/src/test/java/eu/europeana/set/client/integration/web/BaseAPIConnectionTest.java index b6b45df1..5c55a40e 100644 --- a/set-client/src/test/java/eu/europeana/set/client/integration/web/BaseAPIConnectionTest.java +++ b/set-client/src/test/java/eu/europeana/set/client/integration/web/BaseAPIConnectionTest.java @@ -10,18 +10,15 @@ @Disabled("needs configuration file") public class BaseAPIConnectionTest { - - private static final String SERVICE_URI = "testUri"; private static final String API_KEY_1 = "api_key"; - private static final String API_KEY_2 = "test_api_key"; private static final String API_ADMIN_KEY = "apiadmin"; private BaseApiConnection baseApiConnection; @BeforeEach void setup() { - baseApiConnection = new BaseApiConnection(SERVICE_URI, API_KEY_1); + baseApiConnection = new BaseApiConnection(SERVICE_URI, API_KEY_1, null); } @Test @@ -30,14 +27,12 @@ public void Test_getUserSetServiceUri() { assertEquals(SERVICE_URI + WebUserSetFields.SLASH, result.toString()); result = new StringBuilder(); - baseApiConnection = new BaseApiConnection(SERVICE_URI + WebUserSetFields.SLASH, API_KEY_1); + baseApiConnection = new BaseApiConnection(SERVICE_URI + WebUserSetFields.SLASH, API_KEY_1, null); result = baseApiConnection.getUserSetServiceUri(); assertEquals(SERVICE_URI + WebUserSetFields.SLASH, result.toString()); assertEquals(API_KEY_1, baseApiConnection.getApiKey()); assertEquals(API_ADMIN_KEY, baseApiConnection.getAdminApiKey()); - baseApiConnection.setApiKey(API_KEY_2); - assertEquals(API_KEY_2, baseApiConnection.getApiKey()); } } diff --git a/set-client/src/test/java/eu/europeana/set/client/integration/web/BaseWebUserSetProtocol.java b/set-client/src/test/java/eu/europeana/set/client/integration/web/BaseWebUserSetProtocol.java index ccebec74..c8890fef 100644 --- a/set-client/src/test/java/eu/europeana/set/client/integration/web/BaseWebUserSetProtocol.java +++ b/set-client/src/test/java/eu/europeana/set/client/integration/web/BaseWebUserSetProtocol.java @@ -5,6 +5,8 @@ import java.io.InputStream; import java.io.InputStreamReader; +import eu.europeana.set.client.config.ClientConfiguration; +import eu.europeana.set.client.exception.SetApiClientException; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.junit.jupiter.api.BeforeEach; @@ -12,7 +14,7 @@ import org.springframework.http.ResponseEntity; import eu.europeana.set.client.web.WebUserSetApi; -import eu.europeana.set.client.web.WebUserSetApiImpl; +import eu.europeana.set.client.UserSetApiClient; import eu.europeana.set.definitions.model.UserSet; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -29,8 +31,8 @@ public class BaseWebUserSetProtocol { private WebUserSetApi apiClient; @BeforeEach - public void initObjects() { - apiClient = new WebUserSetApiImpl(); + public void initObjects() throws SetApiClientException { + apiClient = new UserSetApiClient(new ClientConfiguration()); } public WebUserSetApi getApiClient() { @@ -68,15 +70,12 @@ protected String getJsonStringInput(String resource) throws IOException { } - /** - * @param user set - */ - protected void deleteUserSet(UserSet set) { + protected void deleteUserSet(UserSet set) throws SetApiClientException { deleteUserSet(set.getIdentifier()); } - protected void deleteUserSet(String identifier) { - WebUserSetApi webUserSetApi = new WebUserSetApiImpl(); + protected void deleteUserSet(String identifier) throws SetApiClientException { + WebUserSetApi webUserSetApi = new UserSetApiClient(new ClientConfiguration()); ResponseEntity re = webUserSetApi.deleteUserSet(identifier); assertEquals(HttpStatus.OK, re.getStatusCode()); log.trace("User set deleted: /" + identifier); diff --git a/set-client/src/test/java/eu/europeana/set/client/integration/web/ClientConfigurationTest.java b/set-client/src/test/java/eu/europeana/set/client/integration/web/ClientConfigurationTest.java index 54b9aa35..d75d23eb 100644 --- a/set-client/src/test/java/eu/europeana/set/client/integration/web/ClientConfigurationTest.java +++ b/set-client/src/test/java/eu/europeana/set/client/integration/web/ClientConfigurationTest.java @@ -11,8 +11,9 @@ public class ClientConfigurationTest { @Test public void clientConfiguration_loadProperties() { - assertTrue(StringUtils.isNotEmpty(ClientConfiguration.getInstance().getOauthRequestParams())); - assertTrue(StringUtils.isNotEmpty(ClientConfiguration.getInstance().getServiceUri())); - assertTrue(StringUtils.isNotEmpty(ClientConfiguration.getInstance().getOauthServiceUri())); + ClientConfiguration configuration = new ClientConfiguration(); + assertTrue(StringUtils.isNotEmpty(configuration.getOauthRequestParams())); + assertTrue(StringUtils.isNotEmpty(configuration.getServiceUri())); + assertTrue(StringUtils.isNotEmpty(configuration.getOauthServiceUri())); } } From 8c5decf3ab9b413756ea9606377970698eac9825 Mon Sep 17 00:00:00 2001 From: SristiSingh-eu Date: Thu, 21 Nov 2024 15:54:18 +0100 Subject: [PATCH 02/21] EA-4015 added serach user set client methods and classes --- .../set/client/UserSetApiClient.java | 118 ++++++++++++------ .../client/connection/BaseApiConnection.java | 35 ++++++ .../connection/UserSetApiConnection.java | 24 ++-- .../set/client/web/SearchUserSetApi.java | 21 ++++ .../web/BaseWebUserSetProtocol.java | 13 +- .../web/WebUserSetProtocolExceptionsTest.java | 10 +- .../web/WebUserSetProtocolTest.java | 8 +- .../model/vocabulary/WebUserSetFields.java | 3 +- 8 files changed, 156 insertions(+), 76 deletions(-) create mode 100644 set-client/src/main/java/eu/europeana/set/client/web/SearchUserSetApi.java diff --git a/set-client/src/main/java/eu/europeana/set/client/UserSetApiClient.java b/set-client/src/main/java/eu/europeana/set/client/UserSetApiClient.java index 76f4e22d..884ce4e6 100644 --- a/set-client/src/main/java/eu/europeana/set/client/UserSetApiClient.java +++ b/set-client/src/main/java/eu/europeana/set/client/UserSetApiClient.java @@ -4,6 +4,7 @@ import eu.europeana.set.client.config.ClientConfiguration; import eu.europeana.set.client.exception.SetApiClientException; +import eu.europeana.set.client.web.SearchUserSetApi; import eu.europeana.set.client.web.WebUserSetApi; import org.springframework.http.ResponseEntity; @@ -16,61 +17,96 @@ * */ -public class UserSetApiClient extends BaseUserSetApi implements WebUserSetApi { +public class UserSetApiClient extends BaseUserSetApi { + + private final WebUserSetClient webUserSetClient; + private final SearchUserSetClient searchUserSetClient; public UserSetApiClient(ClientConfiguration configuration) throws SetApiClientException { super(configuration); + this.webUserSetClient = new WebUserSetClient(); + this.searchUserSetClient = new SearchUserSetClient(); } - @Override - public ResponseEntity createUserSet(String set, String profile) { - ResponseEntity res; - try { - res = getApiConnection().createUserSet(set, profile); - } catch (IOException e) { - throw new TechnicalRuntimeException( - "Exception occured when invoking the UserSetJsonApi createUserSet method", e); + public WebUserSetApi getWebUserSetApi() { + return webUserSetClient; } - return res; - } - @Override - public ResponseEntity deleteUserSet(String identifier) { - ResponseEntity res; - try { - res = getApiConnection().deleteUserSet(identifier); - } catch (IOException e) { - throw new TechnicalRuntimeException( - "Exception occured when invoking the UserSetJsonApi deleteUserSet method", e); + public SearchUserSetApi getSearchUserSetApi() { + return searchUserSetClient; } - return res; - } + /** + * Web User Set Client class + */ + private class WebUserSetClient implements WebUserSetApi { + @Override + public ResponseEntity createUserSet(String set, String profile) { + ResponseEntity res; + try { + res = getApiConnection().createUserSet(set, profile); + } catch (IOException e) { + throw new TechnicalRuntimeException( + "Exception occured when invoking the UserSetJsonApi createUserSet method", e); + } + return res; + } + + @Override + public ResponseEntity deleteUserSet(String identifier) { + ResponseEntity res; + try { + res = getApiConnection().deleteUserSet(identifier); + } catch (IOException e) { + throw new TechnicalRuntimeException( + "Exception occured when invoking the UserSetJsonApi deleteUserSet method", e); + } + + return res; + } - @Override - public ResponseEntity getUserSet(String identifier, String profile) { - ResponseEntity res; - try { - res = getApiConnection().getUserSet(identifier, profile); - } catch (IOException e) { - throw new TechnicalRuntimeException("Exception occured when invoking the UserSetJsonApi getUserSet method", - e); + @Override + public ResponseEntity getUserSet(String identifier, String profile) { + ResponseEntity res; + try { + res = getApiConnection().getUserSet(identifier, profile); + } catch (IOException e) { + throw new TechnicalRuntimeException("Exception occured when invoking the UserSetJsonApi getUserSet method", + e); + } + + return res; + } + + @Override + public ResponseEntity updateUserSet(String identifier, String set, String profile) { + ResponseEntity res; + try { + res = getApiConnection().updateUserSet(identifier, set, profile); + } catch (IOException e) { + throw new TechnicalRuntimeException( + "Exception occured when invoking the UserSetJsonApi updateUserSet method", e); + } + + return res; + } } - return res; - } + public class SearchUserSetClient implements SearchUserSetApi { - @Override - public ResponseEntity updateUserSet(String identifier, String set, String profile) { - ResponseEntity res; - try { - res = getApiConnection().updateUserSet(identifier, set, profile); - } catch (IOException e) { - throw new TechnicalRuntimeException( - "Exception occured when invoking the UserSetJsonApi updateUserSet method", e); + @Override + public ResponseEntity searchUserSet(String query, String[] qf, + String sort, int page, int pageSize, String facet, int facetLimit, String profile) { + ResponseEntity res; + try { + res = getApiConnection().searchUserSet(query, qf, sort, page, pageSize, facet, facetLimit, profile); + } catch (IOException e) { + throw new TechnicalRuntimeException( + "Exception occured when invoking the UserSetSearch API searchUserSet method", e); + } + return res; + } } - return res; - } } \ No newline at end of file diff --git a/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java b/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java index 56d662fb..de3bd14b 100644 --- a/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java +++ b/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java @@ -1,6 +1,8 @@ package eu.europeana.set.client.connection; import java.io.IOException; +import java.net.URI; +import java.util.function.Function; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -8,6 +10,11 @@ import eu.europeana.set.common.http.HttpConnection; import eu.europeana.set.definitions.model.vocabulary.WebUserSetFields; +import org.springframework.web.util.UriBuilder; + +import static eu.europeana.set.definitions.model.vocabulary.WebUserSetFields.FACETS; +import static eu.europeana.set.definitions.model.vocabulary.WebUserSetFields.SEARCH_PATH; +import static eu.europeana.api.commons.definitions.vocabulary.CommonApiConstants.*; public class BaseApiConnection { @@ -128,6 +135,34 @@ public StringBuilder getUserSetServiceUri() { return urlBuilder; } + public static Function buildSearchUrl(String query, String[] qf, String sort, int page, + int pageSize, String facet, int facetLimit, + String profile) { + return uriBuilder -> { + UriBuilder builder = + uriBuilder + .path(SEARCH_PATH) + //.queryParam(WSKEY, wskey) + .queryParam(QUERY_PARAM_QUERY, query) + .queryParam(QUERY_PARAM_PAGE, page) + .queryParam(QUERY_PARAM_PAGE_SIZE, pageSize); + if (qf != null) { + builder.queryParam(QUERY_PARAM_QF, qf); + } + if (sort != null) { + builder.queryParam(QUERY_PARAM_SORT, sort); + } + if (facet != null) { + builder.queryParam(QUERY_PARAM_FACET, facet); + builder.queryParam("facet.limit", facetLimit); + } + if (profile != null) { + builder.queryParam(QUERY_PARAM_PROFILE, profile); + } + return builder.build(); + }; + } + public String getApiKey() { return apiKey; } diff --git a/set-client/src/main/java/eu/europeana/set/client/connection/UserSetApiConnection.java b/set-client/src/main/java/eu/europeana/set/client/connection/UserSetApiConnection.java index 28881aa1..179867c6 100644 --- a/set-client/src/main/java/eu/europeana/set/client/connection/UserSetApiConnection.java +++ b/set-client/src/main/java/eu/europeana/set/client/connection/UserSetApiConnection.java @@ -44,10 +44,6 @@ public ResponseEntity createUserSet(String set, String profile) throws I String resUrl = urlBuilder.toString(); logger.trace("Ivoking create set: {} ", resUrl); - - /** - * Execute Europeana API request - */ return postURL(resUrl, set, regularUserAuthorizationValue); } @@ -69,10 +65,6 @@ public ResponseEntity getUserSet(String identifier, String profile) thro urlBuilder.append(CommonApiConstants.QUERY_PARAM_PROFILE) .append(WebUserSetFields.EQUALS_PARAMETER).append(profile); } - - /** - * Execute Europeana API request - */ return getURL(urlBuilder.toString(), regularUserAuthorizationValue); } @@ -97,10 +89,6 @@ public ResponseEntity updateUserSet(String identifier, String updateUser urlBuilder.append(CommonApiConstants.QUERY_PARAM_PROFILE) .append(WebUserSetFields.EQUALS_PARAMETER).append(profile); } - - /** - * Execute Europeana API request - */ return putURL(urlBuilder.toString(), updateUserSet, regularUserAuthorizationValue); } @@ -116,13 +104,17 @@ public ResponseEntity deleteUserSet(String identifier) throws IOExceptio StringBuilder urlBuilder = getUserSetServiceUri(); urlBuilder.append(identifier).append(WebUserSetFields.JSON_LD_REST); - - /** - * Execute Europeana API request - */ return deleteURL(urlBuilder.toString(), regularUserAuthorizationValue); } + public ResponseEntity searchUserSet(String query, String[] qf, String sort, int page, + int pageSize, String facet, int facetLimit, + String profile) throws IOException { + + StringBuilder urlBuilder = getUserSetServiceUri().append(buildSearchUrl(query, qf, sort, page, pageSize, facet, facetLimit, profile)); + System.out.println(urlBuilder.toString()); + return getURL(urlBuilder.toString(), regularUserAuthorizationValue); + } } diff --git a/set-client/src/main/java/eu/europeana/set/client/web/SearchUserSetApi.java b/set-client/src/main/java/eu/europeana/set/client/web/SearchUserSetApi.java new file mode 100644 index 00000000..41f392c0 --- /dev/null +++ b/set-client/src/main/java/eu/europeana/set/client/web/SearchUserSetApi.java @@ -0,0 +1,21 @@ +package eu.europeana.set.client.web; + +import org.springframework.http.ResponseEntity; + +public interface SearchUserSetApi { + + /** + * This methods retrieves the search results from the db + * @param query + * @param qf + * @param sort + * @param page + * @param pageSize + * @param facet + * @param facetLimit + * @param profile + * @return + */ + public ResponseEntity searchUserSet(String query, String[] qf, String sort, int page, int pageSize, + String facet, int facetLimit, String profile); +} diff --git a/set-client/src/test/java/eu/europeana/set/client/integration/web/BaseWebUserSetProtocol.java b/set-client/src/test/java/eu/europeana/set/client/integration/web/BaseWebUserSetProtocol.java index c8890fef..41c461b6 100644 --- a/set-client/src/test/java/eu/europeana/set/client/integration/web/BaseWebUserSetProtocol.java +++ b/set-client/src/test/java/eu/europeana/set/client/integration/web/BaseWebUserSetProtocol.java @@ -28,17 +28,13 @@ public class BaseWebUserSetProtocol { String START = "{"; String END = "}"; - private WebUserSetApi apiClient; + protected UserSetApiClient apiClient; @BeforeEach public void initObjects() throws SetApiClientException { apiClient = new UserSetApiClient(new ClientConfiguration()); } - public WebUserSetApi getApiClient() { - return apiClient; - } - /** * This method creates test set object * @@ -54,7 +50,7 @@ protected ResponseEntity storeTestUserSet(String resource, String profil /** * store set */ - ResponseEntity storedResponse = getApiClient().createUserSet(requestBody, profile); + ResponseEntity storedResponse = apiClient.getWebUserSetApi().createUserSet(requestBody, profile); return storedResponse; } @@ -75,14 +71,13 @@ protected void deleteUserSet(UserSet set) throws SetApiClientException { } protected void deleteUserSet(String identifier) throws SetApiClientException { - WebUserSetApi webUserSetApi = new UserSetApiClient(new ClientConfiguration()); - ResponseEntity re = webUserSetApi.deleteUserSet(identifier); + ResponseEntity re = apiClient.getWebUserSetApi().deleteUserSet(identifier); assertEquals(HttpStatus.OK, re.getStatusCode()); log.trace("User set deleted: /" + identifier); } protected ResponseEntity getUserSet(UserSet set) { - return getApiClient().getUserSet(set.getIdentifier(), null); + return apiClient.getWebUserSetApi().getUserSet(set.getIdentifier(), null); } diff --git a/set-client/src/test/java/eu/europeana/set/client/integration/web/WebUserSetProtocolExceptionsTest.java b/set-client/src/test/java/eu/europeana/set/client/integration/web/WebUserSetProtocolExceptionsTest.java index 01bdc8f0..99dbac12 100644 --- a/set-client/src/test/java/eu/europeana/set/client/integration/web/WebUserSetProtocolExceptionsTest.java +++ b/set-client/src/test/java/eu/europeana/set/client/integration/web/WebUserSetProtocolExceptionsTest.java @@ -44,7 +44,7 @@ public class WebUserSetProtocolExceptionsTest extends BaseWebUserSetProtocol { @Test public void createWebsetUserSetWithoutBody() throws IOException { - ResponseEntity response = getApiClient().createUserSet( + ResponseEntity response = apiClient.getWebUserSetApi().createUserSet( null, null); assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode()); @@ -53,7 +53,7 @@ public void createWebsetUserSetWithoutBody() throws IOException { @Test public void createWebUserSetWithCorruptedBody() { - ResponseEntity response = getApiClient().createUserSet( + ResponseEntity response = apiClient.getWebUserSetApi().createUserSet( CORRUPTED_JSON, null); assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode()); @@ -61,7 +61,7 @@ public void createWebUserSetWithCorruptedBody() { @Test public void getWebUserSetWithWrongIdentifier() { - ResponseEntity response = getApiClient().getUserSet( + ResponseEntity response = apiClient.getWebUserSetApi().getUserSet( WRONG_GENERATED_IDENTIFIER, null); assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); } @@ -70,7 +70,7 @@ public void getWebUserSetWithWrongIdentifier() { public void updateWebsetUserSetWithWrongIdentifierNumber() throws IOException { String requestBody = getJsonStringInput(USER_SET_CONTENT); - ResponseEntity response = getApiClient().updateUserSet( + ResponseEntity response = apiClient.getWebUserSetApi().updateUserSet( WRONG_GENERATED_IDENTIFIER , requestBody , null); @@ -81,7 +81,7 @@ public void updateWebsetUserSetWithWrongIdentifierNumber() throws IOException { public void updateWebUserSetWithWrongIdentifier() throws IOException { String requestBody = getJsonStringInput(USER_SET_CONTENT); - ResponseEntity response = getApiClient().updateUserSet( + ResponseEntity response = apiClient.getWebUserSetApi().updateUserSet( WRONG_GENERATED_IDENTIFIER , requestBody , null); diff --git a/set-client/src/test/java/eu/europeana/set/client/integration/web/WebUserSetProtocolTest.java b/set-client/src/test/java/eu/europeana/set/client/integration/web/WebUserSetProtocolTest.java index dd5b9977..64e237b9 100644 --- a/set-client/src/test/java/eu/europeana/set/client/integration/web/WebUserSetProtocolTest.java +++ b/set-client/src/test/java/eu/europeana/set/client/integration/web/WebUserSetProtocolTest.java @@ -24,7 +24,7 @@ public class WebUserSetProtocolTest extends BaseWebUserSetProtocol { public void createUserSet() throws IOException { String setId = createTestUserSet(USER_SET_CONTENT, null); assertNotNull(setId); - getApiClient().deleteUserSet(setId); + apiClient.getWebUserSetApi().deleteUserSet(setId); } /** @@ -38,7 +38,7 @@ public void retrieveUserSet() throws IllegalArgumentException, IOException { String testSetId = createTestUserSet(USER_SET_CONTENT, null); assertNotNull(testSetId); // get user set by ID and user identifier - response = getApiClient().getUserSet(testSetId, null); + response = apiClient.getWebUserSetApi().getUserSet(testSetId, null); validateResponse(response, HttpStatus.OK); } @@ -50,7 +50,7 @@ public void updateUserSet() throws IOException { String requestBody = getJsonStringInput(USER_SET_UPDATE_CONTENT); assertNotNull(requestBody); // update user set by identifier URL - ResponseEntity updateResponse = getApiClient().updateUserSet( + ResponseEntity updateResponse = apiClient.getWebUserSetApi().updateUserSet( testSetId, requestBody, null); validateResponse(updateResponse, HttpStatus.OK); } @@ -60,7 +60,7 @@ public void deleteUserSet() throws IOException { String testSetId = createTestUserSet(USER_SET_CONTENT,null); assertNotNull(testSetId); // delete user set by identifier URL - ResponseEntity deleteResponse = getApiClient().deleteUserSet( + ResponseEntity deleteResponse = apiClient.getWebUserSetApi().deleteUserSet( testSetId); assertEquals(HttpStatus.NO_CONTENT, deleteResponse.getStatusCode()); } diff --git a/set-definitions/src/main/java/eu/europeana/set/definitions/model/vocabulary/WebUserSetFields.java b/set-definitions/src/main/java/eu/europeana/set/definitions/model/vocabulary/WebUserSetFields.java index 4f54c40f..2d2e48ec 100644 --- a/set-definitions/src/main/java/eu/europeana/set/definitions/model/vocabulary/WebUserSetFields.java +++ b/set-definitions/src/main/java/eu/europeana/set/definitions/model/vocabulary/WebUserSetFields.java @@ -16,7 +16,8 @@ public class WebUserSetFields extends WebUserSetModelFields { public static final String EQUALS_PARAMETER = "="; public static final String JSON_LD_REST = ".jsonld"; public static final String FORMAT_JSONLD = "jsonld"; - + public static final String SEARCH_PATH = "search"; + public static final int DEFAULT_PAGE = 1; // Query Params Constants From 5724d0a0bd2d129d51ee614e1759cdf2dccfc951 Mon Sep 17 00:00:00 2001 From: srishti Date: Fri, 22 Nov 2024 12:28:52 +0100 Subject: [PATCH 03/21] EA-4015 few tweaks --- .../client/config/ClientConfiguration.java | 2 -- .../client/connection/BaseApiConnection.java | 22 ++++++++++++++++--- .../connection/UserSetApiConnection.java | 16 ++++++++++++-- .../set/client/web/SearchUserSetApi.java | 5 +++++ .../set/client/web/WebUserSetApi.java | 1 - 5 files changed, 38 insertions(+), 8 deletions(-) diff --git a/set-client/src/main/java/eu/europeana/set/client/config/ClientConfiguration.java b/set-client/src/main/java/eu/europeana/set/client/config/ClientConfiguration.java index afc18b10..8ffee8e1 100644 --- a/set-client/src/main/java/eu/europeana/set/client/config/ClientConfiguration.java +++ b/set-client/src/main/java/eu/europeana/set/client/config/ClientConfiguration.java @@ -10,9 +10,7 @@ /** * configuration for accessing remote api - * * @author GordeaS - * */ public final class ClientConfiguration { diff --git a/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java b/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java index de3bd14b..6c4c8203 100644 --- a/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java +++ b/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java @@ -12,7 +12,6 @@ import eu.europeana.set.definitions.model.vocabulary.WebUserSetFields; import org.springframework.web.util.UriBuilder; -import static eu.europeana.set.definitions.model.vocabulary.WebUserSetFields.FACETS; import static eu.europeana.set.definitions.model.vocabulary.WebUserSetFields.SEARCH_PATH; import static eu.europeana.api.commons.definitions.vocabulary.CommonApiConstants.*; @@ -27,13 +26,15 @@ public class BaseApiConnection { private HttpConnection httpConnection = new HttpConnection(); private String apiKey; private String setServiceUri; - String regularUserAuthorizationValue = null; + String regularUserAuthorizationValue; public BaseApiConnection(String setServiceUri, String apiKey, String regularUserAuthorizationValue) { this.setServiceUri = setServiceUri; this.apiKey = apiKey; this.regularUserAuthorizationValue = regularUserAuthorizationValue; } + + // getters public String getAdminApiKey() { return API_ADMIN_KEY; } @@ -47,7 +48,7 @@ public HttpConnection getHttpConnection() { * response body, response headers and status code. * @param url * @param jsonPost - * @param requestHeaderValue + * @param headerValue * @return The response body, response headers and status code. * @throws IOException */ @@ -126,6 +127,9 @@ ResponseEntity deleteURL(String url, String authorizationHeaderValue) th return getHttpConnection().deleteURL(url, authorizationHeaderValue); } + /** + * Fetches the Set api service url with "/" at the end + */ public StringBuilder getUserSetServiceUri() { StringBuilder urlBuilder = new StringBuilder(); urlBuilder.append(this.setServiceUri); @@ -135,6 +139,18 @@ public StringBuilder getUserSetServiceUri() { return urlBuilder; } + /** + * Builds search url with the given params + * @param query + * @param qf + * @param sort + * @param page + * @param pageSize + * @param facet + * @param facetLimit + * @param profile + * @return + */ public static Function buildSearchUrl(String query, String[] qf, String sort, int page, int pageSize, String facet, int facetLimit, String profile) { diff --git a/set-client/src/main/java/eu/europeana/set/client/connection/UserSetApiConnection.java b/set-client/src/main/java/eu/europeana/set/client/connection/UserSetApiConnection.java index 179867c6..bc544428 100644 --- a/set-client/src/main/java/eu/europeana/set/client/connection/UserSetApiConnection.java +++ b/set-client/src/main/java/eu/europeana/set/client/connection/UserSetApiConnection.java @@ -107,6 +107,20 @@ public ResponseEntity deleteUserSet(String identifier) throws IOExceptio return deleteURL(urlBuilder.toString(), regularUserAuthorizationValue); } + /** + * This method searches usersets for the given queries and params + * Example : /set/search?query=visibility:published&pageSize=1000 + * @param query + * @param qf + * @param sort + * @param page + * @param pageSize + * @param facet + * @param facetLimit + * @param profile + * @return + * @throws IOException + */ public ResponseEntity searchUserSet(String query, String[] qf, String sort, int page, int pageSize, String facet, int facetLimit, String profile) throws IOException { @@ -115,6 +129,4 @@ public ResponseEntity searchUserSet(String query, String[] qf, String so System.out.println(urlBuilder.toString()); return getURL(urlBuilder.toString(), regularUserAuthorizationValue); } - - } diff --git a/set-client/src/main/java/eu/europeana/set/client/web/SearchUserSetApi.java b/set-client/src/main/java/eu/europeana/set/client/web/SearchUserSetApi.java index 41f392c0..7209d1d3 100644 --- a/set-client/src/main/java/eu/europeana/set/client/web/SearchUserSetApi.java +++ b/set-client/src/main/java/eu/europeana/set/client/web/SearchUserSetApi.java @@ -2,6 +2,11 @@ import org.springframework.http.ResponseEntity; +/** + * Search Client API interface + * @author Srishti singh + * @since 20 Nov 2024 + */ public interface SearchUserSetApi { /** diff --git a/set-client/src/main/java/eu/europeana/set/client/web/WebUserSetApi.java b/set-client/src/main/java/eu/europeana/set/client/web/WebUserSetApi.java index f6cb275b..d8ca8b31 100644 --- a/set-client/src/main/java/eu/europeana/set/client/web/WebUserSetApi.java +++ b/set-client/src/main/java/eu/europeana/set/client/web/WebUserSetApi.java @@ -5,7 +5,6 @@ /** * Client API interface * @author GordeaS - * */ public interface WebUserSetApi { From 7fa4917537ba587e1be83cc422cc5574302573c3 Mon Sep 17 00:00:00 2001 From: srishti Date: Fri, 22 Nov 2024 13:31:30 +0100 Subject: [PATCH 04/21] EA-4015 fixed the url issues --- .../client/connection/BaseApiConnection.java | 36 +++++++++++++------ .../connection/UserSetApiConnection.java | 11 ++---- set-client/src/main/resources/.gitignore | 4 +-- 3 files changed, 29 insertions(+), 22 deletions(-) diff --git a/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java b/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java index 6c4c8203..cec71c57 100644 --- a/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java +++ b/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java @@ -4,6 +4,7 @@ import java.net.URI; import java.util.function.Function; +import eu.europeana.api.commons.definitions.vocabulary.CommonApiConstants; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.http.ResponseEntity; @@ -11,6 +12,7 @@ import eu.europeana.set.common.http.HttpConnection; import eu.europeana.set.definitions.model.vocabulary.WebUserSetFields; import org.springframework.web.util.UriBuilder; +import org.springframework.web.util.UriComponentsBuilder; import static eu.europeana.set.definitions.model.vocabulary.WebUserSetFields.SEARCH_PATH; import static eu.europeana.api.commons.definitions.vocabulary.CommonApiConstants.*; @@ -139,6 +141,21 @@ public StringBuilder getUserSetServiceUri() { return urlBuilder; } + /** + * Build get user Set get Url + * @param path + * @param profile + * @return + */ + public static URI buildGetUrls(String path, String profile, String apikey) { + UriBuilder builder = UriComponentsBuilder.newInstance().path(path) + .queryParam(CommonApiConstants.PARAM_WSKEY, apikey); // by default pass apikey. If in case outh token is null apikey can be used to authenticate; + if (profile != null) { + builder.queryParam(QUERY_PARAM_PROFILE, profile); + } + return builder.build(); + } + /** * Builds search url with the given params * @param query @@ -151,17 +168,15 @@ public StringBuilder getUserSetServiceUri() { * @param profile * @return */ - public static Function buildSearchUrl(String query, String[] qf, String sort, int page, + public static URI buildSearchUrl(String query, String[] qf, String sort, int page, int pageSize, String facet, int facetLimit, - String profile) { - return uriBuilder -> { - UriBuilder builder = - uriBuilder - .path(SEARCH_PATH) - //.queryParam(WSKEY, wskey) - .queryParam(QUERY_PARAM_QUERY, query) - .queryParam(QUERY_PARAM_PAGE, page) - .queryParam(QUERY_PARAM_PAGE_SIZE, pageSize); + String profile, String apikey) { + UriBuilder builder = UriComponentsBuilder.newInstance().path(SEARCH_PATH) + .queryParam(QUERY_PARAM_QUERY, query) + .queryParam(QUERY_PARAM_PAGE, page) + .queryParam(QUERY_PARAM_PAGE_SIZE, pageSize) + .queryParam(CommonApiConstants.PARAM_WSKEY, apikey); // by default pass apikey. If in case outh token is null apikey can be used to authenticate + if (qf != null) { builder.queryParam(QUERY_PARAM_QF, qf); } @@ -176,7 +191,6 @@ public static Function buildSearchUrl(String query, String[] qf builder.queryParam(QUERY_PARAM_PROFILE, profile); } return builder.build(); - }; } public String getApiKey() { diff --git a/set-client/src/main/java/eu/europeana/set/client/connection/UserSetApiConnection.java b/set-client/src/main/java/eu/europeana/set/client/connection/UserSetApiConnection.java index bc544428..8416f594 100644 --- a/set-client/src/main/java/eu/europeana/set/client/connection/UserSetApiConnection.java +++ b/set-client/src/main/java/eu/europeana/set/client/connection/UserSetApiConnection.java @@ -57,14 +57,7 @@ public ResponseEntity createUserSet(String set, String profile) throws I * @throws IOException */ public ResponseEntity getUserSet(String identifier, String profile) throws IOException { - - StringBuilder urlBuilder = getUserSetServiceUri(); - urlBuilder.append(identifier).append(WebUserSetFields.JSON_LD_REST); - if (StringUtils.isNotEmpty(profile)) { - urlBuilder.append(WebUserSetFields.PAR_CHAR); - urlBuilder.append(CommonApiConstants.QUERY_PARAM_PROFILE) - .append(WebUserSetFields.EQUALS_PARAMETER).append(profile); - } + StringBuilder urlBuilder = getUserSetServiceUri().append(buildGetUrls(identifier + WebUserSetFields.JSON_LD_REST, profile, getApiKey())); return getURL(urlBuilder.toString(), regularUserAuthorizationValue); } @@ -125,7 +118,7 @@ public ResponseEntity searchUserSet(String query, String[] qf, String so int pageSize, String facet, int facetLimit, String profile) throws IOException { - StringBuilder urlBuilder = getUserSetServiceUri().append(buildSearchUrl(query, qf, sort, page, pageSize, facet, facetLimit, profile)); + StringBuilder urlBuilder = getUserSetServiceUri().append(buildSearchUrl(query, qf, sort, page, pageSize, facet, facetLimit, profile, getApiKey())); System.out.println(urlBuilder.toString()); return getURL(urlBuilder.toString(), regularUserAuthorizationValue); } diff --git a/set-client/src/main/resources/.gitignore b/set-client/src/main/resources/.gitignore index d05ae35b..6597be1a 100644 --- a/set-client/src/main/resources/.gitignore +++ b/set-client/src/main/resources/.gitignore @@ -1,4 +1,4 @@ -set.client.properties +set-client.properties **user.properties.template /log4j.xml -/log4j2.xml +/log4j2.xml \ No newline at end of file From 915dfd7c859f1356810f4ad8dc1513288210ac90 Mon Sep 17 00:00:00 2001 From: srishti Date: Mon, 25 Nov 2024 09:57:17 +0100 Subject: [PATCH 05/21] EA-4015 update class to private --- .../src/main/java/eu/europeana/set/client/UserSetApiClient.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/set-client/src/main/java/eu/europeana/set/client/UserSetApiClient.java b/set-client/src/main/java/eu/europeana/set/client/UserSetApiClient.java index 884ce4e6..20e944a6 100644 --- a/set-client/src/main/java/eu/europeana/set/client/UserSetApiClient.java +++ b/set-client/src/main/java/eu/europeana/set/client/UserSetApiClient.java @@ -92,7 +92,7 @@ public ResponseEntity updateUserSet(String identifier, String set, Strin } } - public class SearchUserSetClient implements SearchUserSetApi { + private class SearchUserSetClient implements SearchUserSetApi { @Override public ResponseEntity searchUserSet(String query, String[] qf, From b7f4ba703031cdfc22eb83437ec9df5cfd8a47d6 Mon Sep 17 00:00:00 2001 From: srishti Date: Mon, 25 Nov 2024 10:32:47 +0100 Subject: [PATCH 06/21] EA-4015 sonarq --- .../main/java/eu/europeana/set/client/BaseUserSetApi.java | 1 - .../java/eu/europeana/set/client/UserSetApiClient.java | 1 - .../europeana/set/client/config/ClientConfiguration.java | 2 -- .../set/client/connection/BaseApiConnection.java | 1 - .../set/client/connection/UserSetApiConnection.java | 7 ------- .../eu/europeana/set/client/web/SearchUserSetApi.java | 2 +- .../java/eu/europeana/set/client/web/WebUserSetApi.java | 8 ++++---- 7 files changed, 5 insertions(+), 17 deletions(-) diff --git a/set-client/src/main/java/eu/europeana/set/client/BaseUserSetApi.java b/set-client/src/main/java/eu/europeana/set/client/BaseUserSetApi.java index 25b21c4b..4d26ee00 100644 --- a/set-client/src/main/java/eu/europeana/set/client/BaseUserSetApi.java +++ b/set-client/src/main/java/eu/europeana/set/client/BaseUserSetApi.java @@ -5,7 +5,6 @@ import eu.europeana.set.client.exception.SetApiClientException; import eu.europeana.set.client.exception.TechnicalRuntimeException; import eu.europeana.set.common.http.HttpConnection; -import eu.europeana.set.definitions.model.vocabulary.WebUserSetFields; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; import org.springframework.http.HttpStatus; diff --git a/set-client/src/main/java/eu/europeana/set/client/UserSetApiClient.java b/set-client/src/main/java/eu/europeana/set/client/UserSetApiClient.java index 20e944a6..226666cb 100644 --- a/set-client/src/main/java/eu/europeana/set/client/UserSetApiClient.java +++ b/set-client/src/main/java/eu/europeana/set/client/UserSetApiClient.java @@ -8,7 +8,6 @@ import eu.europeana.set.client.web.WebUserSetApi; import org.springframework.http.ResponseEntity; -import eu.europeana.set.client.BaseUserSetApi; import eu.europeana.set.client.exception.TechnicalRuntimeException; /** diff --git a/set-client/src/main/java/eu/europeana/set/client/config/ClientConfiguration.java b/set-client/src/main/java/eu/europeana/set/client/config/ClientConfiguration.java index 8ffee8e1..16cd302e 100644 --- a/set-client/src/main/java/eu/europeana/set/client/config/ClientConfiguration.java +++ b/set-client/src/main/java/eu/europeana/set/client/config/ClientConfiguration.java @@ -1,10 +1,8 @@ package eu.europeana.set.client.config; import java.io.IOException; -import java.io.InputStream; import java.util.Properties; -import eu.europeana.set.client.exception.TechnicalRuntimeException; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; diff --git a/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java b/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java index cec71c57..a5c84acd 100644 --- a/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java +++ b/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java @@ -2,7 +2,6 @@ import java.io.IOException; import java.net.URI; -import java.util.function.Function; import eu.europeana.api.commons.definitions.vocabulary.CommonApiConstants; import org.apache.logging.log4j.LogManager; diff --git a/set-client/src/main/java/eu/europeana/set/client/connection/UserSetApiConnection.java b/set-client/src/main/java/eu/europeana/set/client/connection/UserSetApiConnection.java index 8416f594..ba4819e8 100644 --- a/set-client/src/main/java/eu/europeana/set/client/connection/UserSetApiConnection.java +++ b/set-client/src/main/java/eu/europeana/set/client/connection/UserSetApiConnection.java @@ -2,15 +2,8 @@ import java.io.IOException; import org.apache.commons.lang3.StringUtils; -import org.codehaus.jettison.json.JSONException; -import org.codehaus.jettison.json.JSONObject; -import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; - import eu.europeana.api.commons.definitions.vocabulary.CommonApiConstants; -import eu.europeana.set.client.config.ClientConfiguration; -import eu.europeana.set.client.exception.TechnicalRuntimeException; -import eu.europeana.set.common.http.HttpConnection; import eu.europeana.set.definitions.model.vocabulary.WebUserSetFields; /** diff --git a/set-client/src/main/java/eu/europeana/set/client/web/SearchUserSetApi.java b/set-client/src/main/java/eu/europeana/set/client/web/SearchUserSetApi.java index 7209d1d3..265408f9 100644 --- a/set-client/src/main/java/eu/europeana/set/client/web/SearchUserSetApi.java +++ b/set-client/src/main/java/eu/europeana/set/client/web/SearchUserSetApi.java @@ -21,6 +21,6 @@ public interface SearchUserSetApi { * @param profile * @return */ - public ResponseEntity searchUserSet(String query, String[] qf, String sort, int page, int pageSize, + ResponseEntity searchUserSet(String query, String[] qf, String sort, int page, int pageSize, String facet, int facetLimit, String profile); } diff --git a/set-client/src/main/java/eu/europeana/set/client/web/WebUserSetApi.java b/set-client/src/main/java/eu/europeana/set/client/web/WebUserSetApi.java index d8ca8b31..63919f57 100644 --- a/set-client/src/main/java/eu/europeana/set/client/web/WebUserSetApi.java +++ b/set-client/src/main/java/eu/europeana/set/client/web/WebUserSetApi.java @@ -14,7 +14,7 @@ public interface WebUserSetApi { * @param profile * @return response entity containing body, headers and status code. */ - public ResponseEntity createUserSet( + ResponseEntity createUserSet( String requestBody, String profile); /** @@ -24,7 +24,7 @@ public ResponseEntity createUserSet( * @param profile * @return response entity that contains response body, headers and status code. */ - public ResponseEntity getUserSet( + ResponseEntity getUserSet( String identifier, String profile); /** @@ -32,7 +32,7 @@ public ResponseEntity getUserSet( * @param identifier * @return response entity containing headers and status code. */ - public ResponseEntity deleteUserSet( + ResponseEntity deleteUserSet( String identifier); /** @@ -42,7 +42,7 @@ public ResponseEntity deleteUserSet( * @param profile * @return response entity containing body, headers and status code. */ - public ResponseEntity updateUserSet( + ResponseEntity updateUserSet( String identifier, String requestBody, String profile); } \ No newline at end of file From 9bfdd58b26e3895c9bde9a83c06ff6e1d6d1cf0b Mon Sep 17 00:00:00 2001 From: srishti Date: Mon, 25 Nov 2024 14:44:27 +0100 Subject: [PATCH 07/21] EA-4015 remove apikey param --- .../set/client/connection/BaseApiConnection.java | 10 ++++------ .../set/client/connection/UserSetApiConnection.java | 5 ++--- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java b/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java index a5c84acd..30a8093e 100644 --- a/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java +++ b/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java @@ -146,9 +146,8 @@ public StringBuilder getUserSetServiceUri() { * @param profile * @return */ - public static URI buildGetUrls(String path, String profile, String apikey) { - UriBuilder builder = UriComponentsBuilder.newInstance().path(path) - .queryParam(CommonApiConstants.PARAM_WSKEY, apikey); // by default pass apikey. If in case outh token is null apikey can be used to authenticate; + public static URI buildGetUrls(String path, String profile) { + UriBuilder builder = UriComponentsBuilder.newInstance().path(path); if (profile != null) { builder.queryParam(QUERY_PARAM_PROFILE, profile); } @@ -169,12 +168,11 @@ public static URI buildGetUrls(String path, String profile, String apikey) { */ public static URI buildSearchUrl(String query, String[] qf, String sort, int page, int pageSize, String facet, int facetLimit, - String profile, String apikey) { + String profile) { UriBuilder builder = UriComponentsBuilder.newInstance().path(SEARCH_PATH) .queryParam(QUERY_PARAM_QUERY, query) .queryParam(QUERY_PARAM_PAGE, page) - .queryParam(QUERY_PARAM_PAGE_SIZE, pageSize) - .queryParam(CommonApiConstants.PARAM_WSKEY, apikey); // by default pass apikey. If in case outh token is null apikey can be used to authenticate + .queryParam(QUERY_PARAM_PAGE_SIZE, pageSize); if (qf != null) { builder.queryParam(QUERY_PARAM_QF, qf); diff --git a/set-client/src/main/java/eu/europeana/set/client/connection/UserSetApiConnection.java b/set-client/src/main/java/eu/europeana/set/client/connection/UserSetApiConnection.java index ba4819e8..e1555fc6 100644 --- a/set-client/src/main/java/eu/europeana/set/client/connection/UserSetApiConnection.java +++ b/set-client/src/main/java/eu/europeana/set/client/connection/UserSetApiConnection.java @@ -50,7 +50,7 @@ public ResponseEntity createUserSet(String set, String profile) throws I * @throws IOException */ public ResponseEntity getUserSet(String identifier, String profile) throws IOException { - StringBuilder urlBuilder = getUserSetServiceUri().append(buildGetUrls(identifier + WebUserSetFields.JSON_LD_REST, profile, getApiKey())); + StringBuilder urlBuilder = getUserSetServiceUri().append(buildGetUrls(identifier + WebUserSetFields.JSON_LD_REST, profile)); return getURL(urlBuilder.toString(), regularUserAuthorizationValue); } @@ -111,8 +111,7 @@ public ResponseEntity searchUserSet(String query, String[] qf, String so int pageSize, String facet, int facetLimit, String profile) throws IOException { - StringBuilder urlBuilder = getUserSetServiceUri().append(buildSearchUrl(query, qf, sort, page, pageSize, facet, facetLimit, profile, getApiKey())); - System.out.println(urlBuilder.toString()); + StringBuilder urlBuilder = getUserSetServiceUri().append(buildSearchUrl(query, qf, sort, page, pageSize, facet, facetLimit, profile)); return getURL(urlBuilder.toString(), regularUserAuthorizationValue); } } From 6eeefd0fe5b3ee798be422464caa4b19de763b9b Mon Sep 17 00:00:00 2001 From: srishti Date: Mon, 25 Nov 2024 14:48:17 +0100 Subject: [PATCH 08/21] EA-4015 make properties public to instantiate the client with other api's propety file --- .../europeana/set/client/config/ClientConfiguration.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/set-client/src/main/java/eu/europeana/set/client/config/ClientConfiguration.java b/set-client/src/main/java/eu/europeana/set/client/config/ClientConfiguration.java index 16cd302e..17dca6b6 100644 --- a/set-client/src/main/java/eu/europeana/set/client/config/ClientConfiguration.java +++ b/set-client/src/main/java/eu/europeana/set/client/config/ClientConfiguration.java @@ -16,10 +16,10 @@ public final class ClientConfiguration { private static final Logger LOGGER = LogManager.getLogger(ClientConfiguration.class); protected static final String SET_CLIENT_PROPERTIES_FILE = "/set-client.user.properties"; - protected static final String PROP_SET_API_KEY = "set.api.key"; - protected static final String PROP_SET_SERVICE_URI = "set.service.uri"; - protected static final String PROP_OAUTH_SERVICE_URI = "oauth.service.uri"; - protected static final String PROP_OAUTH_REQUEST_PARAMS = "oauth.token.request.params"; + public static final String PROP_SET_API_KEY = "set.api.key"; + public static final String PROP_SET_SERVICE_URI = "set.service.uri"; + public static final String PROP_OAUTH_SERVICE_URI = "oauth.service.uri"; + public static final String PROP_OAUTH_REQUEST_PARAMS = "oauth.token.request.params"; private Properties properties; From 291f697674f1fc9bdb677b955aa47909efe3e688 Mon Sep 17 00:00:00 2001 From: srishti Date: Thu, 28 Nov 2024 11:23:54 +0100 Subject: [PATCH 09/21] EA-4015 Update set client to use http5, remove all spring dependency from client, update other modules to adapt tghe new HTTpConnection class, added annotation for deserialising the response --- pom.xml | 4 +- set-client/pom.xml | 27 +- .../europeana/set/client/BaseUserSetApi.java | 31 +- .../set/client/UserSetApiClient.java | 68 +-- .../client/connection/BaseApiConnection.java | 423 ++++++++++-------- .../connection/UserSetApiConnection.java | 36 +- .../exception/SetApiClientException.java | 43 +- .../exception/TechnicalRuntimeException.java | 19 - .../result/AbstractUserSetApiResponse.java | 13 +- .../set/client/web/SearchUserSetApi.java | 8 +- .../set/client/web/WebUserSetApi.java | 17 +- .../web/BaseAPIConnectionTest.java | 4 - .../web/BaseWebUserSetProtocol.java | 21 +- .../web/WebUserSetProtocolExceptionsTest.java | 72 ++- .../web/WebUserSetProtocolTest.java | 72 +-- set-common/pom.xml | 18 +- .../set/common/http/HttpConnection.java | 285 +++--------- set-definitions/pom.xml | 14 + .../definitions/json/AgentDeserializer.java | 36 ++ .../definitions/model/BaseWebResource.java | 3 + .../model/agent/impl/BaseAgent.java | 2 + .../definitions/model/impl/BaseUserSet.java | 37 +- .../connection/http/EuropeanaOauthClient.java | 16 +- .../set/search/service/SearchApiClient.java | 1 - .../service/impl/SearchApiClientImpl.java | 13 +- 25 files changed, 592 insertions(+), 691 deletions(-) delete mode 100644 set-client/src/main/java/eu/europeana/set/client/exception/TechnicalRuntimeException.java create mode 100644 set-definitions/src/main/java/eu/europeana/set/definitions/json/AgentDeserializer.java diff --git a/pom.xml b/pom.xml index 55a07976..48cdc54e 100644 --- a/pom.xml +++ b/pom.xml @@ -56,13 +56,13 @@ 1.6.14 - 3.1 + 5.4.1 1.3.2 1.3 2.3.1 5.7.2 2.8.0 - + 2.18.1 UTF-8 UTF-8 diff --git a/set-client/pom.xml b/set-client/pom.xml index cc1e9c97..0d4a7008 100644 --- a/set-client/pom.xml +++ b/set-client/pom.xml @@ -36,15 +36,30 @@ jettison ${version.jettison} - + + + + jakarta.ws.rs + jakarta.ws.rs-api + 4.0.0 + + + - org.springframework - spring-webmvc - + org.glassfish.jersey.core + jersey-common + 3.1.2 + + + + + + + + org.junit.jupiter diff --git a/set-client/src/main/java/eu/europeana/set/client/BaseUserSetApi.java b/set-client/src/main/java/eu/europeana/set/client/BaseUserSetApi.java index 4d26ee00..cba00c08 100644 --- a/set-client/src/main/java/eu/europeana/set/client/BaseUserSetApi.java +++ b/set-client/src/main/java/eu/europeana/set/client/BaseUserSetApi.java @@ -3,12 +3,13 @@ import eu.europeana.set.client.config.ClientConfiguration; import eu.europeana.set.client.connection.UserSetApiConnection; import eu.europeana.set.client.exception.SetApiClientException; -import eu.europeana.set.client.exception.TechnicalRuntimeException; import eu.europeana.set.common.http.HttpConnection; +import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; +import org.apache.hc.core5.http.HttpStatus; +import org.apache.hc.core5.http.ParseException; +import org.apache.hc.core5.http.io.entity.EntityUtils; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; import java.io.IOException; @@ -25,8 +26,8 @@ public class BaseUserSetApi { protected BaseUserSetApi(ClientConfiguration configuration) throws SetApiClientException { this.configuration = configuration; - if (this.configuration.getServiceUri() == null && this.configuration.getApiKey() == null) { - throw new SetApiClientException(" Set Api Endpoint not provide !!!"); + if (this.configuration.getServiceUri() == null) { + throw new SetApiClientException(" Set Api Endpoint not provided !!!"); } if (this.configuration.getOauthServiceUri() == null || this.configuration.getOauthRequestParams() == null) { @@ -43,27 +44,27 @@ public BaseUserSetApi() throws SetApiClientException { this(new ClientConfiguration()); } - private String getOauthToken(String oauthServiceUri, String oauthRequestParams ) { + private String getOauthToken(String oauthServiceUri, String oauthRequestParams ) throws SetApiClientException{ try { String ACCESS_TOKEN = "access_token"; HttpConnection connection = new HttpConnection(); - ResponseEntity response; - response = connection.post(oauthServiceUri, oauthRequestParams, "application/x-www-form-urlencoded"); - if (HttpStatus.OK == response.getStatusCode()) { - String body = response.getBody(); + CloseableHttpResponse response = connection.post(oauthServiceUri, oauthRequestParams, "application/x-www-form-urlencoded", null); + String body = EntityUtils.toString(response.getEntity()); + + System.out.println(body); + if (HttpStatus.SC_OK == response.getCode()) { JSONObject json = new JSONObject(body); if (json.has(ACCESS_TOKEN)) { return "Bearer " + json.getString(ACCESS_TOKEN); } else { - throw new TechnicalRuntimeException( - "Cannot extract authentication token from reponse:" + body); + throw new SetApiClientException("Cannot extract authentication token from reponse:" + body); } } else { - throw new TechnicalRuntimeException("Error occured when calling oath service! " + response); + throw new SetApiClientException("Error occured when calling oath service! " + response); } - } catch (IOException | JSONException e) { - throw new TechnicalRuntimeException("Cannot retrieve authentication token!", e); + } catch (IOException | JSONException | ParseException e) { + throw new SetApiClientException("Cannot retrieve authentication token!", 0 , e); } } diff --git a/set-client/src/main/java/eu/europeana/set/client/UserSetApiClient.java b/set-client/src/main/java/eu/europeana/set/client/UserSetApiClient.java index 226666cb..84181955 100644 --- a/set-client/src/main/java/eu/europeana/set/client/UserSetApiClient.java +++ b/set-client/src/main/java/eu/europeana/set/client/UserSetApiClient.java @@ -1,14 +1,11 @@ package eu.europeana.set.client; -import java.io.IOException; - +import eu.europeana.api.commons.definitions.search.result.impl.ResultsPageImpl; import eu.europeana.set.client.config.ClientConfiguration; import eu.europeana.set.client.exception.SetApiClientException; import eu.europeana.set.client.web.SearchUserSetApi; import eu.europeana.set.client.web.WebUserSetApi; -import org.springframework.http.ResponseEntity; - -import eu.europeana.set.client.exception.TechnicalRuntimeException; +import eu.europeana.set.definitions.model.UserSet; /** * Implementation of client api @@ -40,70 +37,33 @@ public SearchUserSetApi getSearchUserSetApi() { */ private class WebUserSetClient implements WebUserSetApi { @Override - public ResponseEntity createUserSet(String set, String profile) { - ResponseEntity res; - try { - res = getApiConnection().createUserSet(set, profile); - } catch (IOException e) { - throw new TechnicalRuntimeException( - "Exception occured when invoking the UserSetJsonApi createUserSet method", e); - } - return res; + public UserSet createUserSet(String set, String profile) throws SetApiClientException { + return getApiConnection().createUserSet(set, profile); + } @Override - public ResponseEntity deleteUserSet(String identifier) { - ResponseEntity res; - try { - res = getApiConnection().deleteUserSet(identifier); - } catch (IOException e) { - throw new TechnicalRuntimeException( - "Exception occured when invoking the UserSetJsonApi deleteUserSet method", e); - } - - return res; + public String deleteUserSet(String identifier) throws SetApiClientException { + return getApiConnection().deleteUserSet(identifier); } @Override - public ResponseEntity getUserSet(String identifier, String profile) { - ResponseEntity res; - try { - res = getApiConnection().getUserSet(identifier, profile); - } catch (IOException e) { - throw new TechnicalRuntimeException("Exception occured when invoking the UserSetJsonApi getUserSet method", - e); - } - - return res; + public UserSet getUserSet(String identifier, String profile) throws SetApiClientException { + return getApiConnection().getUserSet(identifier, profile); } @Override - public ResponseEntity updateUserSet(String identifier, String set, String profile) { - ResponseEntity res; - try { - res = getApiConnection().updateUserSet(identifier, set, profile); - } catch (IOException e) { - throw new TechnicalRuntimeException( - "Exception occured when invoking the UserSetJsonApi updateUserSet method", e); - } - - return res; + public UserSet updateUserSet(String identifier, String set, String profile) throws SetApiClientException { + return getApiConnection().updateUserSet(identifier, set, profile); } } private class SearchUserSetClient implements SearchUserSetApi { @Override - public ResponseEntity searchUserSet(String query, String[] qf, - String sort, int page, int pageSize, String facet, int facetLimit, String profile) { - ResponseEntity res; - try { - res = getApiConnection().searchUserSet(query, qf, sort, page, pageSize, facet, facetLimit, profile); - } catch (IOException e) { - throw new TechnicalRuntimeException( - "Exception occured when invoking the UserSetSearch API searchUserSet method", e); - } - return res; + public ResultsPageImpl searchUserSet(String query, String[] qf, + String sort, int page, int pageSize, String facet, int facetLimit, String profile) throws SetApiClientException{ + return getApiConnection().searchUserSet(query, qf, sort, page, pageSize, facet, facetLimit, profile); } } diff --git a/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java b/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java index 30a8093e..c5e0b420 100644 --- a/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java +++ b/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java @@ -1,200 +1,245 @@ package eu.europeana.set.client.connection; -import java.io.IOException; -import java.net.URI; - -import eu.europeana.api.commons.definitions.vocabulary.CommonApiConstants; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import eu.europeana.api.commons.definitions.search.result.impl.ResultsPageImpl; +import eu.europeana.set.client.exception.SetApiClientException; +import eu.europeana.set.client.model.result.AbstractUserSetApiResponse; +import eu.europeana.set.common.http.HttpConnection; +import eu.europeana.set.definitions.model.UserSet; +import eu.europeana.set.definitions.model.impl.BaseUserSet; +import eu.europeana.set.definitions.model.vocabulary.WebUserSetFields; +import jakarta.ws.rs.core.UriBuilder; +import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; +import org.apache.hc.core5.http.HttpStatus; +import org.apache.hc.core5.http.ParseException; +import org.apache.hc.core5.http.io.entity.EntityUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.springframework.http.ResponseEntity; -import eu.europeana.set.common.http.HttpConnection; -import eu.europeana.set.definitions.model.vocabulary.WebUserSetFields; -import org.springframework.web.util.UriBuilder; -import org.springframework.web.util.UriComponentsBuilder; +import java.io.IOException; +import java.net.URI; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; -import static eu.europeana.set.definitions.model.vocabulary.WebUserSetFields.SEARCH_PATH; import static eu.europeana.api.commons.definitions.vocabulary.CommonApiConstants.*; +import static eu.europeana.set.definitions.model.vocabulary.WebUserSetFields.SEARCH_PATH; public class BaseApiConnection { - Logger logger = LogManager.getLogger(getClass().getName()); - - private static final String URL_RESPONSE = " . Returns body, headers and status code."; - private static final String DELETE_URL_RESPONSE = ". Returns headers and status code."; - private static final String API_ADMIN_KEY = "apiadmin"; - - private HttpConnection httpConnection = new HttpConnection(); - private String apiKey; - private String setServiceUri; - String regularUserAuthorizationValue; - - public BaseApiConnection(String setServiceUri, String apiKey, String regularUserAuthorizationValue) { - this.setServiceUri = setServiceUri; - this.apiKey = apiKey; - this.regularUserAuthorizationValue = regularUserAuthorizationValue; - } - - // getters - public String getAdminApiKey() { - return API_ADMIN_KEY; - } - - public HttpConnection getHttpConnection() { - return httpConnection; - } - - /** - * This method makes POST request for given URL, header and JSON body parameter that returns - * response body, response headers and status code. - * @param url - * @param jsonPost - * @param headerValue - * @return The response body, response headers and status code. - * @throws IOException - */ - ResponseEntity postURL(String url, String jsonPost, String headerValue) throws IOException { - logger.trace("Call to UserSet API (POST) with body: {}. Returns body, headers and status code.", url); - return getHttpConnection().postURL(url, jsonPost, headerValue); - } - - /** - * This method makes PUT request for given URL and JSON body parameter that returns - * response body, response headers and status code. - * @param url - * @param jsonPut - * @param authorizationHeaderValue - * @return The response body, response headers and status code. - * @throws IOException - */ - ResponseEntity putURL(String url, String jsonPut, String authorizationHeaderValue) throws IOException { - logger.trace("Call to UserSet API (PUT) with body: {}. Returns body, headers and status code.", url); - - ResponseEntity response = getHttpConnection().putURL(url, jsonPut, authorizationHeaderValue); - - response.getStatusCode(); - - return response; - } - - /** - * This method makes GET request for given URL and returns - * response body, response headers and status code. - * @param url - * @return The response body, response headers and status code. - * @throws IOException - */ - public ResponseEntity getURL(String url) throws IOException { - logger.trace("Call to UserSet API (GET): {} {}", url, URL_RESPONSE); - return getHttpConnection().getURL(url); - } - - /** - * This method makes GET request for given URL and returns - * response body, response headers and status code. - * @param url - * @param authorizationHeaderValue - * @return The response body, response headers and status code. - * @throws IOException - */ - public ResponseEntity getURL(String url, String authorizationHeaderValue) throws IOException { - logger.trace("Call to UserSet API (GET): {}. Returns body, headers and status code.", url); - return getHttpConnection().getURL(url, authorizationHeaderValue); - } - - - /** - * This method makes DELETE request for given URL that returns - * response headers and status code. - * @param url - * @return The response headers and status code. - * @throws IOException - */ - ResponseEntity deleteURL(String url) throws IOException { - logger.trace("Call to UserSet API (DELETE): {} {} ", url, DELETE_URL_RESPONSE); - return getHttpConnection().deleteURL(url); - } - - /** - * This method makes DELETE request for given URL that returns - * response headers and status code. - * @param url - * @param authorizationHeaderValue - * @return The response headers and status code. - * @throws IOException - */ - ResponseEntity deleteURL(String url, String authorizationHeaderValue) throws IOException { - logger.trace("Call to UserSet API (DELETE): {}. Returns headers and status code.", url); - return getHttpConnection().deleteURL(url, authorizationHeaderValue); - } - - /** - * Fetches the Set api service url with "/" at the end - */ - public StringBuilder getUserSetServiceUri() { - StringBuilder urlBuilder = new StringBuilder(); - urlBuilder.append(this.setServiceUri); - if(!this.setServiceUri.endsWith(WebUserSetFields.SLASH)) - urlBuilder.append(WebUserSetFields.SLASH); - - return urlBuilder; - } - - /** - * Build get user Set get Url - * @param path - * @param profile - * @return - */ - public static URI buildGetUrls(String path, String profile) { - UriBuilder builder = UriComponentsBuilder.newInstance().path(path); - if (profile != null) { - builder.queryParam(QUERY_PARAM_PROFILE, profile); - } - return builder.build(); - } - - /** - * Builds search url with the given params - * @param query - * @param qf - * @param sort - * @param page - * @param pageSize - * @param facet - * @param facetLimit - * @param profile - * @return - */ - public static URI buildSearchUrl(String query, String[] qf, String sort, int page, - int pageSize, String facet, int facetLimit, - String profile) { - UriBuilder builder = UriComponentsBuilder.newInstance().path(SEARCH_PATH) - .queryParam(QUERY_PARAM_QUERY, query) - .queryParam(QUERY_PARAM_PAGE, page) - .queryParam(QUERY_PARAM_PAGE_SIZE, pageSize); - - if (qf != null) { - builder.queryParam(QUERY_PARAM_QF, qf); - } - if (sort != null) { - builder.queryParam(QUERY_PARAM_SORT, sort); - } - if (facet != null) { - builder.queryParam(QUERY_PARAM_FACET, facet); - builder.queryParam("facet.limit", facetLimit); - } - if (profile != null) { - builder.queryParam(QUERY_PARAM_PROFILE, profile); - } - return builder.build(); - } - - public String getApiKey() { - return apiKey; - } - - public String getSetServiceUri() { - return setServiceUri; - } + Logger LOGGER = LogManager.getLogger(getClass().getName()); + + private static final String DELETE_URL_RESPONSE = ". Returns status code."; + private static final String ERROR_MESSAGE = "Set API Client call failed - "; + + private HttpConnection httpConnection = new HttpConnection(); + private String apiKey; + private String setServiceUri; + String regularUserAuthorizationValue; + private final ObjectMapper mapper = new ObjectMapper(); + + public BaseApiConnection(String setServiceUri, String apiKey, String regularUserAuthorizationValue) { + this.setServiceUri = setServiceUri; + this.apiKey = apiKey; + this.regularUserAuthorizationValue = regularUserAuthorizationValue; + } + + public HttpConnection getHttpConnection() { + return httpConnection; + } + + + /** + * Fetches the get user set response (GET request) + * + * @param url + * @param authorizationHeaderValue + * @return + * @throws SetApiClientException + */ + protected UserSet getUserSetResponse(String url, String authorizationHeaderValue) throws SetApiClientException { + try { + LOGGER.trace("Call to Get UserSet API (GET) : {}.", url); + return parseSetApiResponse(getHttpConnection().get(url, null, authorizationHeaderValue), new ArrayList<>(Arrays.asList(HttpStatus.SC_OK, HttpStatus.SC_NOT_MODIFIED))); + } catch (IOException | ParseException e) { + throw new SetApiClientException(ERROR_MESSAGE + e.getMessage()); + } + } + + /** + * Fetches the create user set response (GET request) + * + * @param url + * @param authorizationHeaderValue + * @return + * @throws SetApiClientException + */ + protected UserSet getCreateUserSetResponse(String url, String requestBody, String authorizationHeaderValue) throws SetApiClientException { + try { + LOGGER.trace("Call to Create UserSet API (POST) : {}.", url); + return parseSetApiResponse(getHttpConnection().post(url, requestBody,null, authorizationHeaderValue), new ArrayList<>(Arrays.asList(HttpStatus.SC_CREATED))); + } catch (IOException | ParseException e) { + throw new SetApiClientException(ERROR_MESSAGE + e.getMessage()); + } + } + + /** + * Fetches the User Set api response PUT update userset + * + * @param url url to be executed + * @param requestBody request body for POST and PUT request + * @param authorizationHeaderValue authorisation value + * @return + * @throws SetApiClientException + */ + protected UserSet getUpdateUserSetResponse(String url, String requestBody, String authorizationHeaderValue) throws SetApiClientException { + try { + LOGGER.trace("Call to Update UserSet API : {PUT}.", url); + return parseSetApiResponse(getHttpConnection().put(url, requestBody, authorizationHeaderValue), new ArrayList<>(Arrays.asList(HttpStatus.SC_OK))); + } catch (IOException | ParseException e) { + throw new SetApiClientException(ERROR_MESSAGE + e.getMessage()); + } + } + + + /** + * This method makes DELETE request for given URL that returns + * response headers and status code. + * + * @param url + * @return The response headers and status code. + * @throws IOException + */ + protected String deleteURL(String url, String authorizationHeaderValue) throws SetApiClientException { + try { + LOGGER.trace("Call to UserSet API (DELETE): {} {} ", url, DELETE_URL_RESPONSE); + CloseableHttpResponse response = getHttpConnection().deleteURL(url, authorizationHeaderValue); + if (response.getCode() != HttpStatus.SC_NO_CONTENT) { + String responseBody = EntityUtils.toString(response.getEntity()); + AbstractUserSetApiResponse errorResponse = mapper.readValue(responseBody, AbstractUserSetApiResponse.class); + if (LOGGER.isDebugEnabled()) { + LOGGER.debug(ERROR_MESSAGE + " {} ", errorResponse.getMessage()); + } + throw new SetApiClientException(ERROR_MESSAGE + errorResponse.getMessage(), response.getCode()); + } + return String.valueOf(response.getCode()); + } catch (IOException | ParseException e) { + throw new SetApiClientException(ERROR_MESSAGE + e.getMessage()); + } + } + + private UserSet parseSetApiResponse(CloseableHttpResponse response, List statusToCheckList) throws SetApiClientException, IOException, ParseException { + String responseBody = EntityUtils.toString(response.getEntity()); + if (statusToCheckList.contains(response.getCode())) { + return mapper.readValue(responseBody, BaseUserSet.class); + } else { + AbstractUserSetApiResponse errorResponse = mapper.readValue(responseBody, AbstractUserSetApiResponse.class); + if (LOGGER.isDebugEnabled()) { + LOGGER.debug(ERROR_MESSAGE + " {} ", errorResponse.getMessage()); + } + throw new SetApiClientException(ERROR_MESSAGE + errorResponse.getMessage(), response.getCode()); + } + + } + + /** + * Fetches the user Set search api response + * + * @param url + * @param authorizationHeaderValue + * @return + * @throws SetApiClientException + */ + protected ResultsPageImpl getSearchUserSetResponse(String url, String authorizationHeaderValue) throws SetApiClientException { + try { + LOGGER.trace("Call to UserSet API (SEARCH): {} ", url); + CloseableHttpResponse response = getHttpConnection().get(url, "application/json", authorizationHeaderValue); + String responseBody = EntityUtils.toString(response.getEntity()); + if (response.getCode() == HttpStatus.SC_OK) { + TypeReference> typeRef = new TypeReference<>() { + }; + return mapper.readValue(responseBody, typeRef); + } else { + AbstractUserSetApiResponse errorResponse = mapper.readValue(responseBody, AbstractUserSetApiResponse.class); + if (LOGGER.isDebugEnabled()) { + LOGGER.debug(ERROR_MESSAGE + " {} ", errorResponse.getMessage()); + } + throw new SetApiClientException(ERROR_MESSAGE + errorResponse.getMessage(), response.getCode()); + } + } catch (IOException | ParseException e) { + throw new SetApiClientException(ERROR_MESSAGE + e.getMessage()); + } + } + + /** + * Fetches the Set api service url with "/" at the end + */ + public StringBuilder getUserSetServiceUri() { + StringBuilder urlBuilder = new StringBuilder(); + urlBuilder.append(this.setServiceUri); + if (!this.setServiceUri.endsWith(WebUserSetFields.SLASH)) + urlBuilder.append(WebUserSetFields.SLASH); + + return urlBuilder; + } + + /** + * Build get user Set get Url + * + * @param path + * @param profile + * @return + */ + public static URI buildGetUrls(String path, String profile) { + UriBuilder builder = UriBuilder.newInstance().path(path); + if (profile != null) { + builder.queryParam(QUERY_PARAM_PROFILE, profile); + } + return builder.build(); + } + + /** + * Builds search url with the given params + * + * @param query + * @param qf + * @param sort + * @param page + * @param pageSize + * @param facet + * @param facetLimit + * @param profile + * @return + */ + public static URI buildSearchUrl(String query, String[] qf, String sort, int page, + int pageSize, String facet, int facetLimit, + String profile) { + UriBuilder builder = UriBuilder.newInstance().path(SEARCH_PATH) + .queryParam(QUERY_PARAM_QUERY, query) + .queryParam(QUERY_PARAM_PAGE, page) + .queryParam(QUERY_PARAM_PAGE_SIZE, pageSize); + if (qf != null) { + builder.queryParam(QUERY_PARAM_QF, qf); + } + if (sort != null) { + builder.queryParam(QUERY_PARAM_SORT, sort); + } + if (facet != null) { + builder.queryParam(QUERY_PARAM_FACET, facet); + builder.queryParam("facet.limit", facetLimit); + } + if (profile != null) { + builder.queryParam(QUERY_PARAM_PROFILE, profile); + } + return builder.build(); + } + + public String getApiKey() { + return apiKey; + } + + public String getSetServiceUri() { + return setServiceUri; + } } \ No newline at end of file diff --git a/set-client/src/main/java/eu/europeana/set/client/connection/UserSetApiConnection.java b/set-client/src/main/java/eu/europeana/set/client/connection/UserSetApiConnection.java index e1555fc6..97777c98 100644 --- a/set-client/src/main/java/eu/europeana/set/client/connection/UserSetApiConnection.java +++ b/set-client/src/main/java/eu/europeana/set/client/connection/UserSetApiConnection.java @@ -1,8 +1,11 @@ package eu.europeana.set.client.connection; import java.io.IOException; + +import eu.europeana.api.commons.definitions.search.result.impl.ResultsPageImpl; +import eu.europeana.set.client.exception.SetApiClientException; +import eu.europeana.set.definitions.model.UserSet; import org.apache.commons.lang3.StringUtils; -import org.springframework.http.ResponseEntity; import eu.europeana.api.commons.definitions.vocabulary.CommonApiConstants; import eu.europeana.set.definitions.model.vocabulary.WebUserSetFields; @@ -25,19 +28,16 @@ public UserSetApiConnection(String setServiceUri, String apiKey, String regularU * @return response entity that comprises response body, headers and status code. * @throws IOException */ - public ResponseEntity createUserSet(String set, String profile) throws IOException { - + public UserSet createUserSet(String set, String profile) throws SetApiClientException { StringBuilder urlBuilder = getUserSetServiceUri(); if (StringUtils.isNotEmpty(profile)) { urlBuilder.append(WebUserSetFields.PAR_CHAR); urlBuilder.append(CommonApiConstants.QUERY_PARAM_PROFILE) .append(WebUserSetFields.EQUALS_PARAMETER).append(profile); } - String resUrl = urlBuilder.toString(); - - logger.trace("Ivoking create set: {} ", resUrl); - return postURL(resUrl, set, regularUserAuthorizationValue); + LOGGER.trace("Ivoking create set: {} ", resUrl); + return getCreateUserSetResponse(resUrl, set, regularUserAuthorizationValue); } /** @@ -46,12 +46,11 @@ public ResponseEntity createUserSet(String set, String profile) throws I * * @param identifier * @param profile - * @return response entity that comprises response body, headers and status code. * @throws IOException */ - public ResponseEntity getUserSet(String identifier, String profile) throws IOException { + public UserSet getUserSet(String identifier, String profile) throws SetApiClientException { StringBuilder urlBuilder = getUserSetServiceUri().append(buildGetUrls(identifier + WebUserSetFields.JSON_LD_REST, profile)); - return getURL(urlBuilder.toString(), regularUserAuthorizationValue); + return getUserSetResponse(urlBuilder.toString(), regularUserAuthorizationValue); } /** @@ -65,9 +64,7 @@ public ResponseEntity getUserSet(String identifier, String profile) thro * @return response entity that comprises response body, headers and status code. * @throws IOException */ - public ResponseEntity updateUserSet(String identifier, String updateUserSet, - String profile) throws IOException { - + public UserSet updateUserSet(String identifier, String updateUserSet, String profile) throws SetApiClientException { StringBuilder urlBuilder = getUserSetServiceUri(); urlBuilder.append(identifier).append(WebUserSetFields.JSON_LD_REST); if (StringUtils.isNotEmpty(profile)) { @@ -75,7 +72,7 @@ public ResponseEntity updateUserSet(String identifier, String updateUser urlBuilder.append(CommonApiConstants.QUERY_PARAM_PROFILE) .append(WebUserSetFields.EQUALS_PARAMETER).append(profile); } - return putURL(urlBuilder.toString(), updateUserSet, regularUserAuthorizationValue); + return getUpdateUserSetResponse(urlBuilder.toString(), updateUserSet, regularUserAuthorizationValue); } /** @@ -86,8 +83,7 @@ public ResponseEntity updateUserSet(String identifier, String updateUser * @return response entity that comprises response headers and status code. * @throws IOException */ - public ResponseEntity deleteUserSet(String identifier) throws IOException { - + public String deleteUserSet(String identifier) throws SetApiClientException { StringBuilder urlBuilder = getUserSetServiceUri(); urlBuilder.append(identifier).append(WebUserSetFields.JSON_LD_REST); return deleteURL(urlBuilder.toString(), regularUserAuthorizationValue); @@ -107,11 +103,11 @@ public ResponseEntity deleteUserSet(String identifier) throws IOExceptio * @return * @throws IOException */ - public ResponseEntity searchUserSet(String query, String[] qf, String sort, int page, - int pageSize, String facet, int facetLimit, - String profile) throws IOException { + public ResultsPageImpl searchUserSet(String query, String[] qf, String sort, int page, + int pageSize, String facet, int facetLimit, + String profile) throws SetApiClientException { StringBuilder urlBuilder = getUserSetServiceUri().append(buildSearchUrl(query, qf, sort, page, pageSize, facet, facetLimit, profile)); - return getURL(urlBuilder.toString(), regularUserAuthorizationValue); + return getSearchUserSetResponse(urlBuilder.toString(), regularUserAuthorizationValue); } } diff --git a/set-client/src/main/java/eu/europeana/set/client/exception/SetApiClientException.java b/set-client/src/main/java/eu/europeana/set/client/exception/SetApiClientException.java index d742d014..423022bd 100644 --- a/set-client/src/main/java/eu/europeana/set/client/exception/SetApiClientException.java +++ b/set-client/src/main/java/eu/europeana/set/client/exception/SetApiClientException.java @@ -1,14 +1,47 @@ package eu.europeana.set.client.exception; public class SetApiClientException extends Exception { + private static final long serialVersionUID = 8281933808897246375L; + private final int remoteStatusCode; + - public SetApiClientException(String message, Exception e) { - super(message, e); + /** + * Constructor for exception to indicate that an error occurred during invocation of the remote + * service or parsing of service response + * + * @param msg the error message + * @param remoteStatusCode the status code from the remote service + * @param t eventual exception thrown when extracting information from the remote service response + */ + public SetApiClientException(String msg, int remoteStatusCode, Throwable t) { + super(msg, t); + this.remoteStatusCode = remoteStatusCode; } - public SetApiClientException(String message) { - super(message); + /** + * Constructor for exception to indicate that an error occurred during invocation of the remote + * service or parsing of service response + * + * @param msg the error message + * @param remoteStatusCode the status code from the remote service + */ + public SetApiClientException(String msg, int remoteStatusCode) { + super(msg); + this.remoteStatusCode = remoteStatusCode; } -} + /** + * Constructor for exception to indicate that an error occurred during invocation of the remote + * service, to be used when no response from the remote service has been received + * + * @param msg the error message + */ + public SetApiClientException(String msg) { + this(msg, -1); + } + + public int getRemoteStatusCode() { + return remoteStatusCode; + } +} \ No newline at end of file diff --git a/set-client/src/main/java/eu/europeana/set/client/exception/TechnicalRuntimeException.java b/set-client/src/main/java/eu/europeana/set/client/exception/TechnicalRuntimeException.java deleted file mode 100644 index f19d45d7..00000000 --- a/set-client/src/main/java/eu/europeana/set/client/exception/TechnicalRuntimeException.java +++ /dev/null @@ -1,19 +0,0 @@ -package eu.europeana.set.client.exception; - -/** - * This class is meant to be used for marking and handling technical exceptions that might occur within the system - * - * @author GrafR - */ -public class TechnicalRuntimeException extends RuntimeException { - - private static final long serialVersionUID = -2040704860209418649L; - - public TechnicalRuntimeException(String message, Exception e) { - super(message, e); - } - - public TechnicalRuntimeException(String message) { - super(message); - } -} diff --git a/set-client/src/main/java/eu/europeana/set/client/model/result/AbstractUserSetApiResponse.java b/set-client/src/main/java/eu/europeana/set/client/model/result/AbstractUserSetApiResponse.java index 9f2b0c1a..21072443 100644 --- a/set-client/src/main/java/eu/europeana/set/client/model/result/AbstractUserSetApiResponse.java +++ b/set-client/src/main/java/eu/europeana/set/client/model/result/AbstractUserSetApiResponse.java @@ -1,14 +1,18 @@ package eu.europeana.set.client.model.result; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + /** * @author GrafR * */ -public abstract class AbstractUserSetApiResponse { +@JsonIgnoreProperties(ignoreUnknown = true) +public class AbstractUserSetApiResponse { private String action; private String success; private String error; + private String message; public String getAction() { return action; @@ -34,4 +38,11 @@ public void setError(String error) { this.error = error; } + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } } diff --git a/set-client/src/main/java/eu/europeana/set/client/web/SearchUserSetApi.java b/set-client/src/main/java/eu/europeana/set/client/web/SearchUserSetApi.java index 265408f9..0aba31ba 100644 --- a/set-client/src/main/java/eu/europeana/set/client/web/SearchUserSetApi.java +++ b/set-client/src/main/java/eu/europeana/set/client/web/SearchUserSetApi.java @@ -1,6 +1,8 @@ package eu.europeana.set.client.web; -import org.springframework.http.ResponseEntity; +import eu.europeana.api.commons.definitions.search.result.impl.ResultsPageImpl; +import eu.europeana.set.client.exception.SetApiClientException; +import eu.europeana.set.definitions.model.UserSet; /** * Search Client API interface @@ -21,6 +23,6 @@ public interface SearchUserSetApi { * @param profile * @return */ - ResponseEntity searchUserSet(String query, String[] qf, String sort, int page, int pageSize, - String facet, int facetLimit, String profile); + ResultsPageImpl searchUserSet(String query, String[] qf, String sort, int page, int pageSize, + String facet, int facetLimit, String profile) throws SetApiClientException; } diff --git a/set-client/src/main/java/eu/europeana/set/client/web/WebUserSetApi.java b/set-client/src/main/java/eu/europeana/set/client/web/WebUserSetApi.java index 63919f57..67e38bfa 100644 --- a/set-client/src/main/java/eu/europeana/set/client/web/WebUserSetApi.java +++ b/set-client/src/main/java/eu/europeana/set/client/web/WebUserSetApi.java @@ -1,6 +1,7 @@ package eu.europeana.set.client.web; -import org.springframework.http.ResponseEntity; +import eu.europeana.set.client.exception.SetApiClientException; +import eu.europeana.set.definitions.model.UserSet; /** * Client API interface @@ -14,8 +15,7 @@ public interface WebUserSetApi { * @param profile * @return response entity containing body, headers and status code. */ - ResponseEntity createUserSet( - String requestBody, String profile); + UserSet createUserSet(String requestBody, String profile) throws SetApiClientException; /** * This method retrieves user set from database @@ -24,16 +24,16 @@ ResponseEntity createUserSet( * @param profile * @return response entity that contains response body, headers and status code. */ - ResponseEntity getUserSet( - String identifier, String profile); + UserSet getUserSet( + String identifier, String profile) throws SetApiClientException; /** * This method deletes user set by the given identifier * @param identifier * @return response entity containing headers and status code. */ - ResponseEntity deleteUserSet( - String identifier); + String deleteUserSet( + String identifier) throws SetApiClientException; /** * This method updates user set by the given update string in JSON format @@ -42,7 +42,6 @@ ResponseEntity deleteUserSet( * @param profile * @return response entity containing body, headers and status code. */ - ResponseEntity updateUserSet( - String identifier, String requestBody, String profile); + UserSet updateUserSet(String identifier, String requestBody, String profile) throws SetApiClientException; } \ No newline at end of file diff --git a/set-client/src/test/java/eu/europeana/set/client/integration/web/BaseAPIConnectionTest.java b/set-client/src/test/java/eu/europeana/set/client/integration/web/BaseAPIConnectionTest.java index 5c55a40e..615f5155 100644 --- a/set-client/src/test/java/eu/europeana/set/client/integration/web/BaseAPIConnectionTest.java +++ b/set-client/src/test/java/eu/europeana/set/client/integration/web/BaseAPIConnectionTest.java @@ -12,7 +12,6 @@ public class BaseAPIConnectionTest { private static final String SERVICE_URI = "testUri"; private static final String API_KEY_1 = "api_key"; - private static final String API_ADMIN_KEY = "apiadmin"; private BaseApiConnection baseApiConnection; @@ -30,9 +29,6 @@ public void Test_getUserSetServiceUri() { baseApiConnection = new BaseApiConnection(SERVICE_URI + WebUserSetFields.SLASH, API_KEY_1, null); result = baseApiConnection.getUserSetServiceUri(); assertEquals(SERVICE_URI + WebUserSetFields.SLASH, result.toString()); - - assertEquals(API_KEY_1, baseApiConnection.getApiKey()); - assertEquals(API_ADMIN_KEY, baseApiConnection.getAdminApiKey()); } } diff --git a/set-client/src/test/java/eu/europeana/set/client/integration/web/BaseWebUserSetProtocol.java b/set-client/src/test/java/eu/europeana/set/client/integration/web/BaseWebUserSetProtocol.java index 41c461b6..68899a3d 100644 --- a/set-client/src/test/java/eu/europeana/set/client/integration/web/BaseWebUserSetProtocol.java +++ b/set-client/src/test/java/eu/europeana/set/client/integration/web/BaseWebUserSetProtocol.java @@ -7,13 +7,10 @@ import eu.europeana.set.client.config.ClientConfiguration; import eu.europeana.set.client.exception.SetApiClientException; +import org.apache.hc.core5.http.HttpStatus; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.junit.jupiter.api.BeforeEach; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; - -import eu.europeana.set.client.web.WebUserSetApi; import eu.europeana.set.client.UserSetApiClient; import eu.europeana.set.definitions.model.UserSet; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -43,15 +40,9 @@ public void initObjects() throws SetApiClientException { * @return response entity that contains response body, headers and status code. * @throws IOException */ - protected ResponseEntity storeTestUserSet(String resource, String profile) throws IOException { - + protected UserSet storeTestUserSet(String resource, String profile) throws SetApiClientException, IOException { String requestBody = getJsonStringInput(resource); - - /** - * store set - */ - ResponseEntity storedResponse = apiClient.getWebUserSetApi().createUserSet(requestBody, profile); - return storedResponse; + return apiClient.getWebUserSetApi().createUserSet(requestBody, profile); } protected String getJsonStringInput(String resource) throws IOException { @@ -71,12 +62,12 @@ protected void deleteUserSet(UserSet set) throws SetApiClientException { } protected void deleteUserSet(String identifier) throws SetApiClientException { - ResponseEntity re = apiClient.getWebUserSetApi().deleteUserSet(identifier); - assertEquals(HttpStatus.OK, re.getStatusCode()); + String re = apiClient.getWebUserSetApi().deleteUserSet(identifier); + assertEquals(HttpStatus.SC_OK, re); log.trace("User set deleted: /" + identifier); } - protected ResponseEntity getUserSet(UserSet set) { + protected UserSet getUserSet(UserSet set) throws SetApiClientException { return apiClient.getWebUserSetApi().getUserSet(set.getIdentifier(), null); } diff --git a/set-client/src/test/java/eu/europeana/set/client/integration/web/WebUserSetProtocolExceptionsTest.java b/set-client/src/test/java/eu/europeana/set/client/integration/web/WebUserSetProtocolExceptionsTest.java index 99dbac12..08b0021d 100644 --- a/set-client/src/test/java/eu/europeana/set/client/integration/web/WebUserSetProtocolExceptionsTest.java +++ b/set-client/src/test/java/eu/europeana/set/client/integration/web/WebUserSetProtocolExceptionsTest.java @@ -2,10 +2,12 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import java.io.IOException; + +import eu.europeana.set.client.exception.SetApiClientException; +import eu.europeana.set.definitions.model.UserSet; +import org.apache.hc.core5.http.HttpStatus; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; /** @@ -42,65 +44,51 @@ public class WebUserSetProtocolExceptionsTest extends BaseWebUserSetProtocol { public String UNKNOWN_PROVIDED_IDENTIFIER = "unknown_provided_identifier"; @Test - public void createWebsetUserSetWithoutBody() throws IOException { - - ResponseEntity response = apiClient.getWebUserSetApi().createUserSet( - null, null); + public void createWebsetUserSetWithoutBody() { + try { + apiClient.getWebUserSetApi().createUserSet(null, null); + } catch (SetApiClientException e) { + assertEquals(HttpStatus.SC_BAD_REQUEST, e.getRemoteStatusCode()); + } - assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode()); } - @Test public void createWebUserSetWithCorruptedBody() { - ResponseEntity response = apiClient.getWebUserSetApi().createUserSet( - CORRUPTED_JSON, null); - - assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode()); + try { + apiClient.getWebUserSetApi().createUserSet(CORRUPTED_JSON, null); + } catch (SetApiClientException e) { + assertEquals(HttpStatus.SC_BAD_REQUEST, e.getRemoteStatusCode()); + } } @Test public void getWebUserSetWithWrongIdentifier() { - ResponseEntity response = apiClient.getWebUserSetApi().getUserSet( - WRONG_GENERATED_IDENTIFIER, null); - assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); + try { + apiClient.getWebUserSetApi().getUserSet(WRONG_GENERATED_IDENTIFIER, null); + } catch (SetApiClientException e) { + assertEquals(HttpStatus.SC_NOT_FOUND, e.getRemoteStatusCode()); + } } @Test public void updateWebsetUserSetWithWrongIdentifierNumber() throws IOException { String requestBody = getJsonStringInput(USER_SET_CONTENT); - - ResponseEntity response = apiClient.getWebUserSetApi().updateUserSet( - WRONG_GENERATED_IDENTIFIER - , requestBody - , null); - assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); + try { + apiClient.getWebUserSetApi().updateUserSet(WRONG_GENERATED_IDENTIFIER, requestBody, null); + } catch (SetApiClientException e) { + assertEquals(HttpStatus.SC_NOT_FOUND, e.getRemoteStatusCode()); + } } @Test public void updateWebUserSetWithWrongIdentifier() throws IOException { String requestBody = getJsonStringInput(USER_SET_CONTENT); - - ResponseEntity response = apiClient.getWebUserSetApi().updateUserSet( - WRONG_GENERATED_IDENTIFIER - , requestBody - , null); - assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); + try { + apiClient.getWebUserSetApi().updateUserSet(WRONG_GENERATED_IDENTIFIER, requestBody, null); + } catch (SetApiClientException e) { + assertEquals(HttpStatus.SC_NOT_FOUND, e.getRemoteStatusCode()); + } } -// @Test -// public void updateWebsetUserSetWithCorruptedUpdateBody() throws JsonParseException, IOException { -// -// /** -// * store set and retrieve its identifier -// */ -// UserSet set = createTestUserSet(); -// ResponseEntity response = getApiClient().updateUserSet( -// set.getIdentifier() -// , CORRUPTED_UPDATE_JSON -// , TEST_USER_TOKEN -// ); -// assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode()); -// } - } \ No newline at end of file diff --git a/set-client/src/test/java/eu/europeana/set/client/integration/web/WebUserSetProtocolTest.java b/set-client/src/test/java/eu/europeana/set/client/integration/web/WebUserSetProtocolTest.java index 64e237b9..323ddd3d 100644 --- a/set-client/src/test/java/eu/europeana/set/client/integration/web/WebUserSetProtocolTest.java +++ b/set-client/src/test/java/eu/europeana/set/client/integration/web/WebUserSetProtocolTest.java @@ -5,10 +5,13 @@ import java.io.IOException; import java.util.regex.Matcher; import java.util.regex.Pattern; + +import eu.europeana.set.client.exception.SetApiClientException; +import eu.europeana.set.definitions.model.UserSet; +import org.apache.hc.core5.http.HttpStatus; +import org.apache.hc.core5.http.io.entity.EntityUtils; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; /** * This class aims at testing of the annotation methods. @@ -21,48 +24,48 @@ public class WebUserSetProtocolTest extends BaseWebUserSetProtocol { private static final String USER_SET_PATH = "http://data.europeana.eu/set/"; @Test - public void createUserSet() throws IOException { + public void createUserSet() throws SetApiClientException, IOException { String setId = createTestUserSet(USER_SET_CONTENT, null); assertNotNull(setId); apiClient.getWebUserSetApi().deleteUserSet(setId); } /** - * This method creates and retrieves user set + * This method creates and retrieves user set * @throws IllegalArgumentException * @throws IOException */ @Test - public void retrieveUserSet() throws IllegalArgumentException, IOException { - ResponseEntity response; + public void retrieveUserSet() throws IllegalArgumentException, IOException, SetApiClientException { String testSetId = createTestUserSet(USER_SET_CONTENT, null); assertNotNull(testSetId); // get user set by ID and user identifier - response = apiClient.getWebUserSetApi().getUserSet(testSetId, null); - validateResponse(response, HttpStatus.OK); + UserSet userSet = apiClient.getWebUserSetApi().getUserSet(testSetId, null); + assertNotNull(userSet); + assertEquals(testSetId, userSet.getIdentifier()); } - + @Test - public void updateUserSet() throws IOException { + public void updateUserSet() throws IOException, SetApiClientException { String testSetId = createTestUserSet(USER_SET_CONTENT, null); assertNotNull(testSetId); // updated user set value String requestBody = getJsonStringInput(USER_SET_UPDATE_CONTENT); assertNotNull(requestBody); // update user set by identifier URL - ResponseEntity updateResponse = apiClient.getWebUserSetApi().updateUserSet( + UserSet updateResponse = apiClient.getWebUserSetApi().updateUserSet( testSetId, requestBody, null); - validateResponse(updateResponse, HttpStatus.OK); + assertNotNull(updateResponse); + assertEquals(testSetId, updateResponse.getIdentifier()); } - + @Test - public void deleteUserSet() throws IOException { + public void deleteUserSet() throws IOException, SetApiClientException { String testSetId = createTestUserSet(USER_SET_CONTENT,null); assertNotNull(testSetId); // delete user set by identifier URL - ResponseEntity deleteResponse = apiClient.getWebUserSetApi().deleteUserSet( - testSetId); - assertEquals(HttpStatus.NO_CONTENT, deleteResponse.getStatusCode()); + String deleteResponse = apiClient.getWebUserSetApi().deleteUserSet(testSetId); + assertEquals(HttpStatus.SC_NO_CONTENT, deleteResponse); } /** @@ -72,37 +75,8 @@ public void deleteUserSet() throws IOException { * @return id of created user set * @throws IOException */ - private String createTestUserSet(String content, String profile) throws IOException { - ResponseEntity response = storeTestUserSet(content, profile); - validateResponse(response, HttpStatus.CREATED); - String testSetId = matchSetId(response.getBody()); - return testSetId; - } - - /** - * This method matches response body to get user set ID - * e.g. ID 134 from "http://data.europeana.eu/set/134" - * @param body The response body - * @return The matched user set ID - */ - public String matchSetId(String body) { - String res = ""; - log.info("body: " + body); - String regex = "\\b(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]\\b"; - Pattern pat = Pattern.compile(regex); - Matcher mat = pat.matcher(body); - - while (mat.find()) { - res = mat.group(); - if (res.contains(USER_SET_PATH)) { - return res.replaceAll(USER_SET_PATH, ""); - } - } - return res; - } - - protected void validateResponse(ResponseEntity response, HttpStatus expectedStatus) { - assertNotNull(response.getBody()); - assertEquals(expectedStatus, response.getStatusCode()); + private String createTestUserSet(String content, String profile) throws SetApiClientException, IOException { + UserSet response = storeTestUserSet(content, profile); + return response.getIdentifier(); } } diff --git a/set-common/pom.xml b/set-common/pom.xml index 0f57c267..0dec8a3e 100644 --- a/set-common/pom.xml +++ b/set-common/pom.xml @@ -36,27 +36,19 @@ 1.0-SNAPSHOT - - org.springframework - spring-beans - - - - org.springframework - spring-web - - org.apache.commons commons-lang3 + - commons-httpclient - commons-httpclient - ${version.httpClient} + org.apache.httpcomponents.client5 + httpclient5 + ${version.httpClient5} + com.fasterxml.jackson.core diff --git a/set-common/src/main/java/eu/europeana/set/common/http/HttpConnection.java b/set-common/src/main/java/eu/europeana/set/common/http/HttpConnection.java index 1642de1f..1bb95f94 100644 --- a/set-common/src/main/java/eu/europeana/set/common/http/HttpConnection.java +++ b/set-common/src/main/java/eu/europeana/set/common/http/HttpConnection.java @@ -4,41 +4,46 @@ */ package eu.europeana.set.common.http; +import org.apache.commons.lang3.StringUtils; +import org.apache.hc.client5.http.classic.methods.*; +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; +import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; +import org.apache.hc.core5.http.HttpHeaders; +import org.apache.hc.core5.http.io.entity.StringEntity; import java.io.IOException; -import java.io.InputStream; -import java.nio.charset.StandardCharsets; -import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; -import org.apache.commons.httpclient.Header; -import org.apache.commons.httpclient.HttpClient; -import org.apache.commons.httpclient.HttpMethod; -import org.apache.commons.httpclient.methods.DeleteMethod; -import org.apache.commons.httpclient.methods.GetMethod; -import org.apache.commons.httpclient.methods.PostMethod; -import org.apache.commons.httpclient.methods.PutMethod; -import org.apache.commons.httpclient.params.HttpMethodParams; -import org.apache.commons.io.IOUtils; -import org.apache.commons.lang3.StringUtils; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.util.LinkedMultiValueMap; -import org.springframework.util.MultiValueMap; /** * The class encapsulating simple HTTP access. * - * @author GrafR + * @author Srishti Singh + * @since 26 November 2024 */ public class HttpConnection { public static final String HEADER_AUTHORIZATION = "Authorization"; - - private static final int CONNECTION_RETRIES = 3; - private static final int TIMEOUT_CONNECTION = 40000; - private static final int STATUS_OK_START = 200; - private static final int STATUS_OK_END = 299; - private HttpClient httpClient = null; + private final CloseableHttpClient httpClient; + + public HttpConnection() { + httpClient = HttpClientBuilder.create().build(); + } + + /** + *This method makes GET request for given URL. + * @param url + * @param acceptHeaderValue + * @param authorizationHeaderValue + * @return + * @throws IOException + */ + + public CloseableHttpResponse get(String url, String acceptHeaderValue, String authorizationHeaderValue) throws IOException { + HttpGet get = new HttpGet(url); + addHeaders(get, HttpHeaders.ACCEPT, acceptHeaderValue); + addHeaders(get, HEADER_AUTHORIZATION,authorizationHeaderValue); + return executeHttpClient(get); + } /** * This method makes POST request for given URL and JSON body parameter. @@ -47,41 +52,18 @@ public class HttpConnection { * @param requestBody * @param contentType * @return ResponseEntity that comprises response body in JSON format, headers - * and status code. + * and status code. * @throws IOException */ - public ResponseEntity post(String url, String requestBody, String contentType) throws IOException { - PostMethod post = buildPostMethod(url, requestBody, HttpHeaders.CONTENT_TYPE, contentType); - return fetchHttpMethodResponse(post); - } + public CloseableHttpResponse post(String url, String requestBody, String contentType, String authorizationHeaderValue) throws IOException { + HttpPost post = new HttpPost(url); + addHeaders(post, HttpHeaders.CONTENT_TYPE, contentType); + addHeaders(post, HEADER_AUTHORIZATION, authorizationHeaderValue); + post.setEntity(new StringEntity(requestBody)); + return executeHttpClient(post); + } - /** - * This method makes POST request for given URL and JSON body parameter with - * header. - * - * @param url - * @param body - * @param requestHeaderName - * @param authorizationHeaderValue - * @return ResponseEntity that comprises response body in JSON format, headers - * and status code. - * @throws IOException - */ - public ResponseEntity postURL(String url, String body, String authorizationHeaderValue) throws IOException { - PostMethod post = buildPostMethod(url, body, HEADER_AUTHORIZATION, - authorizationHeaderValue); - return fetchHttpMethodResponse(post); - } - @SuppressWarnings("deprecation") - private PostMethod buildPostMethod(String url, String body, String headerName, String headerValue) { - PostMethod post = new PostMethod(url); - if (StringUtils.isNotBlank(headerValue)) { - post.setRequestHeader(headerName, headerValue); - } - post.setRequestBody(body); - return post; - } /** * This method makes PUT request for given URL and JSON body parameter. @@ -89,199 +71,42 @@ private PostMethod buildPostMethod(String url, String body, String headerName, S * @param url * @param jsonParamValue * @return ResponseEntity that comprises response body in JSON format, headers - * and status code. + * and status code. * @throws IOException */ - public ResponseEntity putURL(String url, String jsonParamValue) throws IOException { - return putURL(url, jsonParamValue, null); - } + public CloseableHttpResponse put(String url, String jsonParamValue, String authorizationHeaderValue) throws IOException { + HttpPut put = new HttpPut(url); + addHeaders(put, HEADER_AUTHORIZATION,authorizationHeaderValue); + put.setEntity(new StringEntity(jsonParamValue)); - /** - * This method makes PUT request for given URL and JSON body parameter. - * - * @param url - * @param jsonParamValue - * @param requestHeaderName - * @param authorizationHeaderValue - * @return ResponseEntity that comprises response body in JSON format, headers - * and status code. - * @throws IOException - */ - @SuppressWarnings("deprecation") - public ResponseEntity putURL(String url, String jsonParamValue, String authorizationHeaderValue) - throws IOException { - PutMethod put = new PutMethod(url); - if (StringUtils.isNotBlank(authorizationHeaderValue)) { - put.setRequestHeader(HEADER_AUTHORIZATION, authorizationHeaderValue); - } - put.setRequestBody(jsonParamValue); + return executeHttpClient(put); - return fetchHttpMethodResponse(put); - } + } /** * This method makes DELETE request for given identifier URL. * - * @param url The identifier URL - * @return ResponseEntity that comprises response headers and status code. - * @throws IOException - */ - public ResponseEntity deleteURL(String url) throws IOException { - return deleteURL(url, null); - } - - /** - * This method makes DELETE request for given identifier URL. - * * @param url The identifier URL - * @param requestHeaderName * @param authorizationtHeaderValue * @return ResponseEntity that comprises response headers and status code. * @throws IOException */ - public ResponseEntity deleteURL(String url, String authorizationtHeaderValue) throws IOException { - DeleteMethod delete = new DeleteMethod(url); - if (StringUtils.isNotBlank(authorizationtHeaderValue)) { - delete.setRequestHeader(HEADER_AUTHORIZATION, authorizationtHeaderValue); + public CloseableHttpResponse deleteURL(String url, String authorizationtHeaderValue) throws IOException { + HttpDelete delete = new HttpDelete(url); + addHeaders(delete,HEADER_AUTHORIZATION, authorizationtHeaderValue); + return executeHttpClient(delete); } - return fetchHttpMethodResponse(delete); - } - /** - * This method builds a response entity that comprises response body, headers - * and status code for the passed HTTP method - * - * @param method The HTTP method (e.g. post, put, delete or get) - * @return response entity - * @throws IOException - */ - private ResponseEntity buildResponseEntity(HttpMethod method) throws IOException { - - MultiValueMap headers = new LinkedMultiValueMap<>(15); - for (Header header : method.getResponseHeaders()) - headers.add(header.getName(), header.getValue()); + private CloseableHttpResponse executeHttpClient(T url) throws IOException { + return httpClient.execute(url); - String res = null; - if (method.getResponseBody() != null && method.getResponseBody().length > 0) { - byte[] byteResponse = method.getResponseBody(); - res = new String(byteResponse, StandardCharsets.UTF_8); } - return new ResponseEntity<>(res, headers, HttpStatus.valueOf(method.getStatusCode())); - } - /** - * This method makes GET request for given URL. - * - * @param url - * @return ResponseEntity that comprises response body in JSON format, headers - * and status code. - * @throws IOException - */ - public ResponseEntity getURL(String url) throws IOException { - return getURL(url, null); - } - - /** - * This method makes GET request for given URL. - * - * @param url - * @param requestHeaderName - * @param authorizationHeaderValue - * @return ResponseEntity that comprises response body in JSON format, headers - * and status code. - * @throws IOException - */ - public ResponseEntity getURL(String url, String authorizationHeaderValue) throws IOException { - GetMethod get = new GetMethod(url); - if (StringUtils.isNotBlank(authorizationHeaderValue)) { - get.setRequestHeader(HEADER_AUTHORIZATION, authorizationHeaderValue); - } - - return fetchHttpMethodResponse(get); - } - - private ResponseEntity fetchHttpMethodResponse(HttpMethod http) throws IOException{ - HttpClient client = this.getHttpClient(CONNECTION_RETRIES, TIMEOUT_CONNECTION); - try { - client.executeMethod(http); - return buildResponseEntity(http); - } finally { - http.releaseConnection(); - } - } - - @SuppressWarnings("deprecation") - public String getURLContentWithBody(String url, String jsonParamValue) throws IOException { - PostMethod post = new PostMethod(url); - post.setRequestHeader("Content-Type", "application/json"); - post.setRequestBody(jsonParamValue); - - return callHttpMethod(post); - } - - public String getJsonResponse(String url) throws IOException { - GetMethod get = new GetMethod(url); - get.setRequestHeader("Accept", "application/json"); - return callHttpMethod(get); - } - - private String callHttpMethod(HttpMethod httpMethod) throws IOException { - HttpClient client = this.getHttpClient(CONNECTION_RETRIES, TIMEOUT_CONNECTION); - try { - client.executeMethod(httpMethod); - - if (httpMethod.getStatusCode() >= STATUS_OK_START && httpMethod.getStatusCode() <= STATUS_OK_END) { - InputStream responseBodyAsStream = httpMethod.getResponseBodyAsStream(); - return IOUtils.toString(responseBodyAsStream, StandardCharsets.UTF_8); - } else { - return null; - } - - } finally { - httpMethod.releaseConnection(); - } - } - - private HttpClient getHttpClient(int connectionRetry, int conectionTimeout) { - if (this.httpClient == null) { - HttpClient client = new HttpClient(); - - // configure retry handler - client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, - new DefaultHttpMethodRetryHandler(connectionRetry, false)); - - // when using a http proxy - String proxyHost = System.getProperty("http.proxyHost"); - if ((proxyHost != null) && (proxyHost.length() > 0)) { - String proxyPortSrt = System.getProperty("http.proxyPort"); - if (proxyPortSrt == null) { - proxyPortSrt = "8080"; + private T addHeaders(T url, String headerName, String headerValue) { + if (StringUtils.isNotBlank(headerValue)) { + url.setHeader(headerName, headerValue); } - int proxyPort = Integer.parseInt(proxyPortSrt); - - client.getHostConfiguration().setProxy(proxyHost, proxyPort); - } - - // configure timeouts - boolean bTimeout = false; - String connectTimeOut = System.getProperty("sun.net.client.defaultConnectTimeout"); - if ((connectTimeOut != null) && (connectTimeOut.length() > 0)) { - client.getParams().setIntParameter("sun.net.client.defaultConnectTimeout", - Integer.parseInt(connectTimeOut)); - bTimeout = true; - } - String readTimeOut = System.getProperty("sun.net.client.defaultReadTimeout"); - if ((readTimeOut != null) && (readTimeOut.length() > 0)) { - client.getParams().setIntParameter("sun.net.client.defaultReadTimeout", Integer.parseInt(readTimeOut)); - bTimeout = true; - } - if (!bTimeout) { - client.getParams().setIntParameter(HttpMethodParams.SO_TIMEOUT, conectionTimeout); - } - - this.httpClient = client; + return url; } - return this.httpClient; - } } diff --git a/set-definitions/pom.xml b/set-definitions/pom.xml index b159f6b3..728162b8 100644 --- a/set-definitions/pom.xml +++ b/set-definitions/pom.xml @@ -29,5 +29,19 @@ commons-definitions ${version.commonsApi} + + + + com.fasterxml.jackson.core + jackson-annotations + ${jackson.version} + + + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + diff --git a/set-definitions/src/main/java/eu/europeana/set/definitions/json/AgentDeserializer.java b/set-definitions/src/main/java/eu/europeana/set/definitions/json/AgentDeserializer.java new file mode 100644 index 00000000..d01e9025 --- /dev/null +++ b/set-definitions/src/main/java/eu/europeana/set/definitions/json/AgentDeserializer.java @@ -0,0 +1,36 @@ +package eu.europeana.set.definitions.json; + +import com.fasterxml.jackson.core.JacksonException; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonDeserializer; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import eu.europeana.set.definitions.model.agent.Agent; +import eu.europeana.set.definitions.model.agent.impl.Person; +import eu.europeana.set.definitions.model.agent.impl.SoftwareAgent; +import eu.europeana.set.definitions.model.vocabulary.AgentTypes; +import org.apache.commons.lang3.StringUtils; + +import java.io.IOException; + +public class AgentDeserializer extends JsonDeserializer { + + @Override + public Agent deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JacksonException { + ObjectMapper mapper = (ObjectMapper) jsonParser.getCodec(); + ObjectNode root = mapper.readTree(jsonParser); + + if (root.has("type") ) { + String agentType = root.get("type").asText(); + if (StringUtils.equals(agentType, AgentTypes.PERSON.getJsonValue())) { + return mapper.readValue(root.toString(), Person.class); + } + if (StringUtils.equals(agentType, AgentTypes.SOFTWARE.getJsonValue())) { + return mapper.readValue(root.toString(), SoftwareAgent.class); + } + // There is no class for Organisation type + } + return null; + } +} diff --git a/set-definitions/src/main/java/eu/europeana/set/definitions/model/BaseWebResource.java b/set-definitions/src/main/java/eu/europeana/set/definitions/model/BaseWebResource.java index 932b8eba..19d9b0cd 100644 --- a/set-definitions/src/main/java/eu/europeana/set/definitions/model/BaseWebResource.java +++ b/set-definitions/src/main/java/eu/europeana/set/definitions/model/BaseWebResource.java @@ -1,7 +1,10 @@ package eu.europeana.set.definitions.model; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + import java.util.Objects; +@JsonIgnoreProperties(ignoreUnknown = true) public class BaseWebResource { public static final String TYPE = "WebResource"; diff --git a/set-definitions/src/main/java/eu/europeana/set/definitions/model/agent/impl/BaseAgent.java b/set-definitions/src/main/java/eu/europeana/set/definitions/model/agent/impl/BaseAgent.java index 29ebba22..829f8b7d 100644 --- a/set-definitions/src/main/java/eu/europeana/set/definitions/model/agent/impl/BaseAgent.java +++ b/set-definitions/src/main/java/eu/europeana/set/definitions/model/agent/impl/BaseAgent.java @@ -1,8 +1,10 @@ package eu.europeana.set.definitions.model.agent.impl; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import eu.europeana.set.definitions.model.agent.Agent; import eu.europeana.set.definitions.model.vocabulary.AgentTypes; +@JsonIgnoreProperties(ignoreUnknown = true) public abstract class BaseAgent implements Agent { private String httpUrl; diff --git a/set-definitions/src/main/java/eu/europeana/set/definitions/model/impl/BaseUserSet.java b/set-definitions/src/main/java/eu/europeana/set/definitions/model/impl/BaseUserSet.java index 070dcdb6..a9dc3db0 100644 --- a/set-definitions/src/main/java/eu/europeana/set/definitions/model/impl/BaseUserSet.java +++ b/set-definitions/src/main/java/eu/europeana/set/definitions/model/impl/BaseUserSet.java @@ -3,19 +3,28 @@ import java.util.Date; import java.util.List; import java.util.Map; + +import com.fasterxml.jackson.annotation.JsonGetter; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import eu.europeana.set.definitions.json.AgentDeserializer; import eu.europeana.set.definitions.model.BaseWebResource; import eu.europeana.set.definitions.model.UserSet; import eu.europeana.set.definitions.model.agent.Agent; import eu.europeana.set.definitions.model.vocabulary.UserSetTypes; import eu.europeana.set.definitions.model.vocabulary.VisibilityTypes; import eu.europeana.set.definitions.model.vocabulary.WebUserSetModelFields; +import org.apache.commons.lang3.StringUtils; /** * Europeana Sets API Specification * + * @JsonIgnoreProperties - to ignore "@context" while parsing the results in the Client side code + * * @author GrafR Modified by Srishti Singh 2-2-2021 */ -public abstract class BaseUserSet extends BasePageInfo implements UserSet { +@JsonIgnoreProperties(ignoreUnknown = true) +public class BaseUserSet extends BasePageInfo implements UserSet { // EDM Collection Profile @@ -55,7 +64,10 @@ public abstract class BaseUserSet extends BasePageInfo implements UserSet { * of this set of the Entity user sets */ private List contributor; - + + public BaseUserSet() { + } + /** * depiction, primarily used y Galleries */ @@ -74,6 +86,7 @@ public abstract class BaseUserSet extends BasePageInfo implements UserSet { * A reference to the user agent that gathers objects together following * implicit or explicit criteria or accrual policy. */ + @JsonDeserialize(using = AgentDeserializer.class) private Agent creator; /** @@ -106,10 +119,20 @@ public abstract class BaseUserSet extends BasePageInfo implements UserSet { private Provider provider; + /** + * Adding @JsonGetter, as this field is ignored in the json responses. Will build the identifier value from the field "id" + * exmaple : 'id' : http://data.europeana.eu/set/xyz , identifier : xyz + * @return identifier of the set + */ + @JsonGetter(WebUserSetModelFields.ID) public String getIdentifier() { + if (identifier != null) { + return StringUtils.substringAfterLast(identifier, "/"); + } return identifier; } + public void setIdentifier(String sequenceIdentifier) { this.identifier = sequenceIdentifier; } @@ -284,6 +307,11 @@ public boolean isPublished() { return VisibilityTypes.PUBLISHED.getJsonValue().equals(getVisibility()); } + @Override + public void setBaseUrl(String baseUrl) { + //used only for web userset + } + @Override public boolean isBookmarksFolder() { return UserSetTypes.BOOKMARKSFOLDER.getJsonValue().equals(getType()); @@ -322,6 +350,11 @@ public Provider getProvider() { return provider; } + @Override + public boolean hasItem(String itemId) { + return getItems() != null && getItems().contains(itemId); + } + @Override public void setProvider(Provider provider) { this.provider = provider; diff --git a/set-integration-testing/src/main/java/eu/europeana/api/set/integration/connection/http/EuropeanaOauthClient.java b/set-integration-testing/src/main/java/eu/europeana/api/set/integration/connection/http/EuropeanaOauthClient.java index acb5bfd7..31615a73 100644 --- a/set-integration-testing/src/main/java/eu/europeana/api/set/integration/connection/http/EuropeanaOauthClient.java +++ b/set-integration-testing/src/main/java/eu/europeana/api/set/integration/connection/http/EuropeanaOauthClient.java @@ -2,10 +2,12 @@ import java.io.IOException; +import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; +import org.apache.hc.core5.http.HttpStatus; +import org.apache.hc.core5.http.ParseException; +import org.apache.hc.core5.http.io.entity.EntityUtils; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; import eu.europeana.api.set.integration.config.SetIntegrationConfiguration; import eu.europeana.api.set.integration.exception.TechnicalRuntimeException; @@ -13,6 +15,7 @@ /** * @author GrafR + * Currently only used in test classes */ public class EuropeanaOauthClient { @@ -52,11 +55,10 @@ public String getOauthToken(String user) { } HttpConnection connection = new HttpConnection(); - ResponseEntity response; - response = connection.post(oauthUri, oauthParams, "application/x-www-form-urlencoded"); + CloseableHttpResponse response = connection.post(oauthUri, oauthParams, "application/x-www-form-urlencoded", null); - if (HttpStatus.OK == response.getStatusCode()) { - String body = response.getBody(); + if (HttpStatus.SC_OK == response.getCode()) { + String body = EntityUtils.toString(response.getEntity()); JSONObject json = new JSONObject(body); if (json.has(accessToken)) { return "Bearer " + json.getString(accessToken); @@ -66,7 +68,7 @@ public String getOauthToken(String user) { } else { throw new TechnicalRuntimeException("Error occured when calling oath service! " + response); } - } catch (IOException | JSONException e) { + } catch (IOException | JSONException | ParseException e) { throw new TechnicalRuntimeException("Cannot retrieve authentication token!", e); } } diff --git a/set-search-api-client/src/main/java/eu/europeana/set/search/service/SearchApiClient.java b/set-search-api-client/src/main/java/eu/europeana/set/search/service/SearchApiClient.java index 475618b5..d98fab73 100644 --- a/set-search-api-client/src/main/java/eu/europeana/set/search/service/SearchApiClient.java +++ b/set-search-api-client/src/main/java/eu/europeana/set/search/service/SearchApiClient.java @@ -3,7 +3,6 @@ import java.io.IOException; -import org.apache.commons.httpclient.HttpException; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; import eu.europeana.set.definitions.model.BaseWebResource; diff --git a/set-search-api-client/src/main/java/eu/europeana/set/search/service/impl/SearchApiClientImpl.java b/set-search-api-client/src/main/java/eu/europeana/set/search/service/impl/SearchApiClientImpl.java index 7e50be50..b74379e5 100644 --- a/set-search-api-client/src/main/java/eu/europeana/set/search/service/impl/SearchApiClientImpl.java +++ b/set-search-api-client/src/main/java/eu/europeana/set/search/service/impl/SearchApiClientImpl.java @@ -5,6 +5,9 @@ import java.util.ArrayList; import java.util.List; import org.apache.commons.lang3.StringUtils; +import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; +import org.apache.hc.core5.http.ParseException; +import org.apache.hc.core5.http.io.entity.EntityUtils; import org.apache.http.NameValuePair; import org.apache.http.client.utils.URLEncodedUtils; import org.apache.logging.log4j.LogManager; @@ -165,20 +168,20 @@ protected List extractItemsFromSearchResponse(JSONArray valueObject, Str @Override public JSONObject searchItems(String uri, String postBody) throws SearchApiClientException { - String jsonResponse; + CloseableHttpResponse jsonResponse; try { if (postBody != null) { - jsonResponse = createHttpConnection().getURLContentWithBody(uri, postBody); + jsonResponse = createHttpConnection().post(uri, postBody, null, null); } else { - jsonResponse = createHttpConnection().getJsonResponse(uri); + jsonResponse = createHttpConnection().get(uri, null, null); } if (jsonResponse == null) { // HTTP Error Code throw new SearchApiClientException(SearchApiClientException.MESSAGE_INVALID_ISDEFINEDNBY, null); } - return new JSONObject(jsonResponse); - } catch (IOException e) { + return new JSONObject(EntityUtils.toString(jsonResponse.getEntity())); + } catch (IOException | ParseException e) { throw new SearchApiClientException( SearchApiClientException.MESSAGE_CANNOT_ACCESS_API + e.getMessage(), e); } catch (JSONException e) { From be938be794cd481ff6f57ccc328617727767bbc9 Mon Sep 17 00:00:00 2001 From: srishti Date: Thu, 28 Nov 2024 11:40:39 +0100 Subject: [PATCH 10/21] EA-4015 add another condition --- .../set/definitions/model/impl/BaseUserSet.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/set-definitions/src/main/java/eu/europeana/set/definitions/model/impl/BaseUserSet.java b/set-definitions/src/main/java/eu/europeana/set/definitions/model/impl/BaseUserSet.java index a9dc3db0..fb0f8064 100644 --- a/set-definitions/src/main/java/eu/europeana/set/definitions/model/impl/BaseUserSet.java +++ b/set-definitions/src/main/java/eu/europeana/set/definitions/model/impl/BaseUserSet.java @@ -120,13 +120,20 @@ public BaseUserSet() { private Provider provider; /** - * Adding @JsonGetter, as this field is ignored in the json responses. Will build the identifier value from the field "id" + * For deserialization - + * Adding @JsonGetter, as this field is ignored in the json responses. + * Will build the identifier value from the field "id". + * + * Only if identifier is present and has a baseUrl (which means it is fetched via ID, during deserialization) + * In other cases, return the actual identifier value + * * exmaple : 'id' : http://data.europeana.eu/set/xyz , identifier : xyz + * * @return identifier of the set */ @JsonGetter(WebUserSetModelFields.ID) public String getIdentifier() { - if (identifier != null) { + if (identifier != null && StringUtils.contains(identifier, "/")) { return StringUtils.substringAfterLast(identifier, "/"); } return identifier; From 47b5f5634e75449f41e90924de7bb74eb7685cea Mon Sep 17 00:00:00 2001 From: srishti Date: Thu, 28 Nov 2024 11:43:39 +0100 Subject: [PATCH 11/21] EA-4015 aremove sysout --- .../src/main/java/eu/europeana/set/client/BaseUserSetApi.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/set-client/src/main/java/eu/europeana/set/client/BaseUserSetApi.java b/set-client/src/main/java/eu/europeana/set/client/BaseUserSetApi.java index cba00c08..926b5ae7 100644 --- a/set-client/src/main/java/eu/europeana/set/client/BaseUserSetApi.java +++ b/set-client/src/main/java/eu/europeana/set/client/BaseUserSetApi.java @@ -51,8 +51,6 @@ private String getOauthToken(String oauthServiceUri, String oauthRequestParams ) CloseableHttpResponse response = connection.post(oauthServiceUri, oauthRequestParams, "application/x-www-form-urlencoded", null); String body = EntityUtils.toString(response.getEntity()); - - System.out.println(body); if (HttpStatus.SC_OK == response.getCode()) { JSONObject json = new JSONObject(body); if (json.has(ACCESS_TOKEN)) { From b23ea1ea4853fa51c88569d53d0c7d8c7b020672 Mon Sep 17 00:00:00 2001 From: srishti Date: Thu, 28 Nov 2024 12:31:10 +0100 Subject: [PATCH 12/21] EA-4015 make serach return list of sets --- .../java/eu/europeana/set/client/UserSetApiClient.java | 7 ++++--- .../set/client/connection/BaseApiConnection.java | 4 ++-- .../set/client/connection/UserSetApiConnection.java | 8 ++++---- .../eu/europeana/set/client/web/SearchUserSetApi.java | 7 ++++--- 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/set-client/src/main/java/eu/europeana/set/client/UserSetApiClient.java b/set-client/src/main/java/eu/europeana/set/client/UserSetApiClient.java index 84181955..3c1b79de 100644 --- a/set-client/src/main/java/eu/europeana/set/client/UserSetApiClient.java +++ b/set-client/src/main/java/eu/europeana/set/client/UserSetApiClient.java @@ -1,12 +1,13 @@ package eu.europeana.set.client; -import eu.europeana.api.commons.definitions.search.result.impl.ResultsPageImpl; import eu.europeana.set.client.config.ClientConfiguration; import eu.europeana.set.client.exception.SetApiClientException; import eu.europeana.set.client.web.SearchUserSetApi; import eu.europeana.set.client.web.WebUserSetApi; import eu.europeana.set.definitions.model.UserSet; +import java.util.List; + /** * Implementation of client api * @author GordeaS @@ -61,8 +62,8 @@ public UserSet updateUserSet(String identifier, String set, String profile) thro private class SearchUserSetClient implements SearchUserSetApi { @Override - public ResultsPageImpl searchUserSet(String query, String[] qf, - String sort, int page, int pageSize, String facet, int facetLimit, String profile) throws SetApiClientException{ + public List searchUserSet(String query, String[] qf, + String sort, int page, int pageSize, String facet, int facetLimit, String profile) throws SetApiClientException{ return getApiConnection().searchUserSet(query, qf, sort, page, pageSize, facet, facetLimit, profile); } } diff --git a/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java b/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java index c5e0b420..64c5da30 100644 --- a/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java +++ b/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java @@ -151,7 +151,7 @@ private UserSet parseSetApiResponse(CloseableHttpResponse response, List getSearchUserSetResponse(String url, String authorizationHeaderValue) throws SetApiClientException { + protected List getSearchUserSetResponse(String url, String authorizationHeaderValue) throws SetApiClientException { try { LOGGER.trace("Call to UserSet API (SEARCH): {} ", url); CloseableHttpResponse response = getHttpConnection().get(url, "application/json", authorizationHeaderValue); @@ -159,7 +159,7 @@ protected ResultsPageImpl getSearchUserSetResponse(String url if (response.getCode() == HttpStatus.SC_OK) { TypeReference> typeRef = new TypeReference<>() { }; - return mapper.readValue(responseBody, typeRef); + return mapper.readValue(responseBody, typeRef).getItems(); } else { AbstractUserSetApiResponse errorResponse = mapper.readValue(responseBody, AbstractUserSetApiResponse.class); if (LOGGER.isDebugEnabled()) { diff --git a/set-client/src/main/java/eu/europeana/set/client/connection/UserSetApiConnection.java b/set-client/src/main/java/eu/europeana/set/client/connection/UserSetApiConnection.java index 97777c98..22232ccc 100644 --- a/set-client/src/main/java/eu/europeana/set/client/connection/UserSetApiConnection.java +++ b/set-client/src/main/java/eu/europeana/set/client/connection/UserSetApiConnection.java @@ -1,8 +1,8 @@ package eu.europeana.set.client.connection; import java.io.IOException; +import java.util.List; -import eu.europeana.api.commons.definitions.search.result.impl.ResultsPageImpl; import eu.europeana.set.client.exception.SetApiClientException; import eu.europeana.set.definitions.model.UserSet; import org.apache.commons.lang3.StringUtils; @@ -103,9 +103,9 @@ public String deleteUserSet(String identifier) throws SetApiClientException { * @return * @throws IOException */ - public ResultsPageImpl searchUserSet(String query, String[] qf, String sort, int page, - int pageSize, String facet, int facetLimit, - String profile) throws SetApiClientException { + public List searchUserSet(String query, String[] qf, String sort, int page, + int pageSize, String facet, int facetLimit, + String profile) throws SetApiClientException { StringBuilder urlBuilder = getUserSetServiceUri().append(buildSearchUrl(query, qf, sort, page, pageSize, facet, facetLimit, profile)); return getSearchUserSetResponse(urlBuilder.toString(), regularUserAuthorizationValue); diff --git a/set-client/src/main/java/eu/europeana/set/client/web/SearchUserSetApi.java b/set-client/src/main/java/eu/europeana/set/client/web/SearchUserSetApi.java index 0aba31ba..b5464321 100644 --- a/set-client/src/main/java/eu/europeana/set/client/web/SearchUserSetApi.java +++ b/set-client/src/main/java/eu/europeana/set/client/web/SearchUserSetApi.java @@ -1,9 +1,10 @@ package eu.europeana.set.client.web; -import eu.europeana.api.commons.definitions.search.result.impl.ResultsPageImpl; import eu.europeana.set.client.exception.SetApiClientException; import eu.europeana.set.definitions.model.UserSet; +import java.util.List; + /** * Search Client API interface * @author Srishti singh @@ -23,6 +24,6 @@ public interface SearchUserSetApi { * @param profile * @return */ - ResultsPageImpl searchUserSet(String query, String[] qf, String sort, int page, int pageSize, - String facet, int facetLimit, String profile) throws SetApiClientException; + List searchUserSet(String query, String[] qf, String sort, int page, int pageSize, + String facet, int facetLimit, String profile) throws SetApiClientException; } From ac9dc4bf0f5eaaa72fd57ae996863b2f97e11e35 Mon Sep 17 00:00:00 2001 From: srishti Date: Mon, 2 Dec 2024 15:12:22 +0100 Subject: [PATCH 13/21] EA-4015 handle profile --- .../client/connection/BaseApiConnection.java | 23 ++++++++++++++++--- .../connection/UserSetApiConnection.java | 2 +- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java b/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java index 64c5da30..51dc8c46 100644 --- a/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java +++ b/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java @@ -8,8 +8,10 @@ import eu.europeana.set.common.http.HttpConnection; import eu.europeana.set.definitions.model.UserSet; import eu.europeana.set.definitions.model.impl.BaseUserSet; +import eu.europeana.set.definitions.model.vocabulary.ProfileConstants; import eu.europeana.set.definitions.model.vocabulary.WebUserSetFields; import jakarta.ws.rs.core.UriBuilder; +import org.apache.commons.lang3.StringUtils; import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; import org.apache.hc.core5.http.HttpStatus; import org.apache.hc.core5.http.ParseException; @@ -146,20 +148,35 @@ private UserSet parseSetApiResponse(CloseableHttpResponse response, List getSearchUserSetResponse(String url, String authorizationHeaderValue) throws SetApiClientException { + protected List< ? extends UserSet> getSearchUserSetResponse(String url, String authorizationHeaderValue, String profile) throws SetApiClientException { try { LOGGER.trace("Call to UserSet API (SEARCH): {} ", url); CloseableHttpResponse response = getHttpConnection().get(url, "application/json", authorizationHeaderValue); String responseBody = EntityUtils.toString(response.getEntity()); + System.out.println(responseBody); if (response.getCode() == HttpStatus.SC_OK) { - TypeReference> typeRef = new TypeReference<>() { - }; + if (StringUtils.equals(profile, ProfileConstants.VALUE_PARAM_ITEMS)) { + TypeReference> typeRef = new TypeReference<>() {}; + List setIds = mapper.readValue(responseBody, typeRef).getItems(); + List userSets = new ArrayList<>(); + for (String id: setIds) { + BaseUserSet userSet = new BaseUserSet(); + userSet.setIdentifier(StringUtils.substringAfterLast(id, "/")); + userSets.add(userSet); + } + return userSets; + } + TypeReference> typeRef = new TypeReference<>() {}; return mapper.readValue(responseBody, typeRef).getItems(); + } else { AbstractUserSetApiResponse errorResponse = mapper.readValue(responseBody, AbstractUserSetApiResponse.class); if (LOGGER.isDebugEnabled()) { diff --git a/set-client/src/main/java/eu/europeana/set/client/connection/UserSetApiConnection.java b/set-client/src/main/java/eu/europeana/set/client/connection/UserSetApiConnection.java index 22232ccc..452294a6 100644 --- a/set-client/src/main/java/eu/europeana/set/client/connection/UserSetApiConnection.java +++ b/set-client/src/main/java/eu/europeana/set/client/connection/UserSetApiConnection.java @@ -108,6 +108,6 @@ public List searchUserSet(String query, String[] qf, String s String profile) throws SetApiClientException { StringBuilder urlBuilder = getUserSetServiceUri().append(buildSearchUrl(query, qf, sort, page, pageSize, facet, facetLimit, profile)); - return getSearchUserSetResponse(urlBuilder.toString(), regularUserAuthorizationValue); + return getSearchUserSetResponse(urlBuilder.toString(), regularUserAuthorizationValue, profile); } } From fa4667a3807b3a98c9766fb0418a636b7b5915fb Mon Sep 17 00:00:00 2001 From: srishti Date: Mon, 2 Dec 2024 18:15:39 +0100 Subject: [PATCH 14/21] EA-4015 remove sysout --- .../eu/europeana/set/client/connection/BaseApiConnection.java | 1 - 1 file changed, 1 deletion(-) diff --git a/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java b/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java index 51dc8c46..2d131703 100644 --- a/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java +++ b/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java @@ -161,7 +161,6 @@ private UserSet parseSetApiResponse(CloseableHttpResponse response, List> typeRef = new TypeReference<>() {}; From 1ec71f6df84367c162e2546505524579685210b7 Mon Sep 17 00:00:00 2001 From: SristiSingh-eu Date: Tue, 3 Dec 2024 12:18:21 +0100 Subject: [PATCH 15/21] EA-4015 FIX serach api client --- .../set/search/service/impl/SearchApiClientImpl.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/set-search-api-client/src/main/java/eu/europeana/set/search/service/impl/SearchApiClientImpl.java b/set-search-api-client/src/main/java/eu/europeana/set/search/service/impl/SearchApiClientImpl.java index b74379e5..8a8df077 100644 --- a/set-search-api-client/src/main/java/eu/europeana/set/search/service/impl/SearchApiClientImpl.java +++ b/set-search-api-client/src/main/java/eu/europeana/set/search/service/impl/SearchApiClientImpl.java @@ -171,9 +171,9 @@ public JSONObject searchItems(String uri, String postBody) throws SearchApiClien CloseableHttpResponse jsonResponse; try { if (postBody != null) { - jsonResponse = createHttpConnection().post(uri, postBody, null, null); + jsonResponse = createHttpConnection().post(uri, postBody, "application/json", null); } else { - jsonResponse = createHttpConnection().get(uri, null, null); + jsonResponse = createHttpConnection().get(uri, "application/json", null); } if (jsonResponse == null) { // HTTP Error Code From 0f60373f771f1a293404901994ddc9f0412bcf2b Mon Sep 17 00:00:00 2001 From: SristiSingh-eu Date: Tue, 3 Dec 2024 13:31:57 +0100 Subject: [PATCH 16/21] EA-4015 sonar suggestation --- .../europeana/set/client/BaseUserSetApi.java | 16 +++- .../set/client/UserSetApiClient.java | 93 +++++++++---------- .../client/config/ClientConfiguration.java | 10 +- .../client/connection/BaseApiConnection.java | 30 +++--- .../connection/UserSetApiConnection.java | 7 +- .../exception/SetApiClientException.java | 1 - .../set/client/web/WebUserSetApi.java | 1 + 7 files changed, 91 insertions(+), 67 deletions(-) diff --git a/set-client/src/main/java/eu/europeana/set/client/BaseUserSetApi.java b/set-client/src/main/java/eu/europeana/set/client/BaseUserSetApi.java index 926b5ae7..c2e0a7fd 100644 --- a/set-client/src/main/java/eu/europeana/set/client/BaseUserSetApi.java +++ b/set-client/src/main/java/eu/europeana/set/client/BaseUserSetApi.java @@ -24,6 +24,12 @@ public class BaseUserSetApi { private final ClientConfiguration configuration; private UserSetApiConnection apiConnection; + /** + * Creates BaseUserSetApi instance with client configuration + * This allows user to insert property file + * @param configuration + * @throws SetApiClientException + */ protected BaseUserSetApi(ClientConfiguration configuration) throws SetApiClientException { this.configuration = configuration; if (this.configuration.getServiceUri() == null) { @@ -40,21 +46,25 @@ protected BaseUserSetApi(ClientConfiguration configuration) throws SetApiClientE getOauthToken(this.configuration.getOauthServiceUri(), this.configuration.getOauthRequestParams())); } + /** + * Constructor + * @throws SetApiClientException + */ public BaseUserSetApi() throws SetApiClientException { this(new ClientConfiguration()); } private String getOauthToken(String oauthServiceUri, String oauthRequestParams ) throws SetApiClientException{ try { - String ACCESS_TOKEN = "access_token"; + String accessToken = "access_token"; HttpConnection connection = new HttpConnection(); CloseableHttpResponse response = connection.post(oauthServiceUri, oauthRequestParams, "application/x-www-form-urlencoded", null); String body = EntityUtils.toString(response.getEntity()); if (HttpStatus.SC_OK == response.getCode()) { JSONObject json = new JSONObject(body); - if (json.has(ACCESS_TOKEN)) { - return "Bearer " + json.getString(ACCESS_TOKEN); + if (json.has(accessToken)) { + return "Bearer " + json.getString(accessToken); } else { throw new SetApiClientException("Cannot extract authentication token from reponse:" + body); } diff --git a/set-client/src/main/java/eu/europeana/set/client/UserSetApiClient.java b/set-client/src/main/java/eu/europeana/set/client/UserSetApiClient.java index 3c1b79de..7259ec29 100644 --- a/set-client/src/main/java/eu/europeana/set/client/UserSetApiClient.java +++ b/set-client/src/main/java/eu/europeana/set/client/UserSetApiClient.java @@ -10,63 +10,62 @@ /** * Implementation of client api - * @author GordeaS * + * @author GordeaS + * Refractored by Srishti Singh */ public class UserSetApiClient extends BaseUserSetApi { - private final WebUserSetClient webUserSetClient; - private final SearchUserSetClient searchUserSetClient; + private final WebUserSetClient webUserSetClient; + private final SearchUserSetClient searchUserSetClient; public UserSetApiClient(ClientConfiguration configuration) throws SetApiClientException { - super(configuration); - this.webUserSetClient = new WebUserSetClient(); - this.searchUserSetClient = new SearchUserSetClient(); + super(configuration); + this.webUserSetClient = new WebUserSetClient(); + this.searchUserSetClient = new SearchUserSetClient(); } - public WebUserSetApi getWebUserSetApi() { - return webUserSetClient; - } - - public SearchUserSetApi getSearchUserSetApi() { - return searchUserSetClient; - } - - /** - * Web User Set Client class - */ - private class WebUserSetClient implements WebUserSetApi { - @Override - public UserSet createUserSet(String set, String profile) throws SetApiClientException { - return getApiConnection().createUserSet(set, profile); - - } - - @Override - public String deleteUserSet(String identifier) throws SetApiClientException { - return getApiConnection().deleteUserSet(identifier); - } - - @Override - public UserSet getUserSet(String identifier, String profile) throws SetApiClientException { - return getApiConnection().getUserSet(identifier, profile); - } - - @Override - public UserSet updateUserSet(String identifier, String set, String profile) throws SetApiClientException { - return getApiConnection().updateUserSet(identifier, set, profile); - } - } + public WebUserSetApi getWebUserSetApi() { + return webUserSetClient; + } - private class SearchUserSetClient implements SearchUserSetApi { + public SearchUserSetApi getSearchUserSetApi() { + return searchUserSetClient; + } - @Override - public List searchUserSet(String query, String[] qf, - String sort, int page, int pageSize, String facet, int facetLimit, String profile) throws SetApiClientException{ - return getApiConnection().searchUserSet(query, qf, sort, page, pageSize, facet, facetLimit, profile); - } - } + /** + * Web User Set Client class + */ + private class WebUserSetClient implements WebUserSetApi { + @Override + public UserSet createUserSet(String set, String profile) throws SetApiClientException { + return getApiConnection().createUserSet(set, profile); + + } + + @Override + public String deleteUserSet(String identifier) throws SetApiClientException { + return getApiConnection().deleteUserSet(identifier); + } + + @Override + public UserSet getUserSet(String identifier, String profile) throws SetApiClientException { + return getApiConnection().getUserSet(identifier, profile); + } + + @Override + public UserSet updateUserSet(String identifier, String set, String profile) throws SetApiClientException { + return getApiConnection().updateUserSet(identifier, set, profile); + } + } + private class SearchUserSetClient implements SearchUserSetApi { -} \ No newline at end of file + @Override + public List searchUserSet(String query, String[] qf, + String sort, int page, int pageSize, String facet, int facetLimit, String profile) throws SetApiClientException { + return getApiConnection().searchUserSet(query, qf, sort, page, pageSize, facet, facetLimit, profile); + } + } +} diff --git a/set-client/src/main/java/eu/europeana/set/client/config/ClientConfiguration.java b/set-client/src/main/java/eu/europeana/set/client/config/ClientConfiguration.java index 17dca6b6..bd7e788b 100644 --- a/set-client/src/main/java/eu/europeana/set/client/config/ClientConfiguration.java +++ b/set-client/src/main/java/eu/europeana/set/client/config/ClientConfiguration.java @@ -23,16 +23,24 @@ public final class ClientConfiguration { private Properties properties; + /** + * Creates ClientConfiguration instance with set client properties + */ public ClientConfiguration() { loadProperties(SET_CLIENT_PROPERTIES_FILE); } + /** + * CConstructor to inject properties + * @param properties + */ public ClientConfiguration(Properties properties) { this.properties = properties; } private Properties loadProperties(String propertiesFile) { - try { properties = new Properties(); + try { + properties = new Properties(); properties.load(getClass().getResourceAsStream(propertiesFile)); } catch (IOException e) { LOGGER.error("Error loading the properties file {}", propertiesFile); diff --git a/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java b/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java index 2d131703..901c3395 100644 --- a/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java +++ b/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java @@ -30,17 +30,23 @@ public class BaseApiConnection { - Logger LOGGER = LogManager.getLogger(getClass().getName()); + protected static final Logger LOGGER = LogManager.getLogger(BaseApiConnection.class); private static final String DELETE_URL_RESPONSE = ". Returns status code."; private static final String ERROR_MESSAGE = "Set API Client call failed - "; - private HttpConnection httpConnection = new HttpConnection(); - private String apiKey; - private String setServiceUri; + private final HttpConnection httpConnection = new HttpConnection(); + private final String apiKey; + private final String setServiceUri; String regularUserAuthorizationValue; private final ObjectMapper mapper = new ObjectMapper(); + /** + * BaseApiConnection constructor + * @param setServiceUri set api service url + * @param apiKey apikey + * @param regularUserAuthorizationValue auth value + */ public BaseApiConnection(String setServiceUri, String apiKey, String regularUserAuthorizationValue) { this.setServiceUri = setServiceUri; this.apiKey = apiKey; @@ -65,7 +71,7 @@ protected UserSet getUserSetResponse(String url, String authorizationHeaderValue LOGGER.trace("Call to Get UserSet API (GET) : {}.", url); return parseSetApiResponse(getHttpConnection().get(url, null, authorizationHeaderValue), new ArrayList<>(Arrays.asList(HttpStatus.SC_OK, HttpStatus.SC_NOT_MODIFIED))); } catch (IOException | ParseException e) { - throw new SetApiClientException(ERROR_MESSAGE + e.getMessage()); + throw new SetApiClientException(ERROR_MESSAGE + e.getMessage(), HttpStatus.SC_INTERNAL_SERVER_ERROR, e); } } @@ -80,9 +86,9 @@ protected UserSet getUserSetResponse(String url, String authorizationHeaderValue protected UserSet getCreateUserSetResponse(String url, String requestBody, String authorizationHeaderValue) throws SetApiClientException { try { LOGGER.trace("Call to Create UserSet API (POST) : {}.", url); - return parseSetApiResponse(getHttpConnection().post(url, requestBody,null, authorizationHeaderValue), new ArrayList<>(Arrays.asList(HttpStatus.SC_CREATED))); + return parseSetApiResponse(getHttpConnection().post(url, requestBody,"application/json", authorizationHeaderValue), new ArrayList<>(Arrays.asList(HttpStatus.SC_CREATED))); } catch (IOException | ParseException e) { - throw new SetApiClientException(ERROR_MESSAGE + e.getMessage()); + throw new SetApiClientException(ERROR_MESSAGE + e.getMessage(), HttpStatus.SC_INTERNAL_SERVER_ERROR, e); } } @@ -97,10 +103,10 @@ protected UserSet getCreateUserSetResponse(String url, String requestBody, Strin */ protected UserSet getUpdateUserSetResponse(String url, String requestBody, String authorizationHeaderValue) throws SetApiClientException { try { - LOGGER.trace("Call to Update UserSet API : {PUT}.", url); + LOGGER.trace("Call to Update UserSet API : {PUT}. {} ", url); return parseSetApiResponse(getHttpConnection().put(url, requestBody, authorizationHeaderValue), new ArrayList<>(Arrays.asList(HttpStatus.SC_OK))); } catch (IOException | ParseException e) { - throw new SetApiClientException(ERROR_MESSAGE + e.getMessage()); + throw new SetApiClientException(ERROR_MESSAGE + e.getMessage(), HttpStatus.SC_INTERNAL_SERVER_ERROR, e); } } @@ -127,7 +133,7 @@ protected String deleteURL(String url, String authorizationHeaderValue) throws S } return String.valueOf(response.getCode()); } catch (IOException | ParseException e) { - throw new SetApiClientException(ERROR_MESSAGE + e.getMessage()); + throw new SetApiClientException(ERROR_MESSAGE + e.getMessage(), HttpStatus.SC_INTERNAL_SERVER_ERROR, e); } } @@ -184,7 +190,7 @@ private UserSet parseSetApiResponse(CloseableHttpResponse response, List Date: Tue, 3 Dec 2024 14:30:15 +0100 Subject: [PATCH 17/21] EA-4015 added get user set pagination function --- .../set/client/UserSetApiClient.java | 6 ++ .../client/connection/BaseApiConnection.java | 77 ++++++++++++++++++- .../connection/UserSetApiConnection.java | 19 +++++ .../client/model/result/RecordPreview.java | 77 +++++++++++++++++++ .../result/UserSetOperationResponse.java | 23 ------ .../set/client/web/WebUserSetApi.java | 16 +++- 6 files changed, 192 insertions(+), 26 deletions(-) create mode 100644 set-client/src/main/java/eu/europeana/set/client/model/result/RecordPreview.java delete mode 100644 set-client/src/main/java/eu/europeana/set/client/model/result/UserSetOperationResponse.java diff --git a/set-client/src/main/java/eu/europeana/set/client/UserSetApiClient.java b/set-client/src/main/java/eu/europeana/set/client/UserSetApiClient.java index 7259ec29..004588e4 100644 --- a/set-client/src/main/java/eu/europeana/set/client/UserSetApiClient.java +++ b/set-client/src/main/java/eu/europeana/set/client/UserSetApiClient.java @@ -2,6 +2,7 @@ import eu.europeana.set.client.config.ClientConfiguration; import eu.europeana.set.client.exception.SetApiClientException; +import eu.europeana.set.client.model.result.RecordPreview; import eu.europeana.set.client.web.SearchUserSetApi; import eu.europeana.set.client.web.WebUserSetApi; import eu.europeana.set.definitions.model.UserSet; @@ -58,6 +59,11 @@ public UserSet getUserSet(String identifier, String profile) throws SetApiClient public UserSet updateUserSet(String identifier, String set, String profile) throws SetApiClientException { return getApiConnection().updateUserSet(identifier, set, profile); } + + @Override + public List getPaginationUserSet(String identifier, String sort, String sortOrder, int page, int pageSize, String profile) throws SetApiClientException { + return getApiConnection().getPaginationUserSet(identifier, sort, sortOrder, page, pageSize, profile); + } } private class SearchUserSetClient implements SearchUserSetApi { diff --git a/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java b/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java index 901c3395..c3083048 100644 --- a/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java +++ b/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java @@ -5,6 +5,7 @@ import eu.europeana.api.commons.definitions.search.result.impl.ResultsPageImpl; import eu.europeana.set.client.exception.SetApiClientException; import eu.europeana.set.client.model.result.AbstractUserSetApiResponse; +import eu.europeana.set.client.model.result.RecordPreview; import eu.europeana.set.common.http.HttpConnection; import eu.europeana.set.definitions.model.UserSet; import eu.europeana.set.definitions.model.impl.BaseUserSet; @@ -13,6 +14,7 @@ import jakarta.ws.rs.core.UriBuilder; import org.apache.commons.lang3.StringUtils; import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; +import org.apache.hc.core5.http.ContentType; import org.apache.hc.core5.http.HttpStatus; import org.apache.hc.core5.http.ParseException; import org.apache.hc.core5.http.io.entity.EntityUtils; @@ -26,6 +28,7 @@ import java.util.List; import static eu.europeana.api.commons.definitions.vocabulary.CommonApiConstants.*; +import static eu.europeana.set.definitions.model.vocabulary.WebUserSetFields.PARAM_SORT_ORDER; import static eu.europeana.set.definitions.model.vocabulary.WebUserSetFields.SEARCH_PATH; public class BaseApiConnection { @@ -86,7 +89,7 @@ protected UserSet getUserSetResponse(String url, String authorizationHeaderValue protected UserSet getCreateUserSetResponse(String url, String requestBody, String authorizationHeaderValue) throws SetApiClientException { try { LOGGER.trace("Call to Create UserSet API (POST) : {}.", url); - return parseSetApiResponse(getHttpConnection().post(url, requestBody,"application/json", authorizationHeaderValue), new ArrayList<>(Arrays.asList(HttpStatus.SC_CREATED))); + return parseSetApiResponse(getHttpConnection().post(url, requestBody, ContentType.APPLICATION_JSON.getMimeType(), authorizationHeaderValue), new ArrayList<>(Arrays.asList(HttpStatus.SC_CREATED))); } catch (IOException | ParseException e) { throw new SetApiClientException(ERROR_MESSAGE + e.getMessage(), HttpStatus.SC_INTERNAL_SERVER_ERROR, e); } @@ -151,6 +154,48 @@ private UserSet parseSetApiResponse(CloseableHttpResponse response, List getUserSetPaginatedResponse(String url, String authorizationHeaderValue, String profile) throws SetApiClientException { + try { + LOGGER.trace("Call to Get UserSet API (Paginated): {} ", url); + CloseableHttpResponse response = getHttpConnection().get(url, ContentType.APPLICATION_JSON.getMimeType(), authorizationHeaderValue); + String responseBody = EntityUtils.toString(response.getEntity()); + if (response.getCode() == HttpStatus.SC_OK) { + if (StringUtils.equals(profile, ProfileConstants.VALUE_PARAM_ITEMS)) { + TypeReference> typeRef = new TypeReference<>() {}; + List recordIds = mapper.readValue(responseBody, typeRef).getItems(); + List records = new ArrayList<>(); + for (String id: recordIds) { + records.add(new RecordPreview(id)); + } + return records; + } + TypeReference> typeRef = new TypeReference<>() {}; + return mapper.readValue(responseBody, typeRef).getItems(); + + } else { + AbstractUserSetApiResponse errorResponse = mapper.readValue(responseBody, AbstractUserSetApiResponse.class); + if (LOGGER.isDebugEnabled()) { + LOGGER.debug(ERROR_MESSAGE + " {} ", errorResponse.getMessage()); + } + throw new SetApiClientException(ERROR_MESSAGE + errorResponse.getMessage(), response.getCode()); + } + } catch (IOException | ParseException e) { + throw new SetApiClientException(ERROR_MESSAGE + e.getMessage(), HttpStatus.SC_INTERNAL_SERVER_ERROR, e); + } + } + + /** * Fetches the user Set search api response * @@ -162,7 +207,7 @@ private UserSet parseSetApiResponse(CloseableHttpResponse response, List getSearchUserSetResponse(String url, String authorizationHeaderValue, String profile) throws SetApiClientException { + protected List getSearchUserSetResponse(String url, String authorizationHeaderValue, String profile) throws SetApiClientException { try { LOGGER.trace("Call to UserSet API (SEARCH): {} ", url); CloseableHttpResponse response = getHttpConnection().get(url, "application/json", authorizationHeaderValue); @@ -221,6 +266,34 @@ public static URI buildGetUrls(String path, String profile) { return builder.build(); } + /** + * Build paginated user set get url + * @param path + * @param sort + * @param sortOrder + * @param page + * @param pageSize + * @param profile + * @return + */ + public static URI buildPaginatedGetUrls(String path, String sort, + String sortOrder, int page, int pageSize, String profile) { + UriBuilder builder = UriBuilder.newInstance().path(path) + .queryParam(QUERY_PARAM_PAGE, page) + .queryParam(QUERY_PARAM_PAGE_SIZE, pageSize); + + if (sort != null) { + builder.queryParam(QUERY_PARAM_SORT, sort); + } + if (sortOrder != null) { + builder.queryParam(PARAM_SORT_ORDER, sortOrder); + } + if (profile != null) { + builder.queryParam(QUERY_PARAM_PROFILE, profile); + } + return builder.build(); + } + /** * Builds search url with the given params * diff --git a/set-client/src/main/java/eu/europeana/set/client/connection/UserSetApiConnection.java b/set-client/src/main/java/eu/europeana/set/client/connection/UserSetApiConnection.java index 2773ac17..cadce5b9 100644 --- a/set-client/src/main/java/eu/europeana/set/client/connection/UserSetApiConnection.java +++ b/set-client/src/main/java/eu/europeana/set/client/connection/UserSetApiConnection.java @@ -4,6 +4,7 @@ import java.util.List; import eu.europeana.set.client.exception.SetApiClientException; +import eu.europeana.set.client.model.result.RecordPreview; import eu.europeana.set.definitions.model.UserSet; import org.apache.commons.lang3.StringUtils; import eu.europeana.api.commons.definitions.vocabulary.CommonApiConstants; @@ -90,6 +91,24 @@ public String deleteUserSet(String identifier) throws SetApiClientException { return deleteURL(urlBuilder.toString(), regularUserAuthorizationValue); } + /** + * This method fetches the get user set pagination results + * @param identifier + * @param sort + * @param sortOrder + * @param page + * @param pageSize + * @param profile + * @return + * @throws SetApiClientException + */ + public List getPaginationUserSet(String identifier, String sort, + String sortOrder, int page, int pageSize, String profile) throws SetApiClientException { + StringBuilder urlBuilder = getUserSetServiceUri().append( + buildPaginatedGetUrls(identifier + WebUserSetFields.JSON_LD_REST, sort, sortOrder, page, pageSize, profile)); + return getUserSetPaginatedResponse(urlBuilder.toString(), regularUserAuthorizationValue, profile); + + } /** * This method searches usersets for the given queries and params * Example : /set/search?query=visibility:published&pageSize=1000 diff --git a/set-client/src/main/java/eu/europeana/set/client/model/result/RecordPreview.java b/set-client/src/main/java/eu/europeana/set/client/model/result/RecordPreview.java new file mode 100644 index 00000000..5b23c67b --- /dev/null +++ b/set-client/src/main/java/eu/europeana/set/client/model/result/RecordPreview.java @@ -0,0 +1,77 @@ +package eu.europeana.set.client.model.result; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.util.List; +import java.util.Map; + +/** + * Class to parse the Web User Set pagination response. + * For now, we only have the fields that are required by the IIIF presentation APi to generate gallery + * @author Srishti Singh + * @since 3 December 2024 + */ +@JsonIgnoreProperties(ignoreUnknown = true) +public class RecordPreview { + + @JsonProperty("id") + private String id; + + @JsonProperty("dcDescriptionLangAware") + private Map> description; + + @JsonProperty("dcTitleLangAware") + private Map> title; + + @JsonProperty("edmPreview") + private List edmPreview; + + public RecordPreview() { + // for jackson + } + public RecordPreview(String id) { + this.id = id; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Map> getDescription() { + return description; + } + + public void addDescription(String key, List values) { + if (this.description.get(key) != null) { + this.description.get(key).addAll(values); + } else { + this.description.put(key, values); + } + } + + public Map> getTitle() { + return title; + } + + public void addTitle(String key, List values) { + if (this.title.get(key) != null) { + this.title.get(key).addAll(values); + } else { + this.title.put(key, values); + } + } + + + public List getEdmPreview() { + return edmPreview; + } + + public void setEdmPreview(List edmPreview) { + this.edmPreview = edmPreview; + } +} diff --git a/set-client/src/main/java/eu/europeana/set/client/model/result/UserSetOperationResponse.java b/set-client/src/main/java/eu/europeana/set/client/model/result/UserSetOperationResponse.java deleted file mode 100644 index d1e3d40d..00000000 --- a/set-client/src/main/java/eu/europeana/set/client/model/result/UserSetOperationResponse.java +++ /dev/null @@ -1,23 +0,0 @@ -package eu.europeana.set.client.model.result; - -import eu.europeana.set.definitions.model.UserSet; - -public class UserSetOperationResponse extends AbstractUserSetApiResponse{ - - private UserSet set; - - private String json; - - public UserSet getUserSet() { - return set; - } - public void setUserSet(UserSet set) { - this.set = set; - } - public String getJson() { - return json; - } - public void setJson(String json) { - this.json = json; - } -} diff --git a/set-client/src/main/java/eu/europeana/set/client/web/WebUserSetApi.java b/set-client/src/main/java/eu/europeana/set/client/web/WebUserSetApi.java index f1cc1567..ca3e073d 100644 --- a/set-client/src/main/java/eu/europeana/set/client/web/WebUserSetApi.java +++ b/set-client/src/main/java/eu/europeana/set/client/web/WebUserSetApi.java @@ -1,8 +1,11 @@ package eu.europeana.set.client.web; import eu.europeana.set.client.exception.SetApiClientException; +import eu.europeana.set.client.model.result.RecordPreview; import eu.europeana.set.definitions.model.UserSet; +import java.util.List; + /** * Client API interface * @author GordeaS @@ -44,5 +47,16 @@ String deleteUserSet( */ UserSet updateUserSet(String identifier, String requestBody, String profile) throws SetApiClientException; - + /** + * This method fetches the user set pagination request + * @param identifier set id + * @param sort sort fields + * @param sortOrder order of sort + * @param page page number + * @param pageSize size of the page + * @param profile profile requested + * @return + * @throws SetApiClientException + */ + List getPaginationUserSet(String identifier, String sort, String sortOrder, int page, int pageSize, String profile) throws SetApiClientException; } \ No newline at end of file From ee7594bb63e04287991f7730e02ecaf017c6c773 Mon Sep 17 00:00:00 2001 From: srishti Date: Wed, 4 Dec 2024 12:05:12 +0100 Subject: [PATCH 18/21] EA-4015 add has operations --- .../set/client/model/result/RecordPreview.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/set-client/src/main/java/eu/europeana/set/client/model/result/RecordPreview.java b/set-client/src/main/java/eu/europeana/set/client/model/result/RecordPreview.java index 5b23c67b..0ca37084 100644 --- a/set-client/src/main/java/eu/europeana/set/client/model/result/RecordPreview.java +++ b/set-client/src/main/java/eu/europeana/set/client/model/result/RecordPreview.java @@ -54,6 +54,10 @@ public void addDescription(String key, List values) { } } + public boolean hasDescription() { + return description != null && !description.isEmpty(); + } + public Map> getTitle() { return title; } @@ -66,6 +70,9 @@ public void addTitle(String key, List values) { } } + public boolean hasTitle() { + return title != null && !title.isEmpty(); + } public List getEdmPreview() { return edmPreview; @@ -74,4 +81,8 @@ public List getEdmPreview() { public void setEdmPreview(List edmPreview) { this.edmPreview = edmPreview; } + + public boolean hasPreview() { + return edmPreview != null && !edmPreview.isEmpty(); + } } From ca4857b1b50cfc995e6792655529da3edadf9507 Mon Sep 17 00:00:00 2001 From: srishti Date: Tue, 10 Dec 2024 22:47:45 +0100 Subject: [PATCH 19/21] EA-4015 review comments --- set-client/pom.xml | 15 -- .../set/client/UserSetApiClient.java | 4 +- .../client/connection/BaseApiConnection.java | 145 ++++++++++-------- .../connection/UserSetApiConnection.java | 6 +- .../set/client}/json/AgentDeserializer.java | 5 +- .../set/client/json/UserSetDeserializer.java | 51 ++++++ .../set/client/web/SearchUserSetApi.java | 2 +- .../set/client/web/WebUserSetApi.java | 2 +- set-definitions/pom.xml | 14 -- .../definitions/model/BaseWebResource.java | 3 - .../model/agent/impl/BaseAgent.java | 2 - .../definitions/model/impl/BaseUserSet.java | 31 +--- 12 files changed, 141 insertions(+), 139 deletions(-) rename {set-definitions/src/main/java/eu/europeana/set/definitions => set-client/src/main/java/eu/europeana/set/client}/json/AgentDeserializer.java (89%) create mode 100644 set-client/src/main/java/eu/europeana/set/client/json/UserSetDeserializer.java diff --git a/set-client/pom.xml b/set-client/pom.xml index 0d4a7008..8055cad6 100644 --- a/set-client/pom.xml +++ b/set-client/pom.xml @@ -37,13 +37,6 @@ ${version.jettison} - - - jakarta.ws.rs - jakarta.ws.rs-api - 4.0.0 - - @@ -52,14 +45,6 @@ 3.1.2 - - - - - - - - org.junit.jupiter diff --git a/set-client/src/main/java/eu/europeana/set/client/UserSetApiClient.java b/set-client/src/main/java/eu/europeana/set/client/UserSetApiClient.java index 004588e4..5ae1d68b 100644 --- a/set-client/src/main/java/eu/europeana/set/client/UserSetApiClient.java +++ b/set-client/src/main/java/eu/europeana/set/client/UserSetApiClient.java @@ -61,7 +61,7 @@ public UserSet updateUserSet(String identifier, String set, String profile) thro } @Override - public List getPaginationUserSet(String identifier, String sort, String sortOrder, int page, int pageSize, String profile) throws SetApiClientException { + public List getPaginationUserSet(String identifier, String sort, String sortOrder, String page, String pageSize, String profile) throws SetApiClientException { return getApiConnection().getPaginationUserSet(identifier, sort, sortOrder, page, pageSize, profile); } } @@ -70,7 +70,7 @@ private class SearchUserSetClient implements SearchUserSetApi { @Override public List searchUserSet(String query, String[] qf, - String sort, int page, int pageSize, String facet, int facetLimit, String profile) throws SetApiClientException { + String sort, String page, String pageSize, String facet, int facetLimit, String profile) throws SetApiClientException { return getApiConnection().searchUserSet(query, qf, sort, page, pageSize, facet, facetLimit, profile); } } diff --git a/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java b/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java index c3083048..f9b435e4 100644 --- a/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java +++ b/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java @@ -1,28 +1,34 @@ package eu.europeana.set.client.connection; import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.module.SimpleModule; import eu.europeana.api.commons.definitions.search.result.impl.ResultsPageImpl; import eu.europeana.set.client.exception.SetApiClientException; +import eu.europeana.set.client.json.RecordPreviewDeserializer; +import eu.europeana.set.client.json.UserSetDeserializer; import eu.europeana.set.client.model.result.AbstractUserSetApiResponse; import eu.europeana.set.client.model.result.RecordPreview; import eu.europeana.set.common.http.HttpConnection; import eu.europeana.set.definitions.model.UserSet; -import eu.europeana.set.definitions.model.impl.BaseUserSet; +import eu.europeana.set.definitions.model.agent.Agent; +import eu.europeana.set.client.json.AgentDeserializer; import eu.europeana.set.definitions.model.vocabulary.ProfileConstants; import eu.europeana.set.definitions.model.vocabulary.WebUserSetFields; -import jakarta.ws.rs.core.UriBuilder; import org.apache.commons.lang3.StringUtils; import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; import org.apache.hc.core5.http.ContentType; import org.apache.hc.core5.http.HttpStatus; import org.apache.hc.core5.http.ParseException; import org.apache.hc.core5.http.io.entity.EntityUtils; +import org.apache.hc.core5.net.URIBuilder; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import java.io.IOException; import java.net.URI; +import java.net.URISyntaxException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -54,6 +60,16 @@ public BaseApiConnection(String setServiceUri, String apiKey, String regularUser this.setServiceUri = setServiceUri; this.apiKey = apiKey; this.regularUserAuthorizationValue = regularUserAuthorizationValue; + + // set object mapper + SimpleModule module = new SimpleModule(); + mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + module.addDeserializer(Agent.class, new AgentDeserializer()); + module.addDeserializer(UserSet.class, new UserSetDeserializer()); + module.addDeserializer(RecordPreview.class, new RecordPreviewDeserializer()); + + mapper.registerModule(module); + mapper.findAndRegisterModules(); } public HttpConnection getHttpConnection() { @@ -143,7 +159,7 @@ protected String deleteURL(String url, String authorizationHeaderValue) throws S private UserSet parseSetApiResponse(CloseableHttpResponse response, List statusToCheckList) throws SetApiClientException, IOException, ParseException { String responseBody = EntityUtils.toString(response.getEntity()); if (statusToCheckList.contains(response.getCode())) { - return mapper.readValue(responseBody, BaseUserSet.class); + return mapper.readValue(responseBody, UserSet.class); } else { AbstractUserSetApiResponse errorResponse = mapper.readValue(responseBody, AbstractUserSetApiResponse.class); if (LOGGER.isDebugEnabled()) { @@ -171,15 +187,15 @@ protected List getUserSetPaginatedResponse(String url, String aut CloseableHttpResponse response = getHttpConnection().get(url, ContentType.APPLICATION_JSON.getMimeType(), authorizationHeaderValue); String responseBody = EntityUtils.toString(response.getEntity()); if (response.getCode() == HttpStatus.SC_OK) { - if (StringUtils.equals(profile, ProfileConstants.VALUE_PARAM_ITEMS)) { - TypeReference> typeRef = new TypeReference<>() {}; - List recordIds = mapper.readValue(responseBody, typeRef).getItems(); - List records = new ArrayList<>(); - for (String id: recordIds) { - records.add(new RecordPreview(id)); - } - return records; - } +// if (StringUtils.equals(profile, ProfileConstants.VALUE_PARAM_ITEMS)) { +// TypeReference> typeRef = new TypeReference<>() {}; +// List recordIds = mapper.readValue(responseBody, typeRef).getItems(); +// List records = new ArrayList<>(); +// for (String id: recordIds) { +// records.add(new RecordPreview(id)); +// } +// return records; +// } TypeReference> typeRef = new TypeReference<>() {}; return mapper.readValue(responseBody, typeRef).getItems(); @@ -199,9 +215,6 @@ protected List getUserSetPaginatedResponse(String url, String aut /** * Fetches the user Set search api response * - * Check profile for the deserialization - * META - empty page (no items) , ITEMS_META - only set descriptions, not item descriptions , ITEMS - items set as set ids - * ITEMS_META is the default profile value * @param url * @param authorizationHeaderValue * @return @@ -213,18 +226,7 @@ protected List getSearchUserSetResponse(String url, String au CloseableHttpResponse response = getHttpConnection().get(url, "application/json", authorizationHeaderValue); String responseBody = EntityUtils.toString(response.getEntity()); if (response.getCode() == HttpStatus.SC_OK) { - if (StringUtils.equals(profile, ProfileConstants.VALUE_PARAM_ITEMS)) { - TypeReference> typeRef = new TypeReference<>() {}; - List setIds = mapper.readValue(responseBody, typeRef).getItems(); - List userSets = new ArrayList<>(); - for (String id: setIds) { - BaseUserSet userSet = new BaseUserSet(); - userSet.setIdentifier(StringUtils.substringAfterLast(id, "/")); - userSets.add(userSet); - } - return userSets; - } - TypeReference> typeRef = new TypeReference<>() {}; + TypeReference> typeRef = new TypeReference<>() {}; return mapper.readValue(responseBody, typeRef).getItems(); } else { @@ -258,12 +260,16 @@ public StringBuilder getUserSetServiceUri() { * @param profile * @return */ - public static URI buildGetUrls(String path, String profile) { - UriBuilder builder = UriBuilder.newInstance().path(path); - if (profile != null) { - builder.queryParam(QUERY_PARAM_PROFILE, profile); + public static URI buildGetUrls(String path, String profile) throws SetApiClientException{ + try { + URIBuilder builder = new URIBuilder(path); + if (profile != null) { + builder.addParameter(QUERY_PARAM_PROFILE, profile); + } + return builder.build(); + } catch (URISyntaxException e) { + throw new SetApiClientException("Error creating Get url for " +path); } - return builder.build(); } /** @@ -277,21 +283,25 @@ public static URI buildGetUrls(String path, String profile) { * @return */ public static URI buildPaginatedGetUrls(String path, String sort, - String sortOrder, int page, int pageSize, String profile) { - UriBuilder builder = UriBuilder.newInstance().path(path) - .queryParam(QUERY_PARAM_PAGE, page) - .queryParam(QUERY_PARAM_PAGE_SIZE, pageSize); + String sortOrder, String page, String pageSize, String profile) throws SetApiClientException { + try { + URIBuilder builder = new URIBuilder(path) + .addParameter(QUERY_PARAM_PAGE, page) + .addParameter(QUERY_PARAM_PAGE_SIZE, pageSize); - if (sort != null) { - builder.queryParam(QUERY_PARAM_SORT, sort); - } - if (sortOrder != null) { - builder.queryParam(PARAM_SORT_ORDER, sortOrder); - } - if (profile != null) { - builder.queryParam(QUERY_PARAM_PROFILE, profile); + if (sort != null) { + builder.addParameter(QUERY_PARAM_SORT, sort); + } + if (sortOrder != null) { + builder.addParameter(PARAM_SORT_ORDER, sortOrder); + } + if (profile != null) { + builder.addParameter(QUERY_PARAM_PROFILE, profile); + } + return builder.build(); + } catch (URISyntaxException e) { + throw new SetApiClientException("Error creating Paginated Get Urls url for " +path); } - return builder.build(); } /** @@ -307,27 +317,32 @@ public static URI buildPaginatedGetUrls(String path, String sort, * @param profile * @return search url */ - public static URI buildSearchUrl(String query, String[] qf, String sort, int page, - int pageSize, String facet, int facetLimit, - String profile) { - UriBuilder builder = UriBuilder.newInstance().path(SEARCH_PATH) - .queryParam(QUERY_PARAM_QUERY, query) - .queryParam(QUERY_PARAM_PAGE, page) - .queryParam(QUERY_PARAM_PAGE_SIZE, pageSize); - if (qf != null) { - builder.queryParam(QUERY_PARAM_QF, qf); - } - if (sort != null) { - builder.queryParam(QUERY_PARAM_SORT, sort); - } - if (facet != null) { - builder.queryParam(QUERY_PARAM_FACET, facet); - builder.queryParam("facet.limit", facetLimit); - } - if (profile != null) { - builder.queryParam(QUERY_PARAM_PROFILE, profile); + public static URI buildSearchUrl(String query, String[] qf, String sort, String page, + String pageSize, String facet, int facetLimit, + String profile) throws SetApiClientException { + try { + URIBuilder builder = new URIBuilder(SEARCH_PATH) + .addParameter(QUERY_PARAM_QUERY, query) + .addParameter(QUERY_PARAM_PAGE, page) + .addParameter(QUERY_PARAM_PAGE_SIZE, pageSize); + + if (qf != null) { + builder.addParameter(QUERY_PARAM_QF, String.valueOf(qf)); + } + if (sort != null) { + builder.addParameter(QUERY_PARAM_SORT, sort); + } + if (facet != null) { + builder.addParameter(QUERY_PARAM_FACET, facet); + builder.addParameter("facet.limit", String.valueOf(facetLimit)); + } + if (profile != null) { + builder.addParameter(QUERY_PARAM_PROFILE, profile); + } + return builder.build(); + } catch (URISyntaxException e) { + throw new SetApiClientException("Error creating Search Urls url for " +SEARCH_PATH); } - return builder.build(); } public String getApiKey() { diff --git a/set-client/src/main/java/eu/europeana/set/client/connection/UserSetApiConnection.java b/set-client/src/main/java/eu/europeana/set/client/connection/UserSetApiConnection.java index cadce5b9..8dc2382d 100644 --- a/set-client/src/main/java/eu/europeana/set/client/connection/UserSetApiConnection.java +++ b/set-client/src/main/java/eu/europeana/set/client/connection/UserSetApiConnection.java @@ -103,7 +103,7 @@ public String deleteUserSet(String identifier) throws SetApiClientException { * @throws SetApiClientException */ public List getPaginationUserSet(String identifier, String sort, - String sortOrder, int page, int pageSize, String profile) throws SetApiClientException { + String sortOrder, String page, String pageSize, String profile) throws SetApiClientException { StringBuilder urlBuilder = getUserSetServiceUri().append( buildPaginatedGetUrls(identifier + WebUserSetFields.JSON_LD_REST, sort, sortOrder, page, pageSize, profile)); return getUserSetPaginatedResponse(urlBuilder.toString(), regularUserAuthorizationValue, profile); @@ -123,8 +123,8 @@ public List getPaginationUserSet(String identifier, String sort, * @return * @throws IOException */ - public List searchUserSet(String query, String[] qf, String sort, int page, - int pageSize, String facet, int facetLimit, + public List searchUserSet(String query, String[] qf, String sort, String page, + String pageSize, String facet, int facetLimit, String profile) throws SetApiClientException { StringBuilder urlBuilder = getUserSetServiceUri().append(buildSearchUrl(query, qf, sort, page, pageSize, facet, facetLimit, profile)); diff --git a/set-definitions/src/main/java/eu/europeana/set/definitions/json/AgentDeserializer.java b/set-client/src/main/java/eu/europeana/set/client/json/AgentDeserializer.java similarity index 89% rename from set-definitions/src/main/java/eu/europeana/set/definitions/json/AgentDeserializer.java rename to set-client/src/main/java/eu/europeana/set/client/json/AgentDeserializer.java index d01e9025..688c9238 100644 --- a/set-definitions/src/main/java/eu/europeana/set/definitions/json/AgentDeserializer.java +++ b/set-client/src/main/java/eu/europeana/set/client/json/AgentDeserializer.java @@ -1,6 +1,5 @@ -package eu.europeana.set.definitions.json; +package eu.europeana.set.client.json; -import com.fasterxml.jackson.core.JacksonException; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.JsonDeserializer; @@ -17,7 +16,7 @@ public class AgentDeserializer extends JsonDeserializer { @Override - public Agent deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JacksonException { + public Agent deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { ObjectMapper mapper = (ObjectMapper) jsonParser.getCodec(); ObjectNode root = mapper.readTree(jsonParser); diff --git a/set-client/src/main/java/eu/europeana/set/client/json/UserSetDeserializer.java b/set-client/src/main/java/eu/europeana/set/client/json/UserSetDeserializer.java new file mode 100644 index 00000000..274db2be --- /dev/null +++ b/set-client/src/main/java/eu/europeana/set/client/json/UserSetDeserializer.java @@ -0,0 +1,51 @@ +package eu.europeana.set.client.json; + +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonDeserializer; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import eu.europeana.set.definitions.model.UserSet; +import eu.europeana.set.definitions.model.impl.BaseUserSet; +import org.apache.commons.lang3.StringUtils; + +import java.io.IOException; + +/** + * Used for Deserializing UserSet. + *

+ * Field - {@link UserSet#getIdentifier()} + * As Identifier is always null, we fetch the value from the 'id' field in the response + * exmaple : 'id' : http://data.europeana.eu/set/xyz , identifier : xyz + * + * @author srishti singh + * @since 9 December 2024 + */ +public class UserSetDeserializer extends JsonDeserializer { + + @Override + public UserSet deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { + ObjectMapper mapper = (ObjectMapper) jsonParser.getCodec(); + if (mapper.readTree(jsonParser).isObject()) { + ObjectNode root = mapper.readTree(jsonParser); + BaseUserSet set = mapper.readValue(root.toString(), BaseUserSet.class); + if (root.has("id")) { + String id = root.get("id").asText(); + String identifier = set.getIdentifier(); + if (identifier == null) { + set.setIdentifier(StringUtils.substringAfterLast(id, "/")); + } + } + return set; + } else { + // there are profiles where only id value is returned instead of UserSet object + // hence we will form a userset object with 'id' value and return that + JsonNode root = mapper.readTree(jsonParser); + String id = mapper.readValue(root.toString(), String.class); + UserSet set = new BaseUserSet(); + set.setIdentifier(StringUtils.substringAfterLast(id, "/")); + return set; + } + } +} diff --git a/set-client/src/main/java/eu/europeana/set/client/web/SearchUserSetApi.java b/set-client/src/main/java/eu/europeana/set/client/web/SearchUserSetApi.java index b5464321..cd8ebd54 100644 --- a/set-client/src/main/java/eu/europeana/set/client/web/SearchUserSetApi.java +++ b/set-client/src/main/java/eu/europeana/set/client/web/SearchUserSetApi.java @@ -24,6 +24,6 @@ public interface SearchUserSetApi { * @param profile * @return */ - List searchUserSet(String query, String[] qf, String sort, int page, int pageSize, + List searchUserSet(String query, String[] qf, String sort, String page, String pageSize, String facet, int facetLimit, String profile) throws SetApiClientException; } diff --git a/set-client/src/main/java/eu/europeana/set/client/web/WebUserSetApi.java b/set-client/src/main/java/eu/europeana/set/client/web/WebUserSetApi.java index ca3e073d..56bb6a63 100644 --- a/set-client/src/main/java/eu/europeana/set/client/web/WebUserSetApi.java +++ b/set-client/src/main/java/eu/europeana/set/client/web/WebUserSetApi.java @@ -58,5 +58,5 @@ String deleteUserSet( * @return * @throws SetApiClientException */ - List getPaginationUserSet(String identifier, String sort, String sortOrder, int page, int pageSize, String profile) throws SetApiClientException; + List getPaginationUserSet(String identifier, String sort, String sortOrder, String page, String pageSize, String profile) throws SetApiClientException; } \ No newline at end of file diff --git a/set-definitions/pom.xml b/set-definitions/pom.xml index 728162b8..b159f6b3 100644 --- a/set-definitions/pom.xml +++ b/set-definitions/pom.xml @@ -29,19 +29,5 @@ commons-definitions ${version.commonsApi} - - - - com.fasterxml.jackson.core - jackson-annotations - ${jackson.version} - - - - com.fasterxml.jackson.core - jackson-databind - ${jackson.version} - diff --git a/set-definitions/src/main/java/eu/europeana/set/definitions/model/BaseWebResource.java b/set-definitions/src/main/java/eu/europeana/set/definitions/model/BaseWebResource.java index 19d9b0cd..932b8eba 100644 --- a/set-definitions/src/main/java/eu/europeana/set/definitions/model/BaseWebResource.java +++ b/set-definitions/src/main/java/eu/europeana/set/definitions/model/BaseWebResource.java @@ -1,10 +1,7 @@ package eu.europeana.set.definitions.model; -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; - import java.util.Objects; -@JsonIgnoreProperties(ignoreUnknown = true) public class BaseWebResource { public static final String TYPE = "WebResource"; diff --git a/set-definitions/src/main/java/eu/europeana/set/definitions/model/agent/impl/BaseAgent.java b/set-definitions/src/main/java/eu/europeana/set/definitions/model/agent/impl/BaseAgent.java index 829f8b7d..29ebba22 100644 --- a/set-definitions/src/main/java/eu/europeana/set/definitions/model/agent/impl/BaseAgent.java +++ b/set-definitions/src/main/java/eu/europeana/set/definitions/model/agent/impl/BaseAgent.java @@ -1,10 +1,8 @@ package eu.europeana.set.definitions.model.agent.impl; -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import eu.europeana.set.definitions.model.agent.Agent; import eu.europeana.set.definitions.model.vocabulary.AgentTypes; -@JsonIgnoreProperties(ignoreUnknown = true) public abstract class BaseAgent implements Agent { private String httpUrl; diff --git a/set-definitions/src/main/java/eu/europeana/set/definitions/model/impl/BaseUserSet.java b/set-definitions/src/main/java/eu/europeana/set/definitions/model/impl/BaseUserSet.java index 05b5951d..12d51e94 100644 --- a/set-definitions/src/main/java/eu/europeana/set/definitions/model/impl/BaseUserSet.java +++ b/set-definitions/src/main/java/eu/europeana/set/definitions/model/impl/BaseUserSet.java @@ -4,26 +4,18 @@ import java.util.List; import java.util.Map; -import com.fasterxml.jackson.annotation.JsonGetter; -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.fasterxml.jackson.databind.annotation.JsonDeserialize; -import eu.europeana.set.definitions.json.AgentDeserializer; import eu.europeana.set.definitions.model.BaseWebResource; import eu.europeana.set.definitions.model.UserSet; import eu.europeana.set.definitions.model.agent.Agent; import eu.europeana.set.definitions.model.vocabulary.UserSetTypes; import eu.europeana.set.definitions.model.vocabulary.VisibilityTypes; import eu.europeana.set.definitions.model.vocabulary.WebUserSetModelFields; -import org.apache.commons.lang3.StringUtils; /** * Europeana Sets API Specification * - * @JsonIgnoreProperties - to ignore "@context" while parsing the results in the Client side code - * - * @author GrafR Modified by Srishti Singh 2-2-2021 + * @author GrafR */ -@JsonIgnoreProperties(ignoreUnknown = true) public class BaseUserSet extends BasePageInfo implements UserSet { // EDM Collection Profile @@ -82,11 +74,6 @@ public BaseUserSet() { // Provenance information - /** - * A reference to the user agent that gathers objects together following - * implicit or explicit criteria or accrual policy. - */ - @JsonDeserialize(using = AgentDeserializer.class) private Agent creator; /** @@ -119,23 +106,7 @@ public BaseUserSet() { private Provider provider; - /** - * For deserialization - - * Adding @JsonGetter, as this field is ignored in the json responses. - * Will build the identifier value from the field "id". - * - * Only if identifier is present and has a baseUrl (which means it is fetched via ID, during deserialization) - * In other cases, return the actual identifier value - * - * exmaple : 'id' : http://data.europeana.eu/set/xyz , identifier : xyz - * - * @return identifier of the set - */ - @JsonGetter(WebUserSetModelFields.ID) public String getIdentifier() { - if (identifier != null && StringUtils.contains(identifier, "/")) { - return StringUtils.substringAfterLast(identifier, "/"); - } return identifier; } From a6b9ca08769f512bab6a5cb9943a5a04e3cd175f Mon Sep 17 00:00:00 2001 From: SristiSingh-eu Date: Wed, 11 Dec 2024 11:53:22 +0100 Subject: [PATCH 20/21] EA-4015 fix desrialisation --- .../client/connection/BaseApiConnection.java | 23 ++++++++-------- .../set/client/json/UserSetDeserializer.java | 26 ++++++------------- 2 files changed, 20 insertions(+), 29 deletions(-) diff --git a/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java b/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java index f9b435e4..a1187332 100644 --- a/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java +++ b/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java @@ -6,7 +6,6 @@ import com.fasterxml.jackson.databind.module.SimpleModule; import eu.europeana.api.commons.definitions.search.result.impl.ResultsPageImpl; import eu.europeana.set.client.exception.SetApiClientException; -import eu.europeana.set.client.json.RecordPreviewDeserializer; import eu.europeana.set.client.json.UserSetDeserializer; import eu.europeana.set.client.model.result.AbstractUserSetApiResponse; import eu.europeana.set.client.model.result.RecordPreview; @@ -14,6 +13,7 @@ import eu.europeana.set.definitions.model.UserSet; import eu.europeana.set.definitions.model.agent.Agent; import eu.europeana.set.client.json.AgentDeserializer; +import eu.europeana.set.definitions.model.impl.BaseUserSet; import eu.europeana.set.definitions.model.vocabulary.ProfileConstants; import eu.europeana.set.definitions.model.vocabulary.WebUserSetFields; import org.apache.commons.lang3.StringUtils; @@ -66,7 +66,6 @@ public BaseApiConnection(String setServiceUri, String apiKey, String regularUser mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); module.addDeserializer(Agent.class, new AgentDeserializer()); module.addDeserializer(UserSet.class, new UserSetDeserializer()); - module.addDeserializer(RecordPreview.class, new RecordPreviewDeserializer()); mapper.registerModule(module); mapper.findAndRegisterModules(); @@ -187,15 +186,6 @@ protected List getUserSetPaginatedResponse(String url, String aut CloseableHttpResponse response = getHttpConnection().get(url, ContentType.APPLICATION_JSON.getMimeType(), authorizationHeaderValue); String responseBody = EntityUtils.toString(response.getEntity()); if (response.getCode() == HttpStatus.SC_OK) { -// if (StringUtils.equals(profile, ProfileConstants.VALUE_PARAM_ITEMS)) { -// TypeReference> typeRef = new TypeReference<>() {}; -// List recordIds = mapper.readValue(responseBody, typeRef).getItems(); -// List records = new ArrayList<>(); -// for (String id: recordIds) { -// records.add(new RecordPreview(id)); -// } -// return records; -// } TypeReference> typeRef = new TypeReference<>() {}; return mapper.readValue(responseBody, typeRef).getItems(); @@ -226,6 +216,17 @@ protected List getSearchUserSetResponse(String url, String au CloseableHttpResponse response = getHttpConnection().get(url, "application/json", authorizationHeaderValue); String responseBody = EntityUtils.toString(response.getEntity()); if (response.getCode() == HttpStatus.SC_OK) { + if (StringUtils.equals(profile, ProfileConstants.VALUE_PARAM_ITEMS)) { + TypeReference> typeRef = new TypeReference<>() {}; + List items = mapper.readValue(responseBody, typeRef).getItems(); + List sets = new ArrayList<>(); + for (String id: items) { + UserSet set = new BaseUserSet(); + set.setIdentifier(StringUtils.substringAfterLast(id, "/")); + sets.add(set); + } + return sets; + } TypeReference> typeRef = new TypeReference<>() {}; return mapper.readValue(responseBody, typeRef).getItems(); diff --git a/set-client/src/main/java/eu/europeana/set/client/json/UserSetDeserializer.java b/set-client/src/main/java/eu/europeana/set/client/json/UserSetDeserializer.java index 274db2be..d66662e9 100644 --- a/set-client/src/main/java/eu/europeana/set/client/json/UserSetDeserializer.java +++ b/set-client/src/main/java/eu/europeana/set/client/json/UserSetDeserializer.java @@ -27,25 +27,15 @@ public class UserSetDeserializer extends JsonDeserializer { @Override public UserSet deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { ObjectMapper mapper = (ObjectMapper) jsonParser.getCodec(); - if (mapper.readTree(jsonParser).isObject()) { - ObjectNode root = mapper.readTree(jsonParser); - BaseUserSet set = mapper.readValue(root.toString(), BaseUserSet.class); - if (root.has("id")) { - String id = root.get("id").asText(); - String identifier = set.getIdentifier(); - if (identifier == null) { - set.setIdentifier(StringUtils.substringAfterLast(id, "/")); - } + ObjectNode root = mapper.readTree(jsonParser); + BaseUserSet set = mapper.readValue(root.toString(), BaseUserSet.class); + if (root.has("id")) { + String id = root.get("id").asText(); + String identifier = set.getIdentifier(); + if (identifier == null) { + set.setIdentifier(StringUtils.substringAfterLast(id, "/")); } - return set; - } else { - // there are profiles where only id value is returned instead of UserSet object - // hence we will form a userset object with 'id' value and return that - JsonNode root = mapper.readTree(jsonParser); - String id = mapper.readValue(root.toString(), String.class); - UserSet set = new BaseUserSet(); - set.setIdentifier(StringUtils.substringAfterLast(id, "/")); - return set; } + return set; } } From aa169ed051a23667f1db10ddd0261db12e442e57 Mon Sep 17 00:00:00 2001 From: srishti Date: Thu, 12 Dec 2024 10:37:08 +0100 Subject: [PATCH 21/21] EA-4015 sonar issues --- .../set/client/connection/BaseApiConnection.java | 8 ++++---- .../client/integration/web/BaseWebUserSetProtocol.java | 2 +- .../client/integration/web/WebUserSetProtocolTest.java | 2 +- .../java/eu/europeana/set/common/http/HttpConnection.java | 3 +-- 4 files changed, 7 insertions(+), 8 deletions(-) diff --git a/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java b/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java index a1187332..31140588 100644 --- a/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java +++ b/set-client/src/main/java/eu/europeana/set/client/connection/BaseApiConnection.java @@ -219,7 +219,7 @@ protected List getSearchUserSetResponse(String url, String au if (StringUtils.equals(profile, ProfileConstants.VALUE_PARAM_ITEMS)) { TypeReference> typeRef = new TypeReference<>() {}; List items = mapper.readValue(responseBody, typeRef).getItems(); - List sets = new ArrayList<>(); + List sets = new ArrayList<>(items.size()); for (String id: items) { UserSet set = new BaseUserSet(); set.setIdentifier(StringUtils.substringAfterLast(id, "/")); @@ -269,7 +269,7 @@ public static URI buildGetUrls(String path, String profile) throws SetApiClientE } return builder.build(); } catch (URISyntaxException e) { - throw new SetApiClientException("Error creating Get url for " +path); + throw new SetApiClientException("Error creating Get url - " + e.getMessage(), HttpStatus.SC_INTERNAL_SERVER_ERROR, e); } } @@ -301,7 +301,7 @@ public static URI buildPaginatedGetUrls(String path, String sort, } return builder.build(); } catch (URISyntaxException e) { - throw new SetApiClientException("Error creating Paginated Get Urls url for " +path); + throw new SetApiClientException("Error creating Paginated Get Urls " +e.getMessage(), HttpStatus.SC_INTERNAL_SERVER_ERROR, e); } } @@ -342,7 +342,7 @@ public static URI buildSearchUrl(String query, String[] qf, String sort, String } return builder.build(); } catch (URISyntaxException e) { - throw new SetApiClientException("Error creating Search Urls url for " +SEARCH_PATH); + throw new SetApiClientException("Error creating Search Urls ", HttpStatus.SC_INTERNAL_SERVER_ERROR, e); } } diff --git a/set-client/src/test/java/eu/europeana/set/client/integration/web/BaseWebUserSetProtocol.java b/set-client/src/test/java/eu/europeana/set/client/integration/web/BaseWebUserSetProtocol.java index 68899a3d..32475a8d 100644 --- a/set-client/src/test/java/eu/europeana/set/client/integration/web/BaseWebUserSetProtocol.java +++ b/set-client/src/test/java/eu/europeana/set/client/integration/web/BaseWebUserSetProtocol.java @@ -63,7 +63,7 @@ protected void deleteUserSet(UserSet set) throws SetApiClientException { protected void deleteUserSet(String identifier) throws SetApiClientException { String re = apiClient.getWebUserSetApi().deleteUserSet(identifier); - assertEquals(HttpStatus.SC_OK, re); + assertEquals(String.valueOf(HttpStatus.SC_OK), re); log.trace("User set deleted: /" + identifier); } diff --git a/set-client/src/test/java/eu/europeana/set/client/integration/web/WebUserSetProtocolTest.java b/set-client/src/test/java/eu/europeana/set/client/integration/web/WebUserSetProtocolTest.java index 323ddd3d..de290fd5 100644 --- a/set-client/src/test/java/eu/europeana/set/client/integration/web/WebUserSetProtocolTest.java +++ b/set-client/src/test/java/eu/europeana/set/client/integration/web/WebUserSetProtocolTest.java @@ -65,7 +65,7 @@ public void deleteUserSet() throws IOException, SetApiClientException { assertNotNull(testSetId); // delete user set by identifier URL String deleteResponse = apiClient.getWebUserSetApi().deleteUserSet(testSetId); - assertEquals(HttpStatus.SC_NO_CONTENT, deleteResponse); + assertEquals(String.valueOf(HttpStatus.SC_NO_CONTENT), deleteResponse); } /** diff --git a/set-common/src/main/java/eu/europeana/set/common/http/HttpConnection.java b/set-common/src/main/java/eu/europeana/set/common/http/HttpConnection.java index 1bb95f94..359d8ad0 100644 --- a/set-common/src/main/java/eu/europeana/set/common/http/HttpConnection.java +++ b/set-common/src/main/java/eu/europeana/set/common/http/HttpConnection.java @@ -103,10 +103,9 @@ private CloseableHttpResponse executeHttpClient(T } - private T addHeaders(T url, String headerName, String headerValue) { + private void addHeaders(T url, String headerName, String headerValue) { if (StringUtils.isNotBlank(headerValue)) { url.setHeader(headerName, headerValue); } - return url; } }