<commit_before>/*
 * 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.basic;

import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.wicket.IRequestTarget;
import org.apache.wicket.PageParameters;
import org.apache.wicket.request.RequestParameters;
import org.apache.wicket.request.target.coding.AbstractRequestTargetUrlCodingStrategy;
import org.apache.wicket.util.string.AppendingStringBuffer;
import org.apache.wicket.util.string.Strings;
import org.apache.wicket.util.value.ValueMap;

/**
 * Request coding strategy that uses a simple URI by putting the remaining path
 * in the <tt>uri</tt> page parameter. Override the decode() method to return
 * the appropriate request target, calling getURI(requestParameters) to get
 * requested uri. Note that this request coding strategy takes other page
 * parameters from the query string directly, it does not use hierarchical path
 * for parameters.
 * 
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
public class URIRequestTargetUrlCodingStrategy extends AbstractRequestTargetUrlCodingStrategy
{
	protected static final String URI = "uri";

	/**
	 * @see AbstractRequestTargetUrlCodingStrategy#AbstractRequestTargetUrlCodingStrategy(String)
	 */
	public URIRequestTargetUrlCodingStrategy(String mountPath)
	{
		super(mountPath);
	}

	/**
	 * Get the remaining path after mount point.
	 * 
	 * @param requestParameters
	 *            request parameters provided to the decode() method
	 * @return the URI
	 */
	public PageParameters decodeParameters(RequestParameters requestParameters)
	{
		final String parametersFragment = requestParameters.getPath().substring(
				getMountPath().length());
		return new PageParameters(decodeParameters(parametersFragment, requestParameters
				.getParameters()));
	}

	/**
	 * Does nothing
	 * 
	 * @see wicket.request.target.coding.IRequestTargetUrlCodingStrategy#decode(wicket.request.RequestParameters)
	 */
	public IRequestTarget decode(RequestParameters requestParameters)
	{
		return null;
	}

	/**
	 * Does nothing
	 * 
	 * @see wicket.request.target.coding.IRequestTargetUrlCodingStrategy#encode(wicket.IRequestTarget)
	 */
	public CharSequence encode(IRequestTarget requestTarget)
	{
		return null;
	}

	/**
	 * Does nothing
	 * 
	 * @see wicket.request.target.coding.IRequestTargetUrlCodingStrategy#matches(wicket.IRequestTarget)
	 */
	public boolean matches(IRequestTarget requestTarget)
	{
		return false;
	}

	/**
	 * Gets the encoded URL for the request target. Typically, the result will
	 * be prepended with a protocol specific prefix. In a servlet environment,
	 * the prefix concatenates the context path and the servlet path, for
	 * example "mywebapp/myservlet".
	 * 
	 * @param url
	 *            the relative reference URL
	 * @param parameters
	 *            parameter names mapped to parameter values
	 */
	protected void appendParameters(AppendingStringBuffer url, Map parameters)
	{

		if (parameters != null && parameters.size() > 0)
		{
			boolean firstParam = true;
			Iterator entries = parameters.entrySet().iterator();

			while (entries.hasNext())
			{
				Map.Entry entry = (Entry)entries.next();

				if (entry.getValue() != null)
				{
					String escapedValue = urlEncode(entry.getValue().toString());

					if (!Strings.isEmpty(escapedValue))
					{
						if (entry.getKey().equals(URI))
						{
							url.append("/").append(escapedValue);
						}
						else
						{
							if (firstParam)
							{
								url.append("?"); /* Begin query string. */
								firstParam = false;
							}
							else
							{
								/*
								 * Separate new key=value(s) pair from previous
								 * pair with an ampersand.
								 */
								url.append("&");
							}

							/* Append key=value(s) pair. */
							url.append(entry.getKey());
							url.append("=");
							url.append(escapedValue);
						}
					}
				}
			}
		}
	}

	/**
	 * Decodes parameters object from the provided url fragment
	 * 
	 * @param urlFragment
	 *            fragment of the url after the decoded path and before the
	 *            query string
	 * @param urlParameters
	 *            query string parameters
	 * @return Parameters created from the url fragment and query string
	 */
	protected ValueMap decodeParameters(String urlFragment, Map urlParameters)
	{
		// Hack off any leading slash
		if (urlFragment.startsWith("/"))
		{
			urlFragment = urlFragment.substring(1);
		}

		if (urlFragment.length() == 0)
		{
			return new ValueMap();
		}

		ValueMap parameters = new ValueMap();
		parameters.add(URI, urlFragment);

		if (urlParameters != null)
		{
			parameters.putAll(urlParameters);
		}

		return parameters;
	}
	
	protected String getURI(RequestParameters requestParameters) {
		return decodeParameters(requestParameters).getString(URI);
	}
}<commit_msg>* Copy BookmarkablePageRequestTargetUrlCodingStrategy#encode(IRequestTarget) * Rework appendParameters(): the "uri" parameter is appended first with a slash,   then the remaining parameters are handled like in   QueryStringUrlCodingStrategy#appendParameters()<commit_after>/*
 * 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.basic;

import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
import java.util.Map.Entry;

import org.apache.wicket.IRequestTarget;
import org.apache.wicket.PageParameters;
import org.apache.wicket.protocol.http.UnitTestSettings;
import org.apache.wicket.protocol.http.request.WebRequestCodingStrategy;
import org.apache.wicket.request.RequestParameters;
import org.apache.wicket.request.target.coding.AbstractRequestTargetUrlCodingStrategy;
import org.apache.wicket.request.target.coding.BookmarkablePageRequestTargetUrlCodingStrategy;
import org.apache.wicket.request.target.coding.QueryStringUrlCodingStrategy;
import org.apache.wicket.request.target.coding.WebRequestEncoder;
import org.apache.wicket.request.target.component.IBookmarkablePageRequestTarget;
import org.apache.wicket.request.target.component.IPageRequestTarget;
import org.apache.wicket.util.string.AppendingStringBuffer;
import org.apache.wicket.util.string.Strings;
import org.apache.wicket.util.value.ValueMap;

/**
 * Request coding strategy that uses a simple URI by putting the remaining path
 * in the <tt>uri</tt> page parameter. Override the decode() method to return
 * the appropriate request target, calling getURI(requestParameters) to get
 * requested uri. Note that this request coding strategy takes other page
 * parameters from the query string directly, it does not use hierarchical path
 * for parameters.
 * 
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
public class URIRequestTargetUrlCodingStrategy extends AbstractRequestTargetUrlCodingStrategy
{
	protected static final String URI = "uri";

	/**
	 * @see AbstractRequestTargetUrlCodingStrategy#AbstractRequestTargetUrlCodingStrategy(String)
	 */
	public URIRequestTargetUrlCodingStrategy(String mountPath)
	{
		super(mountPath);
	}

	/**
	 * Get the remaining path after mount point.
	 * 
	 * @param requestParameters
	 *            request parameters provided to the decode() method
	 * @return the URI
	 */
	public PageParameters decodeParameters(RequestParameters requestParameters)
	{
		final String parametersFragment = requestParameters.getPath().substring(
				getMountPath().length());
		return new PageParameters(decodeParameters(parametersFragment, requestParameters
				.getParameters()));
	}

	/**
	 * Does nothing
	 * 
	 * @see wicket.request.target.coding.IRequestTargetUrlCodingStrategy#decode(wicket.request.RequestParameters)
	 */
	public IRequestTarget decode(RequestParameters requestParameters)
	{
		return null;
	}

	/**
	 * Copied from
	 * {@link BookmarkablePageRequestTargetUrlCodingStrategy#encode(IRequestTarget)}
	 * without pageMapName field
	 * 
	 * @see wicket.request.target.coding.IRequestTargetUrlCodingStrategy#encode(wicket.IRequestTarget)
	 */
	public CharSequence encode(IRequestTarget requestTarget)
	{
		if (!(requestTarget instanceof IBookmarkablePageRequestTarget))
		{
			throw new IllegalArgumentException("This encoder can only be used with "
					+ "instances of " + IBookmarkablePageRequestTarget.class.getName());
		}
		final AppendingStringBuffer url = new AppendingStringBuffer(40);
		url.append(getMountPath());
		final IBookmarkablePageRequestTarget target = (IBookmarkablePageRequestTarget)requestTarget;

		PageParameters pageParameters = target.getPageParameters();
		String pagemap = target.getPageMapName();
		if (pagemap != null)
		{
			if (pageParameters == null)
			{
				pageParameters = new PageParameters();
			}
			pageParameters.put(WebRequestCodingStrategy.PAGEMAP, pagemap);
		}
		appendParameters(url, pageParameters);
		return url;
	}

	/**
	 * Always returns false
	 * 
	 * @see wicket.request.target.coding.IRequestTargetUrlCodingStrategy#matches(wicket.IRequestTarget)
	 */
	public boolean matches(IRequestTarget requestTarget)
	{
		return false;
	}

	/**
	 * Gets the encoded URL for the request target. The "uri" parameter is
	 * appended first with a slash, then the remaining parameters are handled
	 * like in {@link QueryStringUrlCodingStrategy#appendParameters()}
	 * 
	 * @param url
	 *            the relative reference URL
	 * @param parameters
	 *            parameter names mapped to parameter values
	 */
	protected void appendParameters(AppendingStringBuffer url, Map parameters)
	{

		if (parameters.get(URI) != null)
		{
			url.append("/").append(parameters.get(URI));
		}

		// Copied from QueryStringUrlCodingStrategy
		if (parameters != null && parameters.size() > 0)
		{
			final Iterator entries;
			if (UnitTestSettings.getSortUrlParameters())
			{
				entries = new TreeMap(parameters).entrySet().iterator();
			}
			else
			{
				entries = parameters.entrySet().iterator();
			}
			WebRequestEncoder encoder = new WebRequestEncoder(url);
			while (entries.hasNext())
			{
				Map.Entry entry = (Entry)entries.next();

				if (entry.getKey().equals(URI))
				{
					// Ignore "uri" parameter already handled
					continue;
				}
				if (entry.getValue() != null)
				{
					encoder.addValue(entry.getKey().toString(), entry.getValue());
				}
			}
		}
	}

	/**
	 * Decodes parameters object from the provided url fragment
	 * 
	 * @param urlFragment
	 *            fragment of the url after the decoded path and before the
	 *            query string
	 * @param urlParameters
	 *            query string parameters
	 * @return Parameters created from the url fragment and query string
	 */
	protected ValueMap decodeParameters(String urlFragment, Map urlParameters)
	{
		// Hack off any leading slash
		if (urlFragment.startsWith("/"))
		{
			urlFragment = urlFragment.substring(1);
		}

		if (urlFragment.length() == 0)
		{
			return new ValueMap();
		}

		ValueMap parameters = new ValueMap();
		parameters.add(URI, urlFragment);

		if (urlParameters != null)
		{
			parameters.putAll(urlParameters);
		}

		return parameters;
	}

	protected String getURI(RequestParameters requestParameters)
	{
		return decodeParameters(requestParameters).getString(URI);
	}
}
{'fraction_non_alphanumeric': 0.09352517985611511, 'fraction_numerical': 0.0012649221282314807, 'mean_word_length': 3.922178988326848, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6966837', 'n_tokens_mistral': 3855, 'n_tokens_neox': 3344, 'n_words': 995}