From e1dba928dc36e5cc27212f1a3c51613fd2842ef0 Mon Sep 17 00:00:00 2001 From: YuriyZ Date: Thu, 3 Feb 2022 11:57:33 +0200 Subject: [PATCH] feat(jans-auth-server): added cache support to /stat endpoint https://github.com/JanssenProject/jans/issues/317 --- .../model/configuration/AppConfiguration.java | 9 ----- .../io/jans/as/server/ws/rs/stat/StatWS.java | 37 +++++++------------ 2 files changed, 14 insertions(+), 32 deletions(-) diff --git a/jans-auth-server/model/src/main/java/io/jans/as/model/configuration/AppConfiguration.java b/jans-auth-server/model/src/main/java/io/jans/as/model/configuration/AppConfiguration.java index 026040ac607..e7d7a780173 100644 --- a/jans-auth-server/model/src/main/java/io/jans/as/model/configuration/AppConfiguration.java +++ b/jans-auth-server/model/src/main/java/io/jans/as/model/configuration/AppConfiguration.java @@ -90,7 +90,6 @@ public class AppConfiguration implements Configuration { private Boolean umaRestrictResourceToAssociatedClient = false; private int statTimerIntervalInSeconds; - private int statWebServiceIntervalLimitInSeconds; private String statAuthorizationScope; private int spontaneousScopeLifetime; @@ -1160,14 +1159,6 @@ public void setUserInfoEncryptionAlgValuesSupported(List userInfoEncrypt this.userInfoEncryptionAlgValuesSupported = userInfoEncryptionAlgValuesSupported; } - public int getStatWebServiceIntervalLimitInSeconds() { - return statWebServiceIntervalLimitInSeconds; - } - - public void setStatWebServiceIntervalLimitInSeconds(int statWebServiceIntervalLimitInSeconds) { - this.statWebServiceIntervalLimitInSeconds = statWebServiceIntervalLimitInSeconds; - } - public int getStatTimerIntervalInSeconds() { return statTimerIntervalInSeconds; } diff --git a/jans-auth-server/server/src/main/java/io/jans/as/server/ws/rs/stat/StatWS.java b/jans-auth-server/server/src/main/java/io/jans/as/server/ws/rs/stat/StatWS.java index 9ded319af97..059eeb93274 100644 --- a/jans-auth-server/server/src/main/java/io/jans/as/server/ws/rs/stat/StatWS.java +++ b/jans-auth-server/server/src/main/java/io/jans/as/server/ws/rs/stat/StatWS.java @@ -1,5 +1,7 @@ package io.jans.as.server.ws.rs.stat; +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; import io.jans.as.common.model.stat.StatEntry; import io.jans.as.model.common.ComponentType; import io.jans.as.model.config.Constants; @@ -39,6 +41,7 @@ import java.util.Base64; import java.util.List; import java.util.Map; +import java.util.concurrent.TimeUnit; import static io.jans.as.model.util.Util.escapeLog; @@ -54,8 +57,6 @@ @Path("/internal/stat") public class StatWS { - private static final int DEFAULT_WS_INTERVAL_LIMIT_IN_SECONDS = 60; - @Inject private Logger log; @@ -74,7 +75,10 @@ public class StatWS { @Inject private TokenService tokenService; - private long lastProcessedAt; + private final Cache responseCache = CacheBuilder + .newBuilder() + .expireAfterWrite(1, TimeUnit.HOURS) + .build(); public static String createOpenMetricsResponse(StatResponse statResponse) throws IOException { Writer writer = new StringWriter(); @@ -169,13 +173,6 @@ public Response stat(String authorization, String month, String format) { validateAuthorization(authorization); final List months = validateMonth(month); - if (!allowToRun()) { - log.trace("Interval request limit exceeded. Request is rejected. Current interval limit: {} (or 60 seconds if not set).", appConfiguration.getStatWebServiceIntervalLimitInSeconds()); - throw errorResponseFactory.createWebApplicationException(Response.Status.FORBIDDEN, TokenErrorResponseType.ACCESS_DENIED, "Interval request limit exceeded."); - } - - lastProcessedAt = System.currentTimeMillis(); - try { if (log.isTraceEnabled()) log.trace("Recognized months: {}", escapeLog(months)); @@ -203,6 +200,12 @@ public Response stat(String authorization, String month, String format) { } private StatResponse buildResponse(List months) { + final String cacheKey = months.toString(); + final StatResponse cachedResponse = responseCache.getIfPresent(cacheKey); + if (cachedResponse != null) { + return cachedResponse; + } + StatResponse response = new StatResponse(); for (String month : months) { final StatResponseItem responseItem = buildItem(month); @@ -211,6 +214,7 @@ private StatResponse buildResponse(List months) { } } + responseCache.put(cacheKey, response); return response; } @@ -326,17 +330,4 @@ private List validateMonth(String month) { return months; } - - private boolean allowToRun() { - int interval = appConfiguration.getStatWebServiceIntervalLimitInSeconds(); - if (interval <= 0) { - interval = DEFAULT_WS_INTERVAL_LIMIT_IN_SECONDS; - } - - long timerInterval = interval * 1000L; - - long timeDiff = System.currentTimeMillis() - lastProcessedAt; - - return timeDiff >= timerInterval; - } }