Skip to content

Commit

Permalink
Fixed the 500 when client reached the limit.
Browse files Browse the repository at this point in the history
  • Loading branch information
Camelia-Orcid committed Nov 7, 2024
1 parent 98baf91 commit fa1053a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"/>
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@
</p>

<p style="font-family: arial, helvetica, sans-serif;font-size: 15px;color: #494A4C;">
<@emailMacros.msg "email.common.you_have_received_this_email" />
</p>
<p style="font-family: arial, helvetica, sans-serif;font-size: 15px;color: #494A4C;">
<#include "email_footer_html.ftl"/>
</p>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -202,8 +206,10 @@ private void rateLimitClientRequest(String clientId, LocalDate today) {

private Map<String, Object> createTemplateParams(String clientId, String clientName, String emailName, String orcidId) {
Map<String, Object> templateParams = new HashMap<String, Object>();
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());
Expand All @@ -218,9 +224,9 @@ private void sendEmail(String clientId, LocalDate requestDate) {
String emailName = recordNameManager.deriveEmailFriendlyName(profile.getId());
Map<String, Object> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit fa1053a

Please sign in to comment.