added URi Resolver

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/application-support-layer/applicationSupportLayerSocial@90331 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Massimiliano Assante 2014-01-21 16:30:25 +00:00
parent 75885be62e
commit eddb500739
4 changed files with 330 additions and 48 deletions

View File

@ -23,7 +23,11 @@ import org.gcube.resources.discovery.client.api.DiscoveryClient;
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author Massimiliano Assante, ISTI-CNR
*
*/
public class FTPManager {
private static final Logger _log = LoggerFactory.getLogger(FTPManager.class);
@ -48,7 +52,7 @@ public class FTPManager {
}
}
public static FTPManager getManager() {
public static FTPManager getInstance() {
if (singleton == null)
singleton = new FTPManager();
return singleton;

View File

@ -1,46 +0,0 @@
package org.gcube.applicationsupportlayer.social.storage;
import org.gcube.common.portal.PortalContext;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.contentmanagement.blobstorage.service.IClient;
import org.gcube.contentmanager.storageclient.wrapper.AccessType;
import org.gcube.contentmanager.storageclient.wrapper.MemoryType;
import org.gcube.contentmanager.storageclient.wrapper.StorageClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author Massimiliano Assante, ISTI-CNR
*
*/
public class StorageManager {
private static final Logger _log = LoggerFactory.getLogger(StorageManager.class);
private static final String STORAGE_OWNER = "gCubeSocialFramework";
private static StorageManager singleton;
private static IClient client;
private StorageManager() {
try {
//set the scope
String currScope = ScopeProvider.instance.get();
ScopeProvider.instance.set("/"+PortalContext.getConfiguration().getInfrastructureName());
client = new StorageClient(STORAGE_OWNER, AccessType.PRIVATE, MemoryType.PERSISTENT).getClient();
ScopeProvider.instance.set(currScope);
singleton = this;
_log.trace("Social Framework Storage initialized - OK");
} catch (Exception e) {
e.printStackTrace();
}
}
public static IClient getClient() {
if (singleton == null) {
singleton = new StorageManager();
}
return client;
}
}

View File

@ -0,0 +1,222 @@
/**
*
*/
package org.gcube.applicationsupportlayer.social.storage;
import static org.gcube.resources.discovery.icclient.ICFactory.clientFor;
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.gcube.common.portal.PortalContext;
import org.gcube.common.resources.gcore.ServiceEndpoint;
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.resources.discovery.client.api.DiscoveryClient;
import org.gcube.resources.discovery.client.queries.impl.XQuery;
/**
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* @Jun 26, 2013
*
*/
public class UriResolverReaderParameter {
//Base Address
// private String uri = "";
private String baseUri = "";
//Query URL parameter
private String smpParameter = "";
private String fileNameParameter = "";
private String contentTypeParameter = "";
private String query = "";
private boolean isAvailable = false;
public static Logger logger = Logger.getLogger(UriResolverReaderParameter.class);
/**
* @throws Exception
*
*/
public UriResolverReaderParameter() throws Exception {
ScopeProvider.instance.set("/"+PortalContext.getConfiguration().getInfrastructureName());
XQuery query = queryFor(ServiceEndpoint.class);
query.addCondition("$resource/Profile/Name/text() eq 'HTTP-URI-Resolver'").setResult("$resource/Profile/AccessPoint");
DiscoveryClient<AccessPoint> client = clientFor(AccessPoint.class);
List<AccessPoint> endpoints = client.submit(query);
if (endpoints.size() == 0)
throw new Exception("No Resolver available");
baseUri = endpoints.get(0)!=null?endpoints.get(0).address():"";
if(endpoints.get(0)!=null){
smpParameter = endpoints.get(0).propertyMap()!=null?endpoints.get(0).propertyMap().get("SMP_URI_parameter").value():"";
fileNameParameter = endpoints.get(0).propertyMap()!=null?endpoints.get(0).propertyMap().get("fileName_parameter").value():"";
contentTypeParameter = endpoints.get(0).propertyMap()!=null?endpoints.get(0).propertyMap().get("contentType_parameter").value():"";
}
// uriRequest = uri+"?"+smpParameter;
isAvailable = true;
//Query URL parameter
// System.out.println(endpoints.get(0).propertyMap().get("parameter").value());
}
/**
* Resolve - open stream with http get method
* @param smp
* @return
* @throws IOException
*/
public InputStream resolveAsInputStream(String smp, String fileName, String contentType) throws Exception{
String query = resolveAsUriRequest(smp, fileName, contentType, true);
URL url = new URL(query);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("GET");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("charset", "utf-8");
connection.connect();
return connection.getInputStream();
}
/**
*
* @param smp
* @param fileName
* @param contentType
* @param encodeParams - if true, translates a string into <code>application/x-www-form-urlencoded</code>
* format using a specific encoding scheme
* @return the http url to get file
* @throws Exception
*/
public String resolveAsUriRequest(String smp, String fileName, String contentType, boolean encodeParams) throws Exception{
if(smp==null || smp.isEmpty())
throw new Exception("smp url is null or empty");
if(!encodeParams){ //ENCODE URI
query = smpParameter+"="+smp;
if(fileName!=null && !fileName.isEmpty())
query+="&"+fileNameParameter+"="+fileName;
if(contentType!=null && !contentType.isEmpty())
query+="&"+contentTypeParameter+"="+contentType;
}
else{
Map<String, String> hashParameters = getHashParemeters(smp, fileName, contentType);
query = UrlEncoderUtil.encodeQuery(hashParameters);
}
String uriRequest = baseUri+"?"+query;
logger.trace("resolve url request: "+uriRequest);
return uriRequest;
}
/**
*
* @param smp
* @param fileName
* @param contentType
* @return
* @throws Exception
*/
public Map<String, String> getHashParemeters(String smp, String fileName, String contentType) throws Exception{
Map<String, String> hashParameters = new HashMap<String, String>();
if(smp==null || smp.isEmpty())
throw new Exception("smp url is null or empty");
hashParameters.put(smpParameter, smp);
if(fileName!=null && !fileName.isEmpty())
hashParameters.put(fileNameParameter, fileName);
if(contentType!=null && !contentType.isEmpty())
hashParameters.put(contentTypeParameter, contentType);
return hashParameters;
}
public boolean isAvailable() {
return isAvailable;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("UriResolverReaderParameter [baseUri=");
builder.append(baseUri);
builder.append(", smpParameter=");
builder.append(smpParameter);
builder.append(", fileNameParameter=");
builder.append(fileNameParameter);
builder.append(", contentTypeParameter=");
builder.append(contentTypeParameter);
builder.append(", isAvailable=");
builder.append(isAvailable);
builder.append("]");
return builder.toString();
}
public String getBaseUri() {
return baseUri;
}
public String getSmpParameter() {
return smpParameter;
}
public String getFileNameParameter() {
return fileNameParameter;
}
public String getContentTypeParameter() {
return contentTypeParameter;
}
public String getQuery() {
return query;
}
}

View File

@ -0,0 +1,102 @@
/**
*
*/
package org.gcube.applicationsupportlayer.social.storage;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Logger;
/**
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* @Jun 4, 2013
*
*/
public class UrlEncoderUtil {
public static String charset = "UTF-8";
public static Logger logger = Logger.getLogger(UrlEncoderUtil.class);
/**
*
* @param url
* @param parameters
* @return
*/
public static String encodeQuery(String... parameters){
String query = "";
for (String string : parameters) {
try {
query+=URLEncoder.encode(string, charset)+"&";
} catch (UnsupportedEncodingException e) {
logger.error(e);
return query;
}
}
return removeLastChar(query);
}
/**
*
* @param url
* @param parameters
* @return
*/
public static String encodeQuery(Map<String, String> parameters){
String query = "";
if(parameters==null)
return query;
for (String key : parameters.keySet()) {
try {
query+=String.format(key+"=%s", URLEncoder.encode(parameters.get(key), charset))+"&";
} catch (UnsupportedEncodingException e) {
logger.error(e);
return query;
}
}
return removeLastChar(query);
}
public static String removeLastChar(String string){
if(string == null)
return null;
if(string.length()>0)
return string.substring(0, string.length()-1);
return string;
}
public static void main(String[] args) {
// System.out.println(UrlEncoderUtil.encodeQuery("request=GetStyles", "layers=test Name", "service=WMS", "version=1.1.1"));
HashMap<String, String> parameters = new HashMap<String, String>();
parameters.put("request", "GetStyles");
parameters.put("layers", "test Name");
parameters.put("version", "1.1.1");
System.out.println(UrlEncoderUtil.encodeQuery(parameters));
}
}