[PORTAL-7] Rebase
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / service / MicroserviceProxyServiceImpl.java
1 /*-\r
2  * ================================================================================\r
3  * ECOMP Portal\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ================================================================================\r
19  */\r
20 package org.openecomp.portalapp.portal.service;\r
21 \r
22 import java.util.List;\r
23 \r
24 import javax.servlet.http.HttpServletRequest;\r
25 \r
26 import org.apache.commons.codec.binary.Base64;\r
27 import org.openecomp.portalapp.portal.domain.EPUser;\r
28 import org.openecomp.portalapp.portal.domain.MicroserviceData;\r
29 import org.openecomp.portalapp.portal.domain.MicroserviceParameter;\r
30 import org.openecomp.portalapp.portal.domain.WidgetCatalog;\r
31 import org.openecomp.portalapp.portal.domain.WidgetCatalogParameter;\r
32 import org.openecomp.portalapp.portal.domain.WidgetServiceHeaders;\r
33 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;\r
34 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;\r
35 import org.openecomp.portalsdk.core.onboarding.util.CipherUtil;\r
36 import org.openecomp.portalsdk.core.util.SystemProperties;\r
37 import org.springframework.beans.factory.annotation.Autowired;\r
38 import org.springframework.context.annotation.EnableAspectJAutoProxy;\r
39 import org.springframework.http.HttpEntity;\r
40 import org.springframework.http.HttpHeaders;\r
41 import org.springframework.http.HttpMethod;\r
42 import org.springframework.http.MediaType;\r
43 import org.springframework.http.ResponseEntity;\r
44 import org.springframework.stereotype.Service;\r
45 import org.springframework.web.client.HttpClientErrorException;\r
46 import org.springframework.web.client.RestTemplate;\r
47 \r
48 @Service("microserviceProxyService")\r
49 @EnableAspectJAutoProxy\r
50 @EPMetricsLog\r
51 public class MicroserviceProxyServiceImpl implements MicroserviceProxyService {\r
52 \r
53         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MicroserviceProxyServiceImpl.class);\r
54         private static final String BASIC_AUTH = "Basic Authentication";\r
55         private static final String NO_AUTH = "No Authentication";\r
56         private static final String COOKIE_AUTH = "Cookie based Authentication";\r
57         private static final String QUESTION_MARK = "?";\r
58         private static final String ADD_MARK = "&";\r
59 \r
60         String whatService = "widgets-service";\r
61 \r
62         @Autowired\r
63         private ConsulHealthService consulHealthService;\r
64         \r
65         @Autowired\r
66         MicroserviceService microserviceService;\r
67 \r
68         @Autowired\r
69         WidgetParameterService widgetParameterService;\r
70 \r
71         RestTemplate template = new RestTemplate();\r
72 \r
73         @Override\r
74         public String proxyToDestination(long serviceId, EPUser user, HttpServletRequest request) throws Exception {\r
75 \r
76                 String response = null;\r
77                 \r
78                 // get the microservice object by the id\r
79                 MicroserviceData data = microserviceService.getMicroserviceDataById(serviceId);\r
80 \r
81                 // No such microservice available\r
82                 if (data == null) {\r
83                         return response;\r
84                 }\r
85                 List<MicroserviceParameter> params = data.getParameterList();\r
86                 if (data.getSecurityType().equals(NO_AUTH)) {\r
87                         HttpHeaders headers = new HttpHeaders();\r
88                         headers.setContentType(MediaType.APPLICATION_JSON);\r
89                         HttpEntity<String> entity = new HttpEntity<String>(headers);\r
90 \r
91                         String url = microserviceUrlConverter(data, params);\r
92                         response = template.exchange(url, HttpMethod.GET, entity, String.class).getBody();\r
93 \r
94                 } else if (data.getSecurityType().equals(BASIC_AUTH)) {\r
95                         // encoding the username and password\r
96                         String plainCreds = data.getUsername() + ":" + decryptedPassword(data.getPassword());\r
97                         byte[] plainCredsBytes = plainCreds.getBytes();\r
98                         byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);\r
99                         String base64Creds = new String(base64CredsBytes);\r
100 \r
101                         HttpHeaders headers = new HttpHeaders();\r
102                         headers.add("Authorization", "Basic " + base64Creds);\r
103                         headers.setContentType(MediaType.APPLICATION_JSON);\r
104                         HttpEntity<String> entity = new HttpEntity<String>(headers);\r
105 \r
106                         String url = microserviceUrlConverter(data, params);\r
107                         response = template.exchange(url, HttpMethod.GET, entity, String.class).getBody();\r
108                 } else if (data.getSecurityType().equals(COOKIE_AUTH)) {\r
109                         HttpHeaders headers = new HttpHeaders();\r
110                         headers.setContentType(MediaType.APPLICATION_JSON);\r
111                         String rawCookie = request.getHeader("Cookie");\r
112                         headers.add("Cookie", rawCookie);\r
113                         HttpEntity<String> entity = new HttpEntity<String>(headers);\r
114 \r
115                         String url = microserviceUrlConverter(data, params);\r
116                         response = template.exchange(url, HttpMethod.GET, entity, String.class).getBody();\r
117                 }\r
118                 return response;\r
119         }\r
120         \r
121         @Override\r
122         public String proxyToDestinationByWidgetId(long widgetId, EPUser user, HttpServletRequest request) throws Exception {\r
123 \r
124                 String response = null;\r
125                 \r
126                 ResponseEntity<Long> ans = (ResponseEntity<Long>) template.exchange(\r
127                                 "https://" + consulHealthService.getServiceLocation(whatService)\r
128                                                 + "/widget/microservices/widgetCatalog/parameters/" + widgetId,\r
129                                 HttpMethod.GET, new HttpEntity(WidgetServiceHeaders.getInstance()), Long.class);\r
130                 Long serviceId = ans.getBody();\r
131                 \r
132                 // get the microservice object by the id\r
133                 MicroserviceData data = microserviceService.getMicroserviceDataById(serviceId);\r
134 \r
135                 // No such microservice available\r
136                 if (data == null) {\r
137                         return response;\r
138                 }\r
139 \r
140                 List<MicroserviceParameter> params = data.getParameterList();\r
141 \r
142                 for (MicroserviceParameter p : params) {\r
143                         WidgetCatalogParameter userValue = widgetParameterService.getUserParamById(widgetId, user.getId(), p.getId());\r
144                         if (userValue != null)\r
145                                 p.setPara_value(userValue.getUser_value());\r
146                 }\r
147 \r
148                 if (data.getSecurityType().equals(NO_AUTH)) {\r
149                         HttpHeaders headers = new HttpHeaders();\r
150                         headers.setContentType(MediaType.APPLICATION_JSON);\r
151                         HttpEntity<String> entity = new HttpEntity<String>(headers);\r
152 \r
153                         String url = microserviceUrlConverter(data, params);\r
154                         try {\r
155                                 response = template.exchange(url, HttpMethod.GET, entity, String.class).getBody();\r
156                         } catch (HttpClientErrorException e) {\r
157                                 throw e;\r
158                         }\r
159                 } else if (data.getSecurityType().equals(BASIC_AUTH)) {\r
160                         // encoding the username and password\r
161                         String plainCreds = data.getUsername() + ":" + decryptedPassword(data.getPassword());\r
162                         byte[] plainCredsBytes = plainCreds.getBytes();\r
163                         byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);\r
164                         String base64Creds = new String(base64CredsBytes);\r
165 \r
166                         HttpHeaders headers = new HttpHeaders();\r
167                         headers.add("Authorization", "Basic " + base64Creds);\r
168                         headers.setContentType(MediaType.APPLICATION_JSON);\r
169                         HttpEntity<String> entity = new HttpEntity<String>(headers);\r
170 \r
171                         String url = microserviceUrlConverter(data, params);\r
172                         try {\r
173                                 response = template.exchange(url, HttpMethod.GET, entity, String.class).getBody();\r
174                         } catch (HttpClientErrorException e) {\r
175                                 throw e;\r
176                         }\r
177                 } else if (data.getSecurityType().equals(COOKIE_AUTH)) {\r
178                         HttpHeaders headers = new HttpHeaders();\r
179                         headers.setContentType(MediaType.APPLICATION_JSON);\r
180                         String rawCookie = request.getHeader("Cookie");\r
181                         headers.add("Cookie", rawCookie);\r
182                         HttpEntity<String> entity = new HttpEntity<String>(headers);\r
183 \r
184                         String url = microserviceUrlConverter(data, params);\r
185                         try {\r
186                                 response = template.exchange(url, HttpMethod.GET, entity, String.class).getBody();\r
187                         } catch (HttpClientErrorException e) {\r
188                                 throw e;\r
189                         }\r
190                 }\r
191                 return response;\r
192         }\r
193 \r
194         private String decryptedPassword(String encryptedPwd) throws Exception {\r
195                 String result = "";\r
196                 if (encryptedPwd != null & encryptedPwd.length() > 0) {\r
197                         try {\r
198                                 result = CipherUtil.decrypt(encryptedPwd,\r
199                                                 SystemProperties.getProperty(SystemProperties.Decryption_Key));\r
200                         } catch (Exception e) {\r
201                                 logger.error(EELFLoggerDelegate.errorLogger, "decryptedPassword failed", e);\r
202                                 throw e;\r
203                         }\r
204                 }\r
205                 return result;\r
206         }\r
207 \r
208         private String microserviceUrlConverter(MicroserviceData data, List<MicroserviceParameter> params) {\r
209                 String url = data.getUrl();\r
210                 for (int i = 0; i < params.size(); i++) {\r
211                         if (i == 0) {\r
212                                 url += QUESTION_MARK;\r
213                         }\r
214                         url += params.get(i).getPara_key() + "=" + params.get(i).getPara_value();\r
215                         if (i != (params.size() - 1)) {\r
216                                 url += ADD_MARK;\r
217                         }\r
218                 }\r
219                 return url;\r
220         }\r
221 \r
222 }\r