Improve json parsing

This commit is contained in:
George Kalampokis 2022-01-18 17:58:47 +02:00
parent 3204547527
commit b8fffd705e
19 changed files with 63 additions and 77 deletions

View File

@ -156,6 +156,11 @@
<artifactId>jackson-databind</artifactId>
<version>2.13.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-jaxb-annotations</artifactId>
<version>2.13.1</version>
</dependency>
<!-- g/a spring -->
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackeu.eudat.corecore/jackson-databind -->

View File

@ -50,7 +50,6 @@
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.12.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
@ -71,7 +70,7 @@
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>2.9.0</version>
<version>3.0.5</version>
</dependency>
@ -80,14 +79,14 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.7.2</version>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.22</version>
<version>42.3.1</version>
</dependency>
<dependency>

View File

@ -7,7 +7,6 @@ import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.*;
import org.springframework.core.env.Environment;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
@ -67,7 +66,7 @@ public class DevelDatabaseConfiguration {
private Properties additionalProperties() {
Properties properties = new Properties();
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQL92Dialect");
properties.setProperty("hibernate.show_sql", "true");
properties.setProperty("hibernate.show_sql", "false");
properties.setProperty("hibernate.temp.use_jdbc_metadata_defaults", "false");
return properties;
}

View File

@ -1,30 +0,0 @@
package eu.eudat.configurations;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import eu.eudat.criteria.entities.Criteria;
import eu.eudat.criteria.serialzier.CriteriaSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.ArrayList;
@Configuration
public class JacksonConfiguration {
@Bean
public ObjectMapper buildObjectMapper() {
ArrayList<Module> modules = new ArrayList<>();
SimpleModule criteriaSerializerModule = new SimpleModule();
criteriaSerializerModule.addDeserializer(Criteria.class, new CriteriaSerializer());
modules.add(criteriaSerializerModule);
return new ObjectMapper()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.registerModules(modules);
}
}

View File

@ -1026,9 +1026,8 @@ public class DatasetManager {
public void getTagsFromProfile(DatasetWizardModel wizardModel, Dataset dataset) throws IOException {
dataset.setProfile(apiContext.getOperationsContext().getDatabaseRepository().getDatasetProfileDao().find(dataset.getProfile().getId()));
wizardModel.setDatasetProfileDefinition(this.getPagedProfile(wizardModel, dataset));
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(wizardModel.getDatasetProfileDefinition());
JsonNode propertiesJson = mapper.readTree(json);
String json = apiContext.getObjectMapper().writeValueAsString(wizardModel.getDatasetProfileDefinition());
JsonNode propertiesJson = apiContext.getObjectMapper().readTree(json);
DatasetCriteria criteria = new DatasetCriteria();
criteria.setHasTags(true);
List<Tag> tags = apiContext.getOperationsContext().getElasticRepository().getDatasetRepository().query(criteria).stream().map(eu.eudat.elastic.entities.Dataset::getTags).flatMap(Collection::stream).filter(StreamDistinctBy.distinctByKey(Tag::getId)).collect(Collectors.toList());

View File

@ -60,9 +60,8 @@ public class ExternalDatasetManager {
List<Map<String, String>> remoteRepos = remoteFetcher.getDatasets(externalUrlCriteria, type);
// Parse items from external sources to listing models.
ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
List<ExternalDatasetListingModel> externalDatasetModels = remoteRepos.stream()
.map(item -> mapper.convertValue(item, ExternalDatasetListingModel.class))
.map(item -> apiContext.getObjectMapper().convertValue(item, ExternalDatasetListingModel.class))
.collect(Collectors.toCollection(LinkedList::new));
// Merge fetched and local.

View File

@ -40,8 +40,7 @@ public class LicenseManager {
List<LicenseModel> licenseModels = new LinkedList<>();
ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
licenseModels.addAll(remoteRepos.stream().map(item -> mapper.convertValue(item, LicenseModel.class)).collect(Collectors.toList()));
licenseModels.addAll(remoteRepos.stream().map(item -> apiContext.getObjectMapper().convertValue(item, LicenseModel.class)).collect(Collectors.toList()));
licenseModels = licenseModels.stream().filter(licenseModel -> licenseModel.getName().toLowerCase().contains(query.toLowerCase())).collect(Collectors.toList());
return licenseModels;
}

View File

@ -51,7 +51,7 @@ public class NotificationManager {
notification.setNotifiedAt(new Date());
notification.setUpdatedAt(new Date());
try {
Map<String, String> data = new ObjectMapper().readValue(notification.getData(), HashMap.class);
Map<String, String> data = apiContext.getObjectMapper().readValue(notification.getData(), HashMap.class);
UserInfo userInfo = this.apiContext.getOperationsContext().getDatabaseRepository().getUserInfoDao().find(UUID.fromString(data.get("userId")));
String subjectTemplate = "";
String contentTemplate = "";

View File

@ -46,8 +46,7 @@ public class ServiceManager {
List<Service> serviceList = (this.apiContext.getOperationsContext().getDatabaseRepository().getServiceDao().getWithCriteria(criteria)).toList();
serviceModels = serviceList.stream().map(item -> new ServiceModel().fromDataModel(item)).collect(Collectors.toList());
}
ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
serviceModels.addAll(remoteRepos.stream().map(item -> mapper.convertValue(item, ServiceModel.class)).collect(Collectors.toList()));
serviceModels.addAll(remoteRepos.stream().map(item -> apiContext.getObjectMapper().convertValue(item, ServiceModel.class)).collect(Collectors.toList()));
return serviceModels;
}

View File

@ -2,6 +2,8 @@ package eu.eudat.logic.proxy.config.configloaders;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule;
import eu.eudat.logic.proxy.config.ExternalUrls;
import eu.eudat.logic.security.customproviders.ConfigurableProvider.entities.ConfigurableProviders;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
@ -106,7 +108,7 @@ public class DefaultConfigLoader implements ConfigLoader {
InputStream is = null;
try {
is = getStreamFromPath(filePath);
ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
ObjectMapper mapper = new ObjectMapper();
this.configurableProviders = mapper.readValue(is, ConfigurableProviders.class);
} catch (IOException | NullPointerException e) {
logger.error(e.getMessage(), e);

View File

@ -23,7 +23,7 @@ public class PrefillingGet{
}
@XmlElement(name = "mapping")
@XmlElementWrapper
@XmlElementWrapper(name = "mappings")
public void setMappings(List<DefaultPrefillingMapping> mappings) {
this.mappings = mappings;
}
@ -33,7 +33,7 @@ public class PrefillingGet{
}
@XmlElement(name = "fixedMapping")
@XmlElementWrapper
@XmlElementWrapper (name = "fixedMappings")
public void setFixedMappings(List<PrefillingFixedMapping> fixedMappings) {
this.fixedMappings = fixedMappings;
}

View File

@ -3,6 +3,8 @@ package eu.eudat.logic.proxy.fetching;
import com.fasterxml.jackson.databind.ObjectMapper;
import eu.eudat.logic.proxy.fetching.entities.Config;
import eu.eudat.logic.proxy.fetching.entities.ConfigSingle;
import eu.eudat.logic.services.ApiContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
@ -14,6 +16,12 @@ import java.util.*;
@Component
public class LocalFetcher {
private final ApiContext apiContext;
@Autowired
public LocalFetcher(ApiContext apiContext) {
this.apiContext = apiContext;
}
@Cacheable("currencies")
public List<Map<String, String>> retrieveCurrency() throws Exception {
@ -45,7 +53,6 @@ public class LocalFetcher {
String camelCaseGetter =configSingle.getParseField() != null && !configSingle.getParseField().isEmpty() ? "get" + configSingle.getParseField().substring(0, 1).toUpperCase() + configSingle.getParseField().substring(1) : "";
reader = aClass.getMethod(camelCaseGetter);
}
ObjectMapper objectMapper = new ObjectMapper();
List<Map<String, String>> values = new ArrayList<>();
int max = 1;
if (reader != null) {
@ -66,7 +73,7 @@ public class LocalFetcher {
} else {
value = object;
}
Map<String, String> map = objectMapper.convertValue(value, Map.class);
Map<String, String> map = apiContext.getObjectMapper().convertValue(value, Map.class);
result.add(new HashMap<>());
result.get(result.size() - 1).put("name", map.get(configSingle.getName()));
result.get(result.size() - 1).put("value", map.get(configSingle.getValue()));

View File

@ -14,6 +14,7 @@ import eu.eudat.logic.proxy.config.entities.GenericUrls;
import eu.eudat.logic.proxy.config.exceptions.HugeResultSet;
import eu.eudat.logic.proxy.config.exceptions.NoURLFound;
import eu.eudat.logic.proxy.fetching.entities.Results;
import eu.eudat.logic.services.ApiContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@ -34,8 +35,9 @@ import java.util.stream.Collectors;
@Service
public class RemoteFetcher {
private static final Logger logger = LoggerFactory.getLogger(RemoteFetcher.class);
private final static ObjectMapper mapper = new ObjectMapper();
private ConfigLoader configLoader;
private final ConfigLoader configLoader;
@Autowired
public RemoteFetcher(ConfigLoader configLoader) {
@ -294,7 +296,7 @@ public class RemoteFetcher {
headers.setAccept(Collections.singletonList(MediaType.valueOf(contentType)));
headers.setContentType(MediaType.valueOf(contentType));
}
JsonNode jsonBody = new ObjectMapper().readTree(requestBody);
JsonNode jsonBody = mapper.readTree(requestBody);
entity = new HttpEntity<>(jsonBody, headers);
response = restTemplate.exchange(urlString, HttpMethod.resolve(requestType), entity, String.class);
@ -325,7 +327,6 @@ public class RemoteFetcher {
String camelCaseGetter = jsonDataPath.getParseField() != null && !jsonDataPath.getParseField().isEmpty() ? "get" + jsonDataPath.getParseField().substring(0, 1).toUpperCase() + jsonDataPath.getParseField().substring(1) : "";
reader = aClass.getMethod(camelCaseGetter);
}
ObjectMapper objectMapper = new ObjectMapper();
List<Map<String, String>> values = new ArrayList<>();
int max = 1;
if (reader != null) {
@ -346,7 +347,7 @@ public class RemoteFetcher {
} else {
value = data;
}
Map<String, String> map = objectMapper.convertValue(value, Map.class);
Map<String, String> map = mapper.convertValue(value, Map.class);
if (jsonDataPath.getMergedFields() != null && !jsonDataPath.getMergedFields().isEmpty() && jsonDataPath.getMergedFieldName() != null && !jsonDataPath.getMergedFieldName().isEmpty()) {
Map<String, String> finalMap = new HashMap<>();
for (Map.Entry<String, String> entry : map.entrySet()) {
@ -385,7 +386,6 @@ public class RemoteFetcher {
List<Map<String, String>> internalResults;
try {
String filePath = Paths.get(path).toUri().toURL().toString();
ObjectMapper mapper = new ObjectMapper();
internalResults = mapper.readValue(new File(filePath), new TypeReference<List<Map<String, String>>>(){});
return searchListMap(internalResults, query);
} catch (Exception e) {

View File

@ -1,8 +1,11 @@
package eu.eudat.logic.security.customproviders.ConfigurableProvider.entities;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.util.ArrayList;
import java.util.List;
@JsonIgnoreProperties(ignoreUnknown = true)
public class ConfigurableProviders {
private List<ConfigurableProvider> providers = new ArrayList<>();

View File

@ -30,49 +30,49 @@ public class NonVerifiedUserEmailAuthenticationService extends AbstractAuthentic
if (user == null) return null;
String avatarUrl;
try {
avatarUrl = user.getAdditionalinfo() != null ? new ObjectMapper().readTree(user.getAdditionalinfo()).get("avatarUrl").asText() : "";
avatarUrl = user.getAdditionalinfo() != null ? apiContext.getObjectMapper().readTree(user.getAdditionalinfo()).get("avatarUrl").asText() : "";
} catch (Exception e) {
avatarUrl = "";
}
String zenodoToken;
try {
zenodoToken = user.getAdditionalinfo() != null ? new ObjectMapper().readTree(user.getAdditionalinfo()).get("zenodoToken").asText() : "";
zenodoToken = user.getAdditionalinfo() != null ? apiContext.getObjectMapper().readTree(user.getAdditionalinfo()).get("zenodoToken").asText() : "";
} catch (Exception e) {
zenodoToken = "";
}
Instant zenodoDuration;
try {
zenodoDuration = user.getAdditionalinfo() != null ? Instant.ofEpochMilli(new ObjectMapper().readTree(user.getAdditionalinfo()).get("expirationDate").asLong()) : Instant.now();
zenodoDuration = user.getAdditionalinfo() != null ? Instant.ofEpochMilli(apiContext.getObjectMapper().readTree(user.getAdditionalinfo()).get("expirationDate").asLong()) : Instant.now();
} catch (Exception e) {
zenodoDuration = Instant.now();
}
String zenodoEmail;
try {
zenodoEmail = user.getAdditionalinfo() != null ? new ObjectMapper().readTree(user.getAdditionalinfo()).get("zenodoEmail").asText() : "";
zenodoEmail = user.getAdditionalinfo() != null ? apiContext.getObjectMapper().readTree(user.getAdditionalinfo()).get("zenodoEmail").asText() : "";
} catch (Exception e) {
zenodoEmail = "";
}
String zenodoRefresh;
try {
zenodoRefresh = user.getAdditionalinfo() != null ? new ObjectMapper().readTree(user.getAdditionalinfo()).get("zenodoRefresh").asText() : "";
zenodoRefresh = user.getAdditionalinfo() != null ? apiContext.getObjectMapper().readTree(user.getAdditionalinfo()).get("zenodoRefresh").asText() : "";
} catch (Exception e) {
zenodoRefresh = "";
}
String culture;
try {
culture = user.getAdditionalinfo() != null ? new ObjectMapper().readTree(user.getAdditionalinfo()).get("culture").get("name").asText() : "";
culture = user.getAdditionalinfo() != null ? apiContext.getObjectMapper().readTree(user.getAdditionalinfo()).get("culture").get("name").asText() : "";
} catch (Exception e) {
culture = "";
}
String language;
try {
language = user.getAdditionalinfo() != null ? new ObjectMapper().readTree(user.getAdditionalinfo()).get("language").get("value").asText() : "";
language = user.getAdditionalinfo() != null ? apiContext.getObjectMapper().readTree(user.getAdditionalinfo()).get("language").get("value").asText() : "";
} catch (Exception e) {
language = "";
}
String timezone;
try {
timezone = user.getAdditionalinfo() != null ? new ObjectMapper().readTree(user.getAdditionalinfo()).get("timezone").asText() : "";
timezone = user.getAdditionalinfo() != null ? apiContext.getObjectMapper().readTree(user.getAdditionalinfo()).get("timezone").asText() : "";
} catch (Exception e) {
timezone = "";
}

View File

@ -33,49 +33,49 @@ public class VerifiedUserAuthenticationService extends AbstractAuthenticationSer
if (user.getEmail() == null) throw new NullEmailException();
String avatarUrl;
try {
avatarUrl = user.getAdditionalinfo() != null ? new ObjectMapper().readTree(user.getAdditionalinfo()).get("avatarUrl").asText() : "";
avatarUrl = user.getAdditionalinfo() != null ? apiContext.getObjectMapper().readTree(user.getAdditionalinfo()).get("avatarUrl").asText() : "";
} catch (Exception e) {
avatarUrl = "";
}
String zenodoToken;
try {
zenodoToken = user.getAdditionalinfo() != null ? new ObjectMapper().readTree(user.getAdditionalinfo()).get("zenodoToken").asText() : "";
zenodoToken = user.getAdditionalinfo() != null ? apiContext.getObjectMapper().readTree(user.getAdditionalinfo()).get("zenodoToken").asText() : "";
} catch (Exception e) {
zenodoToken = "";
}
Instant zenodoDuration;
try {
zenodoDuration = user.getAdditionalinfo() != null ? Instant.ofEpochMilli(new ObjectMapper().readTree(user.getAdditionalinfo()).get("expirationDate").asLong()) : Instant.now();
zenodoDuration = user.getAdditionalinfo() != null ? Instant.ofEpochMilli(apiContext.getObjectMapper().readTree(user.getAdditionalinfo()).get("expirationDate").asLong()) : Instant.now();
} catch (Exception e) {
zenodoDuration = Instant.now();
}
String zenodoEmail;
try {
zenodoEmail = user.getAdditionalinfo() != null ? new ObjectMapper().readTree(user.getAdditionalinfo()).get("zenodoEmail").asText() : "";
zenodoEmail = user.getAdditionalinfo() != null ? apiContext.getObjectMapper().readTree(user.getAdditionalinfo()).get("zenodoEmail").asText() : "";
} catch (Exception e) {
zenodoEmail = "";
}
String zenodoRefresh;
try {
zenodoRefresh = user.getAdditionalinfo() != null ? new ObjectMapper().readTree(user.getAdditionalinfo()).get("zenodoRefresh").asText() : "";
zenodoRefresh = user.getAdditionalinfo() != null ? apiContext.getObjectMapper().readTree(user.getAdditionalinfo()).get("zenodoRefresh").asText() : "";
} catch (Exception e) {
zenodoRefresh = "";
}
String culture;
try {
culture = user.getAdditionalinfo() != null ? new ObjectMapper().readTree(user.getAdditionalinfo()).get("culture").get("name").asText() : "";
culture = user.getAdditionalinfo() != null ? apiContext.getObjectMapper().readTree(user.getAdditionalinfo()).get("culture").get("name").asText() : "";
} catch (Exception e) {
culture = "";
}
String language;
try {
language = user.getAdditionalinfo() != null ? new ObjectMapper().readTree(user.getAdditionalinfo()).get("language").get("value").asText() : "";
language = user.getAdditionalinfo() != null ? apiContext.getObjectMapper().readTree(user.getAdditionalinfo()).get("language").get("value").asText() : "";
} catch (Exception e) {
language = "";
}
String timezone;
try {
timezone = user.getAdditionalinfo() != null ? new ObjectMapper().readTree(user.getAdditionalinfo()).get("timezone").asText() : "";
timezone = user.getAdditionalinfo() != null ? apiContext.getObjectMapper().readTree(user.getAdditionalinfo()).get("timezone").asText() : "";
} catch (Exception e) {
timezone = "";
}

View File

@ -8,6 +8,7 @@ import eu.eudat.models.data.mail.SimpleMail;
import eu.eudat.models.data.security.Principal;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
@ -20,11 +21,11 @@ import java.util.concurrent.CompletableFuture;
@Service("ConfirmationEmailService")
public class ConfirmationEmailServiceImpl implements ConfirmationEmailService {
private static final Logger logger = LoggerFactory.getLogger(ConfirmationEmailServiceImpl.class);
//private Logger logger;
private Environment environment;
private static final ObjectMapper mapper = new ObjectMapper();
private final Environment environment;
public ConfirmationEmailServiceImpl(/*Logger logger,*/ Environment environment) {
// this.logger = logger;
@Autowired
public ConfirmationEmailServiceImpl(Environment environment) {
this.environment = environment;
}
@ -118,7 +119,7 @@ public class ConfirmationEmailServiceImpl implements ConfirmationEmailService {
Map<String, Object> map = new HashMap<>();
map.put("userId", principal.getId());
map.put("provider", provider.toString());
confirmationEmail.setData(new ObjectMapper().writeValueAsString(map));
confirmationEmail.setData(mapper.writeValueAsString(map));
} catch (JsonProcessingException e) {
logger.error(e.getMessage(), e);
}

View File

@ -1,5 +1,6 @@
package eu.eudat.models.data.license;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import eu.eudat.data.entities.DataRepository;
import eu.eudat.data.entities.UserInfo;
import eu.eudat.models.DataModel;
@ -7,6 +8,7 @@ import eu.eudat.models.DataModel;
import java.util.Date;
import java.util.UUID;
@JsonIgnoreProperties(ignoreUnknown = true)
public class LicenseModel implements DataModel<DataRepository, LicenseModel> {
private UUID id;
private String name;

View File

@ -1,5 +1,6 @@
package eu.eudat.models.data.services;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import eu.eudat.data.entities.Service;
import eu.eudat.data.entities.UserInfo;
import eu.eudat.models.DataModel;
@ -7,6 +8,7 @@ import eu.eudat.models.DataModel;
import java.util.Date;
import java.util.UUID;
@JsonIgnoreProperties(ignoreUnknown = true)
public class ServiceModel implements DataModel<Service, ServiceModel> {
private UUID id;
private String label;