Skip to content

Commit

Permalink
EA-4015 sonar suggestation
Browse files Browse the repository at this point in the history
  • Loading branch information
SrishtiSingh-eu committed Dec 3, 2024
1 parent 1ec71f6 commit 0f60373
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<? extends UserSet> 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 {

}
@Override
public List<? extends UserSet> 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}

Expand All @@ -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);
}
}

Expand All @@ -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);
}
}

Expand All @@ -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);
}
}

Expand Down Expand Up @@ -184,7 +190,7 @@ private UserSet parseSetApiResponse(CloseableHttpResponse response, List<Integer
throw new SetApiClientException(ERROR_MESSAGE + errorResponse.getMessage(), 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);
}
}

Expand Down Expand Up @@ -226,7 +232,7 @@ public static URI buildGetUrls(String path, String profile) {
* @param facet
* @param facetLimit
* @param profile
* @return
* @return search url
*/
public static URI buildSearchUrl(String query, String[] qf, String sort, int page,
int pageSize, String facet, int facetLimit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public UserSetApiConnection(String setServiceUri, String apiKey, String regularU
* http://localhost:8080/set/?profile=minimal
*
* @param set The UserSet body
* @param profile
* @param profile profile requested
* @return response entity that comprises response body, headers and status code.
* @throws IOException
*/
Expand All @@ -44,9 +44,10 @@ public UserSet createUserSet(String set, String profile) throws SetApiClientExce
* This method retrieves UserSet object. Example HTTP request for tag object:
* http://localhost:8080/set/{identifier}.jsonld?profile=minimal where identifier is: 496
*
* @param identifier
* @param profile
* @param identifier set id
* @param profile profile requested
* @throws IOException
* @return userset
*/
public UserSet getUserSet(String identifier, String profile) throws SetApiClientException {
StringBuilder urlBuilder = getUserSetServiceUri().append(buildGetUrls(identifier + WebUserSetFields.JSON_LD_REST, profile));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ public class SetApiClientException extends Exception {
private static final long serialVersionUID = 8281933808897246375L;
private final int remoteStatusCode;


/**
* Constructor for exception to indicate that an error occurred during invocation of the remote
* service or parsing of service response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,5 @@ String deleteUserSet(
*/
UserSet updateUserSet(String identifier, String requestBody, String profile) throws SetApiClientException;


}

0 comments on commit 0f60373

Please sign in to comment.