context data serialization

This commit is contained in:
Alfredo Oliviero 2024-02-23 16:41:23 +01:00
parent ccda3995e0
commit 12d5b2a1ad
9 changed files with 261 additions and 36 deletions

View File

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

View File

@ -20,6 +20,7 @@ import org.slf4j.LoggerFactory;
* @author lucio
*
*/
public class HelloWorldManager implements ApplicationManager {
Logger logger = LoggerFactory.getLogger(HelloWorldManager.class);

View File

@ -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<ContainerConfiguration> {
protected ContainerConfigurationSerializer(Class<ContainerConfiguration> 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();
}
}

View File

@ -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<ContainerContext> {
protected ContainerContextSerializer(Class<ContainerContext> 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();
}
}

View File

@ -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<Owner> {
protected OwnerSerializer(Class<Owner> 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();
}
}

View File

@ -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<SimpleCredentials> {
protected SimpleCredentialsSerializer(Class<SimpleCredentials> 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();
}
}

View File

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

View File

@ -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<String, Object> data = new HashMap<>();
Secret secret = SecretManagerProvider.get();
Owner owner = secret.getOwner();
String userId = owner.getId();
String clientName = owner.getClientName();
String clientId = owner.getId();
List<String> 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<String, Object> 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();
}
}
}

View File

@ -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<String, Object> getUserDict(Owner owner) {
String userId = owner.getId();
String clientName = owner.getClientName();
String clientId = owner.getId();
List<String> 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<String, Object> 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<String, Object> getContainerDict(ContainerContext container){
Map<String, Object> 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;
}
}