2016-06-23 12:49:45 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
package org.gcube.portlets.gcubeckan.gcubeckandatacatalog.shared;
|
|
|
|
|
|
|
|
import java.io.Serializable;
|
2017-01-25 18:30:53 +01:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map.Entry;
|
|
|
|
import java.util.Set;
|
2016-06-23 12:49:45 +02:00
|
|
|
|
2016-06-29 11:25:46 +02:00
|
|
|
import org.gcube.portlets.gcubeckan.gcubeckandatacatalog.server.GcubeCkanDataCatalogServiceImpl;
|
|
|
|
|
2016-06-23 12:49:45 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The Class CkanConnectorAccessPoint.
|
|
|
|
*
|
|
|
|
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
|
|
|
|
* Jun 23, 2016
|
|
|
|
*/
|
|
|
|
public class CkanConnectorAccessPoint implements Serializable{
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
private static final long serialVersionUID = 3771903616375335843L;
|
|
|
|
|
2016-06-29 11:25:46 +02:00
|
|
|
public static final String URL_PATH_SEPARATOR = "/";
|
|
|
|
|
2016-11-04 13:01:35 +01:00
|
|
|
public static final String LIST_OF_VRES_PARAMETER = "listOfVres";
|
|
|
|
public static final String GCUBE_TOKEN_PARAMETER = "gcube-token";
|
2016-06-23 12:49:45 +02:00
|
|
|
private String baseUrl;
|
|
|
|
private String pathInfoParameter;
|
|
|
|
private String queryStringParameters;
|
|
|
|
private String gcubeTokenValue;
|
2017-01-25 18:30:53 +01:00
|
|
|
private Map<String, String> mapVresRoles;
|
2016-07-01 16:51:51 +02:00
|
|
|
private String serverviceContext;
|
2016-06-23 12:49:45 +02:00
|
|
|
|
|
|
|
/**
|
2016-06-23 14:57:20 +02:00
|
|
|
* Instantiates a new ckan connector access point.
|
2016-06-23 12:49:45 +02:00
|
|
|
*/
|
|
|
|
public CkanConnectorAccessPoint() {
|
|
|
|
}
|
|
|
|
|
2016-07-01 16:51:51 +02:00
|
|
|
|
2016-06-23 12:49:45 +02:00
|
|
|
/**
|
|
|
|
* Instantiates a new ckan connector access point.
|
|
|
|
*
|
|
|
|
* @param baseUrl the base url
|
2016-07-01 16:51:51 +02:00
|
|
|
* @param serverContext the server context
|
2016-06-23 12:49:45 +02:00
|
|
|
*/
|
2016-07-01 16:51:51 +02:00
|
|
|
public CkanConnectorAccessPoint(String baseUrl, String servericeContext){
|
2016-06-23 12:49:45 +02:00
|
|
|
this.baseUrl = baseUrl;
|
2016-07-01 16:51:51 +02:00
|
|
|
this.serverviceContext = servericeContext;
|
2016-06-23 12:49:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds the generic parameter.
|
|
|
|
*
|
|
|
|
* @param key the key
|
|
|
|
* @param value the value
|
|
|
|
*/
|
|
|
|
public void addGenericParameter(String key, String value){
|
|
|
|
queryStringParameters=queryStringParameters==null?key+"="+value:queryStringParameters+"&"+key+"="+value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds the query string.
|
|
|
|
*
|
|
|
|
* @param queryString the query string
|
|
|
|
*/
|
|
|
|
public void addQueryString(String queryString){
|
|
|
|
|
|
|
|
if(queryString==null || queryString.isEmpty())
|
|
|
|
return;
|
|
|
|
|
2016-06-29 11:25:46 +02:00
|
|
|
if(queryString.startsWith("&") || queryString.startsWith("?")){
|
|
|
|
queryString = queryString.substring(1, queryString.length()); //removes '&' or '?'
|
2016-06-23 12:49:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
queryStringParameters=queryStringParameters==null?queryString:queryStringParameters+"&"+queryString;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds the gube token.
|
|
|
|
*
|
|
|
|
* @param value the value
|
|
|
|
*/
|
|
|
|
public void addGubeToken(String value){
|
|
|
|
this.gcubeTokenValue = value;
|
|
|
|
addGenericParameter(GCUBE_TOKEN_PARAMETER, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds the list of vr es.
|
|
|
|
*
|
|
|
|
* @param listVREs the list vr es
|
|
|
|
*/
|
2017-01-25 18:30:53 +01:00
|
|
|
public void addListOfVREs(Map<String, String> listVREs){
|
|
|
|
this.mapVresRoles = listVREs;
|
2016-06-23 12:49:45 +02:00
|
|
|
String vres = "";
|
|
|
|
if(listVREs!=null && listVREs.size()>0){
|
2017-01-25 18:30:53 +01:00
|
|
|
Set<Entry<String, String>> set = listVREs.entrySet();
|
|
|
|
for (Entry<String, String> entry : set) {
|
|
|
|
vres += entry.getKey().toLowerCase() + "|" + entry.getValue() + ",";
|
2016-06-23 12:49:45 +02:00
|
|
|
}
|
2017-01-25 18:30:53 +01:00
|
|
|
vres = vres.substring(0, vres.length()-1); //remove last "," and to lower case. A CKAN Organization ID must be lower case
|
2016-06-23 12:49:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(vres.length()>0){
|
|
|
|
addGenericParameter(LIST_OF_VRES_PARAMETER, vres);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds the path info.
|
|
|
|
*
|
|
|
|
* @param pathInfo the path info
|
|
|
|
*/
|
|
|
|
public void addPathInfo(String pathInfo){
|
|
|
|
pathInfoParameter = pathInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds the uri to contact the CKAN Connector.
|
|
|
|
*
|
|
|
|
* @return the string
|
|
|
|
*/
|
|
|
|
public String buildURI(){
|
|
|
|
|
2016-06-23 14:57:20 +02:00
|
|
|
String path = "";
|
|
|
|
String query = "";
|
2016-06-29 11:25:46 +02:00
|
|
|
path = checkURLPathSeparator(pathInfoParameter, true, false);
|
|
|
|
query = checkNullString(queryStringParameters);
|
2016-07-01 16:51:51 +02:00
|
|
|
return getBaseUrlWithContext()+path+"?"+query;
|
2016-06-23 12:49:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-07-01 16:51:51 +02:00
|
|
|
* Gets the base url with context.
|
2016-06-23 14:57:20 +02:00
|
|
|
*
|
2016-07-01 16:51:51 +02:00
|
|
|
* @return the base url with context
|
2016-06-23 12:49:45 +02:00
|
|
|
*/
|
2016-07-01 16:51:51 +02:00
|
|
|
public String getBaseUrlWithContext() {
|
2016-06-23 12:49:45 +02:00
|
|
|
|
2016-07-01 16:51:51 +02:00
|
|
|
return baseUrl+serverviceContext;
|
2016-06-23 12:49:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2016-06-23 14:57:20 +02:00
|
|
|
* Gets the path info parameter.
|
|
|
|
*
|
2016-06-23 12:49:45 +02:00
|
|
|
* @return the pathInfoParameter
|
|
|
|
*/
|
|
|
|
public String getPathInfoParameter() {
|
|
|
|
|
|
|
|
return pathInfoParameter;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2016-06-23 14:57:20 +02:00
|
|
|
* Gets the query string parameters.
|
|
|
|
*
|
2016-06-23 12:49:45 +02:00
|
|
|
* @return the queryStringParameters
|
|
|
|
*/
|
|
|
|
public String getQueryStringParameters() {
|
|
|
|
|
|
|
|
return queryStringParameters;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2016-06-23 14:57:20 +02:00
|
|
|
* Gets the gcube token value.
|
|
|
|
*
|
2016-06-23 12:49:45 +02:00
|
|
|
* @return the gcubeTokenValue
|
|
|
|
*/
|
|
|
|
public String getGcubeTokenValue() {
|
|
|
|
|
|
|
|
return gcubeTokenValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2016-06-23 14:57:20 +02:00
|
|
|
* Gets the list of v res.
|
|
|
|
*
|
2016-06-23 12:49:45 +02:00
|
|
|
* @return the listOfVRes
|
|
|
|
*/
|
2017-01-25 18:30:53 +01:00
|
|
|
public Map<String, String> getListOfVRes() {
|
2016-06-23 12:49:45 +02:00
|
|
|
|
2017-01-25 18:30:53 +01:00
|
|
|
return mapVresRoles;
|
2016-06-23 12:49:45 +02:00
|
|
|
}
|
|
|
|
|
2016-06-29 11:25:46 +02:00
|
|
|
/**
|
|
|
|
* Check url path separator.
|
|
|
|
*
|
|
|
|
* @param url the url
|
|
|
|
* @param head - checks the {@link GcubeCkanDataCatalogServiceImpl.URL_PATH_SEPARATOR} in head adding if do not exist
|
|
|
|
* @param tail - checks the {@link GcubeCkanDataCatalogServiceImpl.URL_PATH_SEPARATOR} in tail adding if do not exist
|
|
|
|
* @return the string - if null return an empty string otherwise a string with {@link GcubeCkanDataCatalogServiceImpl.URL_PATH_SEPARATOR}
|
|
|
|
*/
|
|
|
|
public static String checkURLPathSeparator(String url, boolean head, boolean tail){
|
|
|
|
|
|
|
|
if(url!=null && url.length()>0){
|
|
|
|
if(head)
|
|
|
|
url=url.startsWith(URL_PATH_SEPARATOR)?url:URL_PATH_SEPARATOR+url;
|
|
|
|
if(tail)
|
|
|
|
url=url.endsWith(URL_PATH_SEPARATOR)?url:url+URL_PATH_SEPARATOR;
|
|
|
|
}else
|
|
|
|
url = "";
|
|
|
|
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-01 16:51:51 +02:00
|
|
|
/**
|
|
|
|
* Gets the base url.
|
|
|
|
*
|
|
|
|
* @return the baseUrl
|
|
|
|
*/
|
|
|
|
public String getBaseUrl() {
|
|
|
|
|
|
|
|
return baseUrl;
|
|
|
|
}
|
|
|
|
|
2016-06-29 11:25:46 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check null string.
|
|
|
|
*
|
|
|
|
* @param value the value
|
|
|
|
* @return the string if is not null otherwise an empty string
|
|
|
|
*/
|
|
|
|
public static String checkNullString(String value){
|
|
|
|
|
|
|
|
if(value==null)
|
|
|
|
return "";
|
|
|
|
else
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2016-06-23 12:49:45 +02:00
|
|
|
/* (non-Javadoc)
|
|
|
|
* @see java.lang.Object#toString()
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
|
|
|
|
StringBuilder builder = new StringBuilder();
|
|
|
|
builder.append("CkanConnectorAccessPoint [baseUrl=");
|
|
|
|
builder.append(baseUrl);
|
|
|
|
builder.append(", pathInfoParameter=");
|
|
|
|
builder.append(pathInfoParameter);
|
|
|
|
builder.append(", queryStringParameters=");
|
|
|
|
builder.append(queryStringParameters);
|
|
|
|
builder.append(", gcubeTokenValue=");
|
|
|
|
builder.append(gcubeTokenValue);
|
|
|
|
builder.append(", listOfVRes=");
|
2017-01-25 18:30:53 +01:00
|
|
|
builder.append(mapVresRoles);
|
2016-07-01 16:51:51 +02:00
|
|
|
builder.append(", context=");
|
|
|
|
builder.append(serverviceContext);
|
2016-06-23 12:49:45 +02:00
|
|
|
builder.append("]");
|
|
|
|
return builder.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|