diff --git a/.vscode/launch.json b/.vscode/launch.json index b0bea0e..4e2df5a 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -10,7 +10,7 @@ "projectName": "identity-manager", "request": "attach", "hostName": "localhost", - "port": 5005 + "port": 5005 } ] } \ No newline at end of file diff --git a/buildImageAndStart.sh b/buildImageAndStart.sh index ce434b1..7cc0e95 100755 --- a/buildImageAndStart.sh +++ b/buildImageAndStart.sh @@ -1,3 +1,3 @@ mvn clean package -docker build -t smartgears-helloworld . -docker run -p 8080:8080 smartgears-helloworld \ No newline at end of file +docker build -t smartgears-helloworld . +docker run -p 8080:8080 smartgears-helloworld diff --git a/buildImageAndStartWithDebug.sh b/buildImageAndStartWithDebug.sh index b480f01..9a50bb5 100755 --- a/buildImageAndStartWithDebug.sh +++ b/buildImageAndStartWithDebug.sh @@ -1,3 +1,3 @@ mvn package -docker build -t smartgears-helloworld . -docker run -p 8080:8080 -p 5005:5005 -e JAVA_TOOL_OPTIONS="-agentlib:jdwp=transport=dt_socket,address=*:5005,server=y,suspend=y" smartgears-helloworld \ No newline at end of file +docker build -t smartgears-helloworld . +docker run -p 8080:8080 -p 5005:5005 -e JAVA_TOOL_OPTIONS="-agentlib:jdwp=transport=dt_socket,address=*:5005,server=y,suspend=y" smartgears-helloworld diff --git a/pom.xml b/pom.xml index e69c2f8..b4cf3a8 100644 --- a/pom.xml +++ b/pom.xml @@ -1,4 +1,6 @@ - + 4.0.0 org.gcube.tools @@ -90,24 +92,24 @@ org.codehaus.mojo aspectj-maven-plugin ${aspectj-plugin.version} - - 11 - 11 - 11 - - - org.gcube.common - authorization-control-library - - - - - - - compile - - - + + 11 + 11 + 11 + + + org.gcube.common + authorization-control-library + + + + + + + compile + + + diff --git a/src/main/java/org/gcube/service/helloworld/HelloWorld.java b/src/main/java/org/gcube/service/helloworld/HelloWorld.java index 54e9880..b7b31e1 100644 --- a/src/main/java/org/gcube/service/helloworld/HelloWorld.java +++ b/src/main/java/org/gcube/service/helloworld/HelloWorld.java @@ -6,11 +6,14 @@ import java.util.Set; import javax.ws.rs.ApplicationPath; import javax.ws.rs.core.Application; -import org.gcube.service.helloworld.services.AuthorizedMethods; -import org.gcube.service.helloworld.services.ExcludeAuthorization; +import org.gcube.service.helloworld.services.AuthenticatedService; +import org.gcube.service.helloworld.services.ExcludeAuthorizationService; import org.gcube.service.helloworld.services.HelloService; -import org.gcube.service.helloworld.services.TestAuthorizationService; +/** + * + * @author Lucio Lelii (ISTI-CNR) + */ @ApplicationPath("") public class HelloWorld extends Application { @@ -19,9 +22,8 @@ public class HelloWorld extends Application { final Set> classes = new HashSet>(); // register resources classes implementing Servlets classes.add(HelloService.class); - classes.add(ExcludeAuthorization.class); - classes.add(TestAuthorizationService.class); - + classes.add(ExcludeAuthorizationService.class); + classes.add(AuthenticatedService.class); return classes; } } diff --git a/src/main/java/org/gcube/service/helloworld/manager/HelloWorldManager.java b/src/main/java/org/gcube/service/helloworld/manager/HelloWorldManager.java index c2d1659..fa44b4c 100644 --- a/src/main/java/org/gcube/service/helloworld/manager/HelloWorldManager.java +++ b/src/main/java/org/gcube/service/helloworld/manager/HelloWorldManager.java @@ -16,9 +16,7 @@ import org.slf4j.LoggerFactory; * to the app declaring it via the @ManagedBy annotation. (@see HelloWorld * class) * - * - * @author lucio - * + * @author Lucio Lelii (ISTI-CNR) */ public class HelloWorldManager implements ApplicationManager { @@ -26,7 +24,7 @@ public class HelloWorldManager implements ApplicationManager { Logger logger = LoggerFactory.getLogger(HelloWorldManager.class); @Override - public void onInit() { + public void onInit() { if (ContextProvider.get().container().configuration().mode() == Mode.offline) { logger.debug("init called in offline mode"); } else { @@ -35,7 +33,7 @@ public class HelloWorldManager implements ApplicationManager { logger.debug("init called in context {}", secret.getContext()); } else { logger.debug("init called in null context"); - } + } } } @@ -49,7 +47,7 @@ public class HelloWorldManager implements ApplicationManager { logger.debug("shutDown called in context {}", secret.getContext()); } else { logger.debug("shutDown called in null context"); - } + } } } } diff --git a/src/main/java/org/gcube/service/helloworld/serializers/ContainerConfigurationSerializer.java b/src/main/java/org/gcube/service/helloworld/serializers/ContainerConfigurationSerializer.java index 366f334..5d2fdc0 100644 --- a/src/main/java/org/gcube/service/helloworld/serializers/ContainerConfigurationSerializer.java +++ b/src/main/java/org/gcube/service/helloworld/serializers/ContainerConfigurationSerializer.java @@ -8,18 +8,25 @@ import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.ser.std.StdSerializer; -public class ContainerConfigurationSerializer extends StdSerializer { +/** + * Jackson serializer for ContainerConfiguration + * + * @author Alfredo Oliviero (ISTI-CNR) + * + */ + +public class ContainerConfigurationSerializer extends StdSerializer { protected ContainerConfigurationSerializer(Class t) { super(t); } - public ContainerConfigurationSerializer(){ + public ContainerConfigurationSerializer() { super(ContainerConfiguration.class, true); } - @Override - public void serialize(ContainerConfiguration configuration, JsonGenerator jgen, SerializerProvider provider) throws IOException { + public void serialize(ContainerConfiguration configuration, JsonGenerator jgen, SerializerProvider provider) + throws IOException { jgen.writeStartObject(); jgen.writeObjectField("mode", configuration.mode()); jgen.writeObjectField("app", configuration.apps()); @@ -33,8 +40,6 @@ public class ContainerConfigurationSerializer extends StdSerializer { +/** + * Jackson serializer for ContainerContext + * + * @author Alfredo Oliviero (ISTI-CNR) + * + */ + +public class ContainerContextSerializer extends StdSerializer { protected ContainerContextSerializer(Class t) { super(t); } - public ContainerContextSerializer(){ + public ContainerContextSerializer() { super(ContainerContext.class, true); } - @Override - public void serialize(ContainerContext ccontext, JsonGenerator jgen, SerializerProvider provider) throws IOException { + public void serialize(ContainerContext ccontext, JsonGenerator jgen, SerializerProvider provider) + throws IOException { jgen.writeStartObject(); jgen.writeStringField("id", ccontext.id()); // jgen.writeObjectField("configuration.site", ccontext.configuration().site()); - //jgen.writeObjectField("configuration", ccontext.configuration()); + // jgen.writeObjectField("configuration", ccontext.configuration()); jgen.writeObjectField("properties", ccontext.properties()); jgen.writeObjectField("authorizationProvider", ccontext.authorizationProvider()); diff --git a/src/main/java/org/gcube/service/helloworld/serializers/SmartGearSerializator.java b/src/main/java/org/gcube/service/helloworld/serializers/ContextSerializator.java similarity index 83% rename from src/main/java/org/gcube/service/helloworld/serializers/SmartGearSerializator.java rename to src/main/java/org/gcube/service/helloworld/serializers/ContextSerializator.java index f1553ed..d755923 100644 --- a/src/main/java/org/gcube/service/helloworld/serializers/SmartGearSerializator.java +++ b/src/main/java/org/gcube/service/helloworld/serializers/ContextSerializator.java @@ -7,20 +7,26 @@ import org.gcube.smartgears.security.SimpleCredentials; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.module.SimpleModule; -public class SmartGearSerializator { - private static ObjectMapper serializer = null; +/** + * Jackson Serialization utils for Smartgear Context classes + * + * @author Alfredo Oliviero (ISTI-CNR) + * + */ + +public class ContextSerializator { + private static ObjectMapper serializer = null; public static ObjectMapper getSerializer() { if (serializer == null) { ObjectMapper om = new ObjectMapper(); SimpleModule module = new SimpleModule(); // module.addSerializer(Owner.class, new OwnerSerializer()); - + module.addSerializer(ContainerConfiguration.class, new ContainerConfigurationSerializer()); module.addSerializer(ContainerContext.class, new ContainerContextSerializer()); module.addSerializer(SimpleCredentials.class, new SimpleCredentialsSerializer()); - - + om.registerModule(module); serializer = om; } diff --git a/src/main/java/org/gcube/service/helloworld/serializers/OwnerSerializer.java b/src/main/java/org/gcube/service/helloworld/serializers/OwnerSerializer.java index 6b10acd..2193262 100644 --- a/src/main/java/org/gcube/service/helloworld/serializers/OwnerSerializer.java +++ b/src/main/java/org/gcube/service/helloworld/serializers/OwnerSerializer.java @@ -8,17 +8,23 @@ import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.ser.std.StdSerializer; +/** + * Jackson serializer for Owner + * + * @author Alfredo Oliviero (ISTI-CNR) + * + */ + public class OwnerSerializer extends StdSerializer { protected OwnerSerializer(Class t) { super(t); } - public OwnerSerializer(){ + public OwnerSerializer() { super(Owner.class, true); } - @Override public void serialize(Owner owner, JsonGenerator jgen, SerializerProvider provider) throws IOException { jgen.writeStartObject(); diff --git a/src/main/java/org/gcube/service/helloworld/serializers/SimpleCredentialsSerializer.java b/src/main/java/org/gcube/service/helloworld/serializers/SimpleCredentialsSerializer.java index 6560c00..d7d1d87 100644 --- a/src/main/java/org/gcube/service/helloworld/serializers/SimpleCredentialsSerializer.java +++ b/src/main/java/org/gcube/service/helloworld/serializers/SimpleCredentialsSerializer.java @@ -8,19 +8,26 @@ import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.ser.std.StdSerializer; +/** + * Jackson serializer for SimpleCredentials + * + * @author Alfredo Oliviero (ISTI-CNR) + * + */ + public class SimpleCredentialsSerializer extends StdSerializer { protected SimpleCredentialsSerializer(Class t) { super(t); } - public SimpleCredentialsSerializer(){ + public SimpleCredentialsSerializer() { super(SimpleCredentials.class, true); } - @Override - public void serialize(SimpleCredentials credentials, JsonGenerator jgen, SerializerProvider provider) throws IOException { + public void serialize(SimpleCredentials credentials, JsonGenerator jgen, SerializerProvider provider) + throws IOException { jgen.writeStartObject(); jgen.writeStringField("clientId", credentials.getClientID()); jgen.writeStringField("secret", "[*****]"); diff --git a/src/main/java/org/gcube/service/helloworld/services/AuthorizationService.java b/src/main/java/org/gcube/service/helloworld/services/AuthenticatedService.java similarity index 66% rename from src/main/java/org/gcube/service/helloworld/services/AuthorizationService.java rename to src/main/java/org/gcube/service/helloworld/services/AuthenticatedService.java index 4c877da..6a1c8ee 100644 --- a/src/main/java/org/gcube/service/helloworld/services/AuthorizationService.java +++ b/src/main/java/org/gcube/service/helloworld/services/AuthenticatedService.java @@ -10,13 +10,18 @@ import org.gcube.common.security.providers.SecretManagerProvider; import org.gcube.common.security.secrets.Secret; import org.gcube.smartgears.utils.InnerMethodName; +/** + * @author Lucio Le (ISTI - CNR) + * @author Alfredo Oliviero (ISTI - CNR) + */ + @Path("auth") -public class TestAuthorizationService { +public class AuthenticatedService { private static final String ALLOWED_ROLE = "myRole"; private static final String ALLOWED_ROLE_ORG = "OrganizationMember"; - - @AuthorizationControl(allowedRoles={ALLOWED_ROLE_ORG}) + + @AuthorizationControl(allowedRoles = { ALLOWED_ROLE_ORG }) @GET @Path("org_member") @Produces(MediaType.TEXT_PLAIN) @@ -25,10 +30,12 @@ public class TestAuthorizationService { Secret secret = SecretManagerProvider.get(); String userId = secret.getOwner().getId(); String context = secret.getContext(); - return String.format("User %s in context %s is authorized to execute this method because he has the correct role", userId,context); + return String.format( + "User %s in context %s is authorized to execute this method because he has the correct role", userId, + context); } - - @AuthorizationControl(allowedRoles={ALLOWED_ROLE}) + + @AuthorizationControl(allowedRoles = { ALLOWED_ROLE }) @GET @Path("") @Produces(MediaType.TEXT_PLAIN) @@ -37,7 +44,9 @@ public class TestAuthorizationService { Secret secret = SecretManagerProvider.get(); String userId = secret.getOwner().getId(); String context = secret.getContext(); - return String.format("User %s in context %s is authorized to execute this method because he has the correct role", userId,context); + return String.format( + "User %s in context %s is authorized to execute this method because he has the correct role", userId, + context); } - + } diff --git a/src/main/java/org/gcube/service/helloworld/services/HelloService.java b/src/main/java/org/gcube/service/helloworld/services/HelloService.java index 60f1181..f645d2d 100644 --- a/src/main/java/org/gcube/service/helloworld/services/HelloService.java +++ b/src/main/java/org/gcube/service/helloworld/services/HelloService.java @@ -13,7 +13,7 @@ import org.gcube.common.security.Owner; import org.gcube.common.security.providers.SecretManagerProvider; import org.gcube.common.security.secrets.Secret; import org.gcube.service.helloworld.manager.HelloWorldManager; -import org.gcube.service.helloworld.serializers.SmartGearSerializator; +import org.gcube.service.helloworld.serializers.ContextSerializator; import org.gcube.smartgears.ContextProvider; import org.gcube.smartgears.annotations.ManagedBy; import org.gcube.smartgears.context.container.ContainerContext; @@ -24,6 +24,12 @@ import org.slf4j.LoggerFactory; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; +/** + * + * @author Lucio Lelii (ISTI-CNR) + * @author Alfredo Oliviero (ISTI-CNR) + */ + @ManagedBy(HelloWorldManager.class) @Path("") public class HelloService { @@ -38,23 +44,23 @@ public class HelloService { Secret secret = SecretManagerProvider.get(); String userId = secret.getOwner().getId(); String context = secret.getContext(); - + String infrastructureName = ContextProvider.get().container().configuration().infrastructure(); - logger.info("caller id is {}",userId); - return String.format("Hello %s in context %s in infastructure %s -roles %s", userId,context, infrastructureName, secret.getOwner().getRoles()); + logger.info("caller id is {}", userId); + return String.format("Hello %s in context %s in infastructure %s -roles %s", userId, context, + infrastructureName, secret.getOwner().getRoles()); } @GET @Path("details") - @Produces({MediaType.APPLICATION_JSON}) + @Produces({ MediaType.APPLICATION_JSON }) public Response details() { InnerMethodName.set("details"); - + Map data = new HashMap<>(); - + Secret secret = SecretManagerProvider.get(); - String context = secret.getContext(); data.put("context", context); @@ -64,7 +70,7 @@ public class HelloService { ContainerContext container = ContextProvider.get().container(); data.put("container", container); - ObjectMapper objectMapper = SmartGearSerializator.getSerializer(); + ObjectMapper objectMapper = ContextSerializator.getSerializer(); try { String jsonData = objectMapper.writeValueAsString(data); @@ -75,5 +81,5 @@ public class HelloService { return Response.serverError().build(); } } - + } \ No newline at end of file diff --git a/src/main/java/org/gcube/service/helloworld/utils/RestUtils.java b/src/main/java/org/gcube/service/helloworld/utils/RestUtils.java index 5352d6c..9d707f6 100644 --- a/src/main/java/org/gcube/service/helloworld/utils/RestUtils.java +++ b/src/main/java/org/gcube/service/helloworld/utils/RestUtils.java @@ -5,15 +5,12 @@ import java.util.List; import java.util.Map; import org.gcube.common.security.Owner; -import org.gcube.service.helloworld.serializers.ContainerConfigurationSerializer; -import org.gcube.service.helloworld.serializers.ContainerContextSerializer; -import org.gcube.service.helloworld.serializers.SimpleCredentialsSerializer; -import org.gcube.smartgears.configuration.container.ContainerConfiguration; import org.gcube.smartgears.context.container.ContainerContext; -import org.gcube.smartgears.security.SimpleCredentials; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.module.SimpleModule; +/** + * + * @author Lucio Lelii (ISTI-CNR) + */ public class RestUtils { @@ -45,11 +42,11 @@ public class RestUtils { return data; } - public static Map getContainerDict(ContainerContext container){ + public static Map getContainerDict(ContainerContext container) { Map data = new HashMap<>(); data.put("id", container.id()); data.put("configuration", container.configuration()); - //data.put("lifecycle", container.lifecycle()); + // data.put("lifecycle", container.lifecycle()); data.put("properties", container.properties()); data.put("authorizationProvider", container.authorizationProvider()); return data;