Search in sources :

Example 1 with ResourceNotFoundException

use of eu.einfracentral.exception.ResourceNotFoundException in project resource-catalogue by madgeek-arc.

the class RegistrationMailService method sendProviderMails.

@Async
public void sendProviderMails(ProviderBundle providerBundle) {
    Map<String, Object> root = new HashMap<>();
    StringWriter out = new StringWriter();
    String providerMail;
    String regTeamMail;
    String providerSubject;
    String regTeamSubject;
    String serviceOrResource = "Resource";
    if (projectName.equalsIgnoreCase("CatRIS")) {
        serviceOrResource = "Service";
    }
    if (providerBundle == null || providerBundle.getProvider() == null) {
        throw new ResourceNotFoundException("Provider is null");
    }
    List<Service> serviceList = infraServiceManager.getServices(providerBundle.getId());
    Service serviceTemplate = null;
    if (!serviceList.isEmpty()) {
        root.put("service", serviceList.get(0));
        serviceTemplate = serviceList.get(0);
    } else {
        serviceTemplate = new Service();
        serviceTemplate.setName("");
    }
    providerSubject = getProviderSubject(providerBundle, serviceTemplate);
    regTeamSubject = getRegTeamSubject(providerBundle, serviceTemplate);
    root.put("serviceOrResource", serviceOrResource);
    root.put("providerBundle", providerBundle);
    root.put("endpoint", endpoint);
    root.put("project", projectName);
    root.put("registrationEmail", registrationEmail);
    // get the first user's information for the registration team email
    for (LoggingInfo loggingInfo : providerBundle.getLoggingInfo()) {
        if (loggingInfo.getActionType().equals(LoggingInfo.ActionType.REGISTERED.getKey())) {
            User user = new User();
            if (loggingInfo.getUserEmail() != null && !loggingInfo.getUserEmail().equals("")) {
                user.setEmail(loggingInfo.getUserEmail());
            }
            if (loggingInfo.getUserFullName() != null && !loggingInfo.getUserFullName().equals("")) {
                String[] parts = loggingInfo.getUserFullName().split(" ");
                String name = parts[0];
                String surname = parts[1];
                user.setName(name);
                user.setSurname(surname);
            }
            root.put("user", user);
            break;
        }
    }
    if (!root.containsKey("user")) {
        root.put("user", providerBundle.getProvider().getUsers().get(0));
    }
    try {
        Template temp = cfg.getTemplate("registrationTeamMailTemplate.ftl");
        temp.process(root, out);
        regTeamMail = out.getBuffer().toString();
        mailService.sendMail(registrationEmail, regTeamSubject, regTeamMail);
        logger.info("\nRecipient: {}\nTitle: {}\nMail body: \n{}", registrationEmail, regTeamSubject, regTeamMail);
        temp = cfg.getTemplate("providerMailTemplate.ftl");
        for (User user : providerBundle.getProvider().getUsers()) {
            if (user.getEmail() == null || user.getEmail().equals("")) {
                continue;
            }
            root.remove("user");
            out.getBuffer().setLength(0);
            root.put("user", user);
            root.put("project", projectName);
            temp.process(root, out);
            providerMail = out.getBuffer().toString();
            mailService.sendMail(user.getEmail(), providerSubject, providerMail);
            logger.info("\nRecipient: {}\nTitle: {}\nMail body: \n{}", user.getEmail(), providerSubject, providerMail);
        }
        out.close();
    } catch (IOException e) {
        logger.error("Error finding mail template", e);
    } catch (TemplateException e) {
        logger.error("ERROR", e);
    } catch (MessagingException e) {
        logger.error("Could not send mail", e);
    }
}
Also used : TemplateException(freemarker.template.TemplateException) MessagingException(javax.mail.MessagingException) MailService(eu.einfracentral.registry.service.MailService) IOException(java.io.IOException) Template(freemarker.template.Template) StringWriter(java.io.StringWriter) ResourceNotFoundException(eu.einfracentral.exception.ResourceNotFoundException) Async(org.springframework.scheduling.annotation.Async)

Example 2 with ResourceNotFoundException

use of eu.einfracentral.exception.ResourceNotFoundException in project resource-catalogue by madgeek-arc.

the class OIDCSecurityService method userIsServiceProviderAdmin.

@Override
public boolean userIsServiceProviderAdmin(@NotNull User user, String serviceId) {
    InfraService service;
    try {
        service = infraServiceService.get(serviceId);
    } catch (ResourceException | ResourceNotFoundException e) {
        try {
            service = pendingServiceManager.get(serviceId);
        } catch (RuntimeException re) {
            return false;
        }
    } catch (RuntimeException e) {
        return false;
    }
    if (service.getService().getResourceOrganisation() == null || service.getService().getResourceOrganisation().equals("")) {
        throw new ValidationException("Service has no Service Organisation");
    }
    // List<String> allProviders = service.getService().getResourceProviders();
    // allProviders.add(service.getService().getResourceOrganisation());
    List<String> allProviders = Collections.singletonList(service.getService().getResourceOrganisation());
    Optional<List<String>> providers = Optional.of(allProviders);
    return providers.get().stream().filter(Objects::nonNull).anyMatch(id -> userIsProviderAdmin(user, id));
}
Also used : InfraService(eu.einfracentral.domain.InfraService) ValidationException(eu.einfracentral.exception.ValidationException) ResourceException(eu.einfracentral.exception.ResourceException) ResourceNotFoundException(eu.einfracentral.exception.ResourceNotFoundException)

Aggregations

ResourceNotFoundException (eu.einfracentral.exception.ResourceNotFoundException)2 InfraService (eu.einfracentral.domain.InfraService)1 ResourceException (eu.einfracentral.exception.ResourceException)1 ValidationException (eu.einfracentral.exception.ValidationException)1 MailService (eu.einfracentral.registry.service.MailService)1 Template (freemarker.template.Template)1 TemplateException (freemarker.template.TemplateException)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 MessagingException (javax.mail.MessagingException)1 Async (org.springframework.scheduling.annotation.Async)1