All to 2.1.7-SNAPSHOT
[aaf/cadi.git] / sidecar / fproxy / src / main / java / org / onap / aaf / cadi / sidecar / 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.cadi.sidecar.fproxy.cache.utils;
21
22 import javax.servlet.http.HttpServletRequest;
23
24 import org.onap.aaf.cadi.sidecar.fproxy.cache.CredentialCache;
25 import org.onap.aaf.cadi.sidecar.fproxy.data.CredentialCacheData;
26 import org.onap.aaf.cadi.sidecar.fproxy.data.CredentialCacheData.CredentialType;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.beans.factory.annotation.Value;
31 import org.springframework.http.HttpHeaders;
32 import org.springframework.stereotype.Component;
33 import org.springframework.web.util.WebUtils;
34
35 @Component
36 public class CacheUtils {
37
38     Logger logger = LoggerFactory.getLogger(CacheUtils.class);
39
40     @Autowired
41     private CredentialCache credentialCache;
42
43     @Value("${transactionid.header.name}")
44     private String transactionIdHeaderName;
45
46     public void populateCredentialsFromCache(HttpHeaders headers, HttpServletRequest request) {
47         String transactionId = headers.getFirst(transactionIdHeaderName);
48         if (transactionId != null) {
49             CredentialCacheData cacheData = credentialCache.get(transactionId);
50             if (cacheData == null) {
51                 logger.info("Transaction ID {} not found in cache, skipping credential population...", transactionId);
52             } else if (cacheData.getCredentialType().equals(CredentialType.HEADER)) {
53                 logger.info("Populating header credentials from cache for transaction ID: {}", transactionId);
54                 applyHeaderCacheData(cacheData, headers);
55             } else if (cacheData.getCredentialType().equals(CredentialType.COOKIE)) {
56                 logger.info("Populating cookie credentials from cache for transaction ID: {}", transactionId);
57                 applyCookieCacheData(cacheData, headers, request);
58             }
59         } else {
60             logger.info("No transaction ID found in request, skipping credential population...");
61         }
62     }
63
64     private void applyHeaderCacheData(CredentialCacheData cacheData, HttpHeaders headers) {
65         String credentialName = cacheData.getCredentialName();
66         if (!headers.containsKey(credentialName)) {
67             headers.add(credentialName, cacheData.getCredentialValue());
68             logger.info("Header credentials successfully populated.");
69         } else {
70             logger.info("Request already contains header with name: {}, skipping credential population...",
71                     credentialName);
72         }
73     }
74
75     private void applyCookieCacheData(CredentialCacheData cacheData, HttpHeaders headers, HttpServletRequest request) {
76         String credentialName = cacheData.getCredentialName();
77         // Check if Cookie with same name is already set then skip
78         if (WebUtils.getCookie(request, credentialName) == null) {
79             headers.add(HttpHeaders.COOKIE, cacheData.getCredentialValue());
80             logger.info("Cookie credentials successfully populated.");
81         } else {
82             logger.info("Request already contains cookie with name: {}, skipping credential population...",
83                     credentialName);
84         }
85     }
86
87     public void addCredentialsToCache(String transactionId, CredentialCacheData credentialdata, long cacheExpiryMs) {
88         credentialCache.add(transactionId, credentialdata, cacheExpiryMs);
89     }
90 }