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