X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=ecomp-portal-BE-common%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fportalapp%2Fportal%2Fcontroller%2FWidgetsCatalogController.java;h=e2cd13c8d1cf6aa08f871ef6c163ea3a7ddaea62;hb=1e6757ca212e16cdec3ea4bbfc0866a87a58e8ff;hp=57c73a89054320115c4109e2ff12502cba0f4e36;hpb=e993c1a5ce19b73f6fdfd2fdf518d51c2381018b;p=portal.git diff --git a/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/controller/WidgetsCatalogController.java b/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/controller/WidgetsCatalogController.java index 57c73a89..e2cd13c8 100644 --- a/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/controller/WidgetsCatalogController.java +++ b/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/controller/WidgetsCatalogController.java @@ -33,14 +33,13 @@ * * ============LICENSE_END============================================ * - * + * */ package org.onap.portalapp.portal.controller; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; -import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; @@ -81,8 +80,10 @@ import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestTemplate; @@ -100,9 +101,13 @@ public class WidgetsCatalogController extends EPRestrictedBaseController { private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(WidgetsCatalogController.class); private static final String MS_WIDGET_LOCAL_PORT = "microservices.widget.local.port"; - + private static final String UNAUTHORIZED_OR_FORBIDDEN_FOR_A_DISABLED_USER = "Unauthorized or Forbidden for a disabled user"; + private static final String MS_WIDGET_CATALOG_URL = "/widget/microservices/widgetCatalog/"; + + private static final String MS_WIDGET_URL = "/widget/microservices/"; + private RestTemplate template = new RestTemplate(); private String whatService = "widgets-service"; @@ -132,7 +137,7 @@ public class WidgetsCatalogController extends EPRestrictedBaseController { }); } - @RequestMapping(value = { "/portalApi/microservices/widgetCatalog/{loginName}" }, method = RequestMethod.GET) + @GetMapping(value = { "/portalApi/microservices/widgetCatalog/{loginName}" }) public List getUserWidgetCatalog(@PathVariable("loginName") String loginName) { List widgets = new ArrayList<>(); try { @@ -140,7 +145,7 @@ public class WidgetsCatalogController extends EPRestrictedBaseController { EcompPortalUtils.widgetMsProtocol() + "://" + widgetMService.getServiceLocation(whatService, SystemProperties.getProperty(EPCommonSystemProperties.MS_WIDGET_LOCAL_PORT)) - + "/widget/microservices/widgetCatalog/" + loginName, + + MS_WIDGET_CATALOG_URL + loginName, HttpMethod.GET, new HttpEntity<>(WidgetServiceHeaders.getInstance()), List.class); widgets = ans.getBody(); } catch (Exception e) { @@ -152,7 +157,7 @@ public class WidgetsCatalogController extends EPRestrictedBaseController { return widgets; } - @RequestMapping(value = { "/portalApi/microservices/widgetCatalog" }, method = RequestMethod.GET) + @GetMapping(value = { "/portalApi/microservices/widgetCatalog" }) public List getWidgetCatalog() { List widgets = new ArrayList<>(); try { @@ -172,28 +177,28 @@ public class WidgetsCatalogController extends EPRestrictedBaseController { return widgets; } - @RequestMapping(value = { - "/portalApi/microservices/widgetCatalog/{widgetId}" }, method = RequestMethod.PUT, produces = "application/json") + @PutMapping(value = { + "/portalApi/microservices/widgetCatalog/{widgetId}" }, produces = "application/json") public void updateWidgetCatalog(@RequestBody WidgetCatalog newWidgetCatalog, @PathVariable("widgetId") long widgetId) throws Exception { template.exchange( EcompPortalUtils.widgetMsProtocol() + "://" + widgetMService.getServiceLocation(whatService, SystemProperties.getProperty(MS_WIDGET_LOCAL_PORT)) - + "/widget/microservices/widgetCatalog/" + widgetId, + + MS_WIDGET_CATALOG_URL + widgetId, HttpMethod.PUT, new HttpEntity<>(newWidgetCatalog, WidgetServiceHeaders.getInstance()), String.class); } - @RequestMapping(value = { "/portalApi/microservices/widgetCatalog/{widgetId}" }, method = RequestMethod.DELETE) + @DeleteMapping(value = { "/portalApi/microservices/widgetCatalog/{widgetId}" }) public void deleteOnboardingWidget(@PathVariable("widgetId") long widgetId) throws Exception { template.exchange( EcompPortalUtils.widgetMsProtocol() + "://" + widgetMService.getServiceLocation(whatService, SystemProperties.getProperty(MS_WIDGET_LOCAL_PORT)) - + "/widget/microservices/widgetCatalog/" + widgetId, + + MS_WIDGET_CATALOG_URL + widgetId, HttpMethod.DELETE, new HttpEntity<>(WidgetServiceHeaders.getInstance()), String.class); } - @RequestMapping(value = { "/portalApi/microservices/widgetCatalog/{widgetId}" }, method = RequestMethod.POST) + @PostMapping(value = { "/portalApi/microservices/widgetCatalog/{widgetId}" }) public String updateWidgetCatalogWithFiles(HttpServletRequest request, @PathVariable("widgetId") long widgetId) throws RestClientException, Exception { MultipartHttpServletRequest mRequest; @@ -201,16 +206,13 @@ public class WidgetsCatalogController extends EPRestrictedBaseController { String fileName; String tmpFolderName = "/tmp/"; String respond = null; - FileOutputStream fo = null; - try { - mRequest = (MultipartHttpServletRequest) request; - MultipartFile mFile = mRequest.getFile("file"); - fileName = mFile.getOriginalFilename(); - fo = new FileOutputStream(tmpFolderName + fileName); - fo.write(mFile.getBytes()); - // silence sonar scan by calling close here - fo.close(); - fo = null; + try { + mRequest = (MultipartHttpServletRequest) request; + MultipartFile mFile = mRequest.getFile("file"); + fileName = mFile.getOriginalFilename(); + try (FileOutputStream fo = new FileOutputStream(tmpFolderName + fileName)) { + fo.write(mFile.getBytes()); + } HttpHeaders header = new HttpHeaders(); header.setContentType(MediaType.MULTIPART_FORM_DATA); @@ -220,47 +222,38 @@ public class WidgetsCatalogController extends EPRestrictedBaseController { EcompPortalUtils.widgetMsProtocol() + "://" + widgetMService.getServiceLocation(whatService, SystemProperties.getProperty(EPCommonSystemProperties.MS_WIDGET_LOCAL_PORT)) - + "/widget/microservices/widgetCatalog/" + widgetId, + + MS_WIDGET_CATALOG_URL + widgetId, new HttpEntity<>(multipartRequest, WidgetServiceHeaders.getInstance()), String.class); File f = new File(tmpFolderName + fileName); f.delete(); } catch (Exception e) { logger.error(EELFLoggerDelegate.errorLogger, "updateWidgetCatalogWithFiles failed", e); - } finally { - try { - if (fo != null) - fo.close(); - } catch (IOException e) { - logger.error(EELFLoggerDelegate.errorLogger, "updateWidgetCatalogWithFiles failed 2", e); - } - } + } return respond; } - @RequestMapping(value = { "/portalApi/microservices/widgetCatalog" }, method = RequestMethod.POST) + @PostMapping(value = { "/portalApi/microservices/widgetCatalog" }) public String createWidgetCatalog(HttpServletRequest request) throws Exception { - + if (StringUtils.isNotBlank(SystemProperties.getProperty(EPCommonSystemProperties.MS_WIDGET_UPLOAD_FLAG)) - && SystemProperties.getProperty(EPCommonSystemProperties.MS_WIDGET_UPLOAD_FLAG).equalsIgnoreCase("false")) { + && "false".equalsIgnoreCase( + SystemProperties.getProperty(EPCommonSystemProperties.MS_WIDGET_UPLOAD_FLAG))) { return UNAUTHORIZED_OR_FORBIDDEN_FOR_A_DISABLED_USER; } - + MultipartHttpServletRequest mRequest; MultiValueMap multipartRequest = new LinkedMultiValueMap<>(); String fileName; String tmpFolderName = "/tmp/"; String respond = null; - FileOutputStream fo = null; try { mRequest = (MultipartHttpServletRequest) request; MultipartFile mFile = mRequest.getFile("file"); fileName = mFile.getOriginalFilename(); - fo = new FileOutputStream(tmpFolderName + fileName); - fo.write(mFile.getBytes()); - // silence sonar scan by calling close here - fo.close(); - fo = null; + try (FileOutputStream fo = new FileOutputStream(tmpFolderName + fileName)) { + fo.write(mFile.getBytes()); + } HttpHeaders header = new HttpHeaders(); header.setContentType(MediaType.MULTIPART_FORM_DATA); @@ -277,45 +270,29 @@ public class WidgetsCatalogController extends EPRestrictedBaseController { f.delete(); } catch (Exception e) { logger.error(EELFLoggerDelegate.errorLogger, "createWidgetCatalog failed", e); - } finally { - try { - if (fo != null) - fo.close(); - } catch (IOException e) { - logger.error(EELFLoggerDelegate.errorLogger, "createWidgetCatalog failed 2", e); - } } return respond; } - @RequestMapping(value = "/portalApi/microservices/{widgetId}/framework.js", method = RequestMethod.GET) + @GetMapping(value = "/portalApi/microservices/{widgetId}/framework.js") public String getWidgetFramework(@PathVariable("widgetId") long widgetId) throws Exception { return template.getForObject(EcompPortalUtils.widgetMsProtocol() + "://" + widgetMService.getServiceLocation(whatService, SystemProperties.getProperty(MS_WIDGET_LOCAL_PORT)) - + "/widget/microservices/" + widgetId + "/framework.js", String.class, - WidgetServiceHeaders.getInstance()); - } - - @RequestMapping(value = "/portalApi/microservices/{widgetId}/controller.js", method = RequestMethod.GET) - public String getWidgetController(@PathVariable("widgetId") long widgetId) throws Exception { - return template.getForObject(EcompPortalUtils.widgetMsProtocol() + "://" - + widgetMService.getServiceLocation(whatService, - SystemProperties.getProperty(MS_WIDGET_LOCAL_PORT)) - + "/widget/microservices/" + widgetId + "/controller.js", String.class, + + MS_WIDGET_URL + widgetId + "/framework.js", String.class, WidgetServiceHeaders.getInstance()); } - - @RequestMapping(value = "/portalApi/microservices/{widgetId}/style.css", method = RequestMethod.GET) + + @GetMapping(value = "/portalApi/microservices/{widgetId}/style.css") public String getWidgetCSS(@PathVariable("widgetId") long widgetId) throws Exception { return template.getForObject(EcompPortalUtils.widgetMsProtocol() + "://" + widgetMService.getServiceLocation(whatService, SystemProperties.getProperty(MS_WIDGET_LOCAL_PORT)) - + "/widget/microservices/" + widgetId + "/styles.css", String.class, + + MS_WIDGET_URL + widgetId + "/styles.css", String.class, WidgetServiceHeaders.getInstance()); } - @RequestMapping(value = { "/portalApi/microservices/parameters/{widgetId}" }, method = RequestMethod.GET) + @GetMapping(value = { "/portalApi/microservices/parameters/{widgetId}" }) public PortalRestResponse> getWidgetParameterResult(HttpServletRequest request, @PathVariable("widgetId") long widgetId) throws Exception { EPUser user = EPUserUtils.getUserSession(request); @@ -329,7 +306,7 @@ public class WidgetsCatalogController extends EPRestrictedBaseController { HttpMethod.GET, new HttpEntity<>(WidgetServiceHeaders.getInstance()), Long.class).getBody(); if (serviceId == null) { // return ok/sucess and no service parameter for this widget - return new PortalRestResponse>(PortalRestStatusEnum.WARN, + return new PortalRestResponse<>(PortalRestStatusEnum.WARN, "No service parameters for this widget", list); } else { List defaultParam = microserviceService.getParametersById(serviceId); @@ -348,21 +325,20 @@ public class WidgetsCatalogController extends EPRestrictedBaseController { list.add(userResult); } } - return new PortalRestResponse>(PortalRestStatusEnum.OK, "SUCCESS", list); + return new PortalRestResponse<>(PortalRestStatusEnum.OK, "SUCCESS", list); } - @RequestMapping(value = { "/portalApi/microservices/services/{paramId}" }, method = RequestMethod.GET) + @GetMapping(value = { "/portalApi/microservices/services/{paramId}" }) public List getUserParameterById( @PathVariable("paramId") long paramId) { - List list = widgetParameterService.getUserParameterById(paramId); - return list; + return widgetParameterService.getUserParameterById(paramId); } - @RequestMapping(value = { "/portalApi/microservices/services/{paramId}" }, method = RequestMethod.DELETE) + @DeleteMapping(value = { "/portalApi/microservices/services/{paramId}" }) public void deleteUserParameterById(@PathVariable("paramId") long paramId) { widgetParameterService.deleteUserParameterById(paramId); } - @RequestMapping(value = { "/portalApi/microservices/download/{widgetId}" }, method = RequestMethod.GET) + @GetMapping(value = { "/portalApi/microservices/download/{widgetId}" }) public void doDownload(HttpServletRequest request, HttpServletResponse response, @PathVariable("widgetId") long widgetId) throws Exception { @@ -398,7 +374,7 @@ public class WidgetsCatalogController extends EPRestrictedBaseController { String headerValue = String.format("attachment; filename=\"%s\"", downloadFile.getName()); downloadFile.delete(); response.setHeader(headerKey, headerValue); - + byte[] buffer = new byte[32 * 1024]; int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { @@ -411,7 +387,7 @@ public class WidgetsCatalogController extends EPRestrictedBaseController { } } - @RequestMapping(value = { "/portalApi/microservices/parameters" }, method = RequestMethod.POST) + @PostMapping(value = { "/portalApi/microservices/parameters" }) public PortalRestResponse saveWidgetParameter(HttpServletRequest request, @RequestBody WidgetCatalogParameter widgetParameters) { EPUser user = EPUserUtils.getUserSession(request); @@ -426,12 +402,12 @@ public class WidgetsCatalogController extends EPRestrictedBaseController { } catch (Exception e) { logger.error(EELFLoggerDelegate.errorLogger, "saveWidgetParameter failed", e); - return new PortalRestResponse(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage()); + return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage()); } - return new PortalRestResponse(PortalRestStatusEnum.OK, "SUCCESS", ""); + return new PortalRestResponse<>(PortalRestStatusEnum.OK, "SUCCESS", ""); } - - @RequestMapping(value = { "/portalApi/microservices/uploadFlag" }, method = RequestMethod.GET) + + @GetMapping(value = { "/portalApi/microservices/uploadFlag" }) public String getUploadFlag() { String uplaodFlag=""; try {