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