/*
 * $Id: BookmarkablePageRequestTargetUrlCodingStrategy.java,v 1.1 2005/12/10
 * 21:28:56 eelco12 Exp $ $Revision$ $Date$
 * 
 * ==============================================================================
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package wicket.request.target.coding;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import wicket.Application;
import wicket.PageParameters;

/**
 * Abstract class for mount encoders that uses paths and forward slashes.
 * 
 * @author Eelco Hillenius
 * @author Igor Vaynberg (ivaynberg)
 */
public abstract class AbstractRequestTargetUrlCodingStrategy
		implements
			IRequestTargetUrlCodingStrategy
{
	/** log. */
	private static Log log = LogFactory.getLog(AbstractRequestTargetUrlCodingStrategy.class);

	
	/** mounted path. */
	private final String mountPath;

	/**
	 * Construct.
	 * 
	 * @param mountPath
	 *            the mount path
	 */
	public AbstractRequestTargetUrlCodingStrategy(final String mountPath)
	{
		if (mountPath == null)
		{
			throw new IllegalArgumentException("Argument mountPath must be not null");
		}

		this.mountPath = mountPath;
	}

	/**
	 * Gets path.
	 * 
	 * @return path
	 */
	protected final String getMountPath()
	{
		return mountPath;
	}

	/**
	 * Decodes PageParameters object from the provided url fragment
	 * 
	 * @param urlFragment
	 * @return PageParameters object created from the url fragment
	 */
	protected PageParameters decodePageParameters(String urlFragment)
	{
		// Hack off any leading slash
		if (urlFragment.startsWith("/"))
		{
			urlFragment = urlFragment.substring(1);
		}

		if (urlFragment.length()==0) {
			return new PageParameters();
		}
		
		// Split into pairs
		final String[] pairs = urlFragment.split("/");

		// If we don't have an even number of pairs
		if (pairs.length % 2 != 0)
		{
			// give up
			throw new IllegalStateException("URL fragment has unmatched key/value pair: "
					+ urlFragment);
		}

		// Loop through pairs
		PageParameters parameters = new PageParameters();
		for (int i = 0; i < pairs.length; i += 2)
		{
			parameters.put(pairs[i], pairs[i + 1]);
		}
		return parameters;
	}

	/**
	 * Encodes PageParameters into a url fragment and append that to the
	 * provided url buffer.
	 * 
	 * @param url
	 *            url so far
	 * 
	 * @param parameters
	 *            PageParameters object to be encoded
	 */
	protected void appendPageParameters(StringBuffer url, PageParameters parameters)
	{
		if (parameters != null)
		{
			Iterator entries = parameters.entrySet().iterator();
			while (entries.hasNext())
			{
				Map.Entry entry = (Entry)entries.next();
				String escapedValue=(String)entry.getValue().toString();
				try
				{
					escapedValue=URLEncoder.encode(escapedValue, Application.get()
							.getRequestCycleSettings().getResponseRequestEncoding());
				}
				catch (UnsupportedEncodingException e)
				{
					log.error(e.getMessage(), e);
				}
				url.append("/").append(entry.getKey()).append("/").append(escapedValue);
			}
		}
	}
}

@@ -1460,17 +1460,16 @@
 lass);%0A%0A
-%09
 %0A%09/** mo
@@ -2317,13 +2317,17 @@
 th()
+ 
 ==
+ 
 0)
- 
+%0A%09%09
 %7B%0A%09%09
@@ -2360,18 +2360,16 @@
 ();%0A%09%09%7D%0A
-%09%09
 %0A%09%09// Sp
@@ -3331,17 +3331,29 @@
 pedValue
-=
+ = urlEncode(
 (String)
@@ -3383,42 +3383,288 @@
 ng()
+)
 ;%0A%09%09%09%09
-try%0A%09%09%09%09%7B%0A%09%09%09%09%09escapedValue=
+url.append(%22/%22).append(entry.getKey()).append(%22/%22).append(escapedValue);%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D%0A%0A%09/**%0A%09 * Url encodes a string%0A%09 * %0A%09 * @param string%0A%09 *            string to be encoded%0A%09 * @return encoded string%0A%09 */%0A%09protected String urlEncode(String string)%0A%09%7B%0A%09%09try%0A%09%09%7B%0A%09%09%09return 
 URLE
@@ -3677,28 +3677,22 @@
 .encode(
-escapedValue
+string
 , Applic
@@ -3706,16 +3706,8 @@
 et()
-%0A%09%09%09%09%09%09%09
 .get
@@ -3728,16 +3728,22 @@
 ttings()
+%0A%09%09%09%09%09
 .getResp
@@ -3768,22 +3768,18 @@
 g());%0A%09%09
-%09%09%7D%0A%09%09
+%7D%0A
 %09%09catch 
@@ -3815,19 +3815,15 @@
  e)%0A
-%09%09
 %09%09%7B%0A%09%09%09
-%09%09
 log.
@@ -3855,96 +3855,27 @@
 %0A%09%09%09
-%09%7D%0A%09%09%09%09url.append(%22/%22).append(entry.getKey()).append(%22/%22).append(escapedValue)
+return string
 ;%0A
-%09
 %09%09%7D%0A
-%09%09%7D
 %0A%09%7D%0A

factored out url encoding to a helper method