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.fproxy.cache.utils;
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;
35 public class CacheUtils {
37 Logger logger = LoggerFactory.getLogger(CacheUtils.class);
40 private CredentialCache credentialCache;
42 @Value("${transactionid.header.name}")
43 private String transactionIdHeaderName;
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);
59 logger.info("No transaction ID found in request, skipping credential population...");
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.");
69 logger.info("Request already contains header with name: {}, skipping credential population...",
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.");
81 logger.info("Request already contains cookie with name: {}, skipping credential population...",
86 public void addCredentialsToCache(String transactionId, CredentialCacheData credentialdata, long cacheExpiryMs) {
87 credentialCache.add(transactionId, credentialdata, cacheExpiryMs);