* @return The encoded url
*/
protected CharSequence encodeURL(final CharSequence url)
{
// Get the crypt implementation from the application
ICrypt urlCrypt = Application.get().getSecuritySettings().getCryptFactory().newCrypt();
if (urlCrypt != null)
{
// The url must have a query string, otherwise keep the url
// unchanged
final int pos = url.toString().indexOf('?');
if (pos > -1)
{
// The url's path
CharSequence urlPrefix = url.subSequence(0, pos);
// Extract the querystring
String queryString = url.subSequence(pos + 1, url.length()).toString();
// if the querystring starts with a parameter like
// "x=", than don't change the querystring as it
// has been encoded already
if (!queryString.startsWith("x="))
{
// The length of the encrypted string depends on the
// length of the original querystring. Let's try to
// make the querystring shorter first without loosing
// information.
queryString = shortenUrl(queryString).toString();
// encrypt the query string
String encryptedQueryString = urlCrypt.encryptUrlSafe(queryString);
encryptedQueryString = WicketURLEncoder.QUERY_INSTANCE.encode(encryptedQueryString);
// build the new complete url
return new AppendingStringBuffer(urlPrefix).append("?x=").append(