2020-10-27 16:04:34 +01:00
|
|
|
package org.gcube.portlets.user.geoportaldataviewer.client.util;
|
|
|
|
|
2021-09-01 17:37:05 +02:00
|
|
|
// TODO: Auto-generated Javadoc
|
2020-10-29 15:18:14 +01:00
|
|
|
/**
|
|
|
|
* The Class URLUtil.
|
|
|
|
*
|
|
|
|
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
|
|
|
|
*
|
|
|
|
* Oct 29, 2020
|
|
|
|
*/
|
2020-10-27 16:04:34 +01:00
|
|
|
public class URLUtil {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the value of parameter.
|
|
|
|
*
|
|
|
|
* @param paramName the param name
|
|
|
|
* @param url the url
|
|
|
|
* @return the value of parameter
|
|
|
|
*/
|
|
|
|
public static String getValueOfParameter(String paramName, String url) {
|
|
|
|
// logger.trace("finding: "+wmsParam +" into "+url);
|
|
|
|
int index = url.toLowerCase().indexOf(paramName.toLowerCase()+"="); //ADDING CHAR "=" IN TAIL TO BE SECURE IT IS A PARAMETER
|
|
|
|
// logger.trace("start index of "+wmsParam+ " is: "+index);
|
|
|
|
String value = "";
|
|
|
|
if(index > -1){
|
|
|
|
int start = index + paramName.length()+1; //add +1 for char '='
|
|
|
|
String sub = url.substring(start, url.length());
|
|
|
|
int indexOfSeparator = sub.indexOf("&");
|
|
|
|
int end = indexOfSeparator!=-1?indexOfSeparator:sub.length();
|
|
|
|
value = sub.substring(0, end);
|
|
|
|
}else
|
|
|
|
return null;
|
|
|
|
|
|
|
|
// logger.trace("return value: "+value);
|
|
|
|
return value;
|
|
|
|
}
|
2020-10-29 15:18:14 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds the parameter to query string.
|
|
|
|
*
|
|
|
|
* @param key the key
|
|
|
|
* @param value the value
|
|
|
|
* @param prefixAmpersand the prefix ampersand
|
|
|
|
* @param suffixAmpersand the suffix ampersand
|
|
|
|
* @return the string
|
|
|
|
*/
|
|
|
|
public static String addParameterToQueryString(String key, String value, boolean prefixAmpersand, boolean suffixAmpersand) {
|
|
|
|
|
|
|
|
String queryParameter = "";
|
|
|
|
|
|
|
|
if(prefixAmpersand)
|
|
|
|
queryParameter+="&";
|
|
|
|
|
|
|
|
queryParameter+=key+"="+value;
|
|
|
|
|
|
|
|
if(suffixAmpersand)
|
|
|
|
queryParameter+="&";
|
|
|
|
|
|
|
|
return queryParameter;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-09-01 17:37:05 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the value of parameter.
|
|
|
|
*
|
|
|
|
* @param wmsParam the wms param
|
|
|
|
* @param wmsLink the wms link
|
|
|
|
* @param newValue the new value
|
|
|
|
* @param addIfNotExists the add if not exists
|
|
|
|
* @return the string
|
|
|
|
*/
|
|
|
|
public static String setValueOfParameter(String wmsParam, String wmsLink, String newValue, boolean addIfNotExists){
|
|
|
|
String toLowerWmsLink = wmsLink.toLowerCase();
|
|
|
|
String toLowerWmsParam = wmsParam.toLowerCase();
|
|
|
|
|
|
|
|
int index = toLowerWmsLink.indexOf(toLowerWmsParam+"="); //END WITH CHAR "=" TO BE SURE THAT IT IS A PARAMETER
|
|
|
|
if(index > -1){
|
|
|
|
int indexStartValue = index + toLowerWmsParam.length()+1; //add +1 for char '='
|
|
|
|
int indexOfSeparator = toLowerWmsLink.indexOf("&", indexStartValue); //GET THE FIRST "&" STARTING FROM INDEX VALUE
|
|
|
|
// logger.trace("indexOfSeparator index of "+wmsParam+ " is: "+indexOfSeparator);
|
|
|
|
int indexEndValue = indexOfSeparator!=-1?indexOfSeparator:toLowerWmsLink.length();
|
|
|
|
// logger.trace("end: "+indexEndValue);
|
|
|
|
return wmsLink.substring(0, indexStartValue) + newValue +wmsLink.substring(indexEndValue, wmsLink.length());
|
|
|
|
}else if (addIfNotExists){
|
|
|
|
wmsLink+="&"+wmsParam+"="+newValue;
|
|
|
|
}
|
|
|
|
// logger.trace("return value: "+value);
|
|
|
|
return wmsLink;
|
|
|
|
}
|
|
|
|
|
2020-10-29 15:18:14 +01:00
|
|
|
/**
|
|
|
|
* Extract value of parameter from URL.
|
|
|
|
*
|
|
|
|
* @param paramName the param name
|
|
|
|
* @param url the url
|
|
|
|
* @return the string
|
|
|
|
*/
|
|
|
|
public static String extractValueOfParameterFromURL(String paramName, String url) {
|
|
|
|
int index = url.toLowerCase().indexOf(paramName.toLowerCase() + "="); // ADDING CHAR "=" IN TAIL TO BE SURE THAT IT
|
|
|
|
// IS A PARAMETER
|
|
|
|
String value = "";
|
|
|
|
if (index > -1) {
|
|
|
|
|
|
|
|
int start = index + paramName.length() + 1; // add +1 for char '='
|
|
|
|
String sub = url.substring(start, url.length());
|
|
|
|
int indexOfSeparator = sub.indexOf("&");
|
|
|
|
int end = indexOfSeparator != -1 ? indexOfSeparator : sub.length();
|
|
|
|
value = sub.substring(0, end);
|
|
|
|
} else
|
|
|
|
return null;
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2020-10-27 16:04:34 +01:00
|
|
|
|
|
|
|
}
|