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.portalapp.portal.controller;
41 import java.io.FileInputStream;
42 import java.io.FileOutputStream;
43 import java.io.OutputStream;
44 import java.util.ArrayList;
45 import java.util.List;
47 import javax.servlet.ServletContext;
48 import javax.servlet.http.HttpServletRequest;
49 import javax.servlet.http.HttpServletResponse;
51 import org.apache.commons.lang.StringUtils;
52 import org.onap.portalapp.controller.EPRestrictedBaseController;
53 import org.onap.portalapp.portal.domain.EPUser;
54 import org.onap.portalapp.portal.domain.MicroserviceParameter;
55 import org.onap.portalapp.portal.domain.WidgetCatalog;
56 import org.onap.portalapp.portal.domain.WidgetCatalogParameter;
57 import org.onap.portalapp.portal.domain.WidgetParameterResult;
58 import org.onap.portalapp.portal.domain.WidgetServiceHeaders;
59 import org.onap.portalapp.portal.ecomp.model.PortalRestResponse;
60 import org.onap.portalapp.portal.ecomp.model.PortalRestStatusEnum;
61 import org.onap.portalapp.portal.logging.aop.EPAuditLog;
62 import org.onap.portalapp.portal.service.WidgetMService;
63 import org.onap.portalapp.portal.service.MicroserviceService;
64 import org.onap.portalapp.portal.service.WidgetParameterService;
65 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
66 import org.onap.portalapp.portal.utils.EcompPortalUtils;
67 import org.onap.portalapp.util.EPUserUtils;
68 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
69 import org.onap.portalsdk.core.util.SystemProperties;
70 import org.springframework.beans.factory.annotation.Autowired;
71 import org.springframework.context.annotation.Bean;
72 import org.springframework.context.annotation.EnableAspectJAutoProxy;
73 import org.springframework.core.io.FileSystemResource;
74 import org.springframework.http.HttpEntity;
75 import org.springframework.http.HttpHeaders;
76 import org.springframework.http.HttpMethod;
77 import org.springframework.http.MediaType;
78 import org.springframework.http.ResponseEntity;
79 import org.springframework.util.LinkedMultiValueMap;
80 import org.springframework.util.MultiValueMap;
81 import org.springframework.web.bind.annotation.PathVariable;
82 import org.springframework.web.bind.annotation.RequestBody;
83 import org.springframework.web.bind.annotation.GetMapping;
84 import org.springframework.web.bind.annotation.PutMapping;
85 import org.springframework.web.bind.annotation.PostMapping;
86 import org.springframework.web.bind.annotation.DeleteMapping;
87 import org.springframework.web.bind.annotation.RestController;
88 import org.springframework.web.client.RestClientException;
89 import org.springframework.web.client.RestTemplate;
90 import org.springframework.web.multipart.MultipartFile;
91 import org.springframework.web.multipart.MultipartHttpServletRequest;
92 import org.springframework.web.multipart.commons.CommonsMultipartResolver;
94 @SuppressWarnings("unchecked")
96 @org.springframework.context.annotation.Configuration
97 @EnableAspectJAutoProxy
99 public class WidgetsCatalogController extends EPRestrictedBaseController {
101 private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(WidgetsCatalogController.class);
103 private static final String MS_WIDGET_LOCAL_PORT = "microservices.widget.local.port";
105 private static final String UNAUTHORIZED_OR_FORBIDDEN_FOR_A_DISABLED_USER = "Unauthorized or Forbidden for a disabled user";
107 private static final String MS_WIDGET_CATALOG_URL = "/widget/microservices/widgetCatalog/";
109 private static final String MS_WIDGET_URL = "/widget/microservices/";
111 private RestTemplate template = new RestTemplate();
113 private String whatService = "widgets-service";
116 private WidgetMService widgetMService;
119 private MicroserviceService microserviceService;
122 private WidgetParameterService widgetParameterService;
125 public CommonsMultipartResolver multipartResolver() {
126 return new CommonsMultipartResolver();
130 // for localhost testing only
131 javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(new javax.net.ssl.HostnameVerifier() {
132 public boolean verify(String hostname, javax.net.ssl.SSLSession sslSession) {
133 if ("localhost".equals(hostname))
140 @GetMapping(value = { "/portalApi/microservices/widgetCatalog/{loginName}" })
141 public List<WidgetCatalog> getUserWidgetCatalog(@PathVariable("loginName") String loginName) {
142 List<WidgetCatalog> widgets = new ArrayList<>();
144 ResponseEntity<List> ans = template.exchange(
145 EcompPortalUtils.widgetMsProtocol() + "://"
146 + widgetMService.getServiceLocation(whatService,
147 SystemProperties.getProperty(EPCommonSystemProperties.MS_WIDGET_LOCAL_PORT))
148 + MS_WIDGET_CATALOG_URL + loginName,
149 HttpMethod.GET, new HttpEntity<>(WidgetServiceHeaders.getInstance()), List.class);
150 widgets = ans.getBody();
151 } catch (Exception e) {
152 logger.error(EELFLoggerDelegate.errorLogger, "getUserWidgetCatalog failed", e);
153 // returning null because null help check on the UI if there was a
154 // communication problem with Microservice.
160 @GetMapping(value = { "/portalApi/microservices/widgetCatalog" })
161 public List<WidgetCatalog> getWidgetCatalog() {
162 List<WidgetCatalog> widgets = new ArrayList<>();
164 ResponseEntity<List> ans = template.exchange(
165 EcompPortalUtils.widgetMsProtocol() + "://"
166 + widgetMService.getServiceLocation(whatService,
167 SystemProperties.getProperty(EPCommonSystemProperties.MS_WIDGET_LOCAL_PORT))
168 + "/widget/microservices/widgetCatalog",
169 HttpMethod.GET, new HttpEntity<>(WidgetServiceHeaders.getInstance()), List.class);
170 widgets = ans.getBody();
171 } catch (Exception e) {
172 logger.error(EELFLoggerDelegate.errorLogger, "getWidgetCatalog failed", e);
173 // returning null because null help check on the UI if there was a
174 // communication problem with Microservice.
180 @PutMapping(value = {
181 "/portalApi/microservices/widgetCatalog/{widgetId}" }, produces = "application/json")
182 public void updateWidgetCatalog(@RequestBody WidgetCatalog newWidgetCatalog, @PathVariable("widgetId") long widgetId) throws Exception {
184 EcompPortalUtils.widgetMsProtocol() + "://"
185 + widgetMService.getServiceLocation(whatService,
186 SystemProperties.getProperty(MS_WIDGET_LOCAL_PORT))
187 + MS_WIDGET_CATALOG_URL + widgetId,
188 HttpMethod.PUT, new HttpEntity<>(newWidgetCatalog, WidgetServiceHeaders.getInstance()), String.class);
191 @DeleteMapping(value = { "/portalApi/microservices/widgetCatalog/{widgetId}" })
192 public void deleteOnboardingWidget(@PathVariable("widgetId") long widgetId) throws Exception {
194 EcompPortalUtils.widgetMsProtocol() + "://"
195 + widgetMService.getServiceLocation(whatService,
196 SystemProperties.getProperty(MS_WIDGET_LOCAL_PORT))
197 + MS_WIDGET_CATALOG_URL + widgetId,
198 HttpMethod.DELETE, new HttpEntity<>(WidgetServiceHeaders.getInstance()), String.class);
201 @PostMapping(value = { "/portalApi/microservices/widgetCatalog/{widgetId}" })
202 public String updateWidgetCatalogWithFiles(HttpServletRequest request,
203 @PathVariable("widgetId") long widgetId) throws RestClientException, Exception {
204 MultipartHttpServletRequest mRequest;
205 MultiValueMap<String, Object> multipartRequest = new LinkedMultiValueMap<>();
207 String tmpFolderName = "/tmp/";
208 String respond = null;
210 mRequest = (MultipartHttpServletRequest) request;
211 MultipartFile mFile = mRequest.getFile("file");
212 fileName = mFile.getOriginalFilename();
213 try (FileOutputStream fo = new FileOutputStream(tmpFolderName + fileName)) {
214 fo.write(mFile.getBytes());
217 HttpHeaders header = new HttpHeaders();
218 header.setContentType(MediaType.MULTIPART_FORM_DATA);
219 multipartRequest.add("file", new FileSystemResource(tmpFolderName + fileName));
220 multipartRequest.add("widget", request.getParameter("newWidget"));
221 respond = template.postForObject(
222 EcompPortalUtils.widgetMsProtocol() + "://"
223 + widgetMService.getServiceLocation(whatService,
224 SystemProperties.getProperty(EPCommonSystemProperties.MS_WIDGET_LOCAL_PORT))
225 + MS_WIDGET_CATALOG_URL + widgetId,
226 new HttpEntity<>(multipartRequest, WidgetServiceHeaders.getInstance()), String.class);
227 File f = new File(tmpFolderName + fileName);
229 } catch (Exception e) {
230 logger.error(EELFLoggerDelegate.errorLogger, "updateWidgetCatalogWithFiles failed", e);
235 @PostMapping(value = { "/portalApi/microservices/widgetCatalog" })
236 public String createWidgetCatalog(HttpServletRequest request)
239 if (StringUtils.isNotBlank(SystemProperties.getProperty(EPCommonSystemProperties.MS_WIDGET_UPLOAD_FLAG))
240 && "false".equalsIgnoreCase(
241 SystemProperties.getProperty(EPCommonSystemProperties.MS_WIDGET_UPLOAD_FLAG))) {
242 return UNAUTHORIZED_OR_FORBIDDEN_FOR_A_DISABLED_USER;
245 MultipartHttpServletRequest mRequest;
246 MultiValueMap<String, Object> multipartRequest = new LinkedMultiValueMap<>();
248 String tmpFolderName = "/tmp/";
249 String respond = null;
251 mRequest = (MultipartHttpServletRequest) request;
252 MultipartFile mFile = mRequest.getFile("file");
253 fileName = mFile.getOriginalFilename();
254 try (FileOutputStream fo = new FileOutputStream(tmpFolderName + fileName)) {
255 fo.write(mFile.getBytes());
258 HttpHeaders header = new HttpHeaders();
259 header.setContentType(MediaType.MULTIPART_FORM_DATA);
260 multipartRequest.add("file", new FileSystemResource(tmpFolderName + fileName));
261 multipartRequest.add("widget", request.getParameter("newWidget"));
263 respond = template.postForObject(
264 EcompPortalUtils.widgetMsProtocol() + "://"
265 + widgetMService.getServiceLocation(whatService,
266 SystemProperties.getProperty(EPCommonSystemProperties.MS_WIDGET_LOCAL_PORT))
267 + "/widget/microservices/widgetCatalog",
268 new HttpEntity<>(multipartRequest, WidgetServiceHeaders.getInstance()), String.class);
269 File f = new File(tmpFolderName + fileName);
271 } catch (Exception e) {
272 logger.error(EELFLoggerDelegate.errorLogger, "createWidgetCatalog failed", e);
277 @GetMapping(value = "/portalApi/microservices/{widgetId}/framework.js")
278 public String getWidgetFramework(@PathVariable("widgetId") long widgetId) throws Exception {
279 return template.getForObject(EcompPortalUtils.widgetMsProtocol() + "://"
280 + widgetMService.getServiceLocation(whatService,
281 SystemProperties.getProperty(MS_WIDGET_LOCAL_PORT))
282 + MS_WIDGET_URL + widgetId + "/framework.js", String.class,
283 WidgetServiceHeaders.getInstance());
286 @GetMapping(value = "/portalApi/microservices/{widgetId}/controller.js")
287 public String getWidgetController(@PathVariable("widgetId") long widgetId) throws Exception {
288 return template.getForObject(EcompPortalUtils.widgetMsProtocol() + "://"
289 + widgetMService.getServiceLocation(whatService,
290 SystemProperties.getProperty(MS_WIDGET_LOCAL_PORT))
291 + MS_WIDGET_URL + widgetId + "/controller.js", String.class,
292 WidgetServiceHeaders.getInstance());
295 @GetMapping(value = "/portalApi/microservices/{widgetId}/style.css")
296 public String getWidgetCSS(@PathVariable("widgetId") long widgetId) throws Exception {
297 return template.getForObject(EcompPortalUtils.widgetMsProtocol() + "://"
298 + widgetMService.getServiceLocation(whatService,
299 SystemProperties.getProperty(MS_WIDGET_LOCAL_PORT))
300 + MS_WIDGET_URL + widgetId + "/styles.css", String.class,
301 WidgetServiceHeaders.getInstance());
304 @GetMapping(value = { "/portalApi/microservices/parameters/{widgetId}" })
305 public PortalRestResponse<List<WidgetParameterResult>> getWidgetParameterResult(HttpServletRequest request,
306 @PathVariable("widgetId") long widgetId) throws Exception {
307 EPUser user = EPUserUtils.getUserSession(request);
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 = microserviceService.getParametersById(serviceId);
322 for (MicroserviceParameter param : defaultParam) {
323 WidgetParameterResult userResult = new WidgetParameterResult();
324 userResult.setParam_id(param.getId());
325 userResult.setDefault_value(param.getPara_value());
326 userResult.setParam_key(param.getPara_key());
327 WidgetCatalogParameter userValue = widgetParameterService.getUserParamById(widgetId, user.getId(),
329 if (userValue == null)
330 userResult.setUser_value(param.getPara_value());
332 userResult.setUser_value(userValue.getUser_value());
334 list.add(userResult);
337 return new PortalRestResponse<>(PortalRestStatusEnum.OK, "SUCCESS", list);
340 @GetMapping(value = { "/portalApi/microservices/services/{paramId}" })
341 public List<WidgetCatalogParameter> getUserParameterById( @PathVariable("paramId") long paramId) {
342 return widgetParameterService.getUserParameterById(paramId);
345 @DeleteMapping(value = { "/portalApi/microservices/services/{paramId}" })
346 public void deleteUserParameterById(@PathVariable("paramId") long paramId) {
347 widgetParameterService.deleteUserParameterById(paramId);
350 @GetMapping(value = { "/portalApi/microservices/download/{widgetId}" })
351 public void doDownload(HttpServletRequest request, HttpServletResponse response,
352 @PathVariable("widgetId") long widgetId) throws Exception {
354 ServletContext context = request.getServletContext();
355 byte[] byteFile = template
357 EcompPortalUtils.widgetMsProtocol() + "://"
358 + widgetMService.getServiceLocation(whatService,
359 SystemProperties.getProperty(EPCommonSystemProperties.MS_WIDGET_LOCAL_PORT))
360 + "/widget/microservices/download/" + widgetId,
361 HttpMethod.GET, new HttpEntity<>(WidgetServiceHeaders.getInstance()), byte[].class)
364 File downloadFile = File.createTempFile("temp", ".zip");
365 try(FileOutputStream stream = new FileOutputStream(downloadFile.getPath())){
366 stream.write(byteFile);
369 logger.error(EELFLoggerDelegate.errorLogger, "doDownload failed", e);
373 try(FileInputStream inputStream = new FileInputStream(downloadFile);
374 OutputStream outStream = response.getOutputStream()){
375 String mimeType = context.getMimeType(downloadFile.getPath());
376 if (mimeType == null) {
377 mimeType = "application/octet-stream";
380 response.setContentType(mimeType);
381 response.setContentLength((int) downloadFile.length());
382 String headerKey = "Content-Disposition";
383 String headerValue = String.format("attachment; filename=\"%s\"", downloadFile.getName());
384 downloadFile.delete();
385 response.setHeader(headerKey, headerValue);
387 byte[] buffer = new byte[32 * 1024];
389 while ((bytesRead = inputStream.read(buffer)) != -1) {
390 outStream.write(buffer, 0, bytesRead);
394 logger.error(EELFLoggerDelegate.errorLogger, "doDownload failed", e);
399 @PostMapping(value = { "/portalApi/microservices/parameters" })
400 public PortalRestResponse<String> saveWidgetParameter(HttpServletRequest request,
401 @RequestBody WidgetCatalogParameter widgetParameters) {
402 EPUser user = EPUserUtils.getUserSession(request);
403 widgetParameters.setUserId(user.getId());
405 WidgetCatalogParameter oldParam = widgetParameterService.getUserParamById(widgetParameters.getWidgetId(),
406 widgetParameters.getUserId(), widgetParameters.getParamId());
407 if (oldParam != null) {
408 widgetParameters.setId(oldParam.getId());
410 widgetParameterService.saveUserParameter(widgetParameters);
412 } catch (Exception e) {
413 logger.error(EELFLoggerDelegate.errorLogger, "saveWidgetParameter failed", e);
414 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
416 return new PortalRestResponse<>(PortalRestStatusEnum.OK, "SUCCESS", "");
419 @GetMapping(value = { "/portalApi/microservices/uploadFlag" })
420 public String getUploadFlag() {
421 String uplaodFlag="";
423 uplaodFlag = SystemProperties.getProperty(EPCommonSystemProperties.MS_WIDGET_UPLOAD_FLAG);
424 } catch (Exception e) {
425 logger.error(EELFLoggerDelegate.errorLogger, "uploadFlag failed", e);