2018-06-27 12:29:21 +02:00
package eu.eudat.logic.proxy.fetching ;
2017-11-21 17:29:16 +01:00
2019-09-05 16:44:25 +02:00
import com.fasterxml.jackson.core.type.TypeReference ;
2020-11-06 17:45:20 +01:00
import com.fasterxml.jackson.databind.JsonNode ;
2019-09-05 16:44:25 +02:00
import com.fasterxml.jackson.databind.ObjectMapper ;
2017-11-21 17:29:16 +01:00
import com.jayway.jsonpath.DocumentContext ;
import com.jayway.jsonpath.JsonPath ;
2022-05-25 16:37:20 +02:00
import eu.eudat.logic.proxy.config.* ;
2018-06-27 12:29:21 +02:00
import eu.eudat.logic.proxy.config.configloaders.ConfigLoader ;
2021-12-10 14:37:39 +01:00
import eu.eudat.logic.proxy.config.entities.GenericUrls ;
2018-06-27 12:29:21 +02:00
import eu.eudat.logic.proxy.config.exceptions.HugeResultSet ;
import eu.eudat.logic.proxy.config.exceptions.NoURLFound ;
2021-10-13 16:48:46 +02:00
import eu.eudat.logic.proxy.fetching.entities.Results ;
2020-01-16 16:46:24 +01:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
2018-02-16 11:34:02 +01:00
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.cache.annotation.Cacheable ;
2021-10-13 16:48:46 +02:00
import org.springframework.http.* ;
2018-02-16 11:34:02 +01:00
import org.springframework.stereotype.Service ;
2020-10-05 10:26:35 +02:00
import org.springframework.web.client.RestTemplate ;
2018-02-16 11:34:02 +01:00
2020-02-10 17:24:15 +01:00
import javax.xml.bind.JAXBContext ;
import javax.xml.bind.Unmarshaller ;
2021-10-13 16:48:46 +02:00
import java.io.File ;
import java.io.StringReader ;
2020-02-10 17:24:15 +01:00
import java.lang.reflect.Method ;
2019-11-08 14:53:46 +01:00
import java.nio.file.Paths ;
2018-02-16 11:34:02 +01:00
import java.util.* ;
2018-05-28 11:50:42 +02:00
import java.util.stream.Collectors ;
2017-11-21 17:29:16 +01:00
2017-11-22 09:57:51 +01:00
@Service
2017-11-21 17:29:16 +01:00
public class RemoteFetcher {
2020-01-16 16:46:24 +01:00
private static final Logger logger = LoggerFactory . getLogger ( RemoteFetcher . class ) ;
2018-01-17 16:06:35 +01:00
private ConfigLoader configLoader ;
2018-03-28 15:24:47 +02:00
@Autowired
2019-09-05 16:44:25 +02:00
public RemoteFetcher ( ConfigLoader configLoader ) {
2018-03-28 15:24:47 +02:00
this . configLoader = configLoader ;
}
2018-01-17 16:06:35 +01:00
2021-11-29 16:26:29 +01:00
@Cacheable ( value = " repositories " , keyGenerator = " externalUrlsKeyGenerator " )
2019-10-03 13:06:44 +02:00
public List < Map < String , String > > getRepositories ( ExternalUrlCriteria externalUrlCriteria , String key ) throws NoURLFound , HugeResultSet {
2018-06-29 10:29:43 +02:00
List < UrlConfiguration > urlConfigs =
key ! = null & & ! key . isEmpty ( ) ? configLoader . getExternalUrls ( ) . getRepositories ( ) . getUrls ( ) . stream ( ) . filter ( item - > item . getKey ( ) . equals ( key ) ) . collect ( Collectors . toList ( ) )
2018-05-28 11:50:42 +02:00
: configLoader . getExternalUrls ( ) . getRepositories ( ) . getUrls ( ) ;
2018-01-17 16:06:35 +01:00
FetchStrategy fetchStrategy = configLoader . getExternalUrls ( ) . getRepositories ( ) . getFetchMode ( ) ;
2019-10-03 13:06:44 +02:00
return getAll ( urlConfigs , fetchStrategy , externalUrlCriteria ) ;
2018-01-17 16:06:35 +01:00
}
2022-01-21 14:44:51 +01:00
@Cacheable ( value = " pubrepos " , keyGenerator = " externalUrlsKeyGenerator " )
public List < Map < String , String > > getPubRepositories ( ExternalUrlCriteria externalUrlCriteria , String key ) throws NoURLFound , HugeResultSet {
List < UrlConfiguration > urlConfigs =
key ! = null & & ! key . isEmpty ( ) ? configLoader . getExternalUrls ( ) . getPubRepositories ( ) . getUrls ( ) . stream ( ) . filter ( item - > item . getKey ( ) . equals ( key ) ) . collect ( Collectors . toList ( ) )
: configLoader . getExternalUrls ( ) . getPubRepositories ( ) . getUrls ( ) ;
FetchStrategy fetchStrategy = configLoader . getExternalUrls ( ) . getPubRepositories ( ) . getFetchMode ( ) ;
return getAll ( urlConfigs , fetchStrategy , externalUrlCriteria ) ;
}
@Cacheable ( value = " journals " , keyGenerator = " externalUrlsKeyGenerator " )
public List < Map < String , String > > getJournals ( ExternalUrlCriteria externalUrlCriteria , String key ) throws NoURLFound , HugeResultSet {
List < UrlConfiguration > urlConfigs =
key ! = null & & ! key . isEmpty ( ) ? configLoader . getExternalUrls ( ) . getJournals ( ) . getUrls ( ) . stream ( ) . filter ( item - > item . getKey ( ) . equals ( key ) ) . collect ( Collectors . toList ( ) )
: configLoader . getExternalUrls ( ) . getJournals ( ) . getUrls ( ) ;
FetchStrategy fetchStrategy = configLoader . getExternalUrls ( ) . getJournals ( ) . getFetchMode ( ) ;
return getAll ( urlConfigs , fetchStrategy , externalUrlCriteria ) ;
}
@Cacheable ( value = " taxonomies " , keyGenerator = " externalUrlsKeyGenerator " )
public List < Map < String , String > > getTaxonomies ( ExternalUrlCriteria externalUrlCriteria , String key ) throws NoURLFound , HugeResultSet {
List < UrlConfiguration > urlConfigs =
key ! = null & & ! key . isEmpty ( ) ? configLoader . getExternalUrls ( ) . getTaxonomies ( ) . getUrls ( ) . stream ( ) . filter ( item - > item . getKey ( ) . equals ( key ) ) . collect ( Collectors . toList ( ) )
: configLoader . getExternalUrls ( ) . getTaxonomies ( ) . getUrls ( ) ;
FetchStrategy fetchStrategy = configLoader . getExternalUrls ( ) . getTaxonomies ( ) . getFetchMode ( ) ;
return getAll ( urlConfigs , fetchStrategy , externalUrlCriteria ) ;
}
@Cacheable ( value = " publications " , keyGenerator = " externalUrlsKeyGenerator " )
public List < Map < String , String > > getPublications ( ExternalUrlCriteria externalUrlCriteria , String key ) throws NoURLFound , HugeResultSet {
List < UrlConfiguration > urlConfigs =
key ! = null & & ! key . isEmpty ( ) ? configLoader . getExternalUrls ( ) . getPublications ( ) . getUrls ( ) . stream ( ) . filter ( item - > item . getKey ( ) . equals ( key ) ) . collect ( Collectors . toList ( ) )
: configLoader . getExternalUrls ( ) . getPublications ( ) . getUrls ( ) ;
FetchStrategy fetchStrategy = configLoader . getExternalUrls ( ) . getPublications ( ) . getFetchMode ( ) ;
return getAll ( urlConfigs , fetchStrategy , externalUrlCriteria ) ;
}
2018-01-17 16:06:35 +01:00
2021-11-29 16:26:29 +01:00
@Cacheable ( value = " grants " , keyGenerator = " externalUrlsKeyGenerator " )
2019-10-03 13:06:44 +02:00
public List < Map < String , String > > getGrants ( ExternalUrlCriteria externalUrlCriteria ) throws NoURLFound , HugeResultSet {
2019-09-05 16:44:25 +02:00
List < UrlConfiguration > urlConfigs = configLoader . getExternalUrls ( ) . getGrants ( ) . getUrls ( ) ;
2019-07-31 16:57:34 +02:00
FetchStrategy fetchStrategy = configLoader . getExternalUrls ( ) . getGrants ( ) . getFetchMode ( ) ;
2019-10-03 13:06:44 +02:00
return getAll ( urlConfigs , fetchStrategy , externalUrlCriteria ) ;
2018-01-17 16:06:35 +01:00
}
2021-11-29 16:26:29 +01:00
@Cacheable ( value = " projects " , keyGenerator = " externalUrlsKeyGenerator " )
2019-10-03 13:06:44 +02:00
public List < Map < String , String > > getProjects ( ExternalUrlCriteria externalUrlCriteria ) throws NoURLFound , HugeResultSet {
2019-09-05 16:44:25 +02:00
List < UrlConfiguration > urlConfigs = configLoader . getExternalUrls ( ) . getProjects ( ) . getUrls ( ) ;
2019-08-02 10:27:12 +02:00
FetchStrategy fetchStrategy = configLoader . getExternalUrls ( ) . getProjects ( ) . getFetchMode ( ) ;
2019-10-03 13:06:44 +02:00
return getAll ( urlConfigs , fetchStrategy , externalUrlCriteria ) ;
2019-08-02 10:27:12 +02:00
}
2021-11-29 16:26:29 +01:00
@Cacheable ( value = " funders " , keyGenerator = " externalUrlsKeyGenerator " )
2019-10-03 13:06:44 +02:00
public List < Map < String , String > > getFunders ( ExternalUrlCriteria externalUrlCriteria ) throws NoURLFound , HugeResultSet {
2019-09-05 16:44:25 +02:00
List < UrlConfiguration > urlConfigs = configLoader . getExternalUrls ( ) . getFunders ( ) . getUrls ( ) ;
2019-10-01 12:19:39 +02:00
FetchStrategy fetchStrategy = configLoader . getExternalUrls ( ) . getFunders ( ) . getFetchMode ( ) ;
2019-10-03 13:06:44 +02:00
return getAll ( urlConfigs , fetchStrategy , externalUrlCriteria ) ;
2019-08-20 17:22:53 +02:00
}
2021-11-29 16:26:29 +01:00
@Cacheable ( value = " organisations " , keyGenerator = " externalUrlsKeyGenerator " )
2019-10-03 13:06:44 +02:00
public List < Map < String , String > > getOrganisations ( ExternalUrlCriteria externalUrlCriteria , String key ) throws NoURLFound , HugeResultSet {
2018-06-29 10:29:43 +02:00
List < UrlConfiguration > urlConfigs =
key ! = null & & ! key . isEmpty ( ) ? configLoader . getExternalUrls ( ) . getOrganisations ( ) . getUrls ( ) . stream ( ) . filter ( item - > item . getKey ( ) . equals ( key ) ) . collect ( Collectors . toList ( ) )
2018-05-28 11:50:42 +02:00
: configLoader . getExternalUrls ( ) . getOrganisations ( ) . getUrls ( ) ;
2018-01-17 16:06:35 +01:00
FetchStrategy fetchStrategy = configLoader . getExternalUrls ( ) . getOrganisations ( ) . getFetchMode ( ) ;
2019-10-03 13:06:44 +02:00
return getAll ( urlConfigs , fetchStrategy , externalUrlCriteria ) ;
2018-01-17 16:06:35 +01:00
}
2021-11-29 16:26:29 +01:00
@Cacheable ( value = " registries " , keyGenerator = " externalUrlsKeyGenerator " )
2019-10-03 13:06:44 +02:00
public List < Map < String , String > > getRegistries ( ExternalUrlCriteria externalUrlCriteria , String key ) throws NoURLFound , HugeResultSet {
2018-06-29 10:29:43 +02:00
List < UrlConfiguration > urlConfigs =
key ! = null & & ! key . isEmpty ( ) ? configLoader . getExternalUrls ( ) . getRegistries ( ) . getUrls ( ) . stream ( ) . filter ( item - > item . getKey ( ) . equals ( key ) ) . collect ( Collectors . toList ( ) )
2018-05-28 11:50:42 +02:00
: configLoader . getExternalUrls ( ) . getRegistries ( ) . getUrls ( ) ;
2018-01-17 16:06:35 +01:00
FetchStrategy fetchStrategy = configLoader . getExternalUrls ( ) . getRegistries ( ) . getFetchMode ( ) ;
2019-10-03 13:06:44 +02:00
return getAll ( urlConfigs , fetchStrategy , externalUrlCriteria ) ;
2018-01-17 16:06:35 +01:00
}
2021-11-29 16:26:29 +01:00
@Cacheable ( value = " services " , keyGenerator = " externalUrlsKeyGenerator " )
2019-10-03 13:06:44 +02:00
public List < Map < String , String > > getServices ( ExternalUrlCriteria externalUrlCriteria , String key ) throws NoURLFound , HugeResultSet {
2018-06-29 10:29:43 +02:00
List < UrlConfiguration > urlConfigs =
key ! = null & & ! key . isEmpty ( ) ? configLoader . getExternalUrls ( ) . getServices ( ) . getUrls ( ) . stream ( ) . filter ( item - > item . getKey ( ) . equals ( key ) ) . collect ( Collectors . toList ( ) )
2018-05-28 11:50:42 +02:00
: configLoader . getExternalUrls ( ) . getServices ( ) . getUrls ( ) ;
2018-01-17 16:06:35 +01:00
FetchStrategy fetchStrategy = configLoader . getExternalUrls ( ) . getServices ( ) . getFetchMode ( ) ;
2019-10-03 13:06:44 +02:00
return getAll ( urlConfigs , fetchStrategy , externalUrlCriteria ) ;
2018-01-17 16:06:35 +01:00
}
2021-11-29 16:26:29 +01:00
@Cacheable ( value = " researchers " , keyGenerator = " externalUrlsKeyGenerator " )
2019-10-03 13:06:44 +02:00
public List < Map < String , String > > getResearchers ( ExternalUrlCriteria externalUrlCriteria , String key ) throws NoURLFound , HugeResultSet {
2018-06-29 10:29:43 +02:00
List < UrlConfiguration > urlConfigs =
key ! = null & & ! key . isEmpty ( ) ? configLoader . getExternalUrls ( ) . getResearchers ( ) . getUrls ( ) . stream ( ) . filter ( item - > item . getKey ( ) . equals ( key ) ) . collect ( Collectors . toList ( ) )
2018-05-28 11:50:42 +02:00
: configLoader . getExternalUrls ( ) . getResearchers ( ) . getUrls ( ) ;
2018-01-17 16:06:35 +01:00
FetchStrategy fetchStrategy = configLoader . getExternalUrls ( ) . getResearchers ( ) . getFetchMode ( ) ;
2019-10-03 13:06:44 +02:00
return getAll ( urlConfigs , fetchStrategy , externalUrlCriteria ) ;
2018-01-17 16:06:35 +01:00
}
2020-04-01 17:17:17 +02:00
/ * @Cacheable ( " tags " )
2019-10-03 13:06:44 +02:00
public List < Map < String , String > > getTags ( ExternalUrlCriteria externalUrlCriteria , String key ) throws NoURLFound , HugeResultSet {
2018-07-11 15:47:36 +02:00
List < UrlConfiguration > urlConfigs =
key ! = null & & ! key . isEmpty ( ) ? configLoader . getExternalUrls ( ) . getTags ( ) . getUrls ( ) . stream ( ) . filter ( item - > item . getKey ( ) . equals ( key ) ) . collect ( Collectors . toList ( ) )
: configLoader . getExternalUrls ( ) . getTags ( ) . getUrls ( ) ;
FetchStrategy fetchStrategy = configLoader . getExternalUrls ( ) . getTags ( ) . getFetchMode ( ) ;
2019-10-03 13:06:44 +02:00
return getAll ( urlConfigs , fetchStrategy , externalUrlCriteria ) ;
2020-04-01 17:17:17 +02:00
} * /
2018-07-11 15:47:36 +02:00
2021-11-29 16:26:29 +01:00
@Cacheable ( value = " externalDatasets " , keyGenerator = " externalUrlsKeyGenerator " )
2019-10-03 13:06:44 +02:00
public List < Map < String , String > > getDatasets ( ExternalUrlCriteria externalUrlCriteria , String key ) throws NoURLFound , HugeResultSet {
2018-06-29 10:29:43 +02:00
List < UrlConfiguration > urlConfigs =
key ! = null & & ! key . isEmpty ( ) ? configLoader . getExternalUrls ( ) . getDatasets ( ) . getUrls ( ) . stream ( ) . filter ( item - > item . getKey ( ) . equals ( key ) ) . collect ( Collectors . toList ( ) )
2018-05-28 11:50:42 +02:00
: configLoader . getExternalUrls ( ) . getDatasets ( ) . getUrls ( ) ;
2018-01-17 16:06:35 +01:00
FetchStrategy fetchStrategy = configLoader . getExternalUrls ( ) . getDatasets ( ) . getFetchMode ( ) ;
2019-10-03 13:06:44 +02:00
return getAll ( urlConfigs , fetchStrategy , externalUrlCriteria ) ;
2018-01-17 16:06:35 +01:00
}
2021-11-29 16:26:29 +01:00
@Cacheable ( value = " licenses " , keyGenerator = " externalUrlsKeyGenerator " )
2020-06-26 10:46:18 +02:00
public List < Map < String , String > > getlicenses ( ExternalUrlCriteria externalUrlCriteria , String key ) throws NoURLFound , HugeResultSet {
List < UrlConfiguration > urlConfigs =
key ! = null & & ! key . isEmpty ( ) ? configLoader . getExternalUrls ( ) . getLicenses ( ) . getUrls ( ) . stream ( ) . filter ( item - > item . getKey ( ) . equals ( key ) ) . collect ( Collectors . toList ( ) )
: configLoader . getExternalUrls ( ) . getLicenses ( ) . getUrls ( ) ;
FetchStrategy fetchStrategy = configLoader . getExternalUrls ( ) . getLicenses ( ) . getFetchMode ( ) ;
return getAll ( urlConfigs , fetchStrategy , externalUrlCriteria ) ;
}
2020-11-05 17:20:37 +01:00
public Integer findEntries ( ExternalUrlCriteria externalUrlCriteria , String key ) throws NoURLFound , HugeResultSet {
List < UrlConfiguration > urlConfigs =
key ! = null & & ! key . isEmpty ( ) ? configLoader . getExternalUrls ( ) . getValidations ( ) . getUrls ( ) . stream ( ) . filter ( item - > item . getKey ( ) . equals ( key ) ) . collect ( Collectors . toList ( ) )
2020-11-06 15:29:33 +01:00
: configLoader . getExternalUrls ( ) . getValidations ( ) . getUrls ( ) ;
2020-11-12 09:14:42 +01:00
FetchStrategy fetchStrategy = configLoader . getExternalUrls ( ) . getValidations ( ) . getFetchMode ( ) ;
2020-11-05 17:20:37 +01:00
List < Map < String , String > > data = this . getAll ( urlConfigs , fetchStrategy , externalUrlCriteria ) ;
return data . size ( ) ;
}
2020-06-26 10:46:18 +02:00
2021-12-10 14:37:39 +01:00
public List < Map < String , String > > getExternalGeneric ( ExternalUrlCriteria externalUrlCriteria , GenericUrls genericUrls ) {
List < UrlConfiguration > urlConfigurations = genericUrls . getUrls ( ) ;
FetchStrategy fetchStrategy = genericUrls . getFetchMode ( ) ;
return getAll ( urlConfigurations , fetchStrategy , externalUrlCriteria ) ;
}
2018-01-17 16:06:35 +01:00
2021-10-13 16:48:46 +02:00
private List < Map < String , String > > getAll ( List < UrlConfiguration > urlConfigs , FetchStrategy fetchStrategy , ExternalUrlCriteria externalUrlCriteria ) {
2018-01-17 16:06:35 +01:00
2019-12-12 10:26:19 +01:00
List < Map < String , String > > results = new LinkedList < > ( ) ;
2021-10-13 16:48:46 +02:00
if ( urlConfigs = = null | | urlConfigs . isEmpty ( ) ) {
return results ;
}
2019-12-12 10:26:19 +01:00
// throw new NoURLFound("No Repository urls found in configuration");
2018-01-17 16:06:35 +01:00
2019-10-03 13:06:44 +02:00
urlConfigs . sort ( Comparator . comparing ( UrlConfiguration : : getOrdinal ) ) ;
2021-10-13 16:48:46 +02:00
urlConfigs . forEach ( urlConfiguration - > {
ifFunderQueryExist ( urlConfiguration , externalUrlCriteria ) ;
if ( urlConfiguration . getType ( ) = = null | | urlConfiguration . getType ( ) . equals ( " External " ) ) {
try {
2022-05-25 16:37:20 +02:00
results . addAll ( getAllResultsFromUrl ( urlConfiguration . getUrl ( ) , fetchStrategy , urlConfiguration . getData ( ) , urlConfiguration . getPaginationPath ( ) , externalUrlCriteria , urlConfiguration . getLabel ( ) , urlConfiguration . getKey ( ) , urlConfiguration . getContentType ( ) , urlConfiguration . getFirstpage ( ) , urlConfiguration . getRequestBody ( ) , urlConfiguration . getRequestType ( ) , urlConfiguration . getFilterType ( ) , urlConfiguration . getQueries ( ) ) ) ;
2021-10-13 16:48:46 +02:00
} catch ( Exception e ) {
logger . error ( e . getLocalizedMessage ( ) , e ) ;
}
} else if ( urlConfiguration . getType ( ) ! = null & & urlConfiguration . getType ( ) . equals ( " Internal " ) ) {
results . addAll ( getAllResultsFromMockUpJson ( urlConfiguration . getUrl ( ) , externalUrlCriteria . getLike ( ) ) ) ;
}
} ) ;
/ * for ( UrlConfiguration urlConfig : urlConfigs ) {
2019-10-03 13:06:44 +02:00
ifFunderQueryExist ( urlConfig , externalUrlCriteria ) ;
2019-09-05 16:44:25 +02:00
if ( urlConfig . getType ( ) = = null | | urlConfig . getType ( ) . equals ( " External " ) ) {
2020-11-06 17:45:20 +01:00
results . addAll ( getAllResultsFromUrl ( urlConfig . getUrl ( ) , fetchStrategy , urlConfig . getData ( ) , urlConfig . getPaginationPath ( ) , externalUrlCriteria , urlConfig . getLabel ( ) , urlConfig . getKey ( ) , urlConfig . getContentType ( ) , urlConfig . getFirstpage ( ) , urlConfig . getRequestBody ( ) , urlConfig . getRequestType ( ) ) ) ;
2019-10-01 12:19:39 +02:00
} else if ( urlConfig . getType ( ) ! = null & & urlConfig . getType ( ) . equals ( " Internal " ) ) {
2019-10-03 13:06:44 +02:00
results . addAll ( getAllResultsFromMockUpJson ( urlConfig . getUrl ( ) , externalUrlCriteria . getLike ( ) ) ) ;
2019-09-05 16:44:25 +02:00
}
2021-10-13 16:48:46 +02:00
} * /
2018-06-29 10:29:43 +02:00
return results ;
2018-01-17 16:06:35 +01:00
}
2019-10-03 13:06:44 +02:00
private void ifFunderQueryExist ( UrlConfiguration urlConfiguration , ExternalUrlCriteria externalUrlCriteria ) {
if ( urlConfiguration . getFunderQuery ( ) ! = null ) {
2019-12-10 10:58:15 +01:00
if ( externalUrlCriteria . getFunderId ( ) ! = null & & ! urlConfiguration . getFunderQuery ( ) . startsWith ( " dmp: " ) ) {
2019-10-03 13:06:44 +02:00
urlConfiguration . setUrl ( urlConfiguration . getUrl ( ) . replace ( " {funderQuery} " , urlConfiguration . getFunderQuery ( ) ) ) ;
}
else {
urlConfiguration . setUrl ( urlConfiguration . getUrl ( ) . replace ( " {funderQuery} " , " " ) ) ;
}
2019-10-01 12:19:39 +02:00
}
2019-10-03 13:06:44 +02:00
}
2018-01-17 16:06:35 +01:00
2022-05-25 16:37:20 +02:00
private String calculateQuery ( ExternalUrlCriteria externalUrlCriteria , List < QueryConfig > queryConfigs ) {
String finalQuery = " " ;
QueryConfig queryConfig = queryConfigs . stream ( ) . filter ( queryConfigl - > externalUrlCriteria . getLike ( ) . matches ( queryConfigl . getCondition ( ) ) )
2022-05-25 17:06:54 +02:00
. min ( ( Comparator . comparing ( QueryConfig : : getOrdinal ) ) ) . orElse ( null ) ;
2022-05-25 16:37:20 +02:00
if ( queryConfig ! = null ) {
if ( queryConfig . getSeparator ( ) ! = null ) {
String [ ] likes = externalUrlCriteria . getLike ( ) . split ( queryConfig . getSeparator ( ) ) ;
finalQuery = queryConfig . getValue ( ) ;
for ( int i = 0 ; i < likes . length ; i + + ) {
finalQuery = finalQuery . replaceAll ( " \\ {like " + ( i + 1 ) + " } " , likes [ i ] ) ;
}
} else {
finalQuery = queryConfig . getValue ( ) . replaceAll ( " \\ {like} " , externalUrlCriteria . getLike ( ) ) ;
}
}
return finalQuery ;
}
protected String replaceCriteriaOnUrl ( String path , ExternalUrlCriteria externalUrlCriteria , String firstPage , List < QueryConfig > queries ) {
2019-10-03 13:06:44 +02:00
String completedPath = path ;
if ( externalUrlCriteria . getLike ( ) ! = null ) {
2020-03-13 11:04:20 +01:00
if ( ( path . contains ( " openaire " ) | | path . contains ( " orcid " ) | | path . contains ( " ror " ) ) & & externalUrlCriteria . getLike ( ) . equals ( " " ) ) {
2019-10-03 13:06:44 +02:00
completedPath = completedPath . replaceAll ( " \\ {like} " , " * " ) ;
2022-05-25 16:37:20 +02:00
completedPath = completedPath . replaceAll ( " \\ {query} " , " * " ) ;
2020-02-10 17:27:38 +01:00
} else {
2022-05-25 16:37:20 +02:00
if ( completedPath . contains ( " {query} " ) ) {
completedPath = completedPath . replaceAll ( " \\ {query} " , this . calculateQuery ( externalUrlCriteria , queries ) ) ;
} else {
completedPath = completedPath . replaceAll ( " \\ {like} " , externalUrlCriteria . getLike ( ) ) ;
}
2020-02-10 17:27:38 +01:00
}
2019-10-01 12:19:39 +02:00
} else {
2019-10-03 13:06:44 +02:00
completedPath = completedPath . replace ( " {like} " , " " ) ;
}
if ( externalUrlCriteria . getFunderId ( ) ! = null ) {
2020-05-06 17:02:19 +02:00
String funderPrefix = externalUrlCriteria . getFunderId ( ) . split ( " : " ) [ 0 ] ;
String funderId = externalUrlCriteria . getFunderId ( ) . replace ( funderPrefix + " : " , " " ) ;
if ( funderId . toCharArray ( ) [ 0 ] = = ':' ) {
funderId = externalUrlCriteria . getFunderId ( ) ;
}
2020-10-05 15:21:42 +02:00
/ *
try { funderId = URLEncoder . encode ( funderId , " UTF-8 " ) ; } catch
( UnsupportedEncodingException e ) { logger . error ( e . getMessage ( ) , e ) ; }
* /
2019-10-03 13:06:44 +02:00
completedPath = completedPath . replace ( " {funderId} " , funderId ) ;
2019-10-01 12:19:39 +02:00
}
2019-10-03 13:06:44 +02:00
if ( externalUrlCriteria . getPage ( ) ! = null ) {
completedPath = completedPath . replace ( " {page} " , externalUrlCriteria . getPage ( ) ) ;
} else {
2019-10-04 13:33:38 +02:00
if ( firstPage ! = null ) {
completedPath = completedPath . replace ( " {page} " , firstPage ) ;
} else {
completedPath = completedPath . replace ( " {page} " , " 1 " ) ;
}
2019-10-03 13:06:44 +02:00
}
if ( externalUrlCriteria . getPageSize ( ) ! = null ) {
completedPath = completedPath . replace ( " {pageSize} " , externalUrlCriteria . getPageSize ( ) ) ;
} else {
2019-12-13 12:08:32 +01:00
completedPath = completedPath . replace ( " {pageSize} " , " 60 " ) ;
2019-10-03 13:06:44 +02:00
}
2020-02-10 17:24:15 +01:00
if ( externalUrlCriteria . getHost ( ) ! = null ) {
completedPath = completedPath . replace ( " {host} " , externalUrlCriteria . getHost ( ) ) ;
} else {
completedPath = completedPath . replace ( " {host} " , " " ) ;
}
if ( externalUrlCriteria . getPath ( ) ! = null ) {
completedPath = completedPath . replace ( " {path} " , externalUrlCriteria . getPath ( ) ) ;
} else {
completedPath = completedPath . replace ( " {path} " , " " ) ;
}
2019-10-03 13:06:44 +02:00
return completedPath ;
}
2018-01-17 16:06:35 +01:00
2022-05-25 16:37:20 +02:00
private List < Map < String , String > > getAllResultsFromUrl ( String path , FetchStrategy fetchStrategy , final DataUrlConfiguration jsonDataPath , final String jsonPaginationPath , ExternalUrlCriteria externalUrlCriteria , String tag , String key , String contentType , String firstPage , String requestBody , String requestType , String filterType , List < QueryConfig > queries ) throws Exception {
2019-10-03 13:06:44 +02:00
Set < Integer > pages = new HashSet < > ( ) ;
2022-05-25 16:37:20 +02:00
String replacedPath = replaceCriteriaOnUrl ( path , externalUrlCriteria , firstPage , queries ) ;
String replacedBody = replaceCriteriaOnUrl ( requestBody , externalUrlCriteria , firstPage , queries ) ;
2019-10-03 13:06:44 +02:00
2020-11-06 17:45:20 +01:00
Results results = getResultsFromUrl ( replacedPath , jsonDataPath , jsonPaginationPath , contentType , replacedBody , requestType ) ;
2022-03-23 15:03:42 +01:00
if ( filterType ! = null & & filterType . equals ( " local " ) & & ( externalUrlCriteria . getLike ( ) ! = null & & ! externalUrlCriteria . getLike ( ) . isEmpty ( ) ) ) {
results . setResults ( results . getResults ( ) . stream ( )
. filter ( r - > r . get ( " name " ) . toLowerCase ( ) . contains ( externalUrlCriteria . getLike ( ) . toLowerCase ( ) ) )
. collect ( Collectors . toList ( ) ) ) ;
}
2018-01-17 16:06:35 +01:00
if ( fetchStrategy = = FetchStrategy . FIRST )
2020-01-14 12:00:02 +01:00
return results = = null ? new LinkedList < > ( ) : results . getResults ( ) . stream ( ) . peek ( x - > x . put ( " tag " , tag ) ) . peek ( x - > x . put ( " key " , key ) ) . collect ( Collectors . toList ( ) ) ;
2018-01-17 16:06:35 +01:00
2019-10-01 12:19:39 +02:00
if ( results ! = null & & results . getPagination ( ) ! = null & & results . getPagination ( ) . get ( " pages " ) ! = null ) //if has more pages, add them to the pages set
2018-01-17 16:06:35 +01:00
for ( int i = 2 ; i < = results . getPagination ( ) . get ( " pages " ) ; i + + )
pages . add ( i ) ;
Long maxResults = configLoader . getExternalUrls ( ) . getMaxresults ( ) ;
2019-10-01 12:19:39 +02:00
if ( ( maxResults > 0 & & results ! = null ) & & ( results . getPagination ( ) . get ( " count " ) > maxResults ) )
2019-10-03 13:06:44 +02:00
throw new HugeResultSet ( " The submitted search query " + externalUrlCriteria . getLike ( ) + " is about to return " + results . getPagination ( ) . get ( " count " ) + " results... Please submit a more detailed search query " ) ;
2018-01-17 16:06:35 +01:00
Optional < Results > optionalResults = pages . parallelStream ( )
2020-11-06 17:45:20 +01:00
. map ( page - > getResultsFromUrl ( path + " &page= " + page , jsonDataPath , jsonPaginationPath , contentType , replacedBody , requestType ) )
2021-10-13 16:48:46 +02:00
. filter ( Objects : : nonNull )
2018-01-17 16:06:35 +01:00
. reduce ( ( result1 , result2 ) - > {
result1 . getResults ( ) . addAll ( result2 . getResults ( ) ) ;
return result1 ;
} ) ;
2019-10-01 12:19:39 +02:00
Results remainingResults = optionalResults . orElseGet ( Results : : new ) ;
2018-01-17 16:06:35 +01:00
remainingResults . getResults ( ) . addAll ( results . getResults ( ) ) ;
2020-01-14 12:00:02 +01:00
return remainingResults . getResults ( ) . stream ( ) . peek ( x - > x . put ( " tag " , tag ) ) . collect ( Collectors . toList ( ) ) ;
2018-01-17 16:06:35 +01:00
}
2021-10-13 16:48:46 +02:00
protected Results getResultsFromUrl ( String urlString , DataUrlConfiguration jsonDataPath , String jsonPaginationPath , String contentType , String requestBody , String requestType ) {
2018-01-17 16:06:35 +01:00
try {
2020-10-05 10:26:35 +02:00
RestTemplate restTemplate = new RestTemplate ( ) ;
HttpHeaders headers = new HttpHeaders ( ) ;
2020-11-06 17:45:20 +01:00
HttpEntity < JsonNode > entity ;
2020-10-05 15:21:42 +02:00
ResponseEntity < String > response ;
2020-10-05 10:26:35 +02:00
/ *
2020-10-05 15:21:42 +02:00
* URL url = new URL ( urlString . replaceAll ( " " , " %20 " ) ) ;
*
* HttpURLConnection con = ( HttpURLConnection ) url . openConnection ( ) ;
* con . setRequestMethod ( " GET " ) ;
2020-10-05 10:26:35 +02:00
* /
2020-02-10 17:24:15 +01:00
if ( contentType ! = null & & ! contentType . isEmpty ( ) ) {
2020-10-05 10:26:35 +02:00
headers . setAccept ( Collections . singletonList ( MediaType . valueOf ( contentType ) ) ) ;
2020-11-06 17:45:20 +01:00
headers . setContentType ( MediaType . valueOf ( contentType ) ) ;
2020-02-10 17:24:15 +01:00
}
2020-11-06 17:45:20 +01:00
JsonNode jsonBody = new ObjectMapper ( ) . readTree ( requestBody ) ;
entity = new HttpEntity < > ( jsonBody , headers ) ;
2018-01-17 16:06:35 +01:00
2020-11-06 17:45:20 +01:00
response = restTemplate . exchange ( urlString , HttpMethod . resolve ( requestType ) , entity , String . class ) ;
2020-10-05 10:26:35 +02:00
if ( response . getStatusCode ( ) = = HttpStatus . OK ) { // success
2018-01-17 16:06:35 +01:00
//do here all the parsing
2020-02-10 17:24:15 +01:00
Results results = new Results ( ) ;
2020-10-05 10:26:35 +02:00
if ( response . getHeaders ( ) . get ( " Content-Type " ) . get ( 0 ) . contains ( " json " ) ) {
DocumentContext jsonContext = JsonPath . parse ( response . getBody ( ) ) ;
2020-02-10 17:24:15 +01:00
2022-01-19 11:12:20 +01:00
if ( jsonDataPath . getFieldsUrlConfiguration ( ) . getPath ( ) ! = null ) {
2021-10-13 16:48:46 +02:00
results = RemoteFetcherUtils . getFromJsonWithRecursiveFetching ( jsonContext , jsonDataPath , this , requestBody , requestType ) ;
2020-09-11 11:07:49 +02:00
} else if ( jsonDataPath . getFieldsUrlConfiguration ( ) . getFirstName ( ) ! = null ) {
2021-10-13 16:48:46 +02:00
results = RemoteFetcherUtils . getFromJsonWithFirstAndLastName ( jsonContext , jsonDataPath ) ;
2020-02-10 17:24:15 +01:00
} else {
2022-01-19 11:12:20 +01:00
results = RemoteFetcherUtils . getFromJson ( jsonContext , jsonDataPath ) ;
2020-02-10 17:24:15 +01:00
}
2021-10-13 16:48:46 +02:00
results . setResults ( results . getResults ( ) . stream ( ) . map ( e - > e . entrySet ( ) . stream ( ) . collect ( Collectors . toMap ( x - > this . transformKey ( jsonDataPath , x . getKey ( ) ) , Map . Entry : : getValue ) ) )
. collect ( Collectors . toList ( ) ) ) ;
2020-05-29 16:10:18 +02:00
}
2020-10-05 10:26:35 +02:00
else if ( response . getHeaders ( ) . get ( " Content-Type " ) . get ( 0 ) . contains ( " xml " ) ) {
2020-02-10 17:24:15 +01:00
Class < ? > aClass = Class . forName ( jsonDataPath . getParseClass ( ) ) ;
JAXBContext jaxbContext = JAXBContext . newInstance ( aClass ) ;
Unmarshaller unmarshaller = jaxbContext . createUnmarshaller ( ) ;
2020-10-05 15:21:42 +02:00
StringReader stringReader = new StringReader ( response . getBody ( ) ) ;
2020-10-05 10:26:35 +02:00
Object data = unmarshaller . unmarshal ( stringReader ) ;
2020-02-10 17:24:15 +01:00
Method reader = null ;
if ( jsonDataPath . getParseField ( ) ! = null & & ! jsonDataPath . getParseField ( ) . isEmpty ( ) ) {
2021-10-13 16:48:46 +02:00
String camelCaseGetter = jsonDataPath . getParseField ( ) ! = null & & ! jsonDataPath . getParseField ( ) . isEmpty ( ) ? " get " + jsonDataPath . getParseField ( ) . substring ( 0 , 1 ) . toUpperCase ( ) + jsonDataPath . getParseField ( ) . substring ( 1 ) : " " ;
reader = aClass . getMethod ( camelCaseGetter ) ;
2020-02-10 17:24:15 +01:00
}
ObjectMapper objectMapper = new ObjectMapper ( ) ;
List < Map < String , String > > values = new ArrayList < > ( ) ;
2020-03-27 13:34:11 +01:00
int max = 1 ;
if ( reader ! = null ) {
Object invokedField = reader . invoke ( data ) ;
if ( invokedField instanceof Collection ) {
max = ( ( Collection ) invokedField ) . size ( ) ;
}
}
for ( int i = 0 ; i < max ; i + + ) {
Object value ;
if ( reader ! = null ) {
Object invokedField = reader . invoke ( data ) ;
if ( invokedField instanceof Collection ) {
value = ( ( Collection ) invokedField ) . toArray ( ) [ i ] ;
} else {
value = invokedField ;
}
} else {
value = data ;
}
Map < String , String > map = objectMapper . 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 ( ) ) {
if ( jsonDataPath . getMergedFields ( ) . contains ( entry . getKey ( ) ) ) {
if ( ! finalMap . containsKey ( jsonDataPath . getMergedFieldName ( ) ) ) {
finalMap . put ( jsonDataPath . getMergedFieldName ( ) , entry . getValue ( ) ) ;
} else {
finalMap . put ( jsonDataPath . getMergedFieldName ( ) , finalMap . get ( jsonDataPath . getMergedFieldName ( ) ) + " " + entry . getValue ( ) ) ;
}
2020-02-10 17:24:15 +01:00
} else {
2020-03-27 13:34:11 +01:00
finalMap . put ( entry . getKey ( ) , entry . getValue ( ) ) ;
2020-02-10 17:24:15 +01:00
}
}
2020-03-27 13:34:11 +01:00
values . add ( finalMap ) ;
} else {
values . add ( map ) ;
2020-02-10 17:24:15 +01:00
}
}
results = new Results ( values , new HashMap < > ( 1 , 1 ) ) ;
2019-08-27 13:24:55 +02:00
}
2020-02-10 17:24:15 +01:00
2021-12-10 14:37:39 +01:00
if ( results . getPagination ( ) . size ( ) = = 0 ) {
results . getPagination ( ) . put ( " pages " , 1 ) ;
results . getPagination ( ) . put ( " count " , results . getResults ( ) . size ( ) ) ;
}
2018-01-17 16:06:35 +01:00
return results ;
}
2020-10-05 10:26:35 +02:00
} catch ( Exception exception ) {
2020-01-16 16:46:24 +01:00
logger . error ( exception . getMessage ( ) , exception ) ;
2018-01-17 16:06:35 +01:00
} //maybe print smth...
return null ;
2019-09-05 16:44:25 +02:00
}
2018-01-17 16:06:35 +01:00
2019-09-05 16:44:25 +02:00
private List < Map < String , String > > getAllResultsFromMockUpJson ( String path , String query ) {
List < Map < String , String > > internalResults ;
try {
2019-11-08 14:53:46 +01:00
String filePath = Paths . get ( path ) . toUri ( ) . toURL ( ) . toString ( ) ;
2019-09-05 16:44:25 +02:00
ObjectMapper mapper = new ObjectMapper ( ) ;
2021-06-29 13:33:57 +02:00
internalResults = mapper . readValue ( new File ( filePath ) , new TypeReference < List < Map < String , String > > > ( ) { } ) ;
2020-01-14 12:00:02 +01:00
return searchListMap ( internalResults , query ) ;
2019-09-05 16:44:25 +02:00
} catch ( Exception e ) {
2020-01-16 16:46:24 +01:00
logger . error ( e . getMessage ( ) , e ) ;
2019-09-05 16:44:25 +02:00
return new LinkedList < > ( ) ;
}
}
private List < Map < String , String > > searchListMap ( List < Map < String , String > > internalResults , String query ) {
List < Map < String , String > > list = new LinkedList < > ( ) ;
for ( Map < String , String > map : internalResults )
{
2019-09-16 17:26:18 +02:00
if ( map . get ( " name " ) ! = null & & map . get ( " name " ) . toUpperCase ( ) . contains ( query . toUpperCase ( ) ) ) {
list . add ( map ) ;
}
if ( map . get ( " label " ) ! = null & & map . get ( " label " ) . toUpperCase ( ) . contains ( query . toUpperCase ( ) ) ) {
2019-09-05 16:44:25 +02:00
list . add ( map ) ;
}
}
return list ;
2018-01-17 16:06:35 +01:00
}
2018-06-29 10:29:43 +02:00
private String transformKey ( DataUrlConfiguration dataUrlConfiguration , String key ) {
2023-05-17 12:45:12 +02:00
if ( dataUrlConfiguration . getFieldsUrlConfiguration ( ) . getId ( ) ! = null & & key . equals ( dataUrlConfiguration . getFieldsUrlConfiguration ( ) . getId ( ) . replace ( " ' " , " " ) ) ) {
if ( dataUrlConfiguration . getFieldsUrlConfiguration ( ) . getPid ( ) = = null )
return " pid " ;
else
return " originalId " ;
}
if ( dataUrlConfiguration . getFieldsUrlConfiguration ( ) . getPid ( ) ! = null & & key . equals ( " pid " ) ) return " pid " ;
if ( dataUrlConfiguration . getFieldsUrlConfiguration ( ) . getPidTypeField ( ) ! = null & & key . equals ( " pidTypeField " ) ) return " pidTypeField " ;
2019-10-01 12:19:39 +02:00
if ( dataUrlConfiguration . getFieldsUrlConfiguration ( ) . getDescription ( ) ! = null & & key . equals ( dataUrlConfiguration . getFieldsUrlConfiguration ( ) . getDescription ( ) . replace ( " ' " , " " ) ) ) return " description " ;
2019-10-03 13:06:44 +02:00
if ( dataUrlConfiguration . getFieldsUrlConfiguration ( ) . getUri ( ) ! = null & & key . equals ( dataUrlConfiguration . getFieldsUrlConfiguration ( ) . getUri ( ) . replace ( " ' " , " " ) ) ) return " uri " ;
if ( dataUrlConfiguration . getFieldsUrlConfiguration ( ) . getName ( ) ! = null & & key . equals ( dataUrlConfiguration . getFieldsUrlConfiguration ( ) . getName ( ) . replace ( " ' " , " " ) ) ) return " name " ;
2019-10-01 12:19:39 +02:00
if ( dataUrlConfiguration . getFieldsUrlConfiguration ( ) . getSource ( ) ! = null & & key . equals ( dataUrlConfiguration . getFieldsUrlConfiguration ( ) . getSource ( ) . replace ( " ' " , " " ) ) ) return " source " ;
2019-10-03 13:06:44 +02:00
if ( dataUrlConfiguration . getFieldsUrlConfiguration ( ) . getCount ( ) ! = null & & key . equals ( dataUrlConfiguration . getFieldsUrlConfiguration ( ) . getCount ( ) . replace ( " ' " , " " ) ) ) return " count " ;
2020-02-10 17:24:15 +01:00
if ( dataUrlConfiguration . getFieldsUrlConfiguration ( ) . getPath ( ) ! = null & & key . equals ( dataUrlConfiguration . getFieldsUrlConfiguration ( ) . getPath ( ) . replace ( " ' " , " " ) ) ) return " path " ;
if ( dataUrlConfiguration . getFieldsUrlConfiguration ( ) . getHost ( ) ! = null & & key . equals ( dataUrlConfiguration . getFieldsUrlConfiguration ( ) . getHost ( ) . replace ( " ' " , " " ) ) ) return " host " ;
2018-06-29 10:29:43 +02:00
return null ;
}
2018-01-17 16:06:35 +01:00
2017-11-21 17:29:16 +01:00
}