resource-registry-publisher/src/main/java/org/gcube/informationsystem/resourceregistry/publisher/proxy/ResourceRegistryPublisherIm...

193 lines
6.0 KiB
Java

package org.gcube.informationsystem.resourceregistry.publisher.proxy;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.Random;
import org.gcube.common.authorization.client.Constants;
import org.gcube.common.authorization.library.AuthorizationEntry;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.resources.gcore.GCoreEndpoint;
import org.gcube.common.resources.gcore.GCoreEndpoint.Profile.Endpoint;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.informationsystem.impl.utils.Entities;
import org.gcube.informationsystem.model.embedded.Embedded;
import org.gcube.informationsystem.model.entity.Facet;
import org.gcube.informationsystem.model.entity.Resource;
import org.gcube.informationsystem.model.relation.ConsistsOf;
import org.gcube.informationsystem.model.relation.IsRelatedTo;
import org.gcube.informationsystem.resourceregistry.api.rest.EntityPath;
import org.gcube.resources.discovery.client.api.DiscoveryClient;
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
import org.gcube.resources.discovery.icclient.ICFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher {
private static final Logger logger = LoggerFactory.getLogger(ResourceRegistryPublisher.class);
private static final Random random;
protected final List<GCoreEndpoint> endpoints;
static {
random = new Random();
}
protected static List<GCoreEndpoint> getServiceEndpoint(){
SimpleQuery query = ICFactory.queryFor(GCoreEndpoint.class);
query.addCondition(String.format("$resource/Profile/ServiceClass/text() eq '%s'", org.gcube.informationsystem.resourceregistry.Constants.SERVICE_CLASS));
query.addCondition(String.format("$resource/Profile/ServiceName/text() eq '%s'", org.gcube.informationsystem.resourceregistry.Constants.SERVICE_NAME));
query.setResult("$resource");
DiscoveryClient<GCoreEndpoint> client = ICFactory.clientFor(GCoreEndpoint.class);
return client.submit(query);
}
protected ResourceRegistryPublisherImpl(){
this.endpoints = getServiceEndpoint();
};
private static int randInt(int min, int max) {
int randomNum = random.nextInt((max - min) + 1) + min;
return randomNum;
}
protected URL getBaseURL() throws MalformedURLException {
GCoreEndpoint gCoreEndpoint = endpoints.get(randInt(0, endpoints.size()-1));
Endpoint endpoint = gCoreEndpoint.profile().endpointMap().get(org.gcube.informationsystem.resourceregistry.Constants.SERVICE_ENTRY_NAME);
return endpoint.uri().toURL();
}
protected AuthorizationEntry getAuthorizationEntry() {
try{
AuthorizationEntry info = Constants.authorizationService().get(SecurityTokenProvider.instance.get());
logger.info("Context is {} ",info.getContext());
return info;
}catch(Exception e){
String error = "Error retreiving security token";
logger.warn(error,e);
throw new RuntimeException(error,e);
}
}
private HttpURLConnection makeRequest(URL url, String method) throws Exception{
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
if (SecurityTokenProvider.instance.get()==null) {
if(ScopeProvider.instance.get()==null){
throw new RuntimeException("Null Token and Scope. Please set your token first.");
}
connection.setRequestProperty("gcube-scope", ScopeProvider.instance.get());
}else{
connection.setRequestProperty(Constants.TOKEN_HEADER_ENTRY, SecurityTokenProvider.instance.get());
}
connection.setRequestMethod(method);
connection.setDoOutput(true);
return connection;
}
protected HttpURLConnection getHttpURLConnection(String path, String method, Map<String,String> paramenter) throws Exception {
URL url = new URL(getBaseURL(), path);
HttpURLConnection connection = makeRequest(url, method);
return connection;
}
@Override
public <E extends Embedded> E createEmbedded(E embedded) {
return null;
}
@Override
public <E extends Embedded> E updateEmbedded(E embedded) {
// TODO Auto-generated method stub
return null;
}
@Override
public <E extends Embedded> E deleteEmbedded(E embedded) {
// TODO Auto-generated method stub
return null;
}
@Override
public <F extends Facet> F createFacet(F facet) {
String marshalledFacet = Entities.marshal(facet);
String path = EntityPath.ENTITY_PATH_PART + "/" + EntityPath.FACET_PATH_PART + "/" + facet.NAME + "?" + EntityPath.DEFINITION_PARAM + "=" + marshalledFacet;
HttpURLConnection connection = getHttpURLConnection(path,);
// TODO Auto-generated method stub
return null;
}
@Override
public <F extends Facet> F updateFacet(F facet) {
// TODO Auto-generated method stub
return null;
}
@Override
public <F extends Facet> F deleteFacet(F facet) {
// TODO Auto-generated method stub
return null;
}
@Override
public <R extends Resource> R createResource(R resource) {
// TODO Auto-generated method stub
return null;
}
@Override
public <R extends Resource> R deleteResource(R resource) {
// TODO Auto-generated method stub
return null;
}
@Override
public <C extends ConsistsOf<Resource, Facet>> C createConsistsOf(
C consistsOf) {
// TODO Auto-generated method stub
return null;
}
@Override
public <C extends ConsistsOf<Resource, Facet>> C updateConsistsOf(
C consistsOf) {
// TODO Auto-generated method stub
return null;
}
@Override
public <C extends ConsistsOf<Resource, Facet>> C deleteConsistsOf(
C consistsOf) {
// TODO Auto-generated method stub
return null;
}
@Override
public <I extends IsRelatedTo<Resource, Resource>> I create(I isRelatedTo) {
// TODO Auto-generated method stub
return null;
}
@Override
public <I extends IsRelatedTo<Resource, Resource>> I update(I isRelatedTo) {
// TODO Auto-generated method stub
return null;
}
@Override
public <I extends IsRelatedTo<Resource, Resource>> I delete(I isRelatedTo) {
// TODO Auto-generated method stub
return null;
}
}