Improve performance by pre-compiling the regexes.

This commit is contained in:
Lampros Smyrnaios 2023-01-12 19:23:42 +02:00
parent ceb53248ce
commit f136488b0e
4 changed files with 23 additions and 13 deletions

View File

@ -32,14 +32,16 @@ public class FrontEndLinkURIAuthenticationSuccessHandler implements Authenticati
@Value("${services.provide.aai.oidc.domain}")
private String domain;
private static final Pattern AUTH_REGEX = Pattern.compile("^([A-Za-z0-9-_=]+)\\.([A-Za-z0-9-_=]+)\\.?([A-Za-z0-9-_.+=]*)$");
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
OIDCAuthenticationToken authOIDC = (OIDCAuthenticationToken) authentication;
request.getSession().setAttribute(FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME, authOIDC.getUserInfo().getEmail());
Cookie accessToken = new Cookie("AccessToken", authOIDC.getAccessTokenValue());
String regex = "^([A-Za-z0-9-_=]+)\\.([A-Za-z0-9-_=]+)\\.?([A-Za-z0-9-_.+=]*)$";
Matcher matcher = Pattern.compile(regex).matcher(authOIDC.getAccessTokenValue());
Matcher matcher = AUTH_REGEX.matcher(authOIDC.getAccessTokenValue());
if (matcher.find()) {
long exp = new JsonParser().parse(new String(Base64.getDecoder().decode(matcher.group(2)))).getAsJsonObject().get("exp").getAsLong();
accessToken.setMaxAge((int) (exp - (new Date().getTime() / 1000)));

View File

@ -800,7 +800,7 @@ public class RepositoryServiceImpl implements RepositoryService {
if (mode.equalsIgnoreCase(Constants.REPOSITORY_MODE_ALL))
return compatibilityClasses;
else if (mode.equalsIgnoreCase(Constants.REPOSITORY_MODE_RE3DATA)) {
if (entry.getKey().matches("^openaire[1-9].0_data$")) {
if ( ValidatorServiceImpl.OPENAIRE_DATA_REGEX.matcher(entry.getKey()).matches() ) {
retMap.put(entry.getKey(), entry.getValue());
foundData = true;
}
@ -811,7 +811,7 @@ public class RepositoryServiceImpl implements RepositoryService {
foundData = true;
}
} else {
if (entry.getKey().matches("^openaire[1-9].0$") || entry.getKey().equals("driver"))
if ( ValidatorServiceImpl.OPENAIRE_OR_DRIVER_REGEX.matcher(entry.getKey()).matches() )
retMap.put(entry.getKey(), entry.getValue());
}
}

View File

@ -28,6 +28,7 @@ import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@ -68,6 +69,12 @@ public class ValidatorServiceImpl implements ValidatorService {
@Autowired
private MapJobDao crisJobs;
public static final Pattern OPENAIRE_DATA_REGEX = Pattern.compile("^openaire[1-9].0_data$");
public static final Pattern OPENAIRE_OR_DRIVER_REGEX = Pattern.compile("^(?:openaire[1-9].0|driver)$");
public static final Pattern OPENAIRE_CRIS_REGEX = Pattern.compile("^openaire[1-9].0_cris$");
public static final Pattern FAIR_REGEX = Pattern.compile(".*fair$");
@PostConstruct
private void loadRules(){
logger.debug("PostConstruct method! Load rules!");
@ -75,13 +82,14 @@ public class ValidatorServiceImpl implements ValidatorService {
for (RuleSet ruleSet : getValidationService().getRuleSets()) {
if (ruleSet.getVisibility() != null && ruleSet.getVisibility().contains("development")) {
String key = "";
if (ruleSet.getGuidelinesAcronym().matches("^openaire[1-9].0_data$"))
String guidelinesAcronym = ruleSet.getGuidelinesAcronym();
if ( OPENAIRE_DATA_REGEX.matcher(guidelinesAcronym).matches() )
key = Constants.VALIDATION_MODE_DATA;
else if (ruleSet.getGuidelinesAcronym().matches("^openaire[1-9].0$") || ruleSet.getGuidelinesAcronym().equals("driver"))
else if ( OPENAIRE_OR_DRIVER_REGEX.matcher(guidelinesAcronym).matches() )
key = Constants.VALIDATION_MODE_LITERATURE;
else if (ruleSet.getGuidelinesAcronym().matches("^openaire[1-9].0_cris$"))
else if ( OPENAIRE_CRIS_REGEX.matcher(guidelinesAcronym).matches() )
key = Constants.VALIDATION_MODE_CRIS;
else if (ruleSet.getGuidelinesAcronym().matches(".*fair$"))
else if ( FAIR_REGEX.matcher(guidelinesAcronym).matches() )
key = Constants.VALIDATION_MODE_FAIR;
if (rulesetMap.containsKey(key))
@ -143,7 +151,7 @@ public class ValidatorServiceImpl implements ValidatorService {
/////////////////////////////////////////////////////////////////////////////////////////
if (jobForValidation.getSelectedContentRules()!=null && jobForValidation.getSelectedContentRules().size() == 1 &&
jobForValidation.getSelectedContentRules().contains(-1000)
|| jobForValidation.getDesiredCompatibilityLevel().matches("openaire-cris_1.1")) {
|| jobForValidation.getDesiredCompatibilityLevel().equals("openaire-cris_1.1")) {
crisValidatorExecutor.submit(jobForValidation.getBaseUrl(), jobForValidation.getUserEmail());
} else {
this.getValidationService().submitValidationJob(jobForValidation);
@ -197,7 +205,7 @@ public class ValidatorServiceImpl implements ValidatorService {
@Override
public List<RuleSet> getRuleSets(String mode) {
logger.info("Getting rulesets for mode: " + mode);
logger.info("Getting ruleSets for mode: " + mode);
return rulesetMap.get(mode);
}

View File

@ -14,7 +14,7 @@ import java.util.regex.Pattern;
public class AuthoritiesMapper {
private static final Logger logger = Logger.getLogger(AuthoritiesMapper.class);
private static final String ENTITLEMENT_REGEX = "urn:geant:openaire[.]eu:group:([^:]*):?(.*)?:role=member#aai[.]openaire[.]eu";
private static final Pattern ENTITLEMENT_REGEX = Pattern.compile("urn:geant:openaire[.]eu:group:([^:]*):?(.*)?:role=member#aai[.]openaire[.]eu");
private AuthoritiesMapper() {
}
@ -29,7 +29,7 @@ public class AuthoritiesMapper {
List<String> roles = new ArrayList<>();
if (entitlements != null) {
for (JsonElement obj : entitlements) {
Matcher matcher = Pattern.compile(ENTITLEMENT_REGEX).matcher(obj.getAsString());
Matcher matcher = ENTITLEMENT_REGEX.matcher(obj.getAsString());
if (matcher.find()) {
StringBuilder sb = new StringBuilder();
if (matcher.group(1) != null && matcher.group(1).length() > 0) {
@ -50,7 +50,7 @@ public class AuthoritiesMapper {
private static void entityRoles(JsonArray entitlements, Set<GrantedAuthority> authorities) {
if (entitlements != null) {
for (JsonElement obj : entitlements) {
Matcher matcher = Pattern.compile(ENTITLEMENT_REGEX).matcher(obj.getAsString());
Matcher matcher = ENTITLEMENT_REGEX.matcher(obj.getAsString());
if (matcher.find()) {
StringBuilder sb = new StringBuilder();
if (matcher.group(1) != null && matcher.group(1).length() > 0) {