2 * ================================================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property
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 * ================================================================================
20 package org.openecomp.portalapp.portal.service;
22 import java.util.List;
24 import javax.servlet.http.HttpServletRequest;
26 import org.apache.commons.codec.binary.Base64;
27 import org.openecomp.portalapp.portal.domain.EPUser;
28 import org.openecomp.portalapp.portal.domain.MicroserviceData;
29 import org.openecomp.portalapp.portal.domain.MicroserviceParameter;
30 import org.openecomp.portalapp.portal.domain.WidgetCatalogParameter;
31 import org.openecomp.portalapp.portal.domain.WidgetServiceHeaders;
32 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
33 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
34 import org.openecomp.portalsdk.core.onboarding.util.CipherUtil;
35 import org.openecomp.portalsdk.core.util.SystemProperties;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.context.annotation.EnableAspectJAutoProxy;
38 import org.springframework.http.HttpEntity;
39 import org.springframework.http.HttpHeaders;
40 import org.springframework.http.HttpMethod;
41 import org.springframework.http.MediaType;
42 import org.springframework.http.ResponseEntity;
43 import org.springframework.stereotype.Service;
44 import org.springframework.web.client.HttpClientErrorException;
45 import org.springframework.web.client.RestTemplate;
47 @Service("microserviceProxyService")
48 @EnableAspectJAutoProxy
50 public class MicroserviceProxyServiceImpl implements MicroserviceProxyService {
52 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MicroserviceProxyServiceImpl.class);
53 private static final String BASIC_AUTH = "Basic Authentication";
54 private static final String NO_AUTH = "No Authentication";
55 private static final String COOKIE_AUTH = "Cookie based Authentication";
56 private static final String QUESTION_MARK = "?";
57 private static final String ADD_MARK = "&";
59 String whatService = "widgets-service";
62 private ConsulHealthService consulHealthService;
65 MicroserviceService microserviceService;
68 WidgetParameterService widgetParameterService;
70 RestTemplate template = new RestTemplate();
73 public String proxyToDestination(long serviceId, EPUser user, HttpServletRequest request) throws Exception {
75 String response = null;
77 // get the microservice object by the id
78 MicroserviceData data = microserviceService.getMicroserviceDataById(serviceId);
80 // No such microservice available
84 List<MicroserviceParameter> params = data.getParameterList();
85 MicroserviceParameter userId_param = new MicroserviceParameter();
86 userId_param.setPara_key("userId");
87 userId_param.setPara_value(user.getOrgUserId());
88 params.add(userId_param);
90 if (data.getSecurityType().equals(NO_AUTH)) {
91 HttpHeaders headers = new HttpHeaders();
92 headers.setContentType(MediaType.APPLICATION_JSON);
93 HttpEntity<String> entity = new HttpEntity<String>(headers);
95 String url = microserviceUrlConverter(data, params);
96 logger.debug(EELFLoggerDelegate.debugLogger, "Before making no authentication call: {}", url);
97 response = template.exchange(url, HttpMethod.GET, entity, String.class).getBody();
98 logger.debug(EELFLoggerDelegate.debugLogger, "No authentication call response: {}", response);
100 } else if (data.getSecurityType().equals(BASIC_AUTH)) {
101 // encoding the username and password
102 String plainCreds = data.getUsername() + ":" + decryptedPassword(data.getPassword());
103 byte[] plainCredsBytes = plainCreds.getBytes();
104 byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
105 String base64Creds = new String(base64CredsBytes);
107 HttpHeaders headers = new HttpHeaders();
108 headers.add("Authorization", "Basic " + base64Creds);
109 headers.setContentType(MediaType.APPLICATION_JSON);
110 HttpEntity<String> entity = new HttpEntity<String>(headers);
112 String url = microserviceUrlConverter(data, params);
113 logger.debug(EELFLoggerDelegate.debugLogger, "Before making basic authentication call: {}", url);
114 response = template.exchange(url, HttpMethod.GET, entity, String.class).getBody();
115 logger.debug(EELFLoggerDelegate.debugLogger, "Basic authentication call response: {}", response);
117 } else if (data.getSecurityType().equals(COOKIE_AUTH)) {
118 HttpHeaders headers = new HttpHeaders();
119 headers.setContentType(MediaType.APPLICATION_JSON);
120 String rawCookie = request.getHeader("Cookie");
121 headers.add("Cookie", rawCookie);
122 HttpEntity<String> entity = new HttpEntity<String>(headers);
124 String url = microserviceUrlConverter(data, params);
125 logger.debug(EELFLoggerDelegate.debugLogger, "Before making cookie-based authentication call: {}", url);
126 response = template.exchange(url, HttpMethod.GET, entity, String.class).getBody();
127 logger.debug(EELFLoggerDelegate.debugLogger, "Cookie-based authentication call response: {}", response);
133 public String proxyToDestinationByWidgetId(long widgetId, EPUser user, HttpServletRequest request)
136 String response = null;
138 @SuppressWarnings({ "rawtypes", "unchecked" })
139 ResponseEntity<Long> ans = (ResponseEntity<Long>) template.exchange(
141 + consulHealthService.getServiceLocation(whatService,
142 SystemProperties.getProperty("microservices.widget.local.port"))
143 + "/widget/microservices/widgetCatalog/parameters/" + widgetId,
144 HttpMethod.GET, new HttpEntity(WidgetServiceHeaders.getInstance()), Long.class);
145 Long serviceId = ans.getBody();
147 // get the microservice object by the id
148 MicroserviceData data = microserviceService.getMicroserviceDataById(serviceId);
150 // No such microservice available
155 List<MicroserviceParameter> params = data.getParameterList();
156 MicroserviceParameter userId_param = new MicroserviceParameter();
157 userId_param.setPara_key("userId");
158 userId_param.setPara_value(user.getOrgUserId());
159 params.add(userId_param);
161 for (MicroserviceParameter p : params) {
162 WidgetCatalogParameter userValue = widgetParameterService.getUserParamById(widgetId, user.getId(),
164 if (userValue != null)
165 p.setPara_value(userValue.getUser_value());
168 if (data.getSecurityType().equals(NO_AUTH)) {
169 HttpHeaders headers = new HttpHeaders();
170 headers.setContentType(MediaType.APPLICATION_JSON);
171 HttpEntity<String> entity = new HttpEntity<String>(headers);
173 String url = microserviceUrlConverter(data, params);
175 response = template.exchange(url, HttpMethod.GET, entity, String.class).getBody();
176 } catch (HttpClientErrorException e) {
179 } else if (data.getSecurityType().equals(BASIC_AUTH)) {
180 // encoding the username and password
181 String plainCreds = data.getUsername() + ":" + decryptedPassword(data.getPassword());
182 byte[] plainCredsBytes = plainCreds.getBytes();
183 byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
184 String base64Creds = new String(base64CredsBytes);
186 HttpHeaders headers = new HttpHeaders();
187 headers.add("Authorization", "Basic " + base64Creds);
188 headers.setContentType(MediaType.APPLICATION_JSON);
189 HttpEntity<String> entity = new HttpEntity<String>(headers);
191 String url = microserviceUrlConverter(data, params);
193 response = template.exchange(url, HttpMethod.GET, entity, String.class).getBody();
194 } catch (HttpClientErrorException e) {
197 } else if (data.getSecurityType().equals(COOKIE_AUTH)) {
198 HttpHeaders headers = new HttpHeaders();
199 headers.setContentType(MediaType.APPLICATION_JSON);
200 String rawCookie = request.getHeader("Cookie");
201 headers.add("Cookie", rawCookie);
202 HttpEntity<String> entity = new HttpEntity<String>(headers);
204 String url = microserviceUrlConverter(data, params);
206 response = template.exchange(url, HttpMethod.GET, entity, String.class).getBody();
207 } catch (HttpClientErrorException e) {
214 private String decryptedPassword(String encryptedPwd) throws Exception {
216 if (encryptedPwd != null & encryptedPwd.length() > 0) {
218 result = CipherUtil.decrypt(encryptedPwd,
219 SystemProperties.getProperty(SystemProperties.Decryption_Key));
220 } catch (Exception e) {
221 logger.error(EELFLoggerDelegate.errorLogger, "decryptedPassword failed", e);
228 private String microserviceUrlConverter(MicroserviceData data, List<MicroserviceParameter> params) {
229 String url = data.getUrl();
230 for (int i = 0; i < params.size(); i++) {
232 url += QUESTION_MARK;
234 url += params.get(i).getPara_key() + "=" + params.get(i).getPara_value();
235 if (i != (params.size() - 1)) {