Expand my Community achievements bar.

SOLVED

Token Caching with AEM

Avatar

Level 3

Hi All,

 

We have a Rest API Integration with AEM and Before Hitting the actual API we hit a API which generates a Token and Token needs to be passed to actual API to get the Response.

 

So first time once API is hit to get the token ,token is valid for 10 minutes so within 10 min same token should be used for all the API's and should be saved Server Side.

 

What is the best way to store the Token in AEM at server Side.

 

Do we need to use any Java Cache like (Ehcache) or what is the correct approach to handle such sceanrios.

 

Thanks in Advance.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @Sb2512 ,

I would use JavaBean which adhere to an extremely simple coding convention to cache response. 

  • Call API to get Token response.
    Interface
    public interface Response {
         String getToken();
    }
    Implementation
    public class TokenResponse implements Response {
    public String getToken() {
            if (null != this.getToken) {
                return this.getToken();
            }
            return null;
        }
    }
    Invoking
    TokenResponse tokenResponse = callYourHttpAPI();
     if (null ! = tokenResponse {
            //Do Something (Call to 2nd API by passing tokenResponse as a param)
            ..........
    }

Hope that helps!

Regards,
Santosh

View solution in original post

3 Replies

Avatar

Community Advisor

Hi

You can use in memory cache or jcr node to store but if you have multiple instance, in that case the storage will not guaranty that the token will be available.

but if you just want to improve this in term of performance then yes, you can use in memory cache.



Arun Patidar

Avatar

Level 3

Hi Arun,

 

Thanks for the reply...

 

If there are multiple publish instance how the token will be synced....

 

Any thoughts @arunpatidar 

Avatar

Correct answer by
Community Advisor

Hi @Sb2512 ,

I would use JavaBean which adhere to an extremely simple coding convention to cache response. 

  • Call API to get Token response.
    Interface
    public interface Response {
         String getToken();
    }
    Implementation
    public class TokenResponse implements Response {
    public String getToken() {
            if (null != this.getToken) {
                return this.getToken();
            }
            return null;
        }
    }
    Invoking
    TokenResponse tokenResponse = callYourHttpAPI();
     if (null ! = tokenResponse {
            //Do Something (Call to 2nd API by passing tokenResponse as a param)
            ..........
    }

Hope that helps!

Regards,
Santosh