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