From fa1053a714558f104a36d800897a792bc96647c3 Mon Sep 17 00:00:00 2001 From: Camelia Dumitru Date: Thu, 7 Nov 2024 11:34:19 +0000 Subject: [PATCH 1/2] Fixed the 500 when client reached the limit. --- .../orcid/core/template/papi_rate_limit_email.ftl | 2 +- .../core/template/papi_rate_limit_email_html.ftl | 3 --- .../org/orcid/api/filters/ApiRateLimitFilter.java | 14 ++++++++++---- .../scheduler/report/PapiDailyLimitReport.java | 4 ++-- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/orcid-core/src/main/resources/org/orcid/core/template/papi_rate_limit_email.ftl b/orcid-core/src/main/resources/org/orcid/core/template/papi_rate_limit_email.ftl index 0c4345237c..942550a889 100644 --- a/orcid-core/src/main/resources/org/orcid/core/template/papi_rate_limit_email.ftl +++ b/orcid-core/src/main/resources/org/orcid/core/template/papi_rate_limit_email.ftl @@ -15,5 +15,5 @@ To minimize any disruption to your ORCID integration in the future, we would rec Warm Regards, ORCID Support Team https://support.orcid.org -<@emailMacros.msg "email.common.you_have_received_this_email" /> + <#include "email_footer.ftl"/> diff --git a/orcid-core/src/main/resources/org/orcid/core/template/papi_rate_limit_email_html.ftl b/orcid-core/src/main/resources/org/orcid/core/template/papi_rate_limit_email_html.ftl index 569cb86ebe..f7d4ebca59 100644 --- a/orcid-core/src/main/resources/org/orcid/core/template/papi_rate_limit_email_html.ftl +++ b/orcid-core/src/main/resources/org/orcid/core/template/papi_rate_limit_email_html.ftl @@ -25,9 +25,6 @@

- <@emailMacros.msg "email.common.you_have_received_this_email" /> -

-

<#include "email_footer_html.ftl"/>

diff --git a/orcid-pub-web/src/main/java/org/orcid/api/filters/ApiRateLimitFilter.java b/orcid-pub-web/src/main/java/org/orcid/api/filters/ApiRateLimitFilter.java index daa982cae3..d8c902cddc 100644 --- a/orcid-pub-web/src/main/java/org/orcid/api/filters/ApiRateLimitFilter.java +++ b/orcid-pub-web/src/main/java/org/orcid/api/filters/ApiRateLimitFilter.java @@ -37,6 +37,7 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.MessageSource; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Component; import org.springframework.web.filter.OncePerRequestFilter; @@ -79,6 +80,9 @@ public class ApiRateLimitFilter extends OncePerRequestFilter { @Autowired private OrcidTokenStore orcidTokenStore; + + @Autowired + private MessageSource messageSource; @Value("${org.orcid.papi.rate.limit.anonymous.requests:10000}") private int anonymousRequestLimit; @@ -116,7 +120,7 @@ protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServl String clientId = null; if (tokenValue != null) { try { - clientId = orcidTokenStore.readClientId(tokenValue); + clientId = orcidTokenStore.readClientId(tokenValue); } catch (Exception ex) { LOG.error("Exception when trying to get the client id from token value, ignoring and treating as anonymous client", ex); } @@ -202,8 +206,10 @@ private void rateLimitClientRequest(String clientId, LocalDate today) { private Map createTemplateParams(String clientId, String clientName, String emailName, String orcidId) { Map templateParams = new HashMap(); + templateParams.put("messages", messageSource); + templateParams.put("messageArgs", new Object[0]); templateParams.put("clientId", clientId); - templateParams.put("clientId", clientName); + templateParams.put("clientName", clientName); templateParams.put("emailName", emailName); templateParams.put("locale", LocaleUtils.toLocale("en")); templateParams.put("baseUri", orcidUrlManager.getBaseUrl()); @@ -218,9 +224,9 @@ private void sendEmail(String clientId, LocalDate requestDate) { String emailName = recordNameManager.deriveEmailFriendlyName(profile.getId()); Map templateParams = this.createTemplateParams(clientId, clientDetailsEntity.getClientName(), emailName, profile.getId()); // Generate body from template - String body = templateManager.processTemplate("bad_orgs_email.ftl", templateParams); + String body = templateManager.processTemplate("papi_rate_limit_email.ftl", templateParams); // Generate html from template - String html = templateManager.processTemplate("bad_orgs_email_html.ftl", templateParams); + String html = templateManager.processTemplate("papi_rate_limit_email_html.ftl", templateParams); String email = emailManager.findPrimaryEmail(profile.getId()).getEmail(); LOG.info("text email={}", body); diff --git a/orcid-scheduler-web/src/main/java/org/orcid/scheduler/report/PapiDailyLimitReport.java b/orcid-scheduler-web/src/main/java/org/orcid/scheduler/report/PapiDailyLimitReport.java index 898a1896c8..73b1b4258c 100644 --- a/orcid-scheduler-web/src/main/java/org/orcid/scheduler/report/PapiDailyLimitReport.java +++ b/orcid-scheduler-web/src/main/java/org/orcid/scheduler/report/PapiDailyLimitReport.java @@ -71,8 +71,8 @@ public void papiDailyLimitReport() { if (enableRateLimiting) { LocalDate yesterday = LocalDate.now().minusDays(1); String mode = Features.ENABLE_PAPI_RATE_LIMITING.isActive() ? "ENFORCEMENT" : "MONITORING"; - String SLACK_INTRO_MSG = "Public API Rate limit report - Date: " + yesterday.toString() + "\n Current Anonymous Requests Limit: " + anonymousRequestLimit - + "\nCurrent Public API Clients Limit: " + knownRequestLimit + "\n Mode: " + mode; + String SLACK_INTRO_MSG = "Public API Rate limit report - Date: " + yesterday.toString() + "\nCurrent Anonymous Requests Limit: " + anonymousRequestLimit + + "\nCurrent Public API Clients Limit: " + knownRequestLimit + "\nMode: " + mode; LOG .info(SLACK_INTRO_MSG); slackManager.sendAlert(SLACK_INTRO_MSG, slackChannel, webhookUrl, webhookUrl); From 20e64ca5b282dff9b7a65c84792c0808b95bb449 Mon Sep 17 00:00:00 2001 From: Camelia Dumitru <62257307+Camelia-Orcid@users.noreply.github.com> Date: Thu, 7 Nov 2024 11:37:48 +0000 Subject: [PATCH 2/2] Remove extrawhite space --- .../src/main/java/org/orcid/api/filters/ApiRateLimitFilter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/orcid-pub-web/src/main/java/org/orcid/api/filters/ApiRateLimitFilter.java b/orcid-pub-web/src/main/java/org/orcid/api/filters/ApiRateLimitFilter.java index d8c902cddc..e2f9aae1dd 100644 --- a/orcid-pub-web/src/main/java/org/orcid/api/filters/ApiRateLimitFilter.java +++ b/orcid-pub-web/src/main/java/org/orcid/api/filters/ApiRateLimitFilter.java @@ -120,7 +120,7 @@ protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServl String clientId = null; if (tokenValue != null) { try { - clientId = orcidTokenStore.readClientId(tokenValue); + clientId = orcidTokenStore.readClientId(tokenValue); } catch (Exception ex) { LOG.error("Exception when trying to get the client id from token value, ignoring and treating as anonymous client", ex); }