@Override
 public EmailMessage createDigest(String orcid, Collection<Notification> notifications) {
   OrcidProfile orcidProfile =
       orcidProfileManager.retrieveOrcidProfile(orcid, LoadOptions.BIO_AND_INTERNAL_ONLY);
   Locale locale = localeManager.getLocaleFromOrcidProfile(orcidProfile);
   return createDigest(orcidProfile, notifications, locale);
 }
 private OrcidMessage getLegacy500OrcidEntity(Throwable e) {
   OrcidMessage entity = new OrcidMessage();
   entity.setMessageVersion(OrcidMessage.DEFAULT_VERSION);
   entity.setErrorDesc(
       new ErrorDesc(
           StringUtils.isNotBlank(e.getMessage())
               ? e.getMessage()
               : messageSource.getMessage(
                   "apiError.unknown.exception", null, localeManager.getLocale())));
   return entity;
 }
  private Response getOrcidSearchResultsResponse(
      OrcidSearchResults orcidSearchResults, String query) {

    if (orcidSearchResults != null) {
      OrcidMessage orcidMessage = new OrcidMessage();
      orcidMessage.setMessageVersion("1.2");
      orcidMessage.setOrcidSearchResults(orcidSearchResults);
      return Response.ok(orcidMessage).build();
    } else {
      Object params[] = {query};
      throw new NoResultException(
          localeManager.resolveMessage("apiError.no_search_result.exception", params));
    }
  }
  @Override
  public void sendWelcomeEmail(OrcidProfile orcidProfile, String email) {
    Map<String, Object> templateParams = new HashMap<String, Object>();
    String subject = getSubject("email.subject.register.thanks", orcidProfile);
    String emailName = deriveEmailFriendlyName(orcidProfile);
    String verificationUrl = createVerificationUrl(email, orcidUrlManager.getBaseUrl());
    String orcidId = orcidProfile.getOrcidIdentifier().getPath();
    String baseUri = orcidUrlManager.getBaseUrl();
    String baseUriHttp = orcidUrlManager.getBaseUriHttp();

    templateParams.put("subject", subject);
    templateParams.put("emailName", emailName);
    templateParams.put("verificationUrl", verificationUrl);
    templateParams.put("orcidId", orcidId);
    templateParams.put("baseUri", baseUri);
    templateParams.put("baseUriHttp", baseUriHttp);

    SourceEntity source = sourceManager.retrieveSourceEntity();
    if (source != null) {
      String sourceId = source.getSourceId();
      String sourceName = source.getSourceName();
      // If the source is not the user itself
      if (sourceId != null && !sourceId.equals(orcidId)) {
        if (!PojoUtil.isEmpty(sourceName)) {
          String paramValue =
              " " + localeManager.resolveMessage("common.through") + " " + sourceName + ".";
          templateParams.put("source_name_if_exists", paramValue);
        } else {
          templateParams.put("source_name_if_exists", ".");
        }
      } else {
        templateParams.put("source_name_if_exists", ".");
      }
    } else {
      templateParams.put("source_name_if_exists", ".");
    }

    addMessageParams(templateParams, orcidProfile);

    // Generate body from template
    String body = templateManager.processTemplate("welcome_email.ftl", templateParams);
    // Generate html from template
    String html = templateManager.processTemplate("welcome_email_html.ftl", templateParams);

    mailGunManager.sendEmail(SUPPORT_VERIFY_ORCID_ORG, email, subject, body, html);
  }
  private OrcidError getOrcidError(int errorCode, int status, Throwable t) {
    Locale locale = localeManager.getLocale();
    OrcidError orcidError = new OrcidError();
    orcidError.setResponseCode(status);
    orcidError.setErrorCode(errorCode);
    orcidError.setMoreInfo(
        messageSource.getMessage("apiError." + errorCode + ".moreInfo", null, locale));
    Map<String, String> params = null;
    if (t instanceof ApplicationException) {
      params = ((ApplicationException) t).getParams();
    }
    // Returns an empty message if the key is not found
    String devMessage =
        messageSource.getMessage("apiError." + errorCode + ".developerMessage", null, "", locale);

    // Assign message from the exception
    if ("".equals(devMessage)) {
      devMessage = t.getClass().getCanonicalName();
      Throwable cause = t.getCause();
      String exceptionMessage = t.getLocalizedMessage();
      if (exceptionMessage != null) {
        devMessage += ": " + exceptionMessage;
      }

      if (cause != null) {
        String causeMessage = cause.getLocalizedMessage();
        if (causeMessage != null) {
          devMessage += " (" + causeMessage + ")";
        }
      }
      orcidError.setDeveloperMessage(devMessage);
    } else {
      orcidError.setDeveloperMessage(resolveMessage(devMessage, params));
    }

    orcidError.setUserMessage(
        resolveMessage(
            messageSource.getMessage("apiError." + errorCode + ".userMessage", null, locale),
            params));
    return orcidError;
  }
 @Override
 protected Authentication attemptSwitchUser(HttpServletRequest request)
     throws AuthenticationException {
   String targetUserOrcid = request.getParameter(SPRING_SECURITY_SWITCH_USERNAME_KEY);
   ProfileEntity profileEntity = sourceManager.retrieveSourceProfileEntity();
   if (OrcidType.ADMIN.equals(profileEntity.getOrcidType())) {
     return super.attemptSwitchUser(request);
   }
   // If we are switching back to me it is OK
   if (isSwitchingBack(request)) {
     return super.attemptSwitchUser(request);
   }
   for (GivenPermissionByEntity gpbe : profileEntity.getGivenPermissionBy()) {
     if (gpbe.getGiver().getId().equals(targetUserOrcid)) {
       return super.attemptSwitchUser(request);
     }
   }
   Object params[] = {};
   throw new SwitchUserAuthenticationException(
       localeManager.resolveMessage("web.orcid.switchuser.exception", params));
 }
 private String getSubject(String code, OrcidProfile orcidProfile, String... args) {
   Locale locale = localeManager.getLocaleFromOrcidProfile(orcidProfile);
   return messages.getMessage(code, args, locale);
 }
 public String getSubject(String code, OrcidProfile orcidProfile) {
   Locale locale = localeManager.getLocaleFromOrcidProfile(orcidProfile);
   return messages.getMessage(code, null, locale);
 }
 public void addMessageParams(Map<String, Object> templateParams, OrcidProfile orcidProfile) {
   Locale locale = localeManager.getLocaleFromOrcidProfile(orcidProfile);
   templateParams.put("messages", this.messages);
   templateParams.put("messageArgs", new Object[0]);
   templateParams.put("locale", locale);
 }