From 8f2a7b33d6adb4b288fb38024b1ca5eaa66a38a2 Mon Sep 17 00:00:00 2001 From: lucio Date: Thu, 25 Jan 2024 13:27:13 +0100 Subject: [PATCH] moved to REST interface --- .settings/org.eclipse.wst.common.component | 3 - ....eclipse.wst.common.project.facet.core.xml | 2 +- gcube/extra-resources/WEB-INF/sun-jaxws.xml | 9 -- gcube/extra-resources/WEB-INF/web.xml | 43 +----- pom.xml | 80 +++++------ ...hnManagerImpl.java => ContextManager.java} | 77 +++++----- .../vremanagement/whnmanager/WHNManager.java | 22 +++ .../ContextManagerIntegrationTest.java | 134 ++++++++++++++++++ src/test/resources/dataio.war | Bin 1757 -> 0 bytes src/test/resources/log4j.properties | 9 -- src/test/resources/logback-test.xml | 13 ++ 11 files changed, 251 insertions(+), 141 deletions(-) delete mode 100644 gcube/extra-resources/WEB-INF/sun-jaxws.xml rename src/main/java/org/gcube/vremanagement/whnmanager/{jaxws/ws/WhnManagerImpl.java => ContextManager.java} (75%) create mode 100644 src/main/java/org/gcube/vremanagement/whnmanager/WHNManager.java create mode 100644 src/test/java/org/gcube/resourcemanagement/whnmanager/ContextManagerIntegrationTest.java delete mode 100644 src/test/resources/dataio.war delete mode 100644 src/test/resources/log4j.properties create mode 100644 src/test/resources/logback-test.xml diff --git a/.settings/org.eclipse.wst.common.component b/.settings/org.eclipse.wst.common.component index e424036..4413d77 100644 --- a/.settings/org.eclipse.wst.common.component +++ b/.settings/org.eclipse.wst.common.component @@ -11,9 +11,6 @@ uses - - uses - diff --git a/.settings/org.eclipse.wst.common.project.facet.core.xml b/.settings/org.eclipse.wst.common.project.facet.core.xml index c82abf3..71d64dd 100644 --- a/.settings/org.eclipse.wst.common.project.facet.core.xml +++ b/.settings/org.eclipse.wst.common.project.facet.core.xml @@ -3,5 +3,5 @@ - + diff --git a/gcube/extra-resources/WEB-INF/sun-jaxws.xml b/gcube/extra-resources/WEB-INF/sun-jaxws.xml deleted file mode 100644 index 87d03c5..0000000 --- a/gcube/extra-resources/WEB-INF/sun-jaxws.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - \ No newline at end of file diff --git a/gcube/extra-resources/WEB-INF/web.xml b/gcube/extra-resources/WEB-INF/web.xml index 9566646..9fd650f 100644 --- a/gcube/extra-resources/WEB-INF/web.xml +++ b/gcube/extra-resources/WEB-INF/web.xml @@ -1,43 +1,12 @@ - - - - com.sun.xml.ws.transport.http.servlet.WSServletContextListener - - - + - whnmanager - com.sun.xml.ws.transport.http.servlet.WSServlet + org.gcube.resourcemanagement.whnmanager.WHNManager + - - - whnmanager - /gcube/vremanagement/ws/whnmanager + org.gcube.resourcemanagement.whnmanager.WHNManager + /* - - + diff --git a/pom.xml b/pom.xml index bfc81c5..97ea121 100644 --- a/pom.xml +++ b/pom.xml @@ -30,70 +30,54 @@ - - org.slf4j - slf4j-api - + org.gcube.core common-smartgears - - javax.servlet - javax.servlet-api - 4.0.1 - provided - - - - javax.ws.rs - javax.ws.rs-api - - org.gcube.core common-smartgears-app + + - org.gcube.common - gxHTTP + javax.ws.rs + javax.ws.rs-api + + + org.glassfish.jersey.containers + jersey-container-servlet - org.jboss.weld.servlet - weld-servlet-core - 2.0.4.Final - - - jboss-el-api_3.0_spec - org.jboss.spec.javax.el - - + javax.servlet + javax.servlet-api + 3.0.1 + + + + + org.glassfish.jersey.media + jersey-media-json-jackson + + + + org.slf4j + slf4j-api - jboss-el-api_3.0_spec - org.jboss.spec.javax.el - 1.0.0.Alpha1 - provided - - - - org.jboss.weld - weld-core - 2.0.4.Final - - - junit - junit - 4.11 - test - - - org.gcube.resourcemanagement - whnmanager-api - [2.0.2-SNAPSHOT, 3.0.0-SNAPSHOT) + org.glassfish.jersey.test-framework + jersey-test-framework-core + test + + + + org.glassfish.jersey.test-framework.providers + jersey-test-framework-provider-grizzly2 + test diff --git a/src/main/java/org/gcube/vremanagement/whnmanager/jaxws/ws/WhnManagerImpl.java b/src/main/java/org/gcube/vremanagement/whnmanager/ContextManager.java similarity index 75% rename from src/main/java/org/gcube/vremanagement/whnmanager/jaxws/ws/WhnManagerImpl.java rename to src/main/java/org/gcube/vremanagement/whnmanager/ContextManager.java index f3cad93..b25816c 100644 --- a/src/main/java/org/gcube/vremanagement/whnmanager/jaxws/ws/WhnManagerImpl.java +++ b/src/main/java/org/gcube/vremanagement/whnmanager/ContextManager.java @@ -1,42 +1,42 @@ -package org.gcube.vremanagement.whnmanager.jaxws.ws; +package org.gcube.vremanagement.whnmanager; import java.net.HttpURLConnection; import java.util.Map.Entry; +import java.util.Set; -import javax.jws.WebService; +import javax.ws.rs.DELETE; +import javax.ws.rs.FormParam; +import javax.ws.rs.GET; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; import org.gcube.common.gxhttp.request.GXHTTPStringRequest; import org.gcube.common.gxhttp.util.ContentUtils; import org.gcube.common.security.providers.SecretManagerProvider; import org.gcube.common.security.secrets.Secret; -import org.gcube.resourcemanagement.whnmanager.api.WhnManager; -import org.gcube.resourcemanagement.whnmanager.api.exception.GCUBEUnrecoverableException; import org.gcube.smartgears.ContextProvider; import org.gcube.smartgears.context.application.ApplicationContext; import org.gcube.smartgears.managers.ContextEvents; import org.gcube.smartgears.security.SimpleCredentials; import org.gcube.smartgears.security.defaults.DefaultAuthorizationProvider; +import org.gcube.smartgears.utils.InnerMethodName; import org.gcube.vremanagement.whnmanager.utils.ValidationUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -//import org.gcube.common.calls.jaxws.Constants; -@WebService(portName = "WhnManagerIOPort", -serviceName = WhnManager.SERVICE_NAME, -targetNamespace = WhnManager.TNS, -endpointInterface = "org.gcube.resourcemanagement.whnmanager.api.WhnManager" ) -//@Singleton -public class WhnManagerImpl implements WhnManager{ - - - private static Logger logger=LoggerFactory.getLogger(WhnManagerImpl.class); - - /** - * Add a scope to the ghn profile and publish it on IS - */ - @Override - public boolean addToContext(String context) throws GCUBEUnrecoverableException{ +@Path("/contexts") +public class ContextManager { + + private static Logger logger=LoggerFactory.getLogger(ContextManager.class); + + @PUT + @Path("") + public Response add(@FormParam("context") String context){ + InnerMethodName.instance.set("addContext"); Secret secret = SecretManagerProvider.instance.get(); logger.trace("WHNManager: addToContext method invokation with parameters context :{} and caller: {} curentContext: {}",context, secret.getOwner().getId(), secret.getContext() ); ValidationUtils.valid("context", context); @@ -66,21 +66,21 @@ public class WhnManagerImpl implements WhnManager{ appContext.container().events().fire(context, ContextEvents.ADD_CONTEXT_TO_CONTAINER); } catch (Exception e) { logger.error("error adding context {}", context, e); - return false; + return Response.serverError().build(); } }else{ logger.error("context is null"); - return false; + return Response.status(Status.BAD_REQUEST).build(); } - return true; + return Response.ok().build(); } - - /** - * Remove a scope from ghn profile and publish the new profile on IS - */ - @Override - public boolean removeFromContext(String context) throws GCUBEUnrecoverableException { + + + @DELETE + @Path("") + public Response remove(@FormParam("context") String context){ + InnerMethodName.instance.set("removeContext"); Secret secret = SecretManagerProvider.instance.get(); logger.trace("WHNManager: removeFromContext method invokation with parameters context :{} and caller: {} curentContext: {}",context, secret.getOwner().getId(), secret.getContext()); ValidationUtils.valid("context", context); @@ -111,16 +111,25 @@ public class WhnManagerImpl implements WhnManager{ appContext.container().events().fire(context, ContextEvents.REMOVE_CONTEXT_FROM_CONTAINER); } catch (Exception e) { logger.error("error removing context {}", context, e); - return false; + return Response.serverError().build(); } }else{ logger.error("context is null"); - return false; + return Response.status(Status.BAD_REQUEST).build(); } - return true; + return Response.ok().build(); } - - + + @GET + @Path("") + @Produces(MediaType.APPLICATION_JSON) + public String[] get(){ + InnerMethodName.instance.set("addContext"); + ApplicationContext appContext = ContextProvider.get(); + Set contexts = appContext.authorizationProvider().getContexts(); + return contexts.stream().toArray(String[]::new); + } + } diff --git a/src/main/java/org/gcube/vremanagement/whnmanager/WHNManager.java b/src/main/java/org/gcube/vremanagement/whnmanager/WHNManager.java new file mode 100644 index 0000000..d509491 --- /dev/null +++ b/src/main/java/org/gcube/vremanagement/whnmanager/WHNManager.java @@ -0,0 +1,22 @@ +package org.gcube.vremanagement.whnmanager; + +import java.util.HashSet; +import java.util.Set; + +import javax.ws.rs.Path; +import javax.ws.rs.core.Application; + +@Path("/") +public class WHNManager extends Application{ + + + @Override + public Set> getClasses() { + final Set> classes = new HashSet>(); + classes.add(ContextManager.class); + return classes; + } + + + +} diff --git a/src/test/java/org/gcube/resourcemanagement/whnmanager/ContextManagerIntegrationTest.java b/src/test/java/org/gcube/resourcemanagement/whnmanager/ContextManagerIntegrationTest.java new file mode 100644 index 0000000..f0b72d6 --- /dev/null +++ b/src/test/java/org/gcube/resourcemanagement/whnmanager/ContextManagerIntegrationTest.java @@ -0,0 +1,134 @@ +package org.gcube.resourcemanagement.whnmanager; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; + +import java.util.Collections; +import java.util.Set; + +import javax.servlet.ServletContext; +import javax.ws.rs.core.Application; +import javax.ws.rs.core.HttpHeaders; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.Status; + +import org.gcube.common.events.Hub; +import org.gcube.common.security.credentials.Credentials; +import org.gcube.common.security.secrets.Secret; +import org.gcube.smartgears.ContextProvider; +import org.gcube.smartgears.configuration.application.ApplicationConfiguration; +import org.gcube.smartgears.context.Properties; +import org.gcube.smartgears.context.application.ApplicationContext; +import org.gcube.smartgears.context.container.ContainerContext; +import org.gcube.smartgears.lifecycle.application.ApplicationLifecycle; +import org.gcube.smartgears.persistence.PersistenceWriter; +import org.gcube.smartgears.security.AuthorizationProvider; +import org.gcube.vremanagement.whnmanager.ContextManager; +import org.glassfish.jersey.server.ResourceConfig; +import org.glassfish.jersey.test.JerseyTest; +import org.junit.BeforeClass; +import org.junit.Test; + +public class ContextManagerIntegrationTest extends JerseyTest { + + @Override + protected Application configure() { + return new ResourceConfig(ContextManager.class); + } + + @BeforeClass + public static void init() { + ContextProvider.set(new ApplicationContext() { + + @Override + public Properties properties() { + // TODO Auto-generated method stub + return null; + } + + @Override + public PersistenceWriter persistence() { + // TODO Auto-generated method stub + return null; + } + + @Override + public String name() { + // TODO Auto-generated method stub + return null; + } + + @Override + public ApplicationLifecycle lifecycle() { + // TODO Auto-generated method stub + return null; + } + + @Override + public String id() { + // TODO Auto-generated method stub + return null; + } + + @Override + public Hub events() { + // TODO Auto-generated method stub + return null; + } + + @Override + public ContainerContext container() { + // TODO Auto-generated method stub + return null; + } + + @Override + public ApplicationConfiguration configuration() { + // TODO Auto-generated method stub + return null; + } + + @Override + public AuthorizationProvider authorizationProvider() { + return new AuthorizationProvider() { + + @Override + public Set getContexts() { + return Collections.singleton("/test/context"); + } + + @Override + public Secret getSecretForContext(String context) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Credentials getCredentials() { + // TODO Auto-generated method stub + return null; + } + }; + } + + @Override + public ServletContext application() { + // TODO Auto-generated method stub + return null; + } + }); + } + + @Test + public void gettingContexts() { + Response response = target("/contexts").request() + .get(); + + assertEquals("Http Response should be 200: ", Status.OK.getStatusCode(), response.getStatus()); + assertEquals("Http Content-Type should be: ", MediaType.APPLICATION_JSON, response.getHeaderString(HttpHeaders.CONTENT_TYPE)); + + String[] content = response.readEntity(String[].class); + assertArrayEquals(content, new String[]{"/test/context"}); + } +} diff --git a/src/test/resources/dataio.war b/src/test/resources/dataio.war deleted file mode 100644 index a14bf8c3bebd6588e3463c9dd4de7c754c9d526d..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1757 zcmWIWW@Zs#-~hscA~k0QB)|z|`?`iW>U#RQ=`%9~c(ZeWWSO8!0&r>+Le`j+nwVFt zSCO03>wlW>kb;2C?e!TuP6zFscuRkUy8gXcqMb)iT-u>gvyWwQ_}raml_$US7wf$J zCjW@F;qe_A4cc~rpPH9&%5||G3EdoKlI!UE`pmoA9-1e&Onq3_*c&7(^ZoI`GuIy9 z5_DJjoVn?8Os}CtkKP6O#c$8EToDg8*PQuNr!6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J`!O)E0bv2ik*pvd0FB#s)&Kwi diff --git a/src/test/resources/log4j.properties b/src/test/resources/log4j.properties deleted file mode 100644 index f1ebf00..0000000 --- a/src/test/resources/log4j.properties +++ /dev/null @@ -1,9 +0,0 @@ -log4j.rootCategory = WARN,ROOT - -log4j.appender.ROOT=org.apache.log4j.ConsoleAppender -log4j.appender.ROOT.layout=org.apache.log4j.PatternLayout -log4j.appender.ROOT.layout.ConversionPattern=%d{HH:mm:ss,SSS} %-5p %c{2} [%t,%M:%L] %m%n - -log4j.category.org.gcube = TRACE,ROOT -log4j.additivity.org.gcube=false - diff --git a/src/test/resources/logback-test.xml b/src/test/resources/logback-test.xml new file mode 100644 index 0000000..e854089 --- /dev/null +++ b/src/test/resources/logback-test.xml @@ -0,0 +1,13 @@ + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n + + + + + + + + + \ No newline at end of file