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.service;
23 import java.util.Enumeration;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
27 import org.onap.aaf.cadi.sidecar.fproxy.cache.utils.CacheUtils;
28 import org.onap.aaf.cadi.sidecar.fproxy.data.CredentialCacheData;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.beans.factory.annotation.Value;
33 import org.springframework.http.HttpEntity;
34 import org.springframework.http.HttpHeaders;
35 import org.springframework.http.HttpMethod;
36 import org.springframework.http.HttpStatus;
37 import org.springframework.http.ResponseEntity;
38 import org.springframework.web.bind.annotation.PathVariable;
39 import org.springframework.web.bind.annotation.RequestBody;
40 import org.springframework.web.bind.annotation.RequestMapping;
41 import org.springframework.web.bind.annotation.RequestMethod;
42 import org.springframework.web.bind.annotation.RestController;
43 import org.springframework.web.client.RestTemplate;
44 import org.springframework.web.util.UriComponentsBuilder;
47 public class ForwardingProxyService {
49 Logger logger = LoggerFactory.getLogger(ForwardingProxyService.class);
51 private static final long DEFAULT_CACHE_EXPIRY_MS = 180000; // 3 mins
54 RestTemplate restTemplate;
57 CacheUtils cacheUtils;
59 @Value("${credential.cache.timeout.ms:" + DEFAULT_CACHE_EXPIRY_MS + "}")
62 @RequestMapping(value = "/credential-cache/{transactionId}", method = RequestMethod.POST)
63 public ResponseEntity<String> addCredentialToCache(@PathVariable("transactionId") String transactionId,
64 @RequestBody CredentialCacheData credentialdata) {
65 logger.info("Updating credential cache with transaction ID: {}", transactionId);
67 // Update credential cache
68 logger.debug("Credential data: {}", credentialdata);
69 cacheUtils.addCredentialsToCache(transactionId, credentialdata, cacheExpiryMs);
71 logger.info("Credential cache successfully updated with transaction ID: {}", transactionId);
72 return new ResponseEntity<>(transactionId, HttpStatus.OK);
75 @RequestMapping("/**")
76 public ResponseEntity<String> forwardRest(@RequestBody(required = false) String body, HttpMethod method,
77 HttpServletRequest request, HttpServletResponse response) {
79 String requestUrl = request.getRequestURI();
81 logger.info("Request received: {}", requestUrl);
83 URI uri = UriComponentsBuilder.fromHttpUrl(request.getRequestURL().toString()).query(request.getQueryString())
86 HttpHeaders headers = new HttpHeaders();
87 Enumeration<String> headerNames = request.getHeaderNames();
88 while (headerNames.hasMoreElements()) {
89 String headerName = headerNames.nextElement();
90 headers.set(headerName, request.getHeader(headerName));
93 cacheUtils.populateCredentialsFromCache(headers, request);
95 HttpEntity<String> httpEntity = new HttpEntity<>(body, headers);
97 logger.info("Forwarding request...");
99 return restTemplate.exchange(uri, method, httpEntity, String.class);