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=57c73a89054320115c4109e2ff12502cba0f4e36;hb=6a1b66d85bc824234d01627b826fdd528869f2ed;hp=15e198297ebc394ee3b20fe5d7e449df89006352;hpb=21a8761f684745bb300e075c7e98ad897ace9eed;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 15e19829..57c73a89 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,7 +33,7 @@ * * ============LICENSE_END============================================ * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * */ package org.onap.portalapp.portal.controller; @@ -49,6 +49,7 @@ import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.apache.commons.lang.StringUtils; import org.onap.portalapp.controller.EPRestrictedBaseController; import org.onap.portalapp.portal.domain.EPUser; import org.onap.portalapp.portal.domain.MicroserviceParameter; @@ -59,9 +60,10 @@ 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; import org.onap.portalapp.portal.utils.EcompPortalUtils; import org.onap.portalapp.util.EPUserUtils; import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; @@ -98,13 +100,15 @@ 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 RestTemplate template = new RestTemplate(); private String whatService = "widgets-service"; @Autowired - private ConsulHealthService consulHealthService; + private WidgetMService widgetMService; @Autowired private MicroserviceService microserviceService; @@ -134,8 +138,8 @@ public class WidgetsCatalogController extends EPRestrictedBaseController { try { ResponseEntity ans = template.exchange( EcompPortalUtils.widgetMsProtocol() + "://" - + consulHealthService.getServiceLocation(whatService, - SystemProperties.getProperty(MS_WIDGET_LOCAL_PORT)) + + widgetMService.getServiceLocation(whatService, + SystemProperties.getProperty(EPCommonSystemProperties.MS_WIDGET_LOCAL_PORT)) + "/widget/microservices/widgetCatalog/" + loginName, HttpMethod.GET, new HttpEntity<>(WidgetServiceHeaders.getInstance()), List.class); widgets = ans.getBody(); @@ -154,8 +158,8 @@ public class WidgetsCatalogController extends EPRestrictedBaseController { try { ResponseEntity ans = template.exchange( EcompPortalUtils.widgetMsProtocol() + "://" - + consulHealthService.getServiceLocation(whatService, - SystemProperties.getProperty(MS_WIDGET_LOCAL_PORT)) + + widgetMService.getServiceLocation(whatService, + SystemProperties.getProperty(EPCommonSystemProperties.MS_WIDGET_LOCAL_PORT)) + "/widget/microservices/widgetCatalog", HttpMethod.GET, new HttpEntity<>(WidgetServiceHeaders.getInstance()), List.class); widgets = ans.getBody(); @@ -173,7 +177,7 @@ 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, HttpMethod.PUT, new HttpEntity<>(newWidgetCatalog, WidgetServiceHeaders.getInstance()), String.class); @@ -183,7 +187,7 @@ 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, HttpMethod.DELETE, new HttpEntity<>(WidgetServiceHeaders.getInstance()), String.class); @@ -214,8 +218,8 @@ public class WidgetsCatalogController extends EPRestrictedBaseController { multipartRequest.add("widget", request.getParameter("newWidget")); respond = template.postForObject( EcompPortalUtils.widgetMsProtocol() + "://" - + consulHealthService.getServiceLocation(whatService, - SystemProperties.getProperty(MS_WIDGET_LOCAL_PORT)) + + widgetMService.getServiceLocation(whatService, + SystemProperties.getProperty(EPCommonSystemProperties.MS_WIDGET_LOCAL_PORT)) + "/widget/microservices/widgetCatalog/" + widgetId, new HttpEntity<>(multipartRequest, WidgetServiceHeaders.getInstance()), String.class); File f = new File(tmpFolderName + fileName); @@ -236,6 +240,12 @@ public class WidgetsCatalogController extends EPRestrictedBaseController { @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")) { + return UNAUTHORIZED_OR_FORBIDDEN_FOR_A_DISABLED_USER; + } + MultipartHttpServletRequest mRequest; MultiValueMap multipartRequest = new LinkedMultiValueMap<>(); String fileName; @@ -259,8 +269,8 @@ public class WidgetsCatalogController extends EPRestrictedBaseController { respond = template.postForObject( EcompPortalUtils.widgetMsProtocol() + "://" - + consulHealthService.getServiceLocation(whatService, - SystemProperties.getProperty(MS_WIDGET_LOCAL_PORT)) + + widgetMService.getServiceLocation(whatService, + SystemProperties.getProperty(EPCommonSystemProperties.MS_WIDGET_LOCAL_PORT)) + "/widget/microservices/widgetCatalog", new HttpEntity<>(multipartRequest, WidgetServiceHeaders.getInstance()), String.class); File f = new File(tmpFolderName + fileName); @@ -281,7 +291,7 @@ 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, WidgetServiceHeaders.getInstance()); @@ -290,7 +300,7 @@ public class WidgetsCatalogController extends EPRestrictedBaseController { @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, WidgetServiceHeaders.getInstance()); @@ -299,7 +309,7 @@ public class WidgetsCatalogController extends EPRestrictedBaseController { @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, WidgetServiceHeaders.getInstance()); @@ -313,8 +323,8 @@ public class WidgetsCatalogController extends EPRestrictedBaseController { List list = new ArrayList<>(); Long serviceId = template.exchange( EcompPortalUtils.widgetMsProtocol() + "://" - + consulHealthService.getServiceLocation(whatService, - SystemProperties.getProperty(MS_WIDGET_LOCAL_PORT)) + + 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) { @@ -360,39 +370,45 @@ public class WidgetsCatalogController extends EPRestrictedBaseController { byte[] byteFile = template .exchange( EcompPortalUtils.widgetMsProtocol() + "://" - + consulHealthService.getServiceLocation(whatService, - SystemProperties.getProperty(MS_WIDGET_LOCAL_PORT)) + + widgetMService.getServiceLocation(whatService, + SystemProperties.getProperty(EPCommonSystemProperties.MS_WIDGET_LOCAL_PORT)) + "/widget/microservices/download/" + widgetId, HttpMethod.GET, new HttpEntity<>(WidgetServiceHeaders.getInstance()), byte[].class) .getBody(); File downloadFile = File.createTempFile("temp", ".zip"); - FileOutputStream stream = new FileOutputStream(downloadFile.getPath()); - stream.write(byteFile); - stream.close(); - - FileInputStream inputStream = new FileInputStream(downloadFile); - String mimeType = context.getMimeType(downloadFile.getPath()); - if (mimeType == null) { - mimeType = "application/octet-stream"; + try(FileOutputStream stream = new FileOutputStream(downloadFile.getPath())){ + stream.write(byteFile); + }catch(Exception e) + { + logger.error(EELFLoggerDelegate.errorLogger, "doDownload failed", e); + throw e; } - response.setContentType(mimeType); - response.setContentLength((int) downloadFile.length()); - String headerKey = "Content-Disposition"; - String headerValue = String.format("attachment; filename=\"%s\"", downloadFile.getName()); - downloadFile.delete(); - response.setHeader(headerKey, headerValue); - - OutputStream outStream = response.getOutputStream(); - byte[] buffer = new byte[32 * 1024]; - int bytesRead; - while ((bytesRead = inputStream.read(buffer)) != -1) { - outStream.write(buffer, 0, bytesRead); - } + try(FileInputStream inputStream = new FileInputStream(downloadFile); + OutputStream outStream = response.getOutputStream()){ + String mimeType = context.getMimeType(downloadFile.getPath()); + if (mimeType == null) { + mimeType = "application/octet-stream"; + } - inputStream.close(); - outStream.close(); + response.setContentType(mimeType); + response.setContentLength((int) downloadFile.length()); + String headerKey = "Content-Disposition"; + 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) { + outStream.write(buffer, 0, bytesRead); + } + }catch(Exception e) + { + logger.error(EELFLoggerDelegate.errorLogger, "doDownload failed", e); + throw e; + } } @RequestMapping(value = { "/portalApi/microservices/parameters" }, method = RequestMethod.POST) @@ -414,4 +430,16 @@ public class WidgetsCatalogController extends EPRestrictedBaseController { } return new PortalRestResponse(PortalRestStatusEnum.OK, "SUCCESS", ""); } + + @RequestMapping(value = { "/portalApi/microservices/uploadFlag" }, method = RequestMethod.GET) + public String getUploadFlag() { + String uplaodFlag=""; + try { + uplaodFlag = SystemProperties.getProperty(EPCommonSystemProperties.MS_WIDGET_UPLOAD_FLAG); + } catch (Exception e) { + logger.error(EELFLoggerDelegate.errorLogger, "uploadFlag failed", e); + return null; + } + return uplaodFlag; + } }