moved to REST interface

This commit is contained in:
lucio 2024-01-25 13:27:13 +01:00
parent 6bd96ecee2
commit 8f2a7b33d6
11 changed files with 251 additions and 141 deletions

View File

@ -11,9 +11,6 @@
<dependent-module archiveName="common-smartgears-app-3.0.0.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/common-smartgears-app/common-smartgears-app">
<dependency-type>uses</dependency-type>
</dependent-module>
<dependent-module archiveName="whnmanager-api-2.0.2-SNAPSHOT.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/whnmanager-api/whnmanager-api">
<dependency-type>uses</dependency-type>
</dependent-module>
<property name="context-root" value="whn-manager"/>

View File

@ -3,5 +3,5 @@
<fixed facet="wst.jsdt.web"/>
<installed facet="java" version="11"/>
<installed facet="wst.jsdt.web" version="1.0"/>
<installed facet="jst.web" version="4.0"/>
<installed facet="jst.web" version="3.0"/>
</faceted-project>

View File

@ -1,9 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<endpoints
xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"
version="2.0">
<endpoint
name="whnmanager"
implementation="org.gcube.vremanagement.whnmanager.jaxws.ws.WhnManagerImpl"
url-pattern="/gcube/vremanagement/ws/whnmanager"/>
</endpoints>

View File

@ -1,43 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<listener>
<listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>
<!--
<listener>
<listener-class>org.gcube.data.analysis.tabulardata.context.MyContext</listener-class>
</listener>
<listener>
<listener-class>org.gcube.common.ghn.service.ApplicationListener</listener-class>
</listener>
-->
<web-app>
<servlet>
<servlet-name>whnmanager</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
<servlet-name>org.gcube.resourcemanagement.whnmanager.WHNManager</servlet-name>
</servlet>
<servlet-mapping>
<servlet-name>whnmanager</servlet-name>
<url-pattern>/gcube/vremanagement/ws/whnmanager</url-pattern>
<servlet-name>org.gcube.resourcemanagement.whnmanager.WHNManager</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<!--
<filter>
<filter-name>scopeFilter</filter-name>
<filter-class>org.gcube.data.analysis.tabulardata.context.MySoapFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>scopeFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>-->
</web-app>

80
pom.xml
View File

@ -30,70 +30,54 @@
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<!-- smartgears -->
<dependency>
<groupId>org.gcube.core</groupId>
<artifactId>common-smartgears</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
</dependency>
<dependency>
<groupId>org.gcube.core</groupId>
<artifactId>common-smartgears-app</artifactId>
</dependency>
<!-- jersey -->
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>gxHTTP</artifactId>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.weld.servlet</groupId>
<artifactId>weld-servlet-core</artifactId>
<version>2.0.4.Final</version>
<exclusions>
<exclusion>
<artifactId>jboss-el-api_3.0_spec</artifactId>
<groupId>org.jboss.spec.javax.el</groupId>
</exclusion>
</exclusions>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
<!-- add jackson as json provider -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<artifactId>jboss-el-api_3.0_spec</artifactId>
<groupId>org.jboss.spec.javax.el</groupId>
<version>1.0.0.Alpha1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.weld</groupId>
<artifactId>weld-core</artifactId>
<version>2.0.4.Final</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.gcube.resourcemanagement</groupId>
<artifactId>whnmanager-api</artifactId>
<version>[2.0.2-SNAPSHOT, 3.0.0-SNAPSHOT)</version>
<groupId>org.glassfish.jersey.test-framework</groupId>
<artifactId>jersey-test-framework-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-grizzly2</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

View File

@ -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<String> contexts = appContext.authorizationProvider().getContexts();
return contexts.stream().toArray(String[]::new);
}
}

View File

@ -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<Class<?>> getClasses() {
final Set<Class<?>> classes = new HashSet<Class<?>>();
classes.add(ContextManager.class);
return classes;
}
}

View File

@ -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<String> 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"});
}
}

Binary file not shown.

View File

@ -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

View File

@ -0,0 +1,13 @@
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
</encoder>
</appender>
<root level="TRACE">
<appender-ref ref="STDOUT"/>
</root>
<logger name="org.gcube.resourcemanagement.whnmanager" level="TRACE"/>
</configuration>