From ec06374a3bc1221cb76ca349c4b193af5a9db738 Mon Sep 17 00:00:00 2001 From: Giancarlo Panichi Date: Fri, 15 Oct 2021 18:43:22 +0200 Subject: [PATCH] Added check for ssd already present --- .settings/org.eclipse.core.resources.prefs | 1 + .../is/InformationSystemUtils.java | 58 +++++++++++++++++-- .../shared/Constants.java | 1 - .../simpleProgressBar.jsp | 8 +-- .../systemservicedefinition/ssd_create.jsp | 16 +++-- .../html/systemservicedefinition/view.jsp | 2 +- 6 files changed, 70 insertions(+), 16 deletions(-) diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs index 29abf99..ee83b8b 100644 --- a/.settings/org.eclipse.core.resources.prefs +++ b/.settings/org.eclipse.core.resources.prefs @@ -1,6 +1,7 @@ eclipse.preferences.version=1 encoding//src/main/java=UTF-8 encoding//src/main/resources=UTF-8 +encoding//src/main/webapp/html/systemservicedefinition/view.jsp=UTF-8 encoding//src/test/java=UTF-8 encoding//src/test/resources=UTF-8 encoding/=UTF-8 diff --git a/src/main/java/org/gcube/portlets/admin/systemservicedefinition/is/InformationSystemUtils.java b/src/main/java/org/gcube/portlets/admin/systemservicedefinition/is/InformationSystemUtils.java index 19edd7d..c05ef09 100644 --- a/src/main/java/org/gcube/portlets/admin/systemservicedefinition/is/InformationSystemUtils.java +++ b/src/main/java/org/gcube/portlets/admin/systemservicedefinition/is/InformationSystemUtils.java @@ -68,10 +68,58 @@ public class InformationSystemUtils { } } + public static void checkSSDPresent(DefinitionItem definitionItem, String category, String scope) + throws Exception { + try { + if (definitionItem == null || definitionItem.getClientId() == null + || definitionItem.getClientId().isEmpty()) + throw new Exception("Invalid definition: " + definitionItem); + + if (category == null || category.isEmpty()) + throw new Exception("Invalid category: " + category); + + if (scope == null || scope.isEmpty()) + throw new Exception("Invalid scope: " + scope); + + ScopeProvider.instance.set(scope); + // AccessTokenProvider.instance.set(token); + // SecurityTokenProvider.instance.set(token); + + SimpleQuery query = ICFactory.queryFor(ServiceEndpoint.class); + query.addCondition("$resource/Profile/Category/text() eq '" + category + "'") + .addCondition("$resource/Profile/Name/text() eq '" + definitionItem.getClientId() + "'"); + + DiscoveryClient client = ICFactory.clientFor(ServiceEndpoint.class); + + List resources = client.submit(query); + + for(ServiceEndpoint ssd: resources) { + if (definitionItem.getClientId().compareTo(ssd.profile().name()) == 0) { + StringBuilder error=new StringBuilder(); + error.append("System Service "); + error.append(definitionItem.getClientId()); + error.append(" already present in the scope: "); + error.append(scope); + logger.error(error.toString()); + throw new Exception(error.toString()); + } + } + + logger.debug("System Service not already present on IS in scope: "+scope); + return ; + + } catch (Throwable e) { + logger.error(e.getLocalizedMessage(), e); + throw e; + } + } + public static ArrayList retrieveSSD(String category, String scope) throws Exception { try { logger.debug("Retrieve System Services Definition on IS"); + if (category == null || category.isEmpty()) + throw new Exception("Invalid category: " + category); if (scope == null || scope.isEmpty()) throw new Exception("Invalid scope: " + scope); @@ -133,6 +181,8 @@ public class InformationSystemUtils { * if (token == null || token.isEmpty()) throw new Exception("Invalid token: " + * token); */ + + checkSSDPresent(definitionItem, category, scope); IAMService iamService = retrieveIAMService(scope); @@ -217,14 +267,14 @@ public class InformationSystemUtils { List resources = client.submit(query); if (resources != null && !resources.isEmpty()) { - ServiceEndpoint toDelete=resources.get(0); + ServiceEndpoint toDelete = resources.get(0); logger.debug("Requested delete: {}", toDelete); - + try { RegistryPublisher publisher = RegistryPublisherFactory.create(); publisher.remove(toDelete); - + } catch (Exception e) { logger.error("Error publishing the ssd on IS: {}", e.getLocalizedMessage(), e); throw e; @@ -232,7 +282,7 @@ public class InformationSystemUtils { logger.debug("Deleted on IS"); } else { - String error="No resources found with name: "+definitionItem.getClientId(); + String error = "No resources found with name: " + definitionItem.getClientId(); logger.error(error); throw new Exception(error); } diff --git a/src/main/java/org/gcube/portlets/admin/systemservicedefinition/shared/Constants.java b/src/main/java/org/gcube/portlets/admin/systemservicedefinition/shared/Constants.java index 3bbeebf..359fcf3 100644 --- a/src/main/java/org/gcube/portlets/admin/systemservicedefinition/shared/Constants.java +++ b/src/main/java/org/gcube/portlets/admin/systemservicedefinition/shared/Constants.java @@ -11,7 +11,6 @@ public class Constants { public static final String DEFAULT_USER = "giancarlo.panichi"; public static final String DEFAULT_SCOPE = "/gcube"; public static final String DEFAULT_TOKEN = ""; - public static final String DEFAULT_ROLE = "OrganizationMember"; // Session diff --git a/src/main/webapp/html/systemservicedefinition/simpleProgressBar.jsp b/src/main/webapp/html/systemservicedefinition/simpleProgressBar.jsp index 507984e..2c65bd8 100644 --- a/src/main/webapp/html/systemservicedefinition/simpleProgressBar.jsp +++ b/src/main/webapp/html/systemservicedefinition/simpleProgressBar.jsp @@ -45,13 +45,9 @@ function move() { id=setInterval(frame, 10); } -function sendRequest(formName, elem){ - console.log("Send Request"); - elem.disabled=true; +function loadingActive(){ + console.log("Loading..."); move(); - let frm=document.getElementById(formName); - document.getElementById("act").value="submit"; - frm.submit(); } diff --git a/src/main/webapp/html/systemservicedefinition/ssd_create.jsp b/src/main/webapp/html/systemservicedefinition/ssd_create.jsp index e639c66..c6ee1dc 100644 --- a/src/main/webapp/html/systemservicedefinition/ssd_create.jsp +++ b/src/main/webapp/html/systemservicedefinition/ssd_create.jsp @@ -2,11 +2,11 @@ + message="Invalid Client Id!" /> + message="Invalid Client Id! Space characters are not allowed." /> + message="Invalid Description!" />
+ onclick="createDefinition(this)">Save
@@ -38,6 +38,14 @@ function cancel(){ location.href = '<%=createDefinitionURL.toString()%>'; } +function createDefinition(elem){ + elem.disabled=true; + document.getElementById("act").value="submit"; + let frm=document.getElementById("createDefinitionForm"); + frm.submit(); + loadingActive(); +} + <%@include file="simpleProgressBar.jsp" %> diff --git a/src/main/webapp/html/systemservicedefinition/view.jsp b/src/main/webapp/html/systemservicedefinition/view.jsp index b6efafb..53b11b0 100644 --- a/src/main/webapp/html/systemservicedefinition/view.jsp +++ b/src/main/webapp/html/systemservicedefinition/view.jsp @@ -37,7 +37,7 @@ pageContext.setAttribute("currentUsername", currentUsername); -

Use this portlet to automatically create or delete System Services.

+

Use this portlet to automatically create or delete system services, our internal services that carry out "system" operations with the need to be authorised to access other services with every active contexts of the infrastructure.