com.infusers.core.util.InfusersUtility Maven / Gradle / Ivy
package com.infusers.core.util;
import java.util.Date;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.core.env.Environment;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import com.infusers.core.constants.InfuserConstants;
import com.infusers.core.secrets.dto.OtherSecrets;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
public class InfusersUtility {
private static InfusersUtility instance;
private final String jwtTokenSecret = "NoMoreASecretBuddy";
private boolean isEmailConfigrued = false;
private boolean isJWTTokenDynamicEnabled = false;
public boolean isJWTTokenDynamicEnabled() {
return isJWTTokenDynamicEnabled;
}
public void setJWTTokenDynamicEnabled(boolean isJWTTokenDynamicEnabled) {
this.isJWTTokenDynamicEnabled = isJWTTokenDynamicEnabled;
}
public boolean isEmailConfigrued() {
return isEmailConfigrued;
}
public void setEmailConfigrued(boolean isEmailConfigrued) {
this.isEmailConfigrued = isEmailConfigrued;
}
private Logger log = LogManager.getLogger(InfusersUtility.class);
public static final InfusersUtility getInstance() {
if(instance==null) {
instance = new InfusersUtility();
}
return instance;
}
public String getActiveProfile(Environment environment) {
String[] activeProfiles = environment!=null ? environment.getActiveProfiles() : null;
if (activeProfiles!=null && activeProfiles.length > 0) {
return activeProfiles[0];
}
else {
log.error("InfusersUtility.getActiveProfile() No active profiles!, " + activeProfiles);
}
return null;
}
public boolean isActiveProfileDevelopment(Environment environment) {
return environment!=null && isActiveProfileDevelopment(getActiveProfile(environment));
}
public boolean isActiveProfileDevelopment(String activeProfile) {
return activeProfile!=null && activeProfile.contains(InfuserConstants.STATIC_TEXT_ACTIVE_PROFILE_DEV);
}
public String getToken(String email) {
return Jwts.builder()
//.setSubject(email)
.setSubject(String.format("%s", email))
.setIssuer("infusers.in")
.setIssuedAt(new Date())
.setExpiration(new Date(System.currentTimeMillis() + (int)(OtherSecrets.getInstance().getJwtTokenExpiryHoursNumber()*60*60*1000)))
//.setExpiration(new Date(System.currentTimeMillis() + 864_000_000))
.signWith(SignatureAlgorithm.HS512, getJwttokensecret().getBytes())
.compact();
}
public String getJwttokensecret() {
if(isJWTTokenDynamicEnabled) {
return OtherSecrets.getInstance().getJwttokensecret();
}
else {
return jwtTokenSecret;
}
}
public Long parseString2Long(String stringVal) {
return stringVal==null? 0 : Long.parseLong(stringVal.trim());
}
public String getLoggedInUserName() {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
Object pricipal = auth.getPrincipal();
if(pricipal!=null) {
return (String)pricipal;
}
else {
log.error("InfusersUtility.getLoggedInUserName() Principal is NULL, " + pricipal);
return null;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy