Skip to content

Commit

Permalink
EA-4015 review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
SrishtiSingh-eu committed Dec 10, 2024
1 parent ee7594b commit ca4857b
Show file tree
Hide file tree
Showing 12 changed files with 141 additions and 139 deletions.
15 changes: 0 additions & 15 deletions set-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,6 @@
<version>${version.jettison}</version>
</dependency>

<!-- https://mvnrepository.com/artifact/jakarta.ws.rs/jakarta.ws.rs-api -->
<dependency>
<groupId>jakarta.ws.rs</groupId>
<artifactId>jakarta.ws.rs-api</artifactId>
<version>4.0.0</version>
</dependency>

<!-- To fix : "Provider for jakarta.ws.rs.ext.RuntimeDelegate cannot be found error"
This issue is tagged with version 2.30 but it doesn't look like it was ever fixed in newer versions as well-->
<dependency>
Expand All @@ -52,14 +45,6 @@
<version>3.1.2</version>
</dependency>

<!-- <dependency>-->
<!-- <groupId>org.springframework</groupId>-->
<!-- <artifactId>spring-webmvc</artifactId>-->
<!-- &lt;!&ndash;-->
<!-- <version>${spring-framework.version}</version>-->
<!-- &ndash;&gt;-->
<!-- </dependency>-->

<!-- test dependencies -->
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public UserSet updateUserSet(String identifier, String set, String profile) thro
}

@Override
public List<RecordPreview> getPaginationUserSet(String identifier, String sort, String sortOrder, int page, int pageSize, String profile) throws SetApiClientException {
public List<RecordPreview> getPaginationUserSet(String identifier, String sort, String sortOrder, String page, String pageSize, String profile) throws SetApiClientException {
return getApiConnection().getPaginationUserSet(identifier, sort, sortOrder, page, pageSize, profile);
}
}
Expand All @@ -70,7 +70,7 @@ 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 {
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);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -143,7 +159,7 @@ protected String deleteURL(String url, String authorizationHeaderValue) throws S
private UserSet parseSetApiResponse(CloseableHttpResponse response, List<Integer> 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()) {
Expand Down Expand Up @@ -171,15 +187,15 @@ protected List<RecordPreview> 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<ResultsPageImpl<String>> typeRef = new TypeReference<>() {};
List<String> recordIds = mapper.readValue(responseBody, typeRef).getItems();
List<RecordPreview> records = new ArrayList<>();
for (String id: recordIds) {
records.add(new RecordPreview(id));
}
return records;
}
// if (StringUtils.equals(profile, ProfileConstants.VALUE_PARAM_ITEMS)) {
// TypeReference<ResultsPageImpl<String>> typeRef = new TypeReference<>() {};
// List<String> recordIds = mapper.readValue(responseBody, typeRef).getItems();
// List<RecordPreview> records = new ArrayList<>();
// for (String id: recordIds) {
// records.add(new RecordPreview(id));
// }
// return records;
// }
TypeReference<ResultsPageImpl<RecordPreview>> typeRef = new TypeReference<>() {};
return mapper.readValue(responseBody, typeRef).getItems();

Expand All @@ -199,9 +215,6 @@ protected List<RecordPreview> 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
Expand All @@ -213,18 +226,7 @@ protected List<? extends UserSet> 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<ResultsPageImpl<String>> typeRef = new TypeReference<>() {};
List<String> setIds = mapper.readValue(responseBody, typeRef).getItems();
List<UserSet> userSets = new ArrayList<>();
for (String id: setIds) {
BaseUserSet userSet = new BaseUserSet();
userSet.setIdentifier(StringUtils.substringAfterLast(id, "/"));
userSets.add(userSet);
}
return userSets;
}
TypeReference<ResultsPageImpl<BaseUserSet>> typeRef = new TypeReference<>() {};
TypeReference<ResultsPageImpl<UserSet>> typeRef = new TypeReference<>() {};
return mapper.readValue(responseBody, typeRef).getItems();

} else {
Expand Down Expand Up @@ -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();
}

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

/**
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public String deleteUserSet(String identifier) throws SetApiClientException {
* @throws SetApiClientException
*/
public List<RecordPreview> 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);
Expand All @@ -123,8 +123,8 @@ public List<RecordPreview> getPaginationUserSet(String identifier, String sort,
* @return
* @throws IOException
*/
public List<? extends UserSet> searchUserSet(String query, String[] qf, String sort, int page,
int pageSize, String facet, int facetLimit,
public List<? extends UserSet> 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));
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,7 +16,7 @@
public class AgentDeserializer extends JsonDeserializer<Agent> {

@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);

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
* <p>
* 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<UserSet> {

@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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ public interface SearchUserSetApi {
* @param profile
* @return
*/
List<? extends UserSet> searchUserSet(String query, String[] qf, String sort, int page, int pageSize,
List<? extends UserSet> searchUserSet(String query, String[] qf, String sort, String page, String pageSize,
String facet, int facetLimit, String profile) throws SetApiClientException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,5 @@ String deleteUserSet(
* @return
* @throws SetApiClientException
*/
List<RecordPreview> getPaginationUserSet(String identifier, String sort, String sortOrder, int page, int pageSize, String profile) throws SetApiClientException;
List<RecordPreview> getPaginationUserSet(String identifier, String sort, String sortOrder, String page, String pageSize, String profile) throws SetApiClientException;
}
Loading

0 comments on commit ca4857b

Please sign in to comment.