Skip to content

Commit

Permalink
Session events are ignored for public api page calls
Browse files Browse the repository at this point in the history
  • Loading branch information
amontenegro committed Feb 11, 2025
1 parent c3910d2 commit 6846137
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.orcid.api.common.analytics.APIEndpointParser;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
Expand Down Expand Up @@ -52,15 +53,15 @@ public class OrcidRedisIndexedSessionRepository implements FindByIndexNameSessio
private RedisSerializer<Object> defaultSerializer = new JdkSerializationRedisSerializer();
private FlushMode flushMode;
private SaveMode saveMode;
private final List<String> urisToSkip = List.of("/account/biographyForm.json", "/account/countryForm.json", "/account/emails.json",
"/account/nameForm.json", "/account/preferences.json", "/affiliations/affiliationGroups.json", "/affiliations/affiliationDetails.json",
"/assets/vectors/orcid.logo.icon.svg", "/config.json", "/delegators/delegators-and-me.json", "/fundings/fundingDetails.json",
"/fundings/fundingGroups.json", "/inbox/unreadCount.json", "/my-orcid/externalIdentifiers.json", "/my-orcid/keywordsForms.json",
"/my-orcid/otherNamesForms.json", "/my-orcid/websitesForms.json", "/peer-reviews/peer-reviews-minimized.json",
"/research-resources/researchResourcePage.json", "/research-resources/researchResource.json", "/works/worksExtendedPage.json",
"/works/groupingSuggestions.json", "/works/getWorkInfo.json", "/works/work.json", "/works/idTypes.json", "/orgs/disambiguated/ROR",
"/orgs/disambiguated/GRID", "/orgs/disambiguated/FUNDREF", "/ng-cli-ws", "/not-found", "/peer-reviews/peer-review.json",
"/peer-reviews/peer-reviews-by-group-id.json");
private final String PUBLIC_ORCID_PAGE_REGEX = "/(\\d{4}-){3,}\\d{3}[\\dX](/.+)";
private final List<String> urisToSkip = List.of("/2FA/status.json", "/account/", "/account/biographyForm.json", "/account/countryForm.json", "/account/delegates.json", "/account/emails.json",
"/account/get-trusted-orgs.json", "/account/nameForm.json", "/account/preferences.json", "/account/socialAccounts.json", "/affiliations/affiliationDetails.json", "/affiliations/affiliationGroups.json",
"/assets/vectors/orcid.logo.icon.svg", "/config.json", "/delegators/delegators-and-me.json", "/fundings/fundingDetails.json", "/fundings/fundingGroups.json", "/inbox/notifications.json",
"/inbox/totalCount.json", "/inbox/unreadCount.json", "/my-orcid/externalIdentifiers.json", "/my-orcid/keywordsForms.json", "/my-orcid/otherNamesForms.json", "/my-orcid/websitesForms.json",
"/ng-cli-ws", "/not-found", "/notifications/frequencies/view", "/orgs/disambiguated/FUNDREF", "/orgs/disambiguated/GRID", "/orgs/disambiguated/LEI", "/orgs/disambiguated/RINGGOLD",
"/orgs/disambiguated/ROR", "/peer-reviews/peer-review.json", "/peer-reviews/peer-reviews-by-group-id.json", "/peer-reviews/peer-reviews-minimized.json", "/qr-code.png",
"/research-resources/researchResource.json", "/research-resources/researchResourcePage.json", "/works/getWorkInfo.json", "/works/groupingSuggestions.json", "/works/idTypes.json", "/works/work.json",
"/works/worksExtendedPage.json");
private final Set<String> SKIP_SAVE_SESSION = new HashSet<>(urisToSkip);

public OrcidRedisIndexedSessionRepository(RedisOperations<Object, Object> sessionRedisOperations) {
Expand Down Expand Up @@ -129,7 +130,7 @@ public void save(OrcidRedisIndexedSessionRepository.RedisSession session) {

if(updateSession()) {
//TODO: REMOVE THIS LOG ENTRY BEFORE GOING LIVE!!!!
logger.info("Saving session for " + request.getRequestURI());
logger.info("Saving session for " + request.getRequestURI() + " - " + request.getMethod());
session.save();
if (session.isNew) {
String sessionCreatedKey = this.getSessionCreatedChannel(session.getId());
Expand All @@ -138,7 +139,7 @@ public void save(OrcidRedisIndexedSessionRepository.RedisSession session) {
}
} else {
//TODO: REMOVE THIS LOG ENTRY BEFORE GOING LIVE!!!!
logger.info("Skip save session id " + request.getRequestURI());
logger.info("Skip save session id " + request.getRequestURI() + " - " + request.getMethod());
}
}

Expand Down Expand Up @@ -205,11 +206,11 @@ private MapSession loadSession(String id, Map<Object, Object> entries) {
///////////////////////////////////////////////
if(updateSession()) {
// TODO: REMOVE THIS LOG ENTRY BEFORE GOING LIVE!!!
logger.info("Updating last accessed time for " + request.getRequestURI());
logger.info("Updating last accessed time for " + request.getRequestURI() + " - " + request.getMethod());
loaded.setLastAccessedTime(Instant.ofEpochMilli((Long) entry.getValue()));
} else {
// TODO: REMOVE THIS LOG ENTRY BEFORE GOING LIVE!!!
logger.info("Ignoring last accessed time for " + request.getRequestURI());
logger.info("Ignoring last accessed time for " + request.getRequestURI() + " - " + request.getMethod());
}
} else if (key.startsWith("sessionAttr:")) {
loaded.setAttribute(key.substring("sessionAttr:".length()), entry.getValue());
Expand Down Expand Up @@ -362,7 +363,13 @@ private BoundHashOperations<Object, Object, Object> getSessionBoundHashOperation
private boolean updateSession() {
ServletRequestAttributes att = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
HttpServletRequest request = att.getRequest();
return (!SKIP_SAVE_SESSION.contains(request.getRequestURI().substring(request.getContextPath().length())));
if(request.getMethod().equals("GET")) {
String url = request.getRequestURI().substring(request.getContextPath().length());
if(SKIP_SAVE_SESSION.contains(url) || url.matches(PUBLIC_ORCID_PAGE_REGEX)) {
return false;
}
}
return true;
}

static String getSessionAttrNameKey(String attributeName) {
Expand Down Expand Up @@ -402,7 +409,7 @@ public void setLastAccessedTime(Instant lastAccessedTime) {
// TODO: REMOVE THIS BEFORE GOING LIVE!!!!
ServletRequestAttributes att = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
HttpServletRequest request = att.getRequest();
System.out.println("REDIS_SESSION: setLastAccessedTime: " + request.getRequestURI().toString());
System.out.println("REDIS_SESSION: setLastAccessedTime: " + request.getRequestURI().toString() + " - " + request.getMethod());

this.cached.setLastAccessedTime(lastAccessedTime);
this.delta.put("lastAccessedTime", this.getLastAccessedTime().toEpochMilli());
Expand Down
Loading

0 comments on commit 6846137

Please sign in to comment.