All to 2.1.7-SNAPSHOT
[aaf/cadi.git] / sidecar / fproxy / src / main / java / org / onap / aaf / cadi / sidecar / fproxy / service / ForwardingProxyService.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.service;
21
22 import java.net.URI;
23 import java.util.Enumeration;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
26
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;
45
46 @RestController
47 public class ForwardingProxyService {
48
49     Logger logger = LoggerFactory.getLogger(ForwardingProxyService.class);
50
51     private static final long DEFAULT_CACHE_EXPIRY_MS = 180000; // 3 mins
52
53     @Autowired
54     RestTemplate restTemplate;
55
56     @Autowired
57     CacheUtils cacheUtils;
58
59     @Value("${credential.cache.timeout.ms:" + DEFAULT_CACHE_EXPIRY_MS + "}")
60     long cacheExpiryMs;
61
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);
66
67         // Update credential cache
68         logger.debug("Credential data: {}", credentialdata);
69         cacheUtils.addCredentialsToCache(transactionId, credentialdata, cacheExpiryMs);
70
71         logger.info("Credential cache successfully updated with transaction ID: {}", transactionId);
72         return new ResponseEntity<>(transactionId, HttpStatus.OK);
73     }
74
75     @RequestMapping("/**")
76     public ResponseEntity<String> forwardRest(@RequestBody(required = false) String body, HttpMethod method,
77             HttpServletRequest request, HttpServletResponse response) {
78
79         String requestUrl = request.getRequestURI();
80
81         logger.info("Request received: {}", requestUrl);
82
83         URI uri = UriComponentsBuilder.fromHttpUrl(request.getRequestURL().toString()).query(request.getQueryString())
84                 .build(true).toUri();
85
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));
91         }
92
93         cacheUtils.populateCredentialsFromCache(headers, request);
94
95         HttpEntity<String> httpEntity = new HttpEntity<>(body, headers);
96
97         logger.info("Forwarding request...");
98
99         return restTemplate.exchange(uri, method, httpEntity, String.class);
100     }
101 }