11package io .github .isagroup ;
22
3- import java .util .*;
3+ import java .util .HashMap ;
4+ import java .util .Map ;
5+ import java .util .Optional ;
46import java .util .logging .Logger ;
57
6- import io .github .isagroup .models .*;
78import org .springframework .beans .factory .annotation .Autowired ;
89import org .springframework .expression .spel .SpelEvaluationException ;
910import org .springframework .stereotype .Component ;
1011
1112import io .github .isagroup .exceptions .PricingPlanEvaluationException ;
13+ import io .github .isagroup .models .Feature ;
14+ import io .github .isagroup .models .FeatureStatus ;
15+ import io .github .isagroup .models .PlanContextManager ;
16+ import io .github .isagroup .models .PricingManager ;
1217import io .github .isagroup .services .jwt .PricingJwtUtils ;
13- import io .jsonwebtoken .Jwts ;
14- import io .jsonwebtoken .SignatureAlgorithm ;
1518
1619/**
1720 * Utility class that provides methods to generate and manage JWT that contains
@@ -58,23 +61,19 @@ public String generateUserToken() {
5861 planContextManager .setUserContext (pricingContext .getUserContext ());
5962 claims .put ("userContext" , planContextManager .getUserContext ());
6063 } catch (Exception e ) {
61- throw new PricingPlanEvaluationException ("Error while retrieving user context! Please check your PricingContext.getUserContext() method" );
64+ throw new PricingPlanEvaluationException (
65+ "Error while retrieving user context! Please check your PricingContext.getUserContext() method" );
6266 }
6367
6468 if (!pricingContext .userAffectedByPricing ()) {
65- return Jwts .builder ()
66- .setClaims (claims )
67- .setSubject (subject )
68- .setIssuedAt (new Date (System .currentTimeMillis ()))
69- .setExpiration (new Date (System .currentTimeMillis () + pricingContext .getJwtExpiration ()))
70- .signWith (SignatureAlgorithm .HS512 , pricingContext .getJwtSecret ())
71- .compact ();
69+ return jwtUtils .createJwtToken (claims , subject );
7270 }
7371
7472 try {
7573 planContextManager .setPlanContext (pricingContext .getPlanContext ());
7674 } catch (NullPointerException e ) {
77- throw new PricingPlanEvaluationException ("Error while retrieving plan context! Please check your configuration file or add a plan with the given name" );
75+ throw new PricingPlanEvaluationException (
76+ "Error while retrieving plan context! Please check your configuration file or add a plan with the given name" );
7877 }
7978
8079 PricingManager pricingManager = pricingContext .getPricingManager ();
@@ -86,18 +85,11 @@ public String generateUserToken() {
8685 claims .put ("features" , featureStatuses );
8786 claims .put ("planContext" , planContextManager .getPlanContext ());
8887
89- return Jwts .builder ()
90- .setClaims (claims )
91- .setSubject (subject )
92- .setIssuedAt (new Date (System .currentTimeMillis ()))
93- .setExpiration (new Date (System .currentTimeMillis () + pricingContext .getJwtExpiration ()))
94- .signWith (SignatureAlgorithm .HS512 , pricingContext .getJwtSecret ())
95- .compact ();
88+ return jwtUtils .createJwtToken (claims , subject );
9689 }
9790
98-
9991 private Map <String , FeatureStatus > computeFeatureStatuses (PlanContextManager planContextManager ,
100- Map <String , Feature > features ) {
92+ Map <String , Feature > features ) {
10193
10294 Map <String , FeatureStatus > featureStatuses = new HashMap <>();
10395
@@ -109,10 +101,11 @@ private Map<String, FeatureStatus> computeFeatureStatuses(PlanContextManager pla
109101 String expression = features .get (featureName ).getExpression ();
110102 try {
111103 Boolean eval = FeatureStatus .computeFeatureEvaluation (expression , planContextManager )
112- .orElseThrow (() -> new PricingPlanEvaluationException ("Evaluation was null" ));
104+ .orElseThrow (() -> new PricingPlanEvaluationException ("Evaluation was null" ));
113105 featureStatus .setEval (eval );
114106 } catch (SpelEvaluationException e ) {
115- throw new PricingPlanEvaluationException ("Error while evaluating the expression of the feature " + featureName + "! Please check the expression" );
107+ throw new PricingPlanEvaluationException ("Error while evaluating the expression of the feature "
108+ + featureName + "! Please check the expression" );
116109 }
117110
118111 Optional <String > userContextKey = FeatureStatus .computeUserContextVariable (expression );
@@ -124,9 +117,12 @@ private Map<String, FeatureStatus> computeFeatureStatuses(PlanContextManager pla
124117 featureStatus .setUsed (planContextManager .getUserContext ().get (userContextKey .get ()));
125118 if (feature .getExpression ().contains ("usageLimits" )) {
126119 String usageLimitName = feature .getExpression ().split ("usageLimits" )[1 ].split ("[',\" ]" )[2 ];
127- featureStatus .setLimit (((Map <String , Object >) planContextManager .getPlanContext ().get ("usageLimits" )).get (usageLimitName ));
120+ featureStatus
121+ .setLimit (((Map <String , Object >) planContextManager .getPlanContext ().get ("usageLimits" ))
122+ .get (usageLimitName ));
128123 } else {
129- featureStatus .setLimit (((Map <String , Object >) planContextManager .getPlanContext ().get ("features" )).get (featureName ));
124+ featureStatus .setLimit (((Map <String , Object >) planContextManager .getPlanContext ().get ("features" ))
125+ .get (featureName ));
130126 }
131127
132128 }
@@ -148,7 +144,7 @@ private Map<String, FeatureStatus> computeFeatureStatuses(PlanContextManager pla
148144 * @param expression the expression of the feature that will replace its
149145 * evaluation
150146 * @return Modified version of the provided JWT that contains the new expression
151- * in the "eval" attribute of the feature.
147+ * in the "eval" attribute of the feature.
152148 */
153149 public String addExpressionToToken (String token , String featureId , String expression ) {
154150
@@ -173,13 +169,7 @@ private String buildJwtToken(Map<String, Map<String, Object>> features, String s
173169 claims .put ("userContext" , pricingContext .getUserContext ());
174170 claims .put ("planContext" , pricingContext .getPlanContext ());
175171
176- return Jwts .builder ()
177- .setClaims (claims )
178- .setSubject (subject )
179- .setIssuedAt (new Date (System .currentTimeMillis ()))
180- .setExpiration (new Date (System .currentTimeMillis () + pricingContext .getJwtExpiration ()))
181- .signWith (SignatureAlgorithm .HS512 , pricingContext .getJwtSecret ())
182- .compact ();
172+ return jwtUtils .createJwtToken (claims , subject );
183173 }
184174
185175}
0 commit comments