This commit is contained in:
Antonis Lempesis 2021-10-07 13:49:51 +00:00
parent 174059b991
commit 17c6fa5378
4 changed files with 139 additions and 322 deletions

View File

@ -8,9 +8,6 @@ import org.springframework.security.core.Authentication;
public interface EmailUtils { public interface EmailUtils {
void reportException(Exception exception);
void sendAdministratorRequestToEnableMetrics(PiwikInfo piwikInfo) throws Exception; void sendAdministratorRequestToEnableMetrics(PiwikInfo piwikInfo) throws Exception;
void sendUserRequestToEnableMetrics(PiwikInfo piwikInfo) throws Exception; void sendUserRequestToEnableMetrics(PiwikInfo piwikInfo) throws Exception;

View File

@ -4,7 +4,6 @@ import eu.dnetlib.domain.data.PiwikInfo;
import eu.dnetlib.domain.data.Repository; import eu.dnetlib.domain.data.Repository;
import eu.dnetlib.domain.data.RepositoryInterface; import eu.dnetlib.domain.data.RepositoryInterface;
import eu.dnetlib.domain.functionality.validator.JobForValidation; import eu.dnetlib.domain.functionality.validator.JobForValidation;
import eu.dnetlib.repo.manager.config.CascadingPropertyLoader;
import eu.dnetlib.repo.manager.domain.ValidationServiceException; import eu.dnetlib.repo.manager.domain.ValidationServiceException;
import eu.dnetlib.utils.MailLibrary; import eu.dnetlib.utils.MailLibrary;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
@ -15,76 +14,36 @@ import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct; import java.util.Collections;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@Component("emailUtils") @Component("emailUtils")
public class EmailUtilsImpl implements EmailUtils { public class EmailUtilsImpl implements EmailUtils {
private static Logger LOGGER = Logger.getLogger(EmailUtilsImpl.class); private final static Logger LOGGER = Logger.getLogger(EmailUtilsImpl.class);
private List<String> specialRecipients = new ArrayList<String>();
private boolean override = false, logonly = false;
private String overrideEmail = null, from = null;
private final MailLibrary mailLibrary; private final MailLibrary mailLibrary;
private final CascadingPropertyLoader pLoader;
private final RepositoryService repositoryService; private final RepositoryService repositoryService;
@Value("${services.repo-manager.baseUrl}") @Value("${services.repomanager.baseUrl}")
private String baseUrl; private String baseUrl;
@Value("${services.repo-manager.adminEmail}")
private String adminEmail;
@Value("${services.repomanager.usagestats.adminEmail}") @Value("${services.repomanager.usagestats.adminEmail}")
private String usageStatsAdminEmail; private String usageStatsAdminEmail;
@Value("${services.provide.adminEmail}") @Value("${services.repomanager.provide.adminEmail}")
private String provideAdminEmail; private String provideAdminEmail;
@Value("${validator.results.url}") @Value("${validator.results.url}")
private String valBaseUrl; private String valBaseUrl;
@Autowired @Autowired
EmailUtilsImpl(MailLibrary mailLibrary, CascadingPropertyLoader pLoader, EmailUtilsImpl(MailLibrary mailLibrary, RepositoryService repositoryService) {
RepositoryService repositoryService) {
this.mailLibrary = mailLibrary; this.mailLibrary = mailLibrary;
this.pLoader = pLoader;
this.repositoryService = repositoryService; this.repositoryService = repositoryService;
} }
@PostConstruct
public void init(){
System.out.println("url -> " + this.baseUrl);
}
@Override
public void reportException(Exception exception) {
Writer writer = new StringWriter();
PrintWriter printWriter = new PrintWriter(writer);
exception.printStackTrace(printWriter);
List<String> recipients = new ArrayList<String>();
try {
recipients.add(this.adminEmail);
String message = "An exception has occurred:\n"+writer.toString();
String subject = "Automatic Bug Report";
this.sendMail(recipients, subject, message, false, null);
} catch (Exception e) {
LOGGER.error("Error sending error report", e);
}
}
@Override @Override
public void sendAdministratorRequestToEnableMetrics(PiwikInfo piwikInfo) throws Exception { public void sendAdministratorRequestToEnableMetrics(PiwikInfo piwikInfo) throws Exception {
@ -106,7 +65,7 @@ public class EmailUtilsImpl implements EmailUtils {
"Best,\n" + "Best,\n" +
"The OpenAIRE team"; "The OpenAIRE team";
this.sendMail(this.usageStatsAdminEmail, subject, message, false, null); this.sendMail(this.usageStatsAdminEmail, subject, message);
} catch (Exception e) { } catch (Exception e) {
LOGGER.error("Error while sending request to enable metrics email to administrator: " + this.usageStatsAdminEmail, e); LOGGER.error("Error while sending request to enable metrics email to administrator: " + this.usageStatsAdminEmail, e);
@ -143,7 +102,7 @@ public class EmailUtilsImpl implements EmailUtils {
"Best,\n" + "Best,\n" +
"The OpenAIRE team"; "The OpenAIRE team";
this.sendMail(piwikInfo.getRequestorEmail(), subject, message, false, null); this.sendMail(piwikInfo.getRequestorEmail(), subject, message);
} catch (Exception e) { } catch (Exception e) {
LOGGER.error("Error while sending request to enable metrics email to user: " + piwikInfo.getRequestorEmail(), e); LOGGER.error("Error while sending request to enable metrics email to user: " + piwikInfo.getRequestorEmail(), e);
@ -170,7 +129,7 @@ public class EmailUtilsImpl implements EmailUtils {
"Best,\n" + "Best,\n" +
"The OpenAIRE team"; "The OpenAIRE team";
this.sendMail(this.usageStatsAdminEmail, subject, message, false, null); this.sendMail(this.usageStatsAdminEmail, subject, message);
} catch (Exception e) { } catch (Exception e) {
LOGGER.error("Error while sending metrics enabled notification email to administator: " + this.usageStatsAdminEmail, e); LOGGER.error("Error while sending metrics enabled notification email to administator: " + this.usageStatsAdminEmail, e);
@ -198,7 +157,7 @@ public class EmailUtilsImpl implements EmailUtils {
"Best,\n" + "Best,\n" +
"The OpenAIRE team"; "The OpenAIRE team";
this.sendMail(piwikInfo.getRequestorEmail(), subject, message, false, null); this.sendMail(piwikInfo.getRequestorEmail(), subject, message);
} catch (Exception e) { } catch (Exception e) {
LOGGER.error("Error while sending metrics enabled notification email to user: " + piwikInfo.getRequestorEmail(), e); LOGGER.error("Error while sending metrics enabled notification email to user: " + piwikInfo.getRequestorEmail(), e);
@ -224,7 +183,7 @@ public class EmailUtilsImpl implements EmailUtils {
"Regards,\n" + "Regards,\n" +
"the OpenAIRE technical team\n"; "the OpenAIRE technical team\n";
this.sendMail(this.provideAdminEmail, subject, message, false, null); this.sendMail(this.provideAdminEmail, subject, message);
} catch (Exception e) { } catch (Exception e) {
LOGGER.error("Error while sending registration notification email to the administrator", e); LOGGER.error("Error while sending registration notification email to the administrator", e);
@ -250,7 +209,7 @@ public class EmailUtilsImpl implements EmailUtils {
"Regards,\n" + "Regards,\n" +
"the OpenAIRE technical team\n"; "the OpenAIRE technical team\n";
this.sendMail(repository.getRegisteredBy(), subject, message, false, null); this.sendMail(repository.getRegisteredBy(), subject, message);
} catch (Exception e) { } catch (Exception e) {
LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredBy(), e); LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredBy(), e);
@ -285,7 +244,7 @@ public class EmailUtilsImpl implements EmailUtils {
"Regards,\n" + "Regards,\n" +
"the OpenAIRE technical team\n"; "the OpenAIRE technical team\n";
this.sendMail(this.provideAdminEmail, subject, message, false, null); this.sendMail(this.provideAdminEmail, subject, message);
} catch (Exception e) { } catch (Exception e) {
LOGGER.error("Error while sending registration of interface notification email to the administrator", e); LOGGER.error("Error while sending registration of interface notification email to the administrator", e);
@ -319,7 +278,7 @@ public class EmailUtilsImpl implements EmailUtils {
"Regards,\n" + "Regards,\n" +
"the OpenAIRE technical team\n"; "the OpenAIRE technical team\n";
this.sendMail(repository.getRegisteredBy(), subject, message, false, null); this.sendMail(repository.getRegisteredBy(), subject, message);
} catch (Exception e) { } catch (Exception e) {
LOGGER.error("Error while sending registration of interface notification email to user: " + repository.getRegisteredBy(), e); LOGGER.error("Error while sending registration of interface notification email to user: " + repository.getRegisteredBy(), e);
@ -352,7 +311,7 @@ public class EmailUtilsImpl implements EmailUtils {
"Regards,\n" + "Regards,\n" +
"the OpenAIRE technical team\n"; "the OpenAIRE technical team\n";
this.sendMail(repository.getRegisteredBy(), subject, message, false, null); this.sendMail(repository.getRegisteredBy(), subject, message);
} catch (Exception e) { } catch (Exception e) {
LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredBy(), e); LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredBy(), e);
@ -385,7 +344,7 @@ public class EmailUtilsImpl implements EmailUtils {
"Regards,\n" + "Regards,\n" +
"the OpenAIRE technical team\n"; "the OpenAIRE technical team\n";
this.sendMail(this.provideAdminEmail, subject, message, false, null); this.sendMail(this.provideAdminEmail, subject, message);
} catch (Exception e) { } catch (Exception e) {
LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredBy(), e); LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredBy(), e);
@ -417,7 +376,7 @@ public class EmailUtilsImpl implements EmailUtils {
"Regards,\n" + "Regards,\n" +
"the OpenAIRE technical team\n"; "the OpenAIRE technical team\n";
this.sendMail(repository.getRegisteredBy(), subject, message, false, null); this.sendMail(repository.getRegisteredBy(), subject, message);
} catch (Exception e) { } catch (Exception e) {
LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredBy(), e); LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredBy(), e);
@ -450,7 +409,7 @@ public class EmailUtilsImpl implements EmailUtils {
"Regards,\n" + "Regards,\n" +
"the OpenAIRE technical team\n"; "the OpenAIRE technical team\n";
this.sendMail(this.provideAdminEmail, subject, message, false, null); this.sendMail(this.provideAdminEmail, subject, message);
} catch (Exception e) { } catch (Exception e) {
LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredBy(), e); LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredBy(), e);
@ -481,7 +440,7 @@ public class EmailUtilsImpl implements EmailUtils {
"Regards,\n" + "Regards,\n" +
"the OpenAIRE technical team\n"; "the OpenAIRE technical team\n";
this.sendMail(issuer, subject, message, false, null); this.sendMail(issuer, subject, message);
} catch (Exception e) { } catch (Exception e) {
LOGGER.error("Error while sending registration notification email to the administrator", e); LOGGER.error("Error while sending registration notification email to the administrator", e);
@ -512,7 +471,7 @@ public class EmailUtilsImpl implements EmailUtils {
"Regards,\n" + "Regards,\n" +
"the OpenAIRE technical team\n"; "the OpenAIRE technical team\n";
this.sendMail(this.provideAdminEmail, subject, message, false, null); this.sendMail(this.provideAdminEmail, subject, message);
} catch (Exception e) { } catch (Exception e) {
LOGGER.error("Error while sending registration notification email to the administrator", e); LOGGER.error("Error while sending registration notification email to the administrator", e);
@ -545,7 +504,7 @@ public class EmailUtilsImpl implements EmailUtils {
"Regards,\n" + "Regards,\n" +
"the OpenAIRE technical team\n"; "the OpenAIRE technical team\n";
this.sendMail(issuer, subject, message, false, null); this.sendMail(issuer, subject, message);
} catch (Exception e) { } catch (Exception e) {
LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredBy(), e); LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredBy(), e);
@ -578,7 +537,7 @@ public class EmailUtilsImpl implements EmailUtils {
"Regards,\n" + "Regards,\n" +
"the OpenAIRE technical team\n"; "the OpenAIRE technical team\n";
this.sendMail(this.provideAdminEmail, subject, message, false, null); this.sendMail(this.provideAdminEmail, subject, message);
} catch (Exception e) { } catch (Exception e) {
LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredBy(), e); LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredBy(), e);
@ -601,7 +560,7 @@ public class EmailUtilsImpl implements EmailUtils {
"Regards,\n" + "Regards,\n" +
"the OpenAIRE technical team\n"; "the OpenAIRE technical team\n";
this.sendMail(issuer, subject, message, false, null); this.sendMail(issuer, subject, message);
} catch (Exception e) { } catch (Exception e) {
LOGGER.error("Error while sending validation submission notification email to user: " + issuer, e); LOGGER.error("Error while sending validation submission notification email to user: " + issuer, e);
@ -624,7 +583,7 @@ public class EmailUtilsImpl implements EmailUtils {
"Regards,\n" + "Regards,\n" +
"the OpenAIRE technical team\n"; "the OpenAIRE technical team\n";
this.sendMail(this.provideAdminEmail, subject, message, false, null); this.sendMail(this.provideAdminEmail, subject, message);
} catch (Exception e) { } catch (Exception e) {
LOGGER.error("Error while sending validation submission notification email to user: " + issuer, e); LOGGER.error("Error while sending validation submission notification email to user: " + issuer, e);
@ -644,7 +603,7 @@ public class EmailUtilsImpl implements EmailUtils {
"Regards,\n" + "Regards,\n" +
"the OpenAIRE technical team\n"; "the OpenAIRE technical team\n";
this.sendMail(this.provideAdminEmail, subject, message, false, null); this.sendMail(this.provideAdminEmail, subject, message);
} catch (Exception e) { } catch (Exception e) {
LOGGER.error("Error while sending validation submission notification email to user: " + issuer, e); LOGGER.error("Error while sending validation submission notification email to user: " + issuer, e);
@ -666,7 +625,7 @@ public class EmailUtilsImpl implements EmailUtils {
"Regards,\n" + "Regards,\n" +
"the OpenAIRE technical team\n"; "the OpenAIRE technical team\n";
this.sendMail(this.provideAdminEmail, subject, message, false, null); this.sendMail(this.provideAdminEmail, subject, message);
} catch (Exception e) { } catch (Exception e) {
LOGGER.error("Error while sending registration notification email to the administrator", e); LOGGER.error("Error while sending registration notification email to the administrator", e);
@ -690,7 +649,7 @@ public class EmailUtilsImpl implements EmailUtils {
"Regards,\n" + "Regards,\n" +
"the OpenAIRE technical team\n"; "the OpenAIRE technical team\n";
this.sendMail(repository.getRegisteredBy(), subject, message, false, null); this.sendMail(repository.getRegisteredBy(), subject, message);
} catch (Exception e) { } catch (Exception e) {
LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredBy(), e); LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredBy(), e);
@ -723,7 +682,7 @@ public class EmailUtilsImpl implements EmailUtils {
"Regards,\n" + "Regards,\n" +
"the OpenAIRE technical team\n"; "the OpenAIRE technical team\n";
this.sendMail(this.provideAdminEmail, subject, message, false, null); this.sendMail(this.provideAdminEmail, subject, message);
} catch (Exception e) { } catch (Exception e) {
LOGGER.error("Error while sending registration notification email to the administrator", e); LOGGER.error("Error while sending registration notification email to the administrator", e);
@ -757,7 +716,7 @@ public class EmailUtilsImpl implements EmailUtils {
"Regards,\n" + "Regards,\n" +
"the OpenAIRE technical team\n"; "the OpenAIRE technical team\n";
this.sendMail(repository.getRegisteredBy(), subject, message, false, null); this.sendMail(repository.getRegisteredBy(), subject, message);
} catch (Exception e) { } catch (Exception e) {
LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredBy(), e); LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredBy(), e);
@ -780,7 +739,7 @@ public class EmailUtilsImpl implements EmailUtils {
"Regards,\n" + "Regards,\n" +
"the OpenAIRE technical team\n"; "the OpenAIRE technical team\n";
this.sendMail(jobForValidation.getUserEmail(), subject, message, false, null); this.sendMail(jobForValidation.getUserEmail(), subject, message);
} catch (Exception e) { } catch (Exception e) {
LOGGER.error("Error while sending validation submission notification email to user: " + jobForValidation.getUserEmail(), e); LOGGER.error("Error while sending validation submission notification email to user: " + jobForValidation.getUserEmail(), e);
@ -833,73 +792,17 @@ public class EmailUtilsImpl implements EmailUtils {
} }
private void sendMail(String email, String subject, String message) throws Exception {
private void sendMail(String email, String subject, String message, boolean sendToSpecial, List<String> repoAdminMails) throws Exception { this.sendMail(Collections.singletonList(email), subject, message);
ArrayList<String> to = new ArrayList<String>();
to.add(email);
this.sendMail(to,subject,message,sendToSpecial,repoAdminMails);
} }
public void sendMail(List<String> recipients, String subject, String message, boolean sendToSpecial, List<String> repoAdminMails) throws Exception { public void sendMail(List<String> recipients, String subject, String message) throws Exception {
try { try {
if (sendToSpecial) {
recipients.addAll(this.specialRecipients);
}
if (repoAdminMails != null)
recipients.addAll(repoAdminMails);
if (this.override) {
recipients.clear();
recipients.add(overrideEmail);
}
if (!logonly)
mailLibrary.sendEmail(recipients.toArray(new String[]{}), subject, message);
LOGGER.debug("Sending mail to Recipients: " + recipients + " Subject: " + subject + " Message: " + message); LOGGER.debug("Sending mail to Recipients: " + recipients + " Subject: " + subject + " Message: " + message);
mailLibrary.sendEmail(recipients.toArray(new String[]{}), subject, message);
} catch (Exception e) { } catch (Exception e) {
LOGGER.error("Error sending mail to Recipients: " + recipients + " Subject: " + subject + " Message: " + message, e); LOGGER.error("Error sending mail to Recipients: " + recipients + " Subject: " + subject + " Message: " + message, e);
throw new Exception(e); throw e;
} }
} }
private String getEmailProperty(String key) {
return pLoader.getProperties().getProperty(key);
}
public void setSpecialRecipients(String specialRecipients) {
String[] recps = specialRecipients.split(",");
for (String recp : recps) {
recp = recp.trim();
this.specialRecipients.add(recp);
}
}
public void setOverride(boolean override) {
this.override = override;
}
public void setOverrideEmail(String overrideEmail) {
this.overrideEmail = overrideEmail;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public boolean isLogonly() {
return logonly;
}
public void setLogonly(boolean logonly) {
this.logonly = logonly;
}
} }

View File

@ -34,7 +34,6 @@ import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponents; import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder; import org.springframework.web.util.UriComponentsBuilder;
@ -176,17 +175,11 @@ public class RepositoryServiceImpl implements RepositoryService {
UriComponents uriComponents = searchDatasource(Integer.toString(Math.abs(page)), Integer.toString(Math.abs(size))); UriComponents uriComponents = searchDatasource(Integer.toString(Math.abs(page)), Integer.toString(Math.abs(size)));
RequestFilter requestFilter = new RequestFilter(); RequestFilter requestFilter = new RequestFilter();
try { for (String repoId : ids) {
for (String repoId : ids) { requestFilter.setId(repoId);
requestFilter.setId(repoId); String rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String.class);
String rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String.class);
repos.addAll(Converter.jsonToRepositoryList(new JSONObject(rs))); repos.addAll(Converter.jsonToRepositoryList(new JSONObject(rs)));
}
} catch (JSONException e) {
LOGGER.debug("Exception on getRepositoriesOfUser", e);
emailUtils.reportException(e);
throw e;
} }
for (Repository r : repos) for (Repository r : repos)
@ -262,23 +255,17 @@ public class RepositoryServiceImpl implements RepositoryService {
requestFilter.setCountry(country); requestFilter.setCountry(country);
requestFilter.setCollectedfrom(filterKey); requestFilter.setCollectedfrom(filterKey);
try { String rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String.class);
String rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String.class); JSONArray jsonArray = (JSONArray) new JSONObject(rs).get("datasourceInfo");
JSONArray jsonArray = (JSONArray) new JSONObject(rs).get("datasourceInfo"); while (jsonArray.length() > 0) {
while (jsonArray.length() > 0) { resultSet.addAll(mapper.readValue(String.valueOf(jsonArray),
resultSet.addAll(mapper.readValue(String.valueOf(jsonArray), mapper.getTypeFactory().constructCollectionType(List.class, RepositorySnippet.class)));
mapper.getTypeFactory().constructCollectionType(List.class, RepositorySnippet.class))); page += 1;
page += 1; uriComponents = searchSnipperDatasource(String.valueOf(page), String.valueOf(size));
uriComponents = searchSnipperDatasource(String.valueOf(page), String.valueOf(size)); rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String.class);
rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String.class); jsonArray = (JSONArray) new JSONObject(rs).get("datasourceInfo");
jsonArray = (JSONArray) new JSONObject(rs).get("datasourceInfo");
}
return resultSet;
} catch (Exception e) {
LOGGER.debug("Exception on getRepositoriesByCountry", e);
emailUtils.reportException(e);
throw e;
} }
return resultSet;
} }
public List<RepositorySnippet> searchRegisteredRepositories(String country, String typology, String englishName, public List<RepositorySnippet> searchRegisteredRepositories(String country, String typology, String englishName,
@ -371,21 +358,14 @@ public class RepositoryServiceImpl implements RepositoryService {
RequestFilter requestFilter = new RequestFilter(); RequestFilter requestFilter = new RequestFilter();
requestFilter.setId(id); requestFilter.setId(id);
try { String rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String.class);
String rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String.class); JSONArray jsonArray = (JSONArray) new JSONObject(rs).get("datasourceInfo");
JSONArray jsonArray = (JSONArray) new JSONObject(rs).get("datasourceInfo");
if (jsonArray.length() == 0) if (jsonArray.length() == 0)
throw new ResourceNotFoundException(); throw new ResourceNotFoundException();
repo = Converter.jsonToRepositorySnippetObject(jsonArray.getJSONObject(0));
return repo;
} catch (JSONException e) {
LOGGER.debug("Exception on getRepositoryById", e);
emailUtils.reportException(e);
throw e;
}
repo = Converter.jsonToRepositorySnippetObject(jsonArray.getJSONObject(0));
return repo;
} }
@Override @Override
@ -397,21 +377,14 @@ public class RepositoryServiceImpl implements RepositoryService {
RequestFilter requestFilter = new RequestFilter(); RequestFilter requestFilter = new RequestFilter();
requestFilter.setId(id); requestFilter.setId(id);
try { String rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String.class);
String rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String.class); JSONArray jsonArray = (JSONArray) new JSONObject(rs).get("datasourceInfo");
JSONArray jsonArray = (JSONArray) new JSONObject(rs).get("datasourceInfo");
if (jsonArray.length() == 0) if (jsonArray.length() == 0)
throw new ResourceNotFoundException(); throw new ResourceNotFoundException();
repo = Converter.jsonToRepositoryObject(jsonArray.getJSONObject(0));
return updateRepositoryInfo(repo);
} catch (JSONException e) {
LOGGER.debug("Exception on getRepositoryById", e);
emailUtils.reportException(e);
throw e;
}
repo = Converter.jsonToRepositoryObject(jsonArray.getJSONObject(0));
return updateRepositoryInfo(repo);
} }
@ -425,31 +398,25 @@ public class RepositoryServiceImpl implements RepositoryService {
List<AggregationDetails> aggregationHistory = new ArrayList<>(); List<AggregationDetails> aggregationHistory = new ArrayList<>();
try { long start = System.currentTimeMillis();
long start = System.currentTimeMillis(); String rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String.class);
String rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String.class); long end = System.currentTimeMillis();
long end = System.currentTimeMillis();
System.out.println("Aggregations request through rest template took " + (end - start) + "ms"); System.out.println("Aggregations request through rest template took " + (end - start) + "ms");
JSONObject repository = new JSONObject(rs); JSONObject repository = new JSONObject(rs);
if (repository.getJSONArray("datasourceInfo").length() == 0) if (repository.getJSONArray("datasourceInfo").length() == 0)
return aggregationHistory; return aggregationHistory;
start = System.currentTimeMillis(); start = System.currentTimeMillis();
aggregationHistory.addAll(Converter.getAggregationHistoryFromJson(repository.getJSONArray("datasourceInfo").getJSONObject(0))); aggregationHistory.addAll(Converter.getAggregationHistoryFromJson(repository.getJSONArray("datasourceInfo").getJSONObject(0)));
end = System.currentTimeMillis(); end = System.currentTimeMillis();
System.out.println("Getting aggregations history from json " + (end - start) + "ms"); System.out.println("Getting aggregations history from json " + (end - start) + "ms");
return aggregationHistory.size() == 0 ? aggregationHistory : aggregationHistory.stream() return aggregationHistory.size() == 0 ? aggregationHistory : aggregationHistory.stream()
.sorted(Comparator.comparing(AggregationDetails::getDate).reversed()) .sorted(Comparator.comparing(AggregationDetails::getDate).reversed())
.limit(size) .limit(size)
.collect(Collectors.toList()); .collect(Collectors.toList());
} catch (JSONException e) {
LOGGER.debug("Exception on getRepositoryAggregations", e);
emailUtils.reportException(e);
throw e;
}
} }
@ -462,21 +429,15 @@ public class RepositoryServiceImpl implements RepositoryService {
List<AggregationDetails> aggregationHistory = new ArrayList<>(); List<AggregationDetails> aggregationHistory = new ArrayList<>();
Map<String, List<AggregationDetails>> aggregationByYear = new HashMap<>(); Map<String, List<AggregationDetails>> aggregationByYear = new HashMap<>();
try {
String rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String.class);
JSONObject repository = new JSONObject(rs);
if (repository.getJSONArray("datasourceInfo").length() == 0) String rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String.class);
return aggregationByYear; JSONObject repository = new JSONObject(rs);
aggregationHistory.addAll(Converter.getAggregationHistoryFromJson(repository.getJSONArray("datasourceInfo").getJSONObject(0))); if (repository.getJSONArray("datasourceInfo").length() == 0)
return aggregationHistory.size() == 0 ? aggregationByYear : createYearMap(aggregationHistory); return aggregationByYear;
} catch (JSONException e) { aggregationHistory.addAll(Converter.getAggregationHistoryFromJson(repository.getJSONArray("datasourceInfo").getJSONObject(0)));
LOGGER.debug("Exception on getRepositoryAggregations", e); return aggregationHistory.size() == 0 ? aggregationByYear : createYearMap(aggregationHistory);
emailUtils.reportException(e);
throw e;
}
} }
private Map<String, List<AggregationDetails>> createYearMap(List<AggregationDetails> aggregationHistory) { private Map<String, List<AggregationDetails>> createYearMap(List<AggregationDetails> aggregationHistory) {
@ -500,17 +461,11 @@ public class RepositoryServiceImpl implements RepositoryService {
RequestFilter requestFilter = new RequestFilter(); RequestFilter requestFilter = new RequestFilter();
requestFilter.setOfficialname(name); requestFilter.setOfficialname(name);
try { String rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String.class);
String rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String.class); List<Repository> repos = Converter.jsonToRepositoryList(new JSONObject(rs));
List<Repository> repos = Converter.jsonToRepositoryList(new JSONObject(rs)); for (Repository r : repos)
for (Repository r : repos) updateRepositoryInfo(r);
updateRepositoryInfo(r); return repos;
return repos;
} catch (Exception e) {
LOGGER.debug("Exception on getRepositoriesByName", e);
emailUtils.reportException(e);
throw e;
}
} }
@ -522,14 +477,8 @@ public class RepositoryServiceImpl implements RepositoryService {
.path("/{id}") .path("/{id}")
.build().expand(id).encode(); .build().expand(id).encode();
try { String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
String rs = restTemplate.getForObject(uriComponents.toUri(), String.class); return Converter.jsonToRepositoryInterfaceList(new JSONObject(rs));
return Converter.jsonToRepositoryInterfaceList(new JSONObject(rs));
} catch (Exception e) {
LOGGER.debug("Exception on getRepositoryInterface", e);
emailUtils.reportException(e);
throw e;
}
} }
@ -599,25 +548,19 @@ public class RepositoryServiceImpl implements RepositoryService {
.build() .build()
.encode(); .encode();
try { String json_repository = Converter.repositoryObjectToJson(repository);
String json_repository = Converter.repositoryObjectToJson(repository); LOGGER.debug("JSON to add(update) -> " + json_repository);
LOGGER.debug("JSON to add(update) -> " + json_repository);
HttpEntity<String> httpEntity = new HttpEntity<String>(json_repository, httpHeaders); HttpEntity<String> httpEntity = new HttpEntity<String>(json_repository, httpHeaders);
ResponseEntity responseEntity = restTemplate.exchange(uriComponents.toUri(), HttpMethod.POST, httpEntity, ResponseEntity.class); ResponseEntity responseEntity = restTemplate.exchange(uriComponents.toUri(), HttpMethod.POST, httpEntity, ResponseEntity.class);
if (responseEntity.getStatusCode().equals(HttpStatus.OK)) { if (responseEntity.getStatusCode().equals(HttpStatus.OK)) {
emailUtils.sendUserRegistrationEmail(repository, authentication); emailUtils.sendUserRegistrationEmail(repository, authentication);
emailUtils.sendAdminRegistrationEmail(repository, authentication); emailUtils.sendAdminRegistrationEmail(repository, authentication);
} else } else
LOGGER.debug(responseEntity.getBody().toString()); LOGGER.debug(responseEntity.getBody().toString());
return repository; return repository;
} catch (Exception e) {
LOGGER.debug("Exception on updateRepository", e);
emailUtils.reportException(e);
throw e;
}
} }
@ -629,27 +572,21 @@ public class RepositoryServiceImpl implements RepositoryService {
.build() .build()
.encode(); .encode();
try { String json_repository = Converter.repositoryObjectToJson(repository);
String json_repository = Converter.repositoryObjectToJson(repository);
LOGGER.debug("JSON to update -> " + json_repository); LOGGER.debug("JSON to update -> " + json_repository);
HttpEntity<String> httpEntity = new HttpEntity<String>(json_repository, httpHeaders); HttpEntity<String> httpEntity = new HttpEntity<String>(json_repository, httpHeaders);
ResponseEntity responseEntity = restTemplate.exchange(uriComponents.toUri(), HttpMethod.POST, httpEntity ResponseEntity responseEntity = restTemplate.exchange(uriComponents.toUri(), HttpMethod.POST, httpEntity
, ResponseEntity.class); , ResponseEntity.class);
if (responseEntity.getStatusCode().equals(HttpStatus.OK)) { if (responseEntity.getStatusCode().equals(HttpStatus.OK)) {
emailUtils.sendUserUpdateRepositoryInfoEmail(repository, authentication); emailUtils.sendUserUpdateRepositoryInfoEmail(repository, authentication);
emailUtils.sendAdminUpdateRepositoryInfoEmail(repository, authentication); emailUtils.sendAdminUpdateRepositoryInfoEmail(repository, authentication);
} else } else
LOGGER.debug(responseEntity.getBody().toString()); LOGGER.debug(responseEntity.getBody().toString());
return repository; return repository;
} catch (Exception e) {
LOGGER.debug("Exception on updateRepository", e);
emailUtils.reportException(e);
throw e;
}
} }
private void storeRepository(Repository repository, Authentication authentication) throws Exception { private void storeRepository(Repository repository, Authentication authentication) throws Exception {
@ -692,32 +629,25 @@ public class RepositoryServiceImpl implements RepositoryService {
String repoId, String repoId,
String registeredBy, String registeredBy,
String comment, RepositoryInterface repositoryInterface) throws Exception { String comment, RepositoryInterface repositoryInterface) throws Exception {
try { Repository e = this.getRepositoryById(repoId);
Repository e = this.getRepositoryById(repoId); repositoryInterface = createRepositoryInterface(e, repositoryInterface, datatype);
repositoryInterface = createRepositoryInterface(e, repositoryInterface, datatype); String json_interface = Converter.repositoryInterfaceObjectToJson(e, repositoryInterface);
String json_interface = Converter.repositoryInterfaceObjectToJson(e, repositoryInterface);
UriComponents uriComponents = UriComponentsBuilder UriComponents uriComponents = UriComponentsBuilder
.fromHttpUrl(baseAddress + "/ds/api/add/") .fromHttpUrl(baseAddress + "/ds/api/add/")
.build() .build()
.encode(); .encode();
HttpEntity<String> httpEntity = new HttpEntity<>(json_interface, httpHeaders); HttpEntity<String> httpEntity = new HttpEntity<>(json_interface, httpHeaders);
restTemplate.postForObject(uriComponents.toUri(), httpEntity, String.class); restTemplate.postForObject(uriComponents.toUri(), httpEntity, String.class);
emailUtils.sendAdminRegisterInterfaceEmail(e, comment, repositoryInterface, SecurityContextHolder.getContext().getAuthentication()); emailUtils.sendAdminRegisterInterfaceEmail(e, comment, repositoryInterface, SecurityContextHolder.getContext().getAuthentication());
emailUtils.sendUserRegisterInterfaceEmail(e, comment, repositoryInterface, SecurityContextHolder.getContext().getAuthentication()); emailUtils.sendUserRegisterInterfaceEmail(e, comment, repositoryInterface, SecurityContextHolder.getContext().getAuthentication());
submitInterfaceValidation(e, registeredBy, repositoryInterface, false); submitInterfaceValidation(e, registeredBy, repositoryInterface, false);
return repositoryInterface; return repositoryInterface;
} catch (Exception e) {
LOGGER.error("Exception on addRepositoryInterface", e);
emailUtils.reportException(e);
throw e;
}
} }
@Override @Override
@ -822,15 +752,9 @@ public class RepositoryServiceImpl implements RepositoryService {
.path("/{page}/{size}") .path("/{page}/{size}")
.build().expand(page, size).encode(); .build().expand(page, size).encode();
try { RequestFilter requestFilter = new RequestFilter();
RequestFilter requestFilter = new RequestFilter(); requestFilter.setRegisteredby(userEmail);
requestFilter.setRegisteredby(userEmail); return Arrays.asList(restTemplate.postForObject(uriComponents.toUri(), requestFilter, String[].class));
return Arrays.asList(restTemplate.postForObject(uriComponents.toUri(), requestFilter, String[].class));
} catch (Exception e) {
LOGGER.debug("Exception on addRepositoryInterface", e);
emailUtils.reportException(e);
throw e;
}
} }
@Override @Override
@ -947,7 +871,6 @@ public class RepositoryServiceImpl implements RepositoryService {
} catch (Exception e) { } catch (Exception e) {
LOGGER.error("Error while getting metrics info for repository: ", e); LOGGER.error("Error while getting metrics info for repository: ", e);
emailUtils.reportException(e);
throw new RepositoryServiceException("General error", RepositoryServiceException.ErrorCode.GENERAL_ERROR); throw new RepositoryServiceException("General error", RepositoryServiceException.ErrorCode.GENERAL_ERROR);
} }
} }
@ -1004,19 +927,14 @@ public class RepositoryServiceImpl implements RepositoryService {
RestTemplate template = new RestTemplate(); RestTemplate template = new RestTemplate();
template.getMessageConverters().add(new MappingJackson2HttpMessageConverter()); template.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
ResponseEntity<MetricsNumbers> resp; ResponseEntity<MetricsNumbers> resp;
try {
//communicate with endpoint //communicate with endpoint
resp = template.exchange( resp = template.exchange(
builder.build().encode().toUri(), builder.build().encode().toUri(),
HttpMethod.GET, HttpMethod.GET,
null, null,
new ParameterizedTypeReference<MetricsNumbers>() { new ParameterizedTypeReference<MetricsNumbers>() {
}); });
} catch (RestClientException e) {
LOGGER.debug("Exception on getMetricsNumbers", e);
emailUtils.reportException(e);
throw e;
}
return resp.getBody(); return resp.getBody();
} }

View File

@ -96,9 +96,9 @@ ValidatorService.url=http://estella.athenarc.gr:8080/validator-service/services/
#services.validator.ldap.usersDN = ou=users,dc=openaire,dc=eu #services.validator.ldap.usersDN = ou=users,dc=openaire,dc=eu
#AAI-REDIS #AAI-REDIS
redis.host = estella.athenarc.gr redis.host = localhost
redis.port = 6379 redis.port = 6379
redis.password = redis123 redis.password = 2106875370
oidc.dev.home = http://localhost:8480/uoa-repository-manager-service/openid_connect_login oidc.dev.home = http://localhost:8480/uoa-repository-manager-service/openid_connect_login
webapp.dev.front = http://localhost:4200/dashboard webapp.dev.front = http://localhost:4200/dashboard
@ -133,10 +133,7 @@ search.api.baseAddress=https://beta.services.openaire.eu/search/v2/api
search.api.usagestats=https://services.openaire.eu/usagestats search.api.usagestats=https://services.openaire.eu/usagestats
search.api.usageEvents=http://beta.lbs.openaire.eu:8080/ajax/summary search.api.usageEvents=http://beta.lbs.openaire.eu:8080/ajax/summary
api.baseAddress=http://beta.services.openaire.eu/openaire api.baseAddress=http://beta.services.openaire.eu/openaire
services.repo-manager.baseUrl=http://${container.hostname}:${container.port}/${container.context} services.repomanager.baseUrl=http://${container.hostname}:${container.port}/${container.context}
services.repo-manager.adminEmail=antleb@di.uoa.gr
services.repo-manager.repository.testing.mode=false
services.repo-manager.deploy.environment=development
services.repomanager.usageStatisticsDiagramsBaseURL=https://beta.openaire.eu/stats3/ services.repomanager.usageStatisticsDiagramsBaseURL=https://beta.openaire.eu/stats3/
services.repomanager.usageStatisticsNumbersBaseURL=https://beta.services.openaire.eu/usagestats/datasources/ services.repomanager.usageStatisticsNumbersBaseURL=https://beta.services.openaire.eu/usagestats/datasources/
services.repomanager.usagestats.adminEmail=antleb@di.uoa.gr services.repomanager.usagestats.adminEmail=antleb@di.uoa.gr
@ -144,12 +141,14 @@ services.repomanager.usagestats.sushiliteEndpoint=http://beta.services.openaire.
services.repomanager.analyticsURL=https://analytics.openaire.eu/addsite.php? services.repomanager.analyticsURL=https://analytics.openaire.eu/addsite.php?
aai.mode=develop aai.mode=develop
#uoa-repository-manager-service #uoa-repository-manager-service
#oidc.dev.home=http://88.197.53.69:8080/uoa-repository-manager-service/openid_connect_login #oidc.dev.home=http://88.197.53.69:8080/uoa-repository-manager-service/openid_connect_login
#webapp.dev.front=http://88.197.53.69/landing #webapp.dev.front=http://88.197.53.69/landing
services.repo-manager.adminEmail=antleb@di.uoa.gr services.repo-manager.adminEmail=antleb@di.uoa.gr
services.repomanager.usagestats.adminEmail=antleb@di.uoa.gr services.repomanager.usagestats.adminEmail=antleb@di.uoa.gr
services.provide.adminEmail=antleb@di.uoa.gr services.repomanager.provide.adminEmail=antleb@di.uoa.gr
validator.results.url=https://beta.provide.openaire.eu/compatibility/browseHistory/ validator.results.url=https://beta.provide.openaire.eu/compatibility/browseHistory/
topic_types.url=https://beta.services.openaire.eu/provision/mvc/vocabularies/dnet:topic_types.json topic_types.url=https://beta.services.openaire.eu/provision/mvc/vocabularies/dnet:topic_types.json
services.broker.url=http://broker1-dev-dnet.d4science.org services.broker.url=http://broker1-dev-dnet.d4science.org