2017-12-07 18:57:12 +01:00
|
|
|
package eu.dnetlib.repo.manager.service.controllers;
|
|
|
|
|
2018-01-15 21:18:16 +01:00
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
2017-12-07 18:57:12 +01:00
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
import eu.dnetlib.domain.data.Repository;
|
|
|
|
import eu.dnetlib.repo.manager.shared.BrokerException;
|
2018-01-15 21:18:16 +01:00
|
|
|
import eu.dnetlib.repo.manager.shared.Term;
|
2017-12-07 18:57:12 +01:00
|
|
|
import eu.dnetlib.repo.manager.shared.Tuple;
|
|
|
|
import eu.dnetlib.repo.manager.shared.broker.*;
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
import org.springframework.core.ParameterizedTypeReference;
|
|
|
|
import org.springframework.http.HttpEntity;
|
2018-01-24 16:10:47 +01:00
|
|
|
import org.springframework.http.HttpHeaders;
|
2017-12-07 18:57:12 +01:00
|
|
|
import org.springframework.http.HttpMethod;
|
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
|
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import org.springframework.util.LinkedMultiValueMap;
|
|
|
|
import org.springframework.util.MultiValueMap;
|
|
|
|
import org.springframework.web.bind.annotation.PathVariable;
|
|
|
|
import org.springframework.web.client.RestClientException;
|
|
|
|
import org.springframework.web.client.RestTemplate;
|
2018-01-24 16:10:47 +01:00
|
|
|
import org.springframework.web.util.UriComponents;
|
2017-12-07 18:57:12 +01:00
|
|
|
import org.springframework.web.util.UriComponentsBuilder;
|
|
|
|
|
2018-01-15 21:18:16 +01:00
|
|
|
import javax.annotation.PostConstruct;
|
2017-12-07 18:57:12 +01:00
|
|
|
import java.io.IOException;
|
2018-01-15 21:18:16 +01:00
|
|
|
import java.io.InputStream;
|
|
|
|
import java.net.URL;
|
2017-12-07 18:57:12 +01:00
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
@Component
|
|
|
|
public class BrokerApiImpl implements BrokerApi {
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private RepositoryApiImpl repoAPI;
|
|
|
|
@Value("${services.broker.url}:${services.broker.port}/${services.broker.api}${services.broker.openaire}")
|
|
|
|
private String openairePath;
|
|
|
|
@Value("${services.broker.url}:${services.broker.port}/${services.broker.api}")
|
|
|
|
private String apiPath;
|
2018-01-15 21:18:16 +01:00
|
|
|
@Value("${topic_types.url}")
|
|
|
|
private String topicsURL;
|
2017-12-07 18:57:12 +01:00
|
|
|
|
|
|
|
private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger
|
|
|
|
.getLogger(BrokerApiImpl.class);
|
|
|
|
|
2018-01-24 16:10:47 +01:00
|
|
|
private RestTemplate restTemplate = null;
|
|
|
|
|
|
|
|
private HttpHeaders httpHeaders;
|
2018-01-15 21:18:16 +01:00
|
|
|
|
|
|
|
private HashMap<String,Term> topics = new HashMap<String, Term>();
|
|
|
|
|
|
|
|
@PostConstruct
|
|
|
|
private void initDnetTopicsMap() {
|
|
|
|
|
2018-01-24 16:10:47 +01:00
|
|
|
restTemplate = new RestTemplate();
|
|
|
|
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
|
|
|
|
|
|
|
|
httpHeaders = new HttpHeaders();
|
|
|
|
httpHeaders.set("Content-Type", "application/json");
|
|
|
|
|
2018-01-15 21:18:16 +01:00
|
|
|
LOGGER.debug("Init dnet topics!");
|
|
|
|
InputStream is = null;
|
|
|
|
try {
|
|
|
|
is = new URL(topicsURL).openStream();
|
|
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
|
|
JsonNode root = mapper.readTree(is);
|
|
|
|
for (JsonNode term : root.path("terms") )
|
|
|
|
topics.put(term.path("code").textValue(), parseTerm(term));
|
|
|
|
} catch (IOException e) {
|
2018-02-02 14:28:00 +01:00
|
|
|
LOGGER.debug(e);
|
2018-01-15 21:18:16 +01:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private Term parseTerm(JsonNode term) {
|
|
|
|
return new Term(term.path("englishName").textValue(),term.path("nativeName").textValue(),
|
|
|
|
term.path("encoding").textValue(),term.path("code").textValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-07 18:57:12 +01:00
|
|
|
@Override
|
|
|
|
public DatasourcesBroker getDatasourcesOfUser(String params) throws JSONException {
|
|
|
|
JSONObject json_params = new JSONObject(params);
|
|
|
|
DatasourcesBroker ret = new DatasourcesBroker();
|
|
|
|
|
|
|
|
String userEmail = json_params.getString("userEmail");
|
|
|
|
boolean includeShared = Boolean.parseBoolean( json_params.getString("includeShared") );
|
|
|
|
boolean includeByOthers = Boolean.parseBoolean( json_params.getString("includeByOthers") );
|
|
|
|
|
|
|
|
try {
|
|
|
|
ret.setDatasourcesOfUser(getDatasourcesOfUserType(getRepositoriesOfUser(userEmail)));
|
|
|
|
if (includeShared) {
|
|
|
|
//TODO whatever nikonas was saying
|
|
|
|
List<String> sharedDatasourceIds = new ArrayList<String>();
|
|
|
|
ret.setSharedDatasources(getDatasourcesOfUserType(getRepositoriesByIds(sharedDatasourceIds)));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (includeByOthers) {
|
|
|
|
ret.setDatasourcesOfOthers(getDatasourcesOfUserType(getRepositoriesOfUser(userEmail)));
|
|
|
|
}
|
|
|
|
} catch (BrokerException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<BrowseEntry> getTopicsForDatasource(@PathVariable("datasourceName") String datasourceName) throws BrokerException {
|
|
|
|
final String service = "/topicsForDatasource";
|
|
|
|
|
|
|
|
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(openairePath + service)
|
|
|
|
.queryParam("ds", datasourceName);
|
|
|
|
|
|
|
|
ResponseEntity<List<BrowseEntry>> resp;
|
|
|
|
try {
|
2018-01-24 16:10:47 +01:00
|
|
|
resp = restTemplate.exchange(
|
2017-12-07 18:57:12 +01:00
|
|
|
builder.build().encode().toUri(),
|
|
|
|
HttpMethod.GET,
|
|
|
|
null,
|
|
|
|
new ParameterizedTypeReference<List<BrowseEntry>>() {
|
|
|
|
});
|
|
|
|
} catch (RestClientException e) {
|
|
|
|
throw new BrokerException(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.getBody();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-02-06 15:59:03 +01:00
|
|
|
public EventsPage advancedShowEvents(String page,String size,AdvQueryObject advQueryObject) throws BrokerException, JSONException ,IOException {
|
|
|
|
/*JSONObject json_params = new JSONObject(params);
|
2017-12-07 18:57:12 +01:00
|
|
|
|
|
|
|
String page = json_params.getString("page");
|
|
|
|
String pagesize = json_params.getString("pagesize");
|
|
|
|
String json_advQueryObject = json_params.getString("advQueryObject");
|
|
|
|
|
|
|
|
ObjectMapper mapper = new ObjectMapper();
|
2018-02-06 15:59:03 +01:00
|
|
|
AdvQueryObject advQueryObject = mapper.readValue(json_advQueryObject, AdvQueryObject.class);*/
|
2017-12-07 18:57:12 +01:00
|
|
|
|
|
|
|
final String service = "/events/{page}/{pageSize}";
|
|
|
|
|
|
|
|
Map<String, Long> uriParams = new HashMap<>();
|
|
|
|
uriParams.put("page", Long.parseLong(page));
|
2018-02-06 15:59:03 +01:00
|
|
|
uriParams.put("pageSize", Long.parseLong(size));
|
2017-12-07 18:57:12 +01:00
|
|
|
|
|
|
|
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(openairePath + service);
|
|
|
|
|
|
|
|
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
|
|
|
|
advQueryObject.setPage(Long.parseLong(page));
|
2018-02-06 15:59:03 +01:00
|
|
|
HttpEntity<AdvQueryObject> entity = new HttpEntity<>(advQueryObject, httpHeaders);
|
2017-12-07 18:57:12 +01:00
|
|
|
ResponseEntity<EventsPage> resp;
|
|
|
|
try {
|
2018-01-24 16:10:47 +01:00
|
|
|
resp = restTemplate.exchange(
|
2017-12-07 18:57:12 +01:00
|
|
|
builder.buildAndExpand(uriParams).encode().toUri(),
|
|
|
|
HttpMethod.POST,
|
|
|
|
entity,
|
|
|
|
new ParameterizedTypeReference<EventsPage>() {
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} catch (RestClientException e) {
|
|
|
|
throw new BrokerException(e);
|
|
|
|
}
|
|
|
|
return resp.getBody();
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private List<Tuple<BrowseEntry, String>> getDatasourcesOfUserType(List<Repository> repositories) throws BrokerException {
|
|
|
|
|
|
|
|
List<Tuple<BrowseEntry, String>> entries = new ArrayList<>();
|
|
|
|
for (Repository repo : repositories) {
|
|
|
|
BrowseEntry temp = new BrowseEntry();
|
|
|
|
temp.setValue(repo.getOfficialName());
|
|
|
|
temp.setSize(new Long(0));
|
|
|
|
for (BrowseEntry e : getTopicsForDatasource(repo.getOfficialName())) {
|
|
|
|
temp.setSize(temp.getSize() + e.getSize());
|
|
|
|
}
|
|
|
|
Tuple<BrowseEntry, String> tup = new Tuple<>(temp, repo.getLogoUrl());
|
|
|
|
entries.add(tup);
|
|
|
|
}
|
|
|
|
|
|
|
|
// sort the collection by the second field of the tuple which is size
|
|
|
|
Collections.sort(entries, new Comparator<Tuple<BrowseEntry, String>>() {
|
|
|
|
@Override
|
|
|
|
public int compare(Tuple<BrowseEntry, String> e1, Tuple<BrowseEntry, String> e2) {
|
|
|
|
return (int) (e2.getFirst().getSize().longValue() - e1.getFirst().getSize().longValue());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return entries;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private List<Repository> getRepositoriesOfUser(String userEmail) throws JSONException {
|
|
|
|
|
|
|
|
int page = 0;
|
|
|
|
int size = 50;
|
|
|
|
List<Repository> rs ;
|
|
|
|
List<Repository> resultSet = new ArrayList<>();
|
|
|
|
|
|
|
|
while (true){
|
|
|
|
rs = repoAPI.getRepositoriesOfUser(userEmail, String.valueOf(page), String.valueOf(size));
|
|
|
|
resultSet.addAll(rs);
|
|
|
|
page+=1;
|
|
|
|
if(rs.size() == 0) break;
|
|
|
|
}
|
|
|
|
return resultSet;
|
|
|
|
}
|
|
|
|
|
|
|
|
private List<Repository> getRepositoriesByIds(List<String> sharedDatasourceIds) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-02-06 15:59:03 +01:00
|
|
|
public EventsPage showEvents(String datasourceName,String topic,String page) throws BrokerException, JSONException {
|
2017-12-07 18:57:12 +01:00
|
|
|
|
2018-02-06 15:59:03 +01:00
|
|
|
/*JSONObject json_params = new JSONObject(params);
|
2017-12-07 18:57:12 +01:00
|
|
|
|
|
|
|
String datasourceName = json_params.getString("datasourceName");
|
|
|
|
String topic = json_params.getString("topic");
|
2018-02-06 15:59:03 +01:00
|
|
|
String page = json_params.getString("page");*/
|
2017-12-07 18:57:12 +01:00
|
|
|
|
|
|
|
final String service = "/showEvents";
|
|
|
|
|
|
|
|
//build the uri params
|
|
|
|
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(openairePath + service)
|
|
|
|
.queryParam("ds", datasourceName)
|
|
|
|
.queryParam("topic", topic)
|
|
|
|
.queryParam("page", page);
|
|
|
|
|
|
|
|
ResponseEntity<EventsPage> resp;
|
|
|
|
try {
|
|
|
|
//communicate with endpoint
|
2018-01-24 16:10:47 +01:00
|
|
|
resp = restTemplate.exchange(
|
2017-12-07 18:57:12 +01:00
|
|
|
builder.build().encode().toUri(),
|
|
|
|
HttpMethod.GET,
|
|
|
|
null,
|
|
|
|
new ParameterizedTypeReference<EventsPage>() {
|
|
|
|
});
|
|
|
|
} catch (RestClientException e) {
|
|
|
|
throw new BrokerException(e);
|
|
|
|
}
|
|
|
|
return resp.getBody();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-01-25 16:14:19 +01:00
|
|
|
public Map<String, List<SimpleSubscriptionDesc>> getSimpleSubscriptionsOfUser(@PathVariable("userEmail") String userEmail) throws BrokerException {
|
2017-12-07 18:57:12 +01:00
|
|
|
|
|
|
|
final String service = "/subscriptions";
|
|
|
|
|
|
|
|
//build the uri params
|
|
|
|
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(openairePath + service)
|
|
|
|
.queryParam("email", userEmail);
|
|
|
|
|
|
|
|
//create new template engine
|
|
|
|
ResponseEntity<Map<String, List<SimpleSubscriptionDesc>>> resp;
|
|
|
|
try {
|
|
|
|
//communicate with endpoint
|
2018-01-24 16:10:47 +01:00
|
|
|
resp = restTemplate.exchange(
|
2017-12-07 18:57:12 +01:00
|
|
|
builder.build().encode().toUri(),
|
|
|
|
HttpMethod.GET,
|
|
|
|
null,
|
|
|
|
new ParameterizedTypeReference<Map<String, List<SimpleSubscriptionDesc>>>() {
|
|
|
|
});
|
|
|
|
} catch (RestClientException e) {
|
|
|
|
LOGGER.debug("Error " , e);
|
|
|
|
throw new BrokerException(e);
|
|
|
|
}
|
|
|
|
return resp.getBody();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Subscription subscribe(OpenaireSubscription obj) throws BrokerException {
|
|
|
|
final String service = "/subscribe";
|
|
|
|
|
|
|
|
//build the uri params
|
|
|
|
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(openairePath + service);
|
|
|
|
|
2018-01-24 16:10:47 +01:00
|
|
|
HttpEntity<OpenaireSubscription> entity = new HttpEntity<>(obj, httpHeaders);
|
2017-12-07 18:57:12 +01:00
|
|
|
|
|
|
|
//create new template engine
|
|
|
|
RestTemplate template = new RestTemplate();
|
|
|
|
template.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
|
|
|
|
ResponseEntity<Subscription> resp;
|
|
|
|
try {
|
|
|
|
//communicate with endpoint
|
2018-01-24 16:10:47 +01:00
|
|
|
resp = restTemplate.exchange(
|
2017-12-07 18:57:12 +01:00
|
|
|
builder.build().encode().toUri(),
|
|
|
|
HttpMethod.POST,
|
|
|
|
entity,
|
|
|
|
new ParameterizedTypeReference<Subscription>() {
|
|
|
|
});
|
|
|
|
} catch (RestClientException e) {
|
|
|
|
throw new BrokerException(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.getBody();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void unsubscribe(@PathVariable("subscriptionId") String subscriptionId) throws BrokerException {
|
|
|
|
final String service = "/subscriptions/" + subscriptionId;
|
|
|
|
|
|
|
|
//build the uri params
|
|
|
|
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(apiPath + service);
|
|
|
|
|
|
|
|
try {
|
|
|
|
//communicate with endpoint
|
2018-01-24 16:10:47 +01:00
|
|
|
restTemplate.exchange(
|
2017-12-07 18:57:12 +01:00
|
|
|
builder.build().encode().toUri(),
|
|
|
|
HttpMethod.DELETE,
|
|
|
|
null,
|
|
|
|
new ParameterizedTypeReference<Void>() {
|
|
|
|
});
|
|
|
|
} catch (RestClientException e) {
|
|
|
|
throw new BrokerException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Subscription getSubscription(@PathVariable("subscriptionId") String subscriptionId) throws BrokerException {
|
|
|
|
final String service = "/subscriptions/" + subscriptionId;
|
|
|
|
|
|
|
|
//build the uri params
|
|
|
|
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(apiPath + service);
|
|
|
|
|
|
|
|
ResponseEntity<Subscription> resp;
|
|
|
|
try {
|
|
|
|
//communicate with endpoint
|
2018-01-24 16:10:47 +01:00
|
|
|
resp = restTemplate.exchange(
|
2017-12-07 18:57:12 +01:00
|
|
|
builder.build().encode().toUri(),
|
|
|
|
HttpMethod.GET,
|
|
|
|
null,
|
|
|
|
new ParameterizedTypeReference<Subscription>() {
|
|
|
|
});
|
|
|
|
} catch (RestClientException e) {
|
|
|
|
throw new BrokerException(e);
|
|
|
|
}
|
|
|
|
return resp.getBody();
|
|
|
|
}
|
|
|
|
|
2018-01-15 21:18:16 +01:00
|
|
|
@Override
|
|
|
|
public Map<String, Term> getDnetTopics() throws BrokerException {
|
|
|
|
return topics;
|
|
|
|
}
|
2017-12-07 18:57:12 +01:00
|
|
|
|
2018-01-24 16:10:47 +01:00
|
|
|
@Override
|
|
|
|
public EventsPage getNotificationsBySubscriptionId(@PathVariable("subscriptionId") String subscriptionId,
|
|
|
|
@PathVariable("page") String page,
|
|
|
|
@PathVariable("size") String size
|
|
|
|
) throws BrokerException {
|
|
|
|
|
|
|
|
UriComponents uriComponents = UriComponentsBuilder
|
2018-02-02 14:28:00 +01:00
|
|
|
.fromHttpUrl(openairePath + "/notifications/")
|
2018-01-24 16:10:47 +01:00
|
|
|
.path("/{id}/{page}/{size}/")
|
|
|
|
.build().expand(subscriptionId,page, size).encode();
|
|
|
|
|
|
|
|
ResponseEntity<EventsPage> resp;
|
|
|
|
try {
|
|
|
|
resp = restTemplate.exchange(
|
|
|
|
uriComponents.toUri(),
|
|
|
|
HttpMethod.GET,
|
|
|
|
null,
|
|
|
|
new ParameterizedTypeReference<EventsPage>() {
|
|
|
|
});
|
|
|
|
} catch (RestClientException e) {
|
|
|
|
throw new BrokerException(e);
|
|
|
|
}
|
|
|
|
return resp.getBody();
|
|
|
|
}
|
|
|
|
|
2018-01-25 16:14:19 +01:00
|
|
|
@Override
|
|
|
|
public Map<String, List<Subscription>> getSubscriptionsOfUser(@PathVariable("userEmail") String userEmail)
|
|
|
|
throws BrokerException {
|
|
|
|
|
|
|
|
Map<String, List<SimpleSubscriptionDesc>> simpleSubs = getSimpleSubscriptionsOfUser(userEmail);
|
|
|
|
Map<String,List<Subscription>> subs = new HashMap<>();
|
|
|
|
List<Subscription> subscriptions = null;
|
|
|
|
|
|
|
|
for(String s:simpleSubs.keySet()){
|
|
|
|
List<SimpleSubscriptionDesc> simpleSubscriptionDescs = simpleSubs.get(s);
|
|
|
|
for(SimpleSubscriptionDesc simpleSubscriptionDesc : simpleSubscriptionDescs) {
|
|
|
|
subscriptions = new ArrayList<>();
|
|
|
|
subscriptions.add(getSubscription(simpleSubscriptionDesc.getId()));
|
|
|
|
}
|
|
|
|
subs.put(s,subscriptions);
|
|
|
|
}
|
|
|
|
return subs;
|
|
|
|
}
|
|
|
|
|
2017-12-07 18:57:12 +01:00
|
|
|
|
|
|
|
}
|