Skip to content

Commit

Permalink
EA-4015 fix desrialisation
Browse files Browse the repository at this point in the history
  • Loading branch information
SrishtiSingh-eu committed Dec 11, 2024
1 parent ca4857b commit a6b9ca0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
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.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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -187,15 +186,6 @@ 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;
// }
TypeReference<ResultsPageImpl<RecordPreview>> typeRef = new TypeReference<>() {};
return mapper.readValue(responseBody, typeRef).getItems();

Expand Down Expand Up @@ -226,6 +216,17 @@ 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> items = mapper.readValue(responseBody, typeRef).getItems();
List<UserSet> sets = new ArrayList<>();
for (String id: items) {
UserSet set = new BaseUserSet();
set.setIdentifier(StringUtils.substringAfterLast(id, "/"));
sets.add(set);
}
return sets;
}
TypeReference<ResultsPageImpl<UserSet>> typeRef = new TypeReference<>() {};
return mapper.readValue(responseBody, typeRef).getItems();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,15 @@ 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, "/"));
}
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;
}
}

0 comments on commit a6b9ca0

Please sign in to comment.