96 lines
3.2 KiB
Java
96 lines
3.2 KiB
Java
package org.gcube.service.helloworld.services;
|
|
|
|
import java.util.List;
|
|
|
|
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint;
|
|
import org.gcube.service.helloworld.HelloWorldManager;
|
|
import org.gcube.service.helloworld.serializers.CustomSerializator;
|
|
import org.gcube.service.helloworld.utils.InfrastrctureServiceClient;
|
|
import org.gcube.smartgears.annotations.ManagedBy;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
import jakarta.ws.rs.DefaultValue;
|
|
import jakarta.ws.rs.GET;
|
|
import jakarta.ws.rs.NotFoundException;
|
|
import jakarta.ws.rs.Path;
|
|
import jakarta.ws.rs.Produces;
|
|
import jakarta.ws.rs.QueryParam;
|
|
import jakarta.ws.rs.core.Response;
|
|
|
|
/**
|
|
* service example that shows how to query IS and how to access Keycloak
|
|
*
|
|
* @author Alfredo Oliviero (ISTI - CNR)
|
|
*/
|
|
|
|
@ManagedBy(HelloWorldManager.class)
|
|
@Path("is")
|
|
public class IsService {
|
|
|
|
private final static Logger logger = LoggerFactory.getLogger(IsService.class);
|
|
|
|
private final static String RUNTIME_RESOURCE_NAME = "IAM";
|
|
private final static String CATEGORY = "Service";
|
|
private final static String REALM_D4S = "d4science";
|
|
private final static boolean IS_ROOT_SERVICE = true;
|
|
|
|
public static String getClientIdContext(String context) {
|
|
return context.replace("/", "%2F");
|
|
}
|
|
|
|
@GET
|
|
@Path("/test")
|
|
@Produces({ "application/json;charset=UTF-8", "application/vnd.api+json" })
|
|
public Response test(
|
|
|
|
@QueryParam("resource") @DefaultValue(RUNTIME_RESOURCE_NAME) String resourceName,
|
|
@QueryParam("category") @DefaultValue(CATEGORY) String categoryName,
|
|
@QueryParam("realm") @DefaultValue(REALM_D4S) String realm,
|
|
@QueryParam("is_root") @DefaultValue("" + IS_ROOT_SERVICE) boolean is_root) {
|
|
|
|
return this.queryIS(resourceName, categoryName, realm, is_root);
|
|
}
|
|
|
|
@GET
|
|
@Path("/query")
|
|
@Produces({ "application/json;charset=UTF-8", "application/vnd.api+json" })
|
|
public Response queryIS(
|
|
|
|
@QueryParam("resource") String resourceName,
|
|
@QueryParam("category") String categoryName,
|
|
@QueryParam("realm") @DefaultValue(REALM_D4S) String realm,
|
|
@QueryParam("is_root") @DefaultValue("false") boolean is_root) {
|
|
try {
|
|
|
|
List<AccessPoint> accessPoints = InfrastrctureServiceClient.getAccessPointsFromIS(
|
|
resourceName,
|
|
categoryName,
|
|
realm,
|
|
is_root);
|
|
|
|
if (accessPoints.size() == 0) {
|
|
String error_log = "Unable to retrieve service endpoint " + REALM_D4S;
|
|
|
|
logger.error(error_log);
|
|
throw new NotFoundException(error_log);
|
|
}
|
|
|
|
ObjectMapper objectMapper = CustomSerializator.getSerializer();
|
|
|
|
String jsonData = objectMapper.writeValueAsString(accessPoints);
|
|
return Response.ok(jsonData).build();
|
|
|
|
} catch (JsonProcessingException e) {
|
|
e.printStackTrace();
|
|
return Response.serverError().build();
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
return Response.serverError().build();
|
|
}
|
|
}
|
|
}
|