uri-resolver-manager/src/main/java/org/gcube/portlets/user/uriresolvermanager/UriResolverManager.java

135 lines
4.1 KiB
Java
Raw Normal View History

/**
*
*/
package org.gcube.portlets.user.uriresolvermanager;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.gcube.portlets.user.uriresolvermanager.entity.ServiceParameter;
import org.gcube.portlets.user.uriresolvermanager.exception.IllegalArgumentException;
import org.gcube.portlets.user.uriresolvermanager.exception.UriResolverMapException;
import org.gcube.portlets.user.uriresolvermanager.readers.RuntimeResourceReader;
import org.gcube.portlets.user.uriresolvermanager.readers.UriResolverMapReader;
import org.gcube.portlets.user.uriresolvermanager.util.HttpCallerUtil;
import org.gcube.portlets.user.uriresolvermanager.util.UrlEncoderUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* @Oct 14, 2014
*
*/
public class UriResolverManager {
private UriResolverMapReader uriResolverMapReader;
private Map<String, String> applicationTypes;
private String scope;
private String applicationType;
public static final Logger logger = LoggerFactory.getLogger(UriResolverManager.class);
/**
*
* @param scope to read resource = "Uri-Resolver-Map";
* @param applicationType a key Application Type
* @throws Exception
*/
public UriResolverManager(String scope, String applicationType) throws UriResolverMapException, IllegalArgumentException{
try {
this.uriResolverMapReader = new UriResolverMapReader(scope);
this.applicationTypes = uriResolverMapReader.getApplicationTypes();
} catch (Exception e) {
throw new UriResolverMapException("Map Application Type - Resources not found in IS");
}
if(!this.applicationTypes.containsKey(applicationType)){
throw new IllegalArgumentException("Application type '"+applicationType +"' not found in Application Types: "+getApplicationTypes());
}
this.applicationType = applicationType;
this.scope = scope;
}
public String getLink(Map<String, String> parameters) throws IllegalArgumentException, UriResolverMapException{
String resourceName = this.applicationTypes.get(applicationType);
if(parameters==null){
throw new IllegalArgumentException("Input Map parameters is null");
}
try {
RuntimeResourceReader reader = new RuntimeResourceReader(scope, resourceName);
List<ServiceParameter> resourceParameters = reader.getServiceParameters();
//CHECK PARAMETERS
for (ServiceParameter serviceParameter : resourceParameters) {
if(serviceParameter.isMandatory()){
if(!parameters.containsKey(serviceParameter.getKey())){
throw new IllegalArgumentException("Mandatory service key (parameter) '"+serviceParameter.getKey() +"' not found into input map");
}
}
}
String baseURI = reader.getServiceBaseURI();
// String params = UrlEncoderUtil.encodeQuery(parameters);
// String uriRequest = reader.getServiceBaseURI()+"?"+params;
// logger.info("Created HTTP URI request: "+uriRequest);
baseURI = "http://pc-mangiacrapa.isti.cnr.it:8080/uri-resolver-1.2.0-SNAPSHOT/gis";
HttpCallerUtil httpCaller = new HttpCallerUtil(baseURI, "", "");
httpCaller.callGet("", parameters);
} catch (IllegalArgumentException e){
throw e;
} catch (Exception e) {
throw new UriResolverMapException("Runtime Resource reader exception");
}
return "";
}
/**
*
* @return the Application Types availables
*/
public Set<String> getApplicationTypes(){
return this.applicationTypes.keySet();
}
/**
*
* @return a map Application Type - Resource Name
*/
public Map<String, String> getCapabilities(){
return this.applicationTypes;
}
public static void main(String[] args) {
try {
UriResolverManager resolver = new UriResolverManager("/gcube", "GIS");
Map<String, String> params = new HashMap<String, String>();
params.put("gis-UUID", "12345");
params.put("scope", "/gcube");
resolver.getLink(params);
} catch (UriResolverMapException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}