253 lines
7.3 KiB
Java
253 lines
7.3 KiB
Java
/**
|
|
*
|
|
*/
|
|
package org.gcube.portlets.user.workspace.server.resolver;
|
|
|
|
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.resources.gcore.ServiceEndpoint;
|
|
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint;
|
|
import org.gcube.common.scope.api.ScopeProvider;
|
|
import org.gcube.portlets.user.workspace.server.shortener.UrlEncoderUtil;
|
|
import org.gcube.portlets.user.workspace.server.util.scope.ScopeUtilFilter;
|
|
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 storageIDParameter = "";
|
|
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(String scope) throws Exception {
|
|
|
|
logger.trace("UriResolverReaderParameter is istancing with scope: "+scope);
|
|
|
|
ScopeUtilFilter scopeUtil = new ScopeUtilFilter(scope,true);
|
|
|
|
ScopeProvider.instance.set(scopeUtil.getScopeRoot());
|
|
|
|
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");
|
|
|
|
//Base Address
|
|
|
|
//THE FIRST ACCESS POINT (endpoints.get(0)) IS SMP-URI, THE SECOND (endpoints.get(1)) IS SMP-ID
|
|
AccessPoint ap2 = endpoints.get(1);
|
|
baseUri = ap2!=null?endpoints.get(1).address():"";
|
|
|
|
if(ap2!=null){
|
|
|
|
storageIDParameter = ap2.propertyMap()!=null?ap2.propertyMap().get("SMP_ID_parameter").value():"";
|
|
fileNameParameter = ap2.propertyMap()!=null?ap2.propertyMap().get("fileName_parameter").value():"";
|
|
contentTypeParameter = ap2.propertyMap()!=null?ap2.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 storageID
|
|
* @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 storageID, String fileName, String contentType, boolean encodeParams) throws Exception{
|
|
|
|
if(storageID==null || storageID.isEmpty())
|
|
throw new Exception("storage ID is null or empty");
|
|
|
|
if(!encodeParams){ //ENCODE URI
|
|
query = storageIDParameter+"="+storageID;
|
|
|
|
if(fileName!=null && !fileName.isEmpty())
|
|
query+="&"+fileNameParameter+"="+fileName;
|
|
|
|
if(contentType!=null && !contentType.isEmpty())
|
|
query+="&"+contentTypeParameter+"="+contentType;
|
|
}
|
|
else{
|
|
Map<String, String> hashParameters = getHashParemeters(storageID, fileName, contentType);
|
|
query = UrlEncoderUtil.encodeQuery(hashParameters);
|
|
}
|
|
|
|
String uriRequest = baseUri+"?"+query;
|
|
|
|
logger.trace("resolve url request: "+uriRequest);
|
|
|
|
return uriRequest;
|
|
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param storageID
|
|
* @param fileName
|
|
* @param contentType
|
|
* @return
|
|
* @throws Exception
|
|
*/
|
|
public Map<String, String> getHashParemeters(String storageID, String fileName, String contentType) throws Exception{
|
|
|
|
Map<String, String> hashParameters = new HashMap<String, String>();
|
|
|
|
if(storageID==null || storageID.isEmpty())
|
|
throw new Exception("smp url is null or empty");
|
|
|
|
hashParameters.put(storageIDParameter, storageID);
|
|
|
|
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(", storageIDParameter=");
|
|
builder.append(storageIDParameter);
|
|
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;
|
|
}
|
|
/**
|
|
* @return the storageIDParameter
|
|
*/
|
|
public String getStorageIDParameter() {
|
|
return storageIDParameter;
|
|
}
|
|
|
|
public String getFileNameParameter() {
|
|
return fileNameParameter;
|
|
}
|
|
|
|
public String getContentTypeParameter() {
|
|
return contentTypeParameter;
|
|
}
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
String scope = "/gcube/devsec";
|
|
System.out.println(new UriResolverReaderParameter(scope).toString());
|
|
|
|
/*
|
|
UriResolverReaderParameter uriResolver = new UriResolverReaderParameter(scope);
|
|
|
|
String fileName = "Wikipedia_logo_silver.png";
|
|
|
|
|
|
String uriRequest = uriResolver.resolveAsUriRequest("smp://Wikipedia_logo_silver.png?5ezvFfBOLqaqBlwCEtAvz4ch5BUu1ag3yftpCvV+gayz9bAtSsnO1/sX6pemTKbDe0qbchLexXeWgGcJlskYE8td9QSDXSZj5VSl9kdN9SN0/LRYaWUZuP4Q1J7lEiwkU4GKPsiD6PDRVcT4QAqTEy5hSIbr6o4Y", fileName, "image/png", true);
|
|
|
|
System.out.println("uriRequest "+uriRequest);
|
|
*/
|
|
//
|
|
// InputStream is = uriResolver.resolve("smp://Wikipedia_logo_silver.png?5ezvFfBOLqaqBlwCEtAvz4ch5BUu1ag3yftpCvV+gayz9bAtSsnO1/sX6pemTKbDe0qbchLexXeWgGcJlskYE8td9QSDXSZj5VSl9kdN9SN0/LRYaWUZuP4Q1J7lEiwkU4GKPsiD6PDRVcT4QAqTEy5hSIbr6o4Y", fileName, "image/png");
|
|
// File file = new File(fileName);
|
|
//
|
|
// FileOutputStream out = new FileOutputStream(file);
|
|
//
|
|
//
|
|
// IOUtils.copy(is, out);
|
|
// is.close();
|
|
//
|
|
// out.close();
|
|
}
|
|
|
|
public String getQuery() {
|
|
return query;
|
|
}
|
|
|
|
}
|