/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.wicket.request.target.coding;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.apache.wicket.Application;
import org.apache.wicket.util.string.AppendingStringBuffer;

/**
 * {@link AppendingStringBuffer}-based query string encoder, handles String[] and String properly, and properly URL-encodes the values
 * 
 * @author <a href="mailto:jbq@apache.org">Jean-Baptiste Quenot</a>
 */
public class WebRequestEncoder
{
	boolean firstParameter = true;
	AppendingStringBuffer url;
	Application application;

	/**
	 * Construct.
	 * @param url the {@link AppendingStringBuffer} where to append query string
	 */
	public WebRequestEncoder(AppendingStringBuffer url)
	{
		this.url = url;
		this.application = Application.get();
	}

	/**
	 * Add an {@link Object}
	 * @param key
	 * @param value
	 */
	public void addValue(String key, Object value)
	{
		if (value instanceof String[])
		{
			String[] values = (String[])value;
			for (int i = 0; i < values.length; i++)
			{
				addValue(key, values[i]);
			}
		}
		else if (value instanceof String)
		{
			addValue(key, (String)value);
		}
		else
		{
			throw new IllegalArgumentException("PageParameters can only contain String or String[]");
		}
	}

	/**
	 * Add a {@link String}
	 * @param key
	 * @param value
	 */
	public void addValue(String key, String value)
	{
		String escapedValue = value;
		try
		{
			escapedValue = URLEncoder.encode(escapedValue, application.getRequestCycleSettings()
					.getResponseRequestEncoding());
		}
		catch (UnsupportedEncodingException ex)
		{
			// ignore
		}
		if (!firstParameter)
		{
			url.append('&');
		}
		else
		{
			firstParameter = false;
			url.append('?');
		}
		url.append(key);
		url.append('=');
		url.append(escapedValue);
	}

}

@@ -1100,16 +1100,19 @@
 String%5B%5D
+%0A *
  and Str
@@ -1377,16 +1377,21 @@
 struct.%0A
+%09 * %0A
 %09 * @par
@@ -1396,16 +1396,31 @@
 aram url
+%0A%09 *           
  the %7B@l
@@ -1556,21 +1556,16 @@
  url;%0A%09%09
-this.
 applicat
@@ -1624,16 +1624,21 @@
 Object%7D%0A
+%09 * %0A
 %09 * @par
@@ -1732,28 +1732,29 @@
 alue
- instanceof String%5B%5D
+.getClass().isArray()
 )%0A%09%09
@@ -1758,22 +1758,22 @@
 %0A%09%09%7B%0A%09%09%09
-String
+Object
 %5B%5D value
@@ -1777,22 +1777,22 @@
 lues = (
-String
+Object
 %5B%5D)value
@@ -2099,16 +2099,21 @@
 String%7D%0A
+%09 * %0A
 %09 * @par

Fix for arrays of any type.