diff --git a/pom.xml b/pom.xml index adbf992..c62d29a 100644 --- a/pom.xml +++ b/pom.xml @@ -62,13 +62,10 @@ usermanagement-core provided - - org.gcube.resources.discovery - ic-client - org.gcube.resources - registry-publisher + common-gcore-resources + provided javax.portlet diff --git a/src/main/java/org/gcube/portal/auth/AuthUtil.java b/src/main/java/org/gcube/portal/auth/AuthUtil.java index 7396044..a5a65a5 100644 --- a/src/main/java/org/gcube/portal/auth/AuthUtil.java +++ b/src/main/java/org/gcube/portal/auth/AuthUtil.java @@ -1,22 +1,28 @@ package org.gcube.portal.auth; -import static org.gcube.resources.discovery.icclient.ICFactory.clientFor; -import static org.gcube.resources.discovery.icclient.ICFactory.queryFor; - import java.io.UnsupportedEncodingException; +import java.net.HttpURLConnection; +import java.net.URL; +import java.net.URLConnection; +import java.net.URLEncoder; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.transform.dom.DOMSource; + import org.gcube.common.portal.PortalContext; +import org.gcube.common.resources.gcore.Resources; import org.gcube.common.resources.gcore.ServiceEndpoint; import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint; import org.gcube.common.resources.gcore.ServiceEndpoint.Property; import org.gcube.common.resources.gcore.utils.Group; -import org.gcube.common.scope.api.ScopeProvider; -import org.gcube.resources.discovery.client.api.DiscoveryClient; -import org.gcube.resources.discovery.client.queries.api.SimpleQuery; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; @@ -32,18 +38,40 @@ public class AuthUtil { public final static String REDIRECT_URL = "RedirectURL"; public final static String SERVICE_ENDPOINT_CATEGORY = "OnlineService"; public final static String LOGOURL_ATTR = "Logo"; + public final static String TOKEN_ATTR_NAME = "gcube-token"; + public final static String ENDPOINT_TYPE = "ServiceEndpoint"; + public final static String ENDPOINT_CATEGORY = "OnlineService"; - public static List getPortalConfigurationFromIS(String infrastructureName, String clientId) - throws Exception { - String scope = "/" + infrastructureName; - String currScope = ScopeProvider.instance.get(); - ScopeProvider.instance.set(scope); - SimpleQuery query = queryFor(ServiceEndpoint.class); - query.addCondition("$resource/Profile/Category/text() eq '" + SERVICE_ENDPOINT_CATEGORY + "'"); - query.addCondition("$resource/Profile/Name/text() eq '" + clientId + "'"); - DiscoveryClient client = clientFor(ServiceEndpoint.class); - List toReturn = client.submit(query); - ScopeProvider.instance.set(currScope); + public static List getPortalConfigurationFromIS(String clientId) throws Exception { + List toReturn = new ArrayList<>(); + String encodedAppName = URLEncoder.encode(clientId, "UTF-8").replaceAll("\\+", "%20"); + System.out.println(encodedAppName); + + String icproxyEndPoint = PortalContext.getICProxyEndPoint(); + String callToICProxy = new StringBuilder(icproxyEndPoint) + .append("/") + .append(ENDPOINT_TYPE) + .append("/") + .append(ENDPOINT_CATEGORY) + .append("/") + .append(encodedAppName) + .toString(); + + URL pageURL = new URL(callToICProxy); + URLConnection siteConnection = (HttpURLConnection) pageURL.openConnection(); + String portalToken = PortalContext.getPortalApplicationToken(); + siteConnection.addRequestProperty(TOKEN_ATTR_NAME, portalToken); + + //parse the service endpoints + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + Document document = factory.newDocumentBuilder().parse(siteConnection.getInputStream()); + NodeList nodeList = document.getDocumentElement().getChildNodes(); + + for (int i = 0; i < nodeList.getLength(); i++) { + Node node = nodeList.item(i); + ServiceEndpoint res = Resources.unmarshal(ServiceEndpoint.class, new DOMSource(node)); + toReturn.add(res); + } return toReturn; } @@ -55,10 +83,8 @@ public class AuthUtil { public static RequestingApp getAuthorisedApplicationInfoFromIs(String clientId) { RequestingApp toReturn = new RequestingApp(); String infraName = PortalContext.getConfiguration().getInfrastructureName(); - System.out.println("infraName="+infraName); - try { - List list = getPortalConfigurationFromIS(infraName, clientId); + List list = getPortalConfigurationFromIS(clientId); if (list.size() > 1) { _log.error("Too many Service Endpoints having name " + clientId +" in this scope having Category " + SERVICE_ENDPOINT_CATEGORY); return null; @@ -69,7 +95,7 @@ public class AuthUtil { for (ServiceEndpoint res : list) { toReturn.setApplicationId(res.profile().name()); Group apGroup = res.profile().accessPoints(); - AccessPoint[] accessPoints = (AccessPoint[]) apGroup.toArray(new AccessPoint[apGroup.size()]); + AccessPoint[] accessPoints = apGroup.toArray(new AccessPoint[apGroup.size()]); AccessPoint found = accessPoints[0]; for (Property prop : found.properties()) { if (prop.name().compareTo(LOGOURL_ATTR) == 0) { @@ -96,7 +122,7 @@ public class AuthUtil { try { decodedURL = java.net.URLDecoder.decode(redirectionURL, "UTF-8"); } catch (UnsupportedEncodingException e) { - System.out.println("UnsupportedEncodingException=" + e.getMessage()); + _log.error("UnsupportedEncodingException=" + e.getMessage()); return new HashMap(); } String[] url = decodedURL.split("\\?"); diff --git a/src/test/java/org/gcube/portal/auth/AppTest.java b/src/test/java/org/gcube/portal/auth/AppTest.java index 8e12edf..ba4537f 100644 --- a/src/test/java/org/gcube/portal/auth/AppTest.java +++ b/src/test/java/org/gcube/portal/auth/AppTest.java @@ -33,11 +33,12 @@ public class AppTest extends TestCase { * Rigourous Test :-) */ public void testApp() { - RequestingApp app = AuthUtil.getAuthorisedApplicationInfoFromIs("AnAppRequiringUserAuthN"); + RequestingApp app = AuthUtil.getAuthorisedApplicationInfoFromIs("agINFRA+ App"); if (app != null) { System.out.println(app.getApplicationId()); System.out.println(app.getLogoURL()); + } - //assertTrue( true ); + assertTrue( true ); } }