ckan-util-library/src/main/java/org/gcube/datacatalogue/ckanutillibrary/utils/UtilMethods.java

92 lines
2.3 KiB
Java
Raw Normal View History

package org.gcube.datacatalogue.ckanutillibrary.utils;
import java.net.HttpURLConnection;
import java.net.URL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Some utility methods used within the library.
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
*/
public class UtilMethods {
private static final Logger logger = LoggerFactory.getLogger(UtilMethods.class);
/**
* Ckan username has _ instead of . (that is, costantino.perciante -> costantino_perciante)
* @param owner
* @return
*/
public static String fromUsernameToCKanUsername(String username){
if(username == null)
return null;
return username.trim().replaceAll("\\.", "_");
}
/**
* Generate the catalogue's dataset name from its title
* @param title
* @return
*/
public static String fromProductTitleToName(String title) {
if(title == null)
return null;
String regexTitleNameTransform = "[^A-Za-z0-9_-]";
return title.trim().replaceAll(regexTitleNameTransform, "_").replaceAll("_+", "_").toLowerCase();
}
/**
* Convert a display group name to group id
* @param groupName
* @return
*/
public static String fromGroupTitleToName(String groupName){
if(groupName == null)
return null;
String regexGroupNameTransform = "[^A-Za-z0-9_]";
return groupName.trim().replaceAll(regexGroupNameTransform, "_").replaceAll("_+", "_").toLowerCase();
}
/**
* Utility method to check if a something at this url actually exists
* @param URLName
* @return
*/
public static boolean resourceExists(String URLName){
if(URLName == null || URLName.isEmpty())
return false;
try {
HttpURLConnection.setFollowRedirects(true);
HttpURLConnection con = (HttpURLConnection) new URL(URLName).openConnection();
con.setRequestMethod("HEAD");
logger.debug("Return code is " + con.getResponseCode());
return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
}
catch (Exception e) {
logger.error("Exception while checking url", e);
return false;
}
}
/**
* Builds a string made of key + scope
* @param key
* @param scope
* @return
*/
public static String concatenateSessionKeyScope(String key, String scope){
if(key == null || scope == null)
throw new IllegalArgumentException("Key or scope null");
return key.concat(scope);
}
}