2016-10-03 12:21:52 +02:00
package org.gcube.informationsystem.icproxy.resources ;
import static org.gcube.resources.discovery.icclient.ICFactory.client ;
import static org.gcube.resources.discovery.icclient.ICFactory.clientFor ;
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor ;
2023-01-27 15:42:42 +01:00
import java.util.ArrayList ;
2016-10-03 12:21:52 +02:00
import java.util.List ;
2023-01-25 17:38:05 +01:00
import java.util.Objects ;
2016-10-03 12:21:52 +02:00
import javax.validation.constraints.NotNull ;
2023-01-27 15:42:42 +01:00
import javax.ws.rs.* ;
2016-10-03 12:21:52 +02:00
import javax.ws.rs.core.MediaType ;
import lombok.extern.slf4j.Slf4j ;
2023-01-25 17:38:05 +01:00
import org.gcube.common.resources.gcore.* ;
2023-01-27 15:42:42 +01:00
import org.gcube.common.resources.gcore.utils.Group ;
2016-10-03 12:21:52 +02:00
import org.gcube.common.scope.api.ScopeProvider ;
import org.gcube.resources.discovery.client.api.DiscoveryClient ;
import org.gcube.resources.discovery.client.queries.api.SimpleQuery ;
2023-01-25 17:38:05 +01:00
import org.gcube.resources.discovery.client.queries.impl.XQuery ;
2023-01-27 15:42:42 +01:00
import org.gcube.common.encryption.StringEncrypter ;
2016-10-03 12:21:52 +02:00
@Slf4j
@Path ( " ServiceEndpoint " )
public class ServiceEndpointResource {
2021-06-08 10:37:16 +02:00
@GET
@Path ( " /{category} " )
@Produces ( MediaType . APPLICATION_XML )
public List < ServiceEndpoint > retrieve ( @NotNull @PathParam ( " category " ) String resourceCategory ) {
log . info ( " ServiceEndpoint called with category {} in context {} " , resourceCategory , ScopeProvider . instance . get ( ) ) ;
DiscoveryClient < ServiceEndpoint > client = clientFor ( ServiceEndpoint . class ) ;
List < ServiceEndpoint > endpoints = client . submit ( getQuery ( resourceCategory ) ) ;
log . debug ( " retrieved resources are " + endpoints . size ( ) ) ;
return endpoints ;
}
2016-10-03 12:21:52 +02:00
2023-01-27 15:42:42 +01:00
// @GET
// @Path("/{category}/{name}")
// @Produces(MediaType.APPLICATION_XML)
// public List<ServiceEndpoint> retrieve(@NotNull @PathParam("name") String resourceName,
// @NotNull @PathParam("category") String resourceCategory) {
// log.info("ServiceEndpoint called with category {} and name {} in scope {}",resourceCategory, resourceName, ScopeProvider.instance.get());
//
// DiscoveryClient<ServiceEndpoint> client = clientFor(ServiceEndpoint.class);
//
// List<ServiceEndpoint> endpoints = client.submit(getQuery(resourceName, resourceCategory));
// log.debug("retrieved resources are "+endpoints.size());
// return endpoints;
// }
2016-10-03 12:21:52 +02:00
@GET
@Path ( " /{category}/{name} " )
@Produces ( MediaType . APPLICATION_XML )
2023-01-27 15:42:42 +01:00
public List < ServiceEndpoint > retrieve ( @NotNull @PathParam ( " name " ) String resourceName ,
@NotNull @PathParam ( " category " ) String resourceCategory , @QueryParam ( " decrypt " ) boolean isDecrypt ) {
2016-10-03 12:21:52 +02:00
log . info ( " ServiceEndpoint called with category {} and name {} in scope {} " , resourceCategory , resourceName , ScopeProvider . instance . get ( ) ) ;
DiscoveryClient < ServiceEndpoint > client = clientFor ( ServiceEndpoint . class ) ;
List < ServiceEndpoint > endpoints = client . submit ( getQuery ( resourceName , resourceCategory ) ) ;
2023-01-27 15:42:42 +01:00
if ( Objects . nonNull ( endpoints ) ) {
log . debug ( " retrieved resources are " + endpoints . size ( ) ) ;
if ( isDecrypt ) {
List < ServiceEndpoint > ses = new ArrayList < > ( endpoints . size ( ) ) ;
for ( ServiceEndpoint resource : endpoints ) {
ses . add ( decryptResource ( resource ) ) ;
}
return ses ;
}
}
return endpoints ;
2016-10-03 12:21:52 +02:00
}
2023-01-27 15:42:42 +01:00
// @GET
// @Path("/{category}/{name}/{ap}")
// @Produces(MediaType.TEXT_XML)
// public String retrieve(@NotNull @PathParam("name") String resourceName,
// @NotNull @PathParam("category") String resourceCategory,
// @NotNull @PathParam("ap") String accessPoint) {
// log.info("ServiceEndpoint called with category {}, name {} and accessPoint {} in scope {}",resourceCategory, resourceName, accessPoint, ScopeProvider.instance.get());
// XQuery query=queryFor(ServiceEndpoint.class);
// query.addCondition(String.format("$resource/Profile/Name/text() eq '%s'",resourceName));
// query.addCondition(String.format("$resource/Profile/Category/text() eq '%s'",resourceCategory));
// query.setResult("$resource/Profile/AccessPoint/Interface/Endpoint[@EntryName='"+accessPoint+"']/text()");
2023-01-25 17:38:05 +01:00
// DiscoveryClient<String> client = client();
2023-01-27 15:42:42 +01:00
// List<String> accessList= client.submit(query);
// if (Objects.nonNull(accessList))
// return accessList.get(0).toString();
2023-01-25 17:38:05 +01:00
// else
// log.warn("endpoint not found with following coordinates: {} {} and accesspoint: {}", resourceCategory, resourceName,accessPoint);
// return null;
2023-01-27 15:42:42 +01:00
// }
2023-01-25 17:38:05 +01:00
2016-10-03 12:21:52 +02:00
@GET
@Path ( " /{category}/{name}/Result/{result:([^$ \\ ?]+)} " )
@Produces ( MediaType . TEXT_XML )
public String retrieveCustom ( @NotNull @PathParam ( " name " ) String resourceName ,
@NotNull @PathParam ( " category " ) String resourceCategory , @NotNull @PathParam ( " result " ) String resultXPath ) {
log . info ( " ServiceEndpoint called with category {} and name {} and result {} in scope {} "
, resourceCategory , resourceName , resultXPath , ScopeProvider . instance . get ( ) ) ;
SimpleQuery query = getQuery ( resourceName , resourceCategory ) ;
if ( resultXPath . startsWith ( " / " ) )
query . setResult ( " $resource " + resultXPath ) ;
else
query . setResult ( " $resource/ " + resultXPath ) ;
DiscoveryClient < String > client = client ( ) ;
List < String > endpoints = client . submit ( query ) ;
StringBuilder builder = new StringBuilder ( " <Results> " ) ;
for ( String single : endpoints )
builder . append ( " <Result> " ) . append ( single . replaceAll ( " \ n " , " " ) ) . append ( " </Result> " ) ;
builder . append ( " </Results> " ) ;
log . debug ( " retrieved resources are " + endpoints . size ( ) ) ;
return builder . toString ( ) ;
}
private SimpleQuery getQuery ( String resourceName , String resourceCategory ) {
SimpleQuery query = queryFor ( ServiceEndpoint . class ) ;
query . addCondition ( String . format ( " $resource/Profile/Name/text() eq '%s' " , resourceName ) ) ;
query . addCondition ( String . format ( " $resource/Profile/Category/text() eq '%s' " , resourceCategory ) ) ;
return query ;
}
2021-06-08 10:37:16 +02:00
private SimpleQuery getQuery ( String resourceCategory ) {
SimpleQuery query = queryFor ( ServiceEndpoint . class ) ;
query . addCondition ( String . format ( " $resource/Profile/Category/text() eq '%s' " , resourceCategory ) ) ;
return query ;
}
2023-01-25 17:38:05 +01:00
2023-01-27 15:42:42 +01:00
private ServiceEndpoint decryptResource ( ServiceEndpoint resource ) {
Group < ServiceEndpoint . AccessPoint > aps = resource . profile ( ) . accessPoints ( ) ;
for ( ServiceEndpoint . AccessPoint ap : aps ) {
String decrypted = decryptString ( ap . password ( ) ) ;
String user = ap . username ( ) ;
ap . credentials ( decrypted , user ) ;
}
return resource ;
}
public static String decryptString ( String toDecrypt ) {
try {
return StringEncrypter . getEncrypter ( ) . decrypt ( toDecrypt ) ;
} catch ( Exception e ) {
throw new RuntimeException ( " Unable to decrypt : " + toDecrypt , e ) ;
2023-01-25 17:38:05 +01:00
}
}
2016-10-03 12:21:52 +02:00
}