b80fc3249756b247c77914202e40aad5f2c49bcf
[aaf/cadi.git] / sidecar / fproxy / src / main / java / org / onap / aaf / fproxy / cache / utils / CacheUtils.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aaf
4  * ================================================================================
5  * Copyright © 2018 European Software Marketing Ltd.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *       http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.aaf.fproxy.cache.utils;
21
22 import javax.servlet.http.HttpServletRequest;
23 import org.onap.aaf.fproxy.cache.CredentialCache;
24 import org.onap.aaf.fproxy.data.CredentialCacheData;
25 import org.onap.aaf.fproxy.data.CredentialCacheData.CredentialType;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.beans.factory.annotation.Value;
30 import org.springframework.http.HttpHeaders;
31 import org.springframework.stereotype.Component;
32 import org.springframework.web.util.WebUtils;
33
34 @Component
35 public class CacheUtils {
36
37     Logger logger = LoggerFactory.getLogger(CacheUtils.class);
38
39     @Autowired
40     private CredentialCache credentialCache;
41
42     @Value("${transactionid.header.name}")
43     private String transactionIdHeaderName;
44
45     public void populateCredentialsFromCache(HttpHeaders headers, HttpServletRequest request) {
46         String transactionId = headers.getFirst(transactionIdHeaderName);
47         if (transactionId != null) {
48             CredentialCacheData cacheData = credentialCache.get(transactionId);
49             if (cacheData == null) {
50                 logger.info("Transaction ID {} not found in cache, skipping credential population...", transactionId);
51             } else if (cacheData.getCredentialType().equals(CredentialType.HEADER)) {
52                 logger.info("Populating header credentials from cache for transaction ID: {}", transactionId);
53                 applyHeaderCacheData(cacheData, headers);
54             } else if (cacheData.getCredentialType().equals(CredentialType.COOKIE)) {
55                 logger.info("Populating cookie credentials from cache for transaction ID: {}", transactionId);
56                 applyCookieCacheData(cacheData, headers, request);
57             }
58         } else {
59             logger.info("No transaction ID found in request, skipping credential population...");
60         }
61     }
62
63     private void applyHeaderCacheData(CredentialCacheData cacheData, HttpHeaders headers) {
64         String credentialName = cacheData.getCredentialName();
65         if (!headers.containsKey(credentialName)) {
66             headers.add(credentialName, cacheData.getCredentialValue());
67             logger.info("Header credentials successfully populated.");
68         } else {
69             logger.info("Request already contains header with name: {}, skipping credential population...",
70                     credentialName);
71         }
72     }
73
74     private void applyCookieCacheData(CredentialCacheData cacheData, HttpHeaders headers, HttpServletRequest request) {
75         String credentialName = cacheData.getCredentialName();
76         // Check if Cookie with same name is already set then skip
77         if (WebUtils.getCookie(request, credentialName) == null) {
78             headers.add(HttpHeaders.COOKIE, cacheData.getCredentialValue());
79             logger.info("Cookie credentials successfully populated.");
80         } else {
81             logger.info("Request already contains cookie with name: {}, skipping credential population...",
82                     credentialName);
83         }
84     }
85
86     public void addCredentialsToCache(String transactionId, CredentialCacheData credentialdata, long cacheExpiryMs) {
87         credentialCache.add(transactionId, credentialdata, cacheExpiryMs);
88     }
89 }