2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.aaf.cadi.sidecar.fproxy.cache.utils;
22 import javax.servlet.http.HttpServletRequest;
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;
36 public class CacheUtils {
38 Logger logger = LoggerFactory.getLogger(CacheUtils.class);
41 private CredentialCache credentialCache;
43 @Value("${transactionid.header.name}")
44 private String transactionIdHeaderName;
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);
60 logger.info("No transaction ID found in request, skipping credential population...");
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.");
70 logger.info("Request already contains header with name: {}, skipping credential population...",
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.");
82 logger.info("Request already contains cookie with name: {}, skipping credential population...",
87 public void addCredentialsToCache(String transactionId, CredentialCacheData credentialdata, long cacheExpiryMs) {
88 credentialCache.add(transactionId, credentialdata, cacheExpiryMs);