* @throws JspException
*/
public static String addParams(String url, Map params, String encoding)
throws JspException
{
InternalStringBuilder urlBuffer = new InternalStringBuilder(url);
try {
// Add dynamic parameters if requested
if ((params != null) && (params.size() > 0)) {
// Add the required request parameters
boolean question = url.indexOf('?') >= 0;
Iterator keys = params.keySet().iterator();
while (keys.hasNext()) {
String key = (String) keys.next();
Object value = params.get(key);
if (value == null) {
if (!question) {
urlBuffer.append('?');
question = true;
}
else {
urlBuffer.append("&");
}
urlBuffer.append(URLCodec.encode(key, encoding));
urlBuffer.append('='); // Interpret null as "no value"
}
else if (value instanceof String) {
if (!question) {
urlBuffer.append('?');
question = true;
}
else {
urlBuffer.append("&");
}
urlBuffer.append(URLCodec.encode(key, encoding));
urlBuffer.append('=');
urlBuffer.append(URLCodec.encode((String) value, encoding));
}
else if (value instanceof String[]) {
String values[] = (String[]) value;
for (int i = 0; i < values.length; i++) {
if (!question) {
urlBuffer.append('?');
question = true;
}
else {
urlBuffer.append("&");
}
urlBuffer.append(URLCodec.encode(key, encoding));
urlBuffer.append('=');
urlBuffer.append(URLCodec.encode(values[i], encoding));
}
}
else /* Convert other objects to a string */ {
if (!question) {
urlBuffer.append('?');
question = true;
}
else {
urlBuffer.append("&");
}
urlBuffer.append(URLCodec.encode(key, encoding));
urlBuffer.append('=');
urlBuffer.append(URLCodec.encode(value.toString(),
encoding));
}
}
}
}
catch (UnsupportedEncodingException uee) {
uee.printStackTrace();
throw new JspException("Unsupported Encoding" + encoding, uee);
}
return urlBuffer.toString();
}