performfish-analytics-portlet/src/main/java/org/gcube/portlets/user/performfishanalytics/server/util/UrlEncoderUtil.java

96 lines
1.7 KiB
Java

/**
*
*/
package org.gcube.portlets.user.performfishanalytics.server.util;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
/**
* The Class UrlEncoderUtil.
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* Feb 19, 2015
*/
public class UrlEncoderUtil {
public static String charset = "UTF-8";
public static Logger logger = Logger.getLogger(UrlEncoderUtil.class);
// /**
// * Encode query.
// *
// * @param parameters the parameters
// * @return the string
// */
// 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);
// }
/**
* Encode query.
*
* @param parameters the parameters
* @return the string
*/
public static String encodeQuery(Map<String, List<String>> parameters){
String query = "";
if(parameters==null)
return query;
for (String key : parameters.keySet()) {
try {
for (String value : parameters.get(key)) {
query+=String.format(key+"=%s", URLEncoder.encode(value, charset))+"&";
}
} catch (UnsupportedEncodingException e) {
logger.error(e);
return query;
}
}
return removeLastChar(query);
}
/**
* Removes the last char.
*
* @param string the string
* @return the string
*/
public static String removeLastChar(String string){
if(string == null)
return null;
if(string.length()>0)
return string.substring(0, string.length()-1);
return string;
}
}