2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
8 * Unless otherwise specified, all software contained herein is licensed
9 * under the Apache License, Version 2.0 (the "License");
10 * you may not use this software except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * Unless otherwise specified, all documentation contained herein is licensed
22 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23 * you may not use this documentation except in compliance with the License.
24 * You may obtain a copy of the License at
26 * https://creativecommons.org/licenses/by/4.0/
28 * Unless required by applicable law or agreed to in writing, documentation
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
34 * ============LICENSE_END============================================
38 package org.onap.portal.controller;
41 import java.io.FileInputStream;
42 import java.io.FileOutputStream;
43 import java.io.IOException;
44 import java.io.OutputStream;
45 import java.security.Principal;
46 import java.util.ArrayList;
47 import java.util.List;
48 import javax.servlet.ServletContext;
49 import javax.servlet.http.HttpServletRequest;
50 import javax.servlet.http.HttpServletResponse;
51 import org.apache.commons.lang.StringUtils;
52 import org.onap.portal.domain.db.ep.EpWidgetCatalogParameter;
53 import org.onap.portal.domain.db.fn.FnUser;
54 import org.onap.portal.domain.dto.PortalRestResponse;
55 import org.onap.portal.domain.dto.PortalRestStatusEnum;
56 import org.onap.portal.domain.dto.ecomp.MicroserviceParameter;
57 import org.onap.portal.domain.dto.ecomp.WidgetCatalog;
58 import org.onap.portal.domain.dto.ecomp.WidgetParameterResult;
59 import org.onap.portal.domain.dto.ecomp.WidgetServiceHeaders;
60 import org.onap.portal.logging.aop.EPAuditLog;
61 import org.onap.portal.service.WidgetMService;
62 import org.onap.portal.service.ep.EpMicroserviceParameterService;
63 import org.onap.portal.service.ep.EpWidgetCatalogParameterService;
64 import org.onap.portal.service.fn.FnUserService;
65 import org.onap.portal.utils.EPCommonSystemProperties;
66 import org.onap.portal.utils.EcompPortalUtils;
67 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
68 import org.onap.portalsdk.core.util.SystemProperties;
69 import org.springframework.beans.factory.annotation.Autowired;
70 import org.springframework.context.annotation.EnableAspectJAutoProxy;
71 import org.springframework.core.io.FileSystemResource;
72 import org.springframework.http.HttpEntity;
73 import org.springframework.http.HttpHeaders;
74 import org.springframework.http.HttpMethod;
75 import org.springframework.http.MediaType;
76 import org.springframework.http.ResponseEntity;
77 import org.springframework.util.LinkedMultiValueMap;
78 import org.springframework.util.MultiValueMap;
79 import org.springframework.web.bind.annotation.DeleteMapping;
80 import org.springframework.web.bind.annotation.GetMapping;
81 import org.springframework.web.bind.annotation.PathVariable;
82 import org.springframework.web.bind.annotation.PostMapping;
83 import org.springframework.web.bind.annotation.PutMapping;
84 import org.springframework.web.bind.annotation.RequestBody;
85 import org.springframework.web.bind.annotation.RestController;
86 import org.springframework.web.client.RestTemplate;
87 import org.springframework.web.multipart.MultipartFile;
88 import org.springframework.web.multipart.MultipartHttpServletRequest;
91 @EnableAspectJAutoProxy
93 public class WidgetsCatalogController {
95 private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(WidgetsCatalogController.class);
97 private static final String MS_WIDGET_LOCAL_PORT = "microservices.widget.local.port";
98 private static final String UNAUTHORIZED_OR_FORBIDDEN_FOR_A_DISABLED_USER = "Unauthorized or Forbidden for a disabled user";
99 private RestTemplate template = new RestTemplate();
100 private String whatService = "widgets-service";
102 private final EpWidgetCatalogParameterService epWidgetCatalogParameterService;
103 private final EpMicroserviceParameterService epMicroserviceParameterService;
104 private final WidgetMService widgetMService;
105 private final FnUserService fnUserService;
108 public WidgetsCatalogController(final EpWidgetCatalogParameterService epWidgetCatalogParameterService,
109 final EpMicroserviceParameterService epMicroserviceParameterService,
110 final WidgetMService widgetMService,
111 final FnUserService fnUserService) {
112 this.epWidgetCatalogParameterService = epWidgetCatalogParameterService;
113 this.epMicroserviceParameterService = epMicroserviceParameterService;
114 this.widgetMService = widgetMService;
115 this.fnUserService = fnUserService;
119 @GetMapping(value = {
120 "/portalApi/microservices/widgetCatalog/{loginName}"}, produces = MediaType.APPLICATION_JSON_VALUE)
121 public List<WidgetCatalog> getUserWidgetCatalog(@PathVariable("loginName") String loginName) {
122 List<WidgetCatalog> widgets;
124 ResponseEntity<List> ans = template.exchange(
125 EcompPortalUtils.widgetMsProtocol() + "://"
126 + widgetMService.getServiceLocation(whatService,
127 SystemProperties.getProperty(EPCommonSystemProperties.MS_WIDGET_LOCAL_PORT))
128 + "/widget/microservices/widgetCatalog/" + loginName,
129 HttpMethod.GET, new HttpEntity<>(WidgetServiceHeaders.getInstance()), List.class);
130 widgets = ans.getBody();
131 } catch (Exception e) {
132 logger.error(EELFLoggerDelegate.errorLogger, "getUserWidgetCatalog failed", e);
138 @GetMapping(value = {"/portalApi/microservices/widgetCatalog"}, produces = MediaType.APPLICATION_JSON_VALUE)
139 public List<WidgetCatalog> getWidgetCatalog() {
140 List<WidgetCatalog> widgets;
142 ResponseEntity<List> ans = template.exchange(
143 EcompPortalUtils.widgetMsProtocol() + "://"
144 + widgetMService.getServiceLocation(whatService,
145 SystemProperties.getProperty(EPCommonSystemProperties.MS_WIDGET_LOCAL_PORT))
146 + "/widget/microservices/widgetCatalog",
147 HttpMethod.GET, new HttpEntity<>(WidgetServiceHeaders.getInstance()), List.class);
148 widgets = ans.getBody();
149 } catch (Exception e) {
150 logger.error(EELFLoggerDelegate.errorLogger, "getWidgetCatalog failed", e);
156 @PutMapping(value = {
157 "/portalApi/microservices/widgetCatalog/{widgetId}"}, produces = MediaType.APPLICATION_JSON_VALUE)
158 public void updateWidgetCatalog(@RequestBody WidgetCatalog newWidgetCatalog,
159 @PathVariable("widgetId") long widgetId) throws Exception {
161 EcompPortalUtils.widgetMsProtocol() + "://"
162 + widgetMService.getServiceLocation(whatService,
163 SystemProperties.getProperty(MS_WIDGET_LOCAL_PORT))
164 + "/widget/microservices/widgetCatalog/" + widgetId,
165 HttpMethod.PUT, new HttpEntity<>(newWidgetCatalog, WidgetServiceHeaders.getInstance()),
169 @DeleteMapping(value = {"/portalApi/microservices/widgetCatalog/{widgetId}"})
170 public void deleteOnboardingWidget(@PathVariable("widgetId") long widgetId) throws Exception {
172 EcompPortalUtils.widgetMsProtocol() + "://"
173 + widgetMService.getServiceLocation(whatService,
174 SystemProperties.getProperty(MS_WIDGET_LOCAL_PORT))
175 + "/widget/microservices/widgetCatalog/" + widgetId,
176 HttpMethod.DELETE, new HttpEntity<>(WidgetServiceHeaders.getInstance()), String.class);
179 @PostMapping(value = {"/portalApi/microservices/widgetCatalog/{widgetId}"})
180 public String updateWidgetCatalogWithFiles(HttpServletRequest request,
181 @PathVariable("widgetId") long widgetId) {
182 MultipartHttpServletRequest mRequest;
183 MultiValueMap<String, Object> multipartRequest = new LinkedMultiValueMap<>();
185 String tmpFolderName = "/tmp/";
186 String respond = null;
187 FileOutputStream fo = null;
189 mRequest = (MultipartHttpServletRequest) request;
190 MultipartFile mFile = mRequest.getFile("file");
191 fileName = mFile.getOriginalFilename();
192 fo = new FileOutputStream(tmpFolderName + fileName);
193 fo.write(mFile.getBytes());
197 HttpHeaders header = new HttpHeaders();
198 header.setContentType(MediaType.MULTIPART_FORM_DATA);
199 multipartRequest.add("file", new FileSystemResource(tmpFolderName + fileName));
200 multipartRequest.add("widget", request.getParameter("newWidget"));
201 respond = template.postForObject(
202 EcompPortalUtils.widgetMsProtocol() + "://"
203 + widgetMService.getServiceLocation(whatService,
204 SystemProperties.getProperty(EPCommonSystemProperties.MS_WIDGET_LOCAL_PORT))
205 + "/widget/microservices/widgetCatalog/" + widgetId,
206 new HttpEntity<>(multipartRequest, WidgetServiceHeaders.getInstance()), String.class);
207 File f = new File(tmpFolderName + fileName);
209 } catch (Exception e) {
210 logger.error(EELFLoggerDelegate.errorLogger, "updateWidgetCatalogWithFiles failed", e);
216 } catch (IOException e) {
217 logger.error(EELFLoggerDelegate.errorLogger, "updateWidgetCatalogWithFiles failed 2", e);
223 @PostMapping(value = {"/portalApi/microservices/widgetCatalog"})
224 public String createWidgetCatalog(HttpServletRequest request)
227 if (StringUtils.isNotBlank(SystemProperties.getProperty(EPCommonSystemProperties.MS_WIDGET_UPLOAD_FLAG))
228 && SystemProperties.getProperty(EPCommonSystemProperties.MS_WIDGET_UPLOAD_FLAG)
229 .equalsIgnoreCase("false")) {
230 return UNAUTHORIZED_OR_FORBIDDEN_FOR_A_DISABLED_USER;
233 MultipartHttpServletRequest mRequest;
234 MultiValueMap<String, Object> multipartRequest = new LinkedMultiValueMap<>();
236 String tmpFolderName = "/tmp/";
237 String respond = null;
238 FileOutputStream fo = null;
240 mRequest = (MultipartHttpServletRequest) request;
241 MultipartFile mFile = mRequest.getFile("file");
242 fileName = mFile.getOriginalFilename();
243 fo = new FileOutputStream(tmpFolderName + fileName);
244 fo.write(mFile.getBytes());
245 // silence sonar scan by calling close here
249 HttpHeaders header = new HttpHeaders();
250 header.setContentType(MediaType.MULTIPART_FORM_DATA);
251 multipartRequest.add("file", new FileSystemResource(tmpFolderName + fileName));
252 multipartRequest.add("widget", request.getParameter("newWidget"));
254 respond = template.postForObject(
255 EcompPortalUtils.widgetMsProtocol() + "://"
256 + widgetMService.getServiceLocation(whatService,
257 SystemProperties.getProperty(EPCommonSystemProperties.MS_WIDGET_LOCAL_PORT))
258 + "/widget/microservices/widgetCatalog",
259 new HttpEntity<>(multipartRequest, WidgetServiceHeaders.getInstance()), String.class);
260 File f = new File(tmpFolderName + fileName);
262 } catch (Exception e) {
263 logger.error(EELFLoggerDelegate.errorLogger, "createWidgetCatalog failed", e);
269 } catch (IOException e) {
270 logger.error(EELFLoggerDelegate.errorLogger, "createWidgetCatalog failed 2", e);
276 @GetMapping(value = "/portalApi/microservices/{widgetId}/framework.js")
277 public String getWidgetFramework(@PathVariable("widgetId") long widgetId) throws Exception {
278 return template.getForObject(EcompPortalUtils.widgetMsProtocol() + "://"
279 + widgetMService.getServiceLocation(whatService,
280 SystemProperties.getProperty(MS_WIDGET_LOCAL_PORT))
281 + "/widget/microservices/" + widgetId + "/framework.js", String.class,
282 WidgetServiceHeaders.getInstance());
285 @GetMapping(value = "/portalApi/microservices/{widgetId}/controller.js")
286 public String getWidgetController(@PathVariable("widgetId") long widgetId) throws Exception {
287 return template.getForObject(EcompPortalUtils.widgetMsProtocol() + "://"
288 + widgetMService.getServiceLocation(whatService,
289 SystemProperties.getProperty(MS_WIDGET_LOCAL_PORT))
290 + "/widget/microservices/" + widgetId + "/controller.js", String.class,
291 WidgetServiceHeaders.getInstance());
294 @GetMapping(value = "/portalApi/microservices/{widgetId}/style.css")
295 public String getWidgetCSS(@PathVariable("widgetId") long widgetId) throws Exception {
296 return template.getForObject(EcompPortalUtils.widgetMsProtocol() + "://"
297 + widgetMService.getServiceLocation(whatService,
298 SystemProperties.getProperty(MS_WIDGET_LOCAL_PORT))
299 + "/widget/microservices/" + widgetId + "/styles.css", String.class,
300 WidgetServiceHeaders.getInstance());
303 @GetMapping(value = {"/portalApi/microservices/parameters/{widgetId}"})
304 public PortalRestResponse<List<WidgetParameterResult>> getWidgetParameterResult(Principal principal,
305 HttpServletRequest request,
306 @PathVariable("widgetId") long widgetId) throws Exception {
307 FnUser user = fnUserService.loadUserByUsername(principal.getName());
309 List<WidgetParameterResult> list = new ArrayList<>();
310 Long serviceId = template.exchange(
311 EcompPortalUtils.widgetMsProtocol() + "://"
312 + widgetMService.getServiceLocation(whatService,
313 SystemProperties.getProperty(EPCommonSystemProperties.MS_WIDGET_LOCAL_PORT))
314 + "/widget/microservices/widgetCatalog/parameters/" + widgetId,
315 HttpMethod.GET, new HttpEntity<>(WidgetServiceHeaders.getInstance()), Long.class).getBody();
316 if (serviceId == null) {
317 // return ok/sucess and no service parameter for this widget
318 return new PortalRestResponse<>(PortalRestStatusEnum.WARN,
319 "No service parameters for this widget", list);
321 List<MicroserviceParameter> defaultParam = epMicroserviceParameterService
322 .getParametersById(serviceId);
323 for (MicroserviceParameter param : defaultParam) {
324 WidgetParameterResult userResult = new WidgetParameterResult();
325 userResult.setParamId(param.getId());
326 userResult.setDefaultValue(param.getPara_value());
327 userResult.setParamKey(param.getPara_key());
328 EpWidgetCatalogParameter userValue = epWidgetCatalogParameterService
329 .getUserParamById(widgetId, user.getUserId(),
331 if (userValue == null) {
332 userResult.setUserValue(param.getPara_value());
334 userResult.setUserValue(userValue.getUserValue());
336 list.add(userResult);
339 return new PortalRestResponse<>(PortalRestStatusEnum.OK, "SUCCESS", list);
342 @GetMapping(value = {"/portalApi/microservices/services/{paramId}"})
343 public List<EpWidgetCatalogParameter> getUserParameterById(@PathVariable("paramId") long paramId) {
344 return epWidgetCatalogParameterService.getUserParameterById(paramId);
347 @DeleteMapping(value = {"/portalApi/microservices/services/{paramId}"})
348 public boolean deleteUserParameterById(@PathVariable("paramId") long paramId) {
350 epWidgetCatalogParameterService.deleteUserParameterById(paramId);
352 } catch (Exception e) {
353 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage());
358 @GetMapping(value = {"/portalApi/microservices/download/{widgetId}"})
359 public void doDownload(HttpServletRequest request, HttpServletResponse response,
360 @PathVariable("widgetId") long widgetId) throws Exception {
362 ServletContext context = request.getServletContext();
363 byte[] byteFile = template
365 EcompPortalUtils.widgetMsProtocol() + "://"
366 + widgetMService.getServiceLocation(whatService,
367 SystemProperties.getProperty(EPCommonSystemProperties.MS_WIDGET_LOCAL_PORT))
368 + "/widget/microservices/download/" + widgetId,
369 HttpMethod.GET, new HttpEntity<>(WidgetServiceHeaders.getInstance()), byte[].class)
372 File downloadFile = File.createTempFile("temp", ".zip");
373 try (FileOutputStream stream = new FileOutputStream(downloadFile.getPath())) {
374 stream.write(byteFile);
375 } catch (Exception e) {
376 logger.error(EELFLoggerDelegate.errorLogger, "doDownload failed", e);
380 try (FileInputStream inputStream = new FileInputStream(downloadFile);
381 OutputStream outStream = response.getOutputStream()) {
382 String mimeType = context.getMimeType(downloadFile.getPath());
383 if (mimeType == null) {
384 mimeType = "application/octet-stream";
387 response.setContentType(mimeType);
388 response.setContentLength((int) downloadFile.length());
389 String headerKey = "Content-Disposition";
390 String headerValue = String.format("attachment; filename=\"%s\"", downloadFile.getName());
391 downloadFile.delete();
392 response.setHeader(headerKey, headerValue);
394 byte[] buffer = new byte[32 * 1024];
396 while ((bytesRead = inputStream.read(buffer)) != -1) {
397 outStream.write(buffer, 0, bytesRead);
399 } catch (Exception e) {
400 logger.error(EELFLoggerDelegate.errorLogger, "doDownload failed", e);
405 @PostMapping(value = {"/portalApi/microservices/parameters"})
406 public PortalRestResponse<String> saveWidgetParameter(Principal principal,
407 @RequestBody EpWidgetCatalogParameter widgetParameters) {
408 FnUser user = fnUserService.loadUserByUsername(principal.getName());
409 widgetParameters.setUserId(user);
411 EpWidgetCatalogParameter oldParam = epWidgetCatalogParameterService
412 .getUserParamById(widgetParameters.getWidgetId().getWidgetId(),
413 widgetParameters.getUserId().getUserId(), widgetParameters.getParamId().getId());
414 if (oldParam != null) {
415 oldParam.setParamId(widgetParameters.getParamId());
416 oldParam.setUserId(widgetParameters.getUserId());
417 oldParam.setUserValue(widgetParameters.getUserValue());
418 oldParam.setWidgetId(widgetParameters.getWidgetId());
419 epWidgetCatalogParameterService.saveUserParameter(oldParam);
421 epWidgetCatalogParameterService.saveUserParameter(widgetParameters);
424 } catch (Exception e) {
425 logger.error(EELFLoggerDelegate.errorLogger, "saveWidgetParameter failed", e);
426 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
428 return new PortalRestResponse<>(PortalRestStatusEnum.OK, "SUCCESS", "");
431 @GetMapping(value = {"/portalApi/microservices/uploadFlag"})
432 public String getUploadFlag() {
435 uplaodFlag = SystemProperties.getProperty(EPCommonSystemProperties.MS_WIDGET_UPLOAD_FLAG);
436 } catch (Exception e) {
437 logger.error(EELFLoggerDelegate.errorLogger, "uploadFlag failed", e);