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=a4037375cbbe589fff32430e16417962c030c0ac;hb=3bd3ecd644964416972ecc3e6e6eb7e5e1fb132d;hp=a7ce3557baea5dacce747cb58e716ee35da26abd;hpb=c7684e595461b70b09d0d9ff15b5ad23aeabebc9;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 a7ce3557..a4037375 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; @@ -60,7 +59,7 @@ import org.onap.portalapp.portal.domain.WidgetServiceHeaders; import org.onap.portalapp.portal.ecomp.model.PortalRestResponse; import org.onap.portalapp.portal.ecomp.model.PortalRestStatusEnum; import org.onap.portalapp.portal.logging.aop.EPAuditLog; -import org.onap.portalapp.portal.service.ConsulHealthService; +import org.onap.portalapp.portal.service.WidgetMService; import org.onap.portalapp.portal.service.MicroserviceService; import org.onap.portalapp.portal.service.WidgetParameterService; import org.onap.portalapp.portal.utils.EPCommonSystemProperties; @@ -100,15 +99,19 @@ 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"; @Autowired - private ConsulHealthService consulHealthService; + private WidgetMService widgetMService; @Autowired private MicroserviceService microserviceService; @@ -138,9 +141,9 @@ public class WidgetsCatalogController extends EPRestrictedBaseController { try { ResponseEntity ans = template.exchange( EcompPortalUtils.widgetMsProtocol() + "://" - + consulHealthService.getServiceLocation(whatService, + + 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) { @@ -158,7 +161,7 @@ public class WidgetsCatalogController extends EPRestrictedBaseController { try { ResponseEntity ans = template.exchange( EcompPortalUtils.widgetMsProtocol() + "://" - + consulHealthService.getServiceLocation(whatService, + + widgetMService.getServiceLocation(whatService, SystemProperties.getProperty(EPCommonSystemProperties.MS_WIDGET_LOCAL_PORT)) + "/widget/microservices/widgetCatalog", HttpMethod.GET, new HttpEntity<>(WidgetServiceHeaders.getInstance()), List.class); @@ -177,9 +180,9 @@ public class WidgetsCatalogController extends EPRestrictedBaseController { public void updateWidgetCatalog(@RequestBody WidgetCatalog newWidgetCatalog, @PathVariable("widgetId") long widgetId) throws Exception { template.exchange( EcompPortalUtils.widgetMsProtocol() + "://" - + consulHealthService.getServiceLocation(whatService, + + 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); } @@ -187,9 +190,9 @@ public class WidgetsCatalogController extends EPRestrictedBaseController { public void deleteOnboardingWidget(@PathVariable("widgetId") long widgetId) throws Exception { template.exchange( EcompPortalUtils.widgetMsProtocol() + "://" - + consulHealthService.getServiceLocation(whatService, + + 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); } @@ -201,16 +204,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); @@ -218,49 +218,40 @@ public class WidgetsCatalogController extends EPRestrictedBaseController { multipartRequest.add("widget", request.getParameter("newWidget")); respond = template.postForObject( EcompPortalUtils.widgetMsProtocol() + "://" - + consulHealthService.getServiceLocation(whatService, + + 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) 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); @@ -269,7 +260,7 @@ public class WidgetsCatalogController extends EPRestrictedBaseController { respond = template.postForObject( EcompPortalUtils.widgetMsProtocol() + "://" - + consulHealthService.getServiceLocation(whatService, + + widgetMService.getServiceLocation(whatService, SystemProperties.getProperty(EPCommonSystemProperties.MS_WIDGET_LOCAL_PORT)) + "/widget/microservices/widgetCatalog", new HttpEntity<>(multipartRequest, WidgetServiceHeaders.getInstance()), String.class); @@ -277,13 +268,6 @@ 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; } @@ -291,27 +275,27 @@ public class WidgetsCatalogController extends EPRestrictedBaseController { @RequestMapping(value = "/portalApi/microservices/{widgetId}/framework.js", method = RequestMethod.GET) public String getWidgetFramework(@PathVariable("widgetId") long widgetId) throws Exception { return template.getForObject(EcompPortalUtils.widgetMsProtocol() + "://" - + consulHealthService.getServiceLocation(whatService, + + widgetMService.getServiceLocation(whatService, SystemProperties.getProperty(MS_WIDGET_LOCAL_PORT)) - + "/widget/microservices/" + widgetId + "/framework.js", String.class, + + MS_WIDGET_URL + 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() + "://" - + consulHealthService.getServiceLocation(whatService, + + widgetMService.getServiceLocation(whatService, SystemProperties.getProperty(MS_WIDGET_LOCAL_PORT)) - + "/widget/microservices/" + widgetId + "/controller.js", String.class, + + MS_WIDGET_URL + widgetId + "/controller.js", String.class, WidgetServiceHeaders.getInstance()); } @RequestMapping(value = "/portalApi/microservices/{widgetId}/style.css", method = RequestMethod.GET) public String getWidgetCSS(@PathVariable("widgetId") long widgetId) throws Exception { return template.getForObject(EcompPortalUtils.widgetMsProtocol() + "://" - + consulHealthService.getServiceLocation(whatService, + + 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()); } @@ -323,13 +307,13 @@ public class WidgetsCatalogController extends EPRestrictedBaseController { List list = new ArrayList<>(); Long serviceId = template.exchange( EcompPortalUtils.widgetMsProtocol() + "://" - + consulHealthService.getServiceLocation(whatService, + + widgetMService.getServiceLocation(whatService, SystemProperties.getProperty(EPCommonSystemProperties.MS_WIDGET_LOCAL_PORT)) + "/widget/microservices/widgetCatalog/parameters/" + widgetId, 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,13 +332,12 @@ 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) 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) @@ -370,7 +353,7 @@ public class WidgetsCatalogController extends EPRestrictedBaseController { byte[] byteFile = template .exchange( EcompPortalUtils.widgetMsProtocol() + "://" - + consulHealthService.getServiceLocation(whatService, + + widgetMService.getServiceLocation(whatService, SystemProperties.getProperty(EPCommonSystemProperties.MS_WIDGET_LOCAL_PORT)) + "/widget/microservices/download/" + widgetId, HttpMethod.GET, new HttpEntity<>(WidgetServiceHeaders.getInstance()), byte[].class) @@ -398,7 +381,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) { @@ -426,11 +409,11 @@ 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) public String getUploadFlag() { String uplaodFlag="";