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.controller;
23 import java.io.FileInputStream;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.io.OutputStream;
27 import java.util.ArrayList;
28 import java.util.List;
30 import javax.servlet.ServletContext;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
34 import org.openecomp.portalapp.controller.EPRestrictedBaseController;
35 import org.openecomp.portalapp.portal.domain.EPUser;
36 import org.openecomp.portalapp.portal.domain.MicroserviceParameter;
37 import org.openecomp.portalapp.portal.domain.WidgetCatalog;
38 import org.openecomp.portalapp.portal.domain.WidgetCatalogParameter;
39 import org.openecomp.portalapp.portal.domain.WidgetParameterResult;
40 import org.openecomp.portalapp.portal.domain.WidgetServiceHeaders;
41 import org.openecomp.portalapp.portal.ecomp.model.PortalRestResponse;
42 import org.openecomp.portalapp.portal.ecomp.model.PortalRestStatusEnum;
43 import org.openecomp.portalapp.portal.logging.aop.EPAuditLog;
44 import org.openecomp.portalapp.portal.service.ConsulHealthService;
45 import org.openecomp.portalapp.portal.service.MicroserviceService;
46 import org.openecomp.portalapp.portal.service.WidgetParameterService;
47 import org.openecomp.portalapp.portal.utils.CustomLoggingFilter;
48 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
49 import org.openecomp.portalapp.util.EPUserUtils;
50 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
51 import org.openecomp.portalsdk.core.util.SystemProperties;
52 import org.springframework.beans.factory.annotation.Autowired;
53 import org.springframework.context.annotation.Bean;
54 import org.springframework.context.annotation.EnableAspectJAutoProxy;
55 import org.springframework.core.io.FileSystemResource;
56 import org.springframework.http.HttpEntity;
57 import org.springframework.http.HttpHeaders;
58 import org.springframework.http.HttpMethod;
59 import org.springframework.http.MediaType;
60 import org.springframework.http.ResponseEntity;
61 import org.springframework.util.LinkedMultiValueMap;
62 import org.springframework.util.MultiValueMap;
63 import org.springframework.web.bind.annotation.PathVariable;
64 import org.springframework.web.bind.annotation.RequestBody;
65 import org.springframework.web.bind.annotation.RequestMapping;
66 import org.springframework.web.bind.annotation.RequestMethod;
67 import org.springframework.web.bind.annotation.RestController;
68 import org.springframework.web.client.RestClientException;
69 import org.springframework.web.client.RestTemplate;
70 import org.springframework.web.multipart.MultipartFile;
71 import org.springframework.web.multipart.MultipartHttpServletRequest;
72 import org.springframework.web.multipart.commons.CommonsMultipartResolver;
74 @SuppressWarnings("unchecked")
76 @org.springframework.context.annotation.Configuration
77 @EnableAspectJAutoProxy
79 public class WidgetsCatalogController extends EPRestrictedBaseController {
81 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(WidgetsCatalogController.class);
82 RestTemplate template = new RestTemplate();
83 String whatService = "widgets-service";
86 private ConsulHealthService consulHealthService;
89 private MicroserviceService microserviceService;
92 private WidgetParameterService widgetParameterService;
95 public CommonsMultipartResolver multipartResolver() {
96 return new CommonsMultipartResolver();
100 // for localhost testing only
101 javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(new javax.net.ssl.HostnameVerifier() {
103 public boolean verify(String hostname, javax.net.ssl.SSLSession sslSession) {
104 if (hostname.equals("localhost")) {
112 @RequestMapping(value = { "/portalApi/microservices/widgetCatalog/{loginName}" }, method = RequestMethod.GET)
113 public List<WidgetCatalog> getUserWidgetCatalog(HttpServletRequest request, HttpServletResponse response,
114 @PathVariable("loginName") String loginName) throws RestClientException, Exception {
115 List<WidgetCatalog> widgets = new ArrayList<>();
116 try {CustomLoggingFilter d;
117 ResponseEntity<ArrayList> ans = template.exchange(
118 EcompPortalUtils.widgetMsProtocol() + "://" + consulHealthService.getServiceLocation(whatService,
119 SystemProperties.getProperty("microservices.widget.local.port"))
120 + "/widget/microservices/widgetCatalog/" + loginName,
121 HttpMethod.GET, new HttpEntity(WidgetServiceHeaders.getInstance()), ArrayList.class);
122 widgets = ans.getBody();
123 } catch (Exception e) {
124 logger.error(EELFLoggerDelegate.errorLogger, "getUserWidgetCatalog failed", e);
125 // returning null because null help check on the UI if there was a
126 // communication problem with Microservice.
132 @RequestMapping(value = { "/portalApi/microservices/widgetCatalog" }, method = RequestMethod.GET)
133 public List<WidgetCatalog> getWidgetCatalog(HttpServletRequest request, HttpServletResponse response)
134 throws RestClientException, Exception {
135 List<WidgetCatalog> widgets = new ArrayList<>();
137 String p = EcompPortalUtils.widgetMsProtocol();
139 ResponseEntity<ArrayList> ans = template.exchange(
140 EcompPortalUtils.widgetMsProtocol() + "://" + consulHealthService.getServiceLocation(whatService,
141 SystemProperties.getProperty("microservices.widget.local.port"))
142 + "/widget/microservices/widgetCatalog",
143 HttpMethod.GET, new HttpEntity(WidgetServiceHeaders.getInstance()), ArrayList.class);
144 widgets = ans.getBody();
145 } catch (Exception e) {
146 logger.error(EELFLoggerDelegate.errorLogger, "getWidgetCatalog failed", e);
147 // returning null because null help check on the UI if there was a
148 // communication problem with Microservice.
154 @RequestMapping(value = {
155 "/portalApi/microservices/widgetCatalog/{widgetId}" }, method = RequestMethod.PUT, produces = "application/json")
156 public void updateWidgetCatalog(HttpServletRequest request, HttpServletResponse response,
157 @RequestBody WidgetCatalog newWidgetCatalog, @PathVariable("widgetId") long widgetId)
158 throws RestClientException, Exception {
160 EcompPortalUtils.widgetMsProtocol() + "://" + consulHealthService.getServiceLocation(whatService,
161 SystemProperties.getProperty("microservices.widget.local.port"))
162 + "/widget/microservices/widgetCatalog/" + widgetId,
163 HttpMethod.PUT, new HttpEntity(newWidgetCatalog, WidgetServiceHeaders.getInstance()), String.class);
166 @RequestMapping(value = { "/portalApi/microservices/widgetCatalog/{widgetId}" }, method = RequestMethod.DELETE)
167 public void deleteOnboardingWidget(HttpServletRequest request, HttpServletResponse response,
168 @PathVariable("widgetId") long widgetId) throws RestClientException, Exception {
170 EcompPortalUtils.widgetMsProtocol() + "://" + consulHealthService.getServiceLocation(whatService,
171 SystemProperties.getProperty("microservices.widget.local.port"))
172 + "/widget/microservices/widgetCatalog/" + widgetId,
173 HttpMethod.DELETE, new HttpEntity(WidgetServiceHeaders.getInstance()), String.class);
176 @RequestMapping(value = { "/portalApi/microservices/widgetCatalog/{widgetId}" }, method = RequestMethod.POST)
177 public String updateWidgetCatalogWithFiles(HttpServletRequest request, HttpServletResponse response,
178 @PathVariable("widgetId") long widgetId) throws RestClientException, Exception {
179 MultipartHttpServletRequest mRequest;
180 MultiValueMap<String, Object> multipartRequest = new LinkedMultiValueMap<>();
182 String tmp_folder = "/tmp/";
183 String respond = null;
184 FileOutputStream fo = null;
186 mRequest = (MultipartHttpServletRequest) request;
187 MultipartFile mFile = mRequest.getFile("file");
188 fileName = mFile.getOriginalFilename();
189 fo = new FileOutputStream(tmp_folder + fileName);
190 fo.write(mFile.getBytes());
192 HttpHeaders header = new HttpHeaders();
193 header.setContentType(MediaType.MULTIPART_FORM_DATA);
194 multipartRequest.add("file", new FileSystemResource(tmp_folder + fileName));
195 multipartRequest.add("widget", request.getParameter("newWidget"));
196 respond = template.postForObject(
197 EcompPortalUtils.widgetMsProtocol() + "://" + consulHealthService.getServiceLocation(whatService,
198 SystemProperties.getProperty("microservices.widget.local.port"))
199 + "/widget/microservices/widgetCatalog/" + widgetId,
200 new HttpEntity<>(multipartRequest, WidgetServiceHeaders.getInstance()), String.class);
201 File f = new File(tmp_folder + fileName);
203 } catch (Exception e) {
204 logger.error(EELFLoggerDelegate.errorLogger, "updateWidgetCatalogWithFiles failed", e);
209 } catch (IOException e) {
210 logger.error(EELFLoggerDelegate.errorLogger, "updateWidgetCatalogWithFiles failed 2", e);
216 @RequestMapping(value = { "/portalApi/microservices/widgetCatalog" }, method = RequestMethod.POST)
217 public String createWidgetCatalog(HttpServletRequest request, HttpServletResponse response)
218 throws RestClientException, Exception {
219 MultipartHttpServletRequest mRequest;
220 MultiValueMap<String, Object> multipartRequest = new LinkedMultiValueMap<>();
222 String tmp_folder = "/tmp/";
223 String respond = null;
224 FileOutputStream fo = null;
226 mRequest = (MultipartHttpServletRequest) request;
227 MultipartFile mFile = mRequest.getFile("file");
228 fileName = mFile.getOriginalFilename();
229 fo = new FileOutputStream(tmp_folder + fileName);
230 fo.write(mFile.getBytes());
232 HttpHeaders header = new HttpHeaders();
233 header.setContentType(MediaType.MULTIPART_FORM_DATA);
234 multipartRequest.add("file", new FileSystemResource(tmp_folder + fileName));
235 multipartRequest.add("widget", request.getParameter("newWidget"));
237 respond = template.postForObject(
238 EcompPortalUtils.widgetMsProtocol() + "://" + consulHealthService.getServiceLocation(whatService,
239 SystemProperties.getProperty("microservices.widget.local.port"))
240 + "/widget/microservices/widgetCatalog",
241 new HttpEntity<>(multipartRequest, WidgetServiceHeaders.getInstance()), String.class);
242 File f = new File(tmp_folder + fileName);
244 } catch (Exception e) {
245 logger.error(EELFLoggerDelegate.errorLogger, "createWidgetCatalog failed", e);
250 } catch (IOException e) {
251 logger.error(EELFLoggerDelegate.errorLogger, "createWidgetCatalog failed 2", e);
257 @RequestMapping(value = "/portalApi/microservices/{widgetId}/framework.js", method = RequestMethod.GET)
258 public String getWidgetFramework(HttpServletRequest request, HttpServletResponse response,
259 @PathVariable("widgetId") long widgetId) throws RestClientException, Exception {
260 return template.getForObject(EcompPortalUtils.widgetMsProtocol() + "://"
261 + consulHealthService.getServiceLocation(whatService,
262 SystemProperties.getProperty("microservices.widget.local.port"))
263 + "/widget/microservices/" + widgetId + "/framework.js", String.class,
264 WidgetServiceHeaders.getInstance());
267 @RequestMapping(value = "/portalApi/microservices/{widgetId}/controller.js", method = RequestMethod.GET)
268 public String getWidgetController(HttpServletRequest request, HttpServletResponse response,
269 @PathVariable("widgetId") long widgetId) throws RestClientException, Exception {
270 return template.getForObject(EcompPortalUtils.widgetMsProtocol() + "://"
271 + consulHealthService.getServiceLocation(whatService,
272 SystemProperties.getProperty("microservices.widget.local.port"))
273 + "/widget/microservices/" + widgetId + "/controller.js", String.class,
274 WidgetServiceHeaders.getInstance());
277 @RequestMapping(value = "/portalApi/microservices/{widgetId}/style.css", method = RequestMethod.GET)
278 public String getWidgetCSS(HttpServletRequest request, HttpServletResponse response,
279 @PathVariable("widgetId") long widgetId) throws RestClientException, Exception {
280 return template.getForObject(EcompPortalUtils.widgetMsProtocol() + "://"
281 + consulHealthService.getServiceLocation(whatService,
282 SystemProperties.getProperty("microservices.widget.local.port"))
283 + "/widget/microservices/" + widgetId + "/styles.css", String.class,
284 WidgetServiceHeaders.getInstance());
287 @RequestMapping(value = { "/portalApi/microservices/parameters/{widgetId}" }, method = RequestMethod.GET)
288 public PortalRestResponse<List<WidgetParameterResult>> getWidgetParameterResult(HttpServletRequest request,
289 HttpServletResponse response, @PathVariable("widgetId") long widgetId) throws Exception {
290 EPUser user = EPUserUtils.getUserSession(request);
292 List<WidgetParameterResult> list = new ArrayList<>();
293 Long serviceId = template.exchange(
294 EcompPortalUtils.widgetMsProtocol() + "://" + consulHealthService.getServiceLocation(whatService,
295 SystemProperties.getProperty("microservices.widget.local.port"))
296 + "/widget/microservices/widgetCatalog/parameters/" + widgetId,
297 HttpMethod.GET, new HttpEntity(WidgetServiceHeaders.getInstance()), Long.class).getBody();
298 if (serviceId == null) {
299 // return ok/sucess and no service parameter for this widget
300 return new PortalRestResponse<List<WidgetParameterResult>>(PortalRestStatusEnum.WARN,
301 "No service parameters for this widget", list);
303 List<MicroserviceParameter> defaultParam = microserviceService.getParametersById(serviceId);
304 for (MicroserviceParameter param : defaultParam) {
305 WidgetParameterResult user_result = new WidgetParameterResult();
306 user_result.setParam_id(param.getId());
307 user_result.setDefault_value(param.getPara_value());
308 user_result.setParam_key(param.getPara_key());
309 WidgetCatalogParameter userValue = widgetParameterService.getUserParamById(widgetId, user.getId(),
311 if (userValue == null)
312 user_result.setUser_value(param.getPara_value());
314 user_result.setUser_value(userValue.getUser_value());
316 list.add(user_result);
319 return new PortalRestResponse<List<WidgetParameterResult>>(PortalRestStatusEnum.OK, "SUCCESS", list);
322 @RequestMapping(value = { "/portalApi/microservices/services/{paramId}" }, method = RequestMethod.GET)
323 public List<WidgetCatalogParameter> getUserParameterById(HttpServletRequest request, HttpServletResponse response,
324 @PathVariable("paramId") long paramId) throws Exception {
325 List<WidgetCatalogParameter> list = widgetParameterService.getUserParameterById(paramId);
329 @RequestMapping(value = { "/portalApi/microservices/services/{paramId}" }, method = RequestMethod.DELETE)
330 public void deleteUserParameterById(HttpServletRequest request, HttpServletResponse response,
331 @PathVariable("paramId") long paramId) throws Exception {
332 widgetParameterService.deleteUserParameterById(paramId);
335 @RequestMapping(value = { "/portalApi/microservices/download/{widgetId}" }, method = RequestMethod.GET)
336 public void doDownload(HttpServletRequest request, HttpServletResponse response,
337 @PathVariable("widgetId") long widgetId) throws RestClientException, Exception {
339 ServletContext context = request.getServletContext();
340 byte[] byteFile = template.exchange(
341 EcompPortalUtils.widgetMsProtocol() + "://" + consulHealthService.getServiceLocation(whatService,
342 SystemProperties.getProperty("microservices.widget.local.port"))
343 + "/widget/microservices/download/" + widgetId,
344 HttpMethod.GET, new HttpEntity(WidgetServiceHeaders.getInstance()), byte[].class).getBody();
346 File downloadFile = File.createTempFile("temp", ".zip");
347 FileOutputStream stream = new FileOutputStream(downloadFile.getPath());
348 stream.write(byteFile);
351 FileInputStream inputStream = new FileInputStream(downloadFile);
352 String mimeType = context.getMimeType(downloadFile.getPath());
353 if (mimeType == null) {
354 mimeType = "application/octet-stream";
357 response.setContentType(mimeType);
358 response.setContentLength((int) downloadFile.length());
359 String headerKey = "Content-Disposition";
360 String headerValue = String.format("attachment; filename=\"%s\"", downloadFile.getName());
361 downloadFile.delete();
362 response.setHeader(headerKey, headerValue);
364 OutputStream outStream = response.getOutputStream();
365 byte[] buffer = new byte[32 * 1024];
367 while ((bytesRead = inputStream.read(buffer)) != -1) {
368 outStream.write(buffer, 0, bytesRead);
375 @RequestMapping(value = { "/portalApi/microservices/parameters" }, method = RequestMethod.POST)
376 public PortalRestResponse<String> saveWidgetParameter(HttpServletRequest request, HttpServletResponse response,
377 @RequestBody WidgetCatalogParameter widgetParameters) throws Exception {
378 EPUser user = EPUserUtils.getUserSession(request);
379 widgetParameters.setUserId(user.getId());
381 WidgetCatalogParameter oldParam = widgetParameterService.getUserParamById(widgetParameters.getWidgetId(),
382 widgetParameters.getUserId(), widgetParameters.getParamId());
383 if (oldParam != null) {
384 widgetParameters.setId(oldParam.getId());
386 widgetParameterService.saveUserParameter(widgetParameters);
388 } catch (Exception e) {
389 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
391 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "SUCCESS", "");