diff --git a/README.md b/README.md index 466fb13..52de1f2 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,6 @@ HelloWorld service for smartgears4 ## Documentation -obtain {{TOKEN}} at https://next.dev.d4science.org/group/gcube/home start the docker container @@ -28,6 +27,15 @@ http://localhost:8080/helloworld/auth?gcube-token={{TOKEN}} (checks for myRole r http://localhost:8080/helloworld/auth/orm_member?gcube-token={{TOKEN}} (checks for OrganizationMember role) +### Authentication: + +#### GCUBE-TOKEN param + +obtain personal token at https://next.dev.d4science.org/group/gcube/home + +add + + ### DEBUG start the docker container in debug Mode 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 96ca3f8..c2d1659 100644 --- a/src/main/java/org/gcube/service/helloworld/manager/HelloWorldManager.java +++ b/src/main/java/org/gcube/service/helloworld/manager/HelloWorldManager.java @@ -20,6 +20,7 @@ import org.slf4j.LoggerFactory; * @author lucio * */ + public class HelloWorldManager implements ApplicationManager { Logger logger = LoggerFactory.getLogger(HelloWorldManager.class); diff --git a/src/main/java/org/gcube/service/helloworld/serializers/ContainerConfigurationSerializer.java b/src/main/java/org/gcube/service/helloworld/serializers/ContainerConfigurationSerializer.java new file mode 100644 index 0000000..366f334 --- /dev/null +++ b/src/main/java/org/gcube/service/helloworld/serializers/ContainerConfigurationSerializer.java @@ -0,0 +1,40 @@ +package org.gcube.service.helloworld.serializers; + +import java.io.IOException; + +import org.gcube.smartgears.configuration.container.ContainerConfiguration; + +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 { + protected ContainerConfigurationSerializer(Class t) { + super(t); + } + + public ContainerConfigurationSerializer(){ + super(ContainerConfiguration.class, true); + } + + + @Override + public void serialize(ContainerConfiguration configuration, JsonGenerator jgen, SerializerProvider provider) throws IOException { + jgen.writeStartObject(); + jgen.writeObjectField("mode", configuration.mode()); + jgen.writeObjectField("app", configuration.apps()); + jgen.writeObjectField("site", configuration.site()); + jgen.writeObjectField("infrastructure", configuration.infrastructure()); + jgen.writeObjectField("hostname", configuration.hostname()); + jgen.writeObjectField("port", configuration.port()); + jgen.writeObjectField("protocol", configuration.protocol()); + jgen.writeObjectField("authorizeChildrenContext", configuration.authorizeChildrenContext()); + jgen.writeObjectField("proxy", configuration.proxy()); + + jgen.writeObjectField("desc", configuration.toString()); + + + + jgen.writeEndObject(); + } +} diff --git a/src/main/java/org/gcube/service/helloworld/serializers/ContainerContextSerializer.java b/src/main/java/org/gcube/service/helloworld/serializers/ContainerContextSerializer.java new file mode 100644 index 0000000..fb27024 --- /dev/null +++ b/src/main/java/org/gcube/service/helloworld/serializers/ContainerContextSerializer.java @@ -0,0 +1,35 @@ +package org.gcube.service.helloworld.serializers; + +import java.io.IOException; + +import org.gcube.smartgears.context.container.ContainerContext; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.ser.std.StdSerializer; + +public class ContainerContextSerializer extends StdSerializer { + protected ContainerContextSerializer(Class t) { + super(t); + } + + public ContainerContextSerializer(){ + super(ContainerContext.class, true); + } + + + @Override + 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("properties", ccontext.properties()); + jgen.writeObjectField("authorizationProvider", ccontext.authorizationProvider()); + + jgen.writeObjectField("configuration", ccontext.configuration()); + jgen.writeObjectField("desc", ccontext.toString()); + + jgen.writeEndObject(); + } +} diff --git a/src/main/java/org/gcube/service/helloworld/serializers/OwnerSerializer.java b/src/main/java/org/gcube/service/helloworld/serializers/OwnerSerializer.java new file mode 100644 index 0000000..6b10acd --- /dev/null +++ b/src/main/java/org/gcube/service/helloworld/serializers/OwnerSerializer.java @@ -0,0 +1,41 @@ +package org.gcube.service.helloworld.serializers; + +import java.io.IOException; + +import org.gcube.common.security.Owner; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.ser.std.StdSerializer; + +public class OwnerSerializer extends StdSerializer { + + protected OwnerSerializer(Class t) { + super(t); + } + + public OwnerSerializer(){ + super(Owner.class, true); + } + + + @Override + public void serialize(Owner owner, JsonGenerator jgen, SerializerProvider provider) throws IOException { + jgen.writeStartObject(); + jgen.writeStringField("ownerId", owner.getId()); + jgen.writeStringField("clientName", owner.getClientName()); + jgen.writeArrayFieldStart("roles"); + for (String role : owner.getRoles()) { + jgen.writeString(role); + } + jgen.writeEndArray(); + jgen.writeStringField("email", owner.getEmail()); + jgen.writeStringField("firstName", owner.getFirstName()); + jgen.writeStringField("lastName", owner.getLastName()); + jgen.writeBooleanField("externalClient", owner.isExternalClient()); + jgen.writeStringField("contactPerson", owner.getClientName()); + jgen.writeStringField("contactOrganisation", owner.getContactOrganisation()); + + jgen.writeEndObject(); + } +} diff --git a/src/main/java/org/gcube/service/helloworld/serializers/SimpleCredentialsSerializer.java b/src/main/java/org/gcube/service/helloworld/serializers/SimpleCredentialsSerializer.java new file mode 100644 index 0000000..6560c00 --- /dev/null +++ b/src/main/java/org/gcube/service/helloworld/serializers/SimpleCredentialsSerializer.java @@ -0,0 +1,29 @@ +package org.gcube.service.helloworld.serializers; + +import java.io.IOException; + +import org.gcube.smartgears.security.SimpleCredentials; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.ser.std.StdSerializer; + +public class SimpleCredentialsSerializer extends StdSerializer { + + protected SimpleCredentialsSerializer(Class t) { + super(t); + } + + public SimpleCredentialsSerializer(){ + super(SimpleCredentials.class, true); + } + + + @Override + public void serialize(SimpleCredentials credentials, JsonGenerator jgen, SerializerProvider provider) throws IOException { + jgen.writeStartObject(); + jgen.writeStringField("clientId", credentials.getClientID()); + jgen.writeStringField("secret", "[*****]"); + jgen.writeEndObject(); + } +} diff --git a/src/main/java/org/gcube/service/helloworld/serializers/SmartGearSerializator.java b/src/main/java/org/gcube/service/helloworld/serializers/SmartGearSerializator.java new file mode 100644 index 0000000..f1553ed --- /dev/null +++ b/src/main/java/org/gcube/service/helloworld/serializers/SmartGearSerializator.java @@ -0,0 +1,30 @@ +package org.gcube.service.helloworld.serializers; + +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; + +public class SmartGearSerializator { + 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; + } + return serializer; + } + +} 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 e8ac67c..60f1181 100644 --- a/src/main/java/org/gcube/service/helloworld/services/HelloService.java +++ b/src/main/java/org/gcube/service/helloworld/services/HelloService.java @@ -1,7 +1,6 @@ package org.gcube.service.helloworld.services; import java.util.HashMap; -import java.util.List; import java.util.Map; import javax.ws.rs.GET; @@ -14,8 +13,10 @@ 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.smartgears.ContextProvider; import org.gcube.smartgears.annotations.ManagedBy; +import org.gcube.smartgears.context.container.ContainerContext; import org.gcube.smartgears.utils.InnerMethodName; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -24,8 +25,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; @ManagedBy(HelloWorldManager.class) -@Path("hello") - +@Path("") public class HelloService { private final Logger logger = LoggerFactory.getLogger(HelloService.class); @@ -41,50 +41,33 @@ public class HelloService { String infrastructureName = ContextProvider.get().container().configuration().infrastructure(); logger.info("caller id is {}",userId); - return String.format("Hello %s in context %s in infastructure {} -roles %s", userId,context, infrastructureName, secret.getOwner().getRoles()); + 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}) public Response details() { - InnerMethodName.set("hello"); + InnerMethodName.set("details"); + + Map data = new HashMap<>(); + Secret secret = SecretManagerProvider.get(); - Owner owner = secret.getOwner(); - - String userId = owner.getId(); - String clientName = owner.getClientName(); - - String clientId = owner.getId(); - List roles = owner.getRoles(); - String email = owner.getEmail(); - String firstName = owner.getFirstName(); - String lastName = owner.getLastName(); - boolean externalClient = owner.isExternalClient(); - - String contactPerson = owner.getContactPerson(); - String contactOrganisation = owner.getContactOrganisation(); - + String context = secret.getContext(); - - Map data = new HashMap<>(); - data.put("userid", userId); - data.put("clientName", clientName); - data.put("clientId", clientId); - data.put("roles", roles); - data.put("email", email); - data.put("firstName", firstName); - data.put("lastName", lastName); - data.put("externalClient", externalClient); - data.put("contactPerson", contactPerson); - data.put("contactOrganisation", contactOrganisation); data.put("context", context); - ObjectMapper objectMapper = new ObjectMapper(); - String jsonData; + Owner owner = secret.getOwner(); + data.put("owner", owner); + + ContainerContext container = ContextProvider.get().container(); + data.put("container", container); + + ObjectMapper objectMapper = SmartGearSerializator.getSerializer(); + try { - jsonData = objectMapper.writeValueAsString(data); + String jsonData = objectMapper.writeValueAsString(data); return Response.ok(jsonData).build(); } catch (JsonProcessingException e) { @@ -92,4 +75,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 new file mode 100644 index 0000000..5352d6c --- /dev/null +++ b/src/main/java/org/gcube/service/helloworld/utils/RestUtils.java @@ -0,0 +1,57 @@ +package org.gcube.service.helloworld.utils; + +import java.util.HashMap; +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; + +public class RestUtils { + + public static Map getUserDict(Owner owner) { + String userId = owner.getId(); + String clientName = owner.getClientName(); + + String clientId = owner.getId(); + List roles = owner.getRoles(); + String email = owner.getEmail(); + String firstName = owner.getFirstName(); + String lastName = owner.getLastName(); + boolean externalClient = owner.isExternalClient(); + + String contactPerson = owner.getContactPerson(); + String contactOrganisation = owner.getContactOrganisation(); + + Map data = new HashMap<>(); + data.put("userid", userId); + data.put("clientName", clientName); + data.put("clientId", clientId); + data.put("roles", roles); + data.put("email", email); + data.put("firstName", firstName); + data.put("lastName", lastName); + data.put("externalClient", externalClient); + data.put("contactPerson", contactPerson); + data.put("contactOrganisation", contactOrganisation); + return data; + } + + 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("properties", container.properties()); + data.put("authorizationProvider", container.authorizationProvider()); + return data; + } +}