added examples
This commit is contained in:
parent
c59fa0b7a8
commit
1cef40fbbd
|
@ -2,6 +2,8 @@ package org.gcube.service.helloworld.services;
|
||||||
|
|
||||||
import javax.ws.rs.GET;
|
import javax.ws.rs.GET;
|
||||||
import javax.ws.rs.Path;
|
import javax.ws.rs.Path;
|
||||||
|
import javax.ws.rs.Produces;
|
||||||
|
import javax.ws.rs.core.MediaType;
|
||||||
|
|
||||||
import org.gcube.common.authorization.control.annotations.AuthorizationControl;
|
import org.gcube.common.authorization.control.annotations.AuthorizationControl;
|
||||||
import org.gcube.common.security.providers.SecretManagerProvider;
|
import org.gcube.common.security.providers.SecretManagerProvider;
|
||||||
|
@ -12,9 +14,24 @@ import org.gcube.smartgears.utils.InnerMethodName;
|
||||||
public class AuthorizedMethods {
|
public class AuthorizedMethods {
|
||||||
|
|
||||||
private static final String ALLOWED_ROLE = "myRole";
|
private static final String ALLOWED_ROLE = "myRole";
|
||||||
|
private static final String ALLOWED_ROLE_ORG = "OrganizationMember";
|
||||||
|
|
||||||
|
@AuthorizationControl(allowedRoles={ALLOWED_ROLE_ORG})
|
||||||
|
@GET
|
||||||
|
@Path("org_member")
|
||||||
|
@Produces(MediaType.TEXT_PLAIN)
|
||||||
|
public String authorizedOrg() {
|
||||||
|
InnerMethodName.set("auth");
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
@AuthorizationControl(allowedRoles={ALLOWED_ROLE})
|
@AuthorizationControl(allowedRoles={ALLOWED_ROLE})
|
||||||
@GET
|
@GET
|
||||||
|
@Path("")
|
||||||
|
@Produces(MediaType.TEXT_PLAIN)
|
||||||
public String authorized() {
|
public String authorized() {
|
||||||
InnerMethodName.set("auth");
|
InnerMethodName.set("auth");
|
||||||
Secret secret = SecretManagerProvider.get();
|
Secret secret = SecretManagerProvider.get();
|
||||||
|
|
|
@ -1,10 +1,16 @@
|
||||||
package org.gcube.service.helloworld.services;
|
package org.gcube.service.helloworld.services;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.ws.rs.GET;
|
import javax.ws.rs.GET;
|
||||||
import javax.ws.rs.Path;
|
import javax.ws.rs.Path;
|
||||||
import javax.ws.rs.Produces;
|
import javax.ws.rs.Produces;
|
||||||
import javax.ws.rs.core.MediaType;
|
import javax.ws.rs.core.MediaType;
|
||||||
|
import javax.ws.rs.core.Response;
|
||||||
|
|
||||||
|
import org.gcube.common.security.Owner;
|
||||||
import org.gcube.common.security.providers.SecretManagerProvider;
|
import org.gcube.common.security.providers.SecretManagerProvider;
|
||||||
import org.gcube.common.security.secrets.Secret;
|
import org.gcube.common.security.secrets.Secret;
|
||||||
import org.gcube.service.helloworld.manager.HelloWorldManager;
|
import org.gcube.service.helloworld.manager.HelloWorldManager;
|
||||||
|
@ -14,13 +20,18 @@ import org.gcube.smartgears.utils.InnerMethodName;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
@ManagedBy(HelloWorldManager.class)
|
@ManagedBy(HelloWorldManager.class)
|
||||||
@Path("hello")
|
@Path("hello")
|
||||||
|
|
||||||
public class HelloService {
|
public class HelloService {
|
||||||
|
|
||||||
private final Logger logger = LoggerFactory.getLogger(HelloService.class);
|
private final Logger logger = LoggerFactory.getLogger(HelloService.class);
|
||||||
|
|
||||||
@GET
|
@GET
|
||||||
|
@Path("hello")
|
||||||
@Produces(MediaType.TEXT_PLAIN)
|
@Produces(MediaType.TEXT_PLAIN)
|
||||||
public String hello() {
|
public String hello() {
|
||||||
InnerMethodName.set("hello");
|
InnerMethodName.set("hello");
|
||||||
|
@ -30,7 +41,55 @@ public class HelloService {
|
||||||
|
|
||||||
String infrastructureName = ContextProvider.get().container().configuration().infrastructure();
|
String infrastructureName = ContextProvider.get().container().configuration().infrastructure();
|
||||||
logger.info("caller id is {}",userId);
|
logger.info("caller id is {}",userId);
|
||||||
return String.format("Hello %s in context %s in infastructure {}", userId,context, infrastructureName);
|
return String.format("Hello %s in context %s in infastructure {} -roles %s", userId,context, infrastructureName, secret.getOwner().getRoles());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
@GET
|
||||||
|
@Path("details")
|
||||||
|
@Produces({MediaType.APPLICATION_JSON})
|
||||||
|
public Response details() {
|
||||||
|
InnerMethodName.set("hello");
|
||||||
|
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;
|
||||||
|
try {
|
||||||
|
jsonData = objectMapper.writeValueAsString(data);
|
||||||
|
return Response.ok(jsonData).build();
|
||||||
|
|
||||||
|
} catch (JsonProcessingException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return Response.serverError().build();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue