Charset charset = url.getCharset();
Url mergedUrl = Url.parse(url.toString(), charset);
UrlEncoder urlEncoder = UrlEncoder.QUERY_INSTANCE;
Set<String> setParameters = new HashSet<String>();
int indexedCount = params.getIndexedCount();
if (indexedCount > 0)
{
String jsessionidString = null;
List<String> segments = mergedUrl.getSegments();
if (segments.size() > 0)
{
String lastSegment = segments.get(segments.size() - 1);
int jsessionidIndex = lastSegment.indexOf(";jsessionid=");
if (jsessionidIndex != -1)
{
segments.set(segments.size() - 1, lastSegment.substring(0, jsessionidIndex));
jsessionidString = lastSegment.substring(jsessionidIndex);
}
}
for (int i = 0; i < indexedCount; i++)
{
segments.add(params.get(i).toString());
}
if (jsessionidString != null)
{
segments.set(segments.size() - 1, segments.get(segments.size() - 1).concat(jsessionidString));
}
}
for (INamedParameters.NamedPair pair : params.getAllNamed())
{
String key = urlEncoder.encode(pair.getKey(), charset);
String value = urlEncoder.encode(pair.getValue(), charset);
if (setParameters.contains(key)) {
mergedUrl.addQueryParameter(key, value);
} else {
mergedUrl.setQueryParameter(key, value);