⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 abstractrequesttargeturlcodingstrategy.java

📁 Wicket一个开发Java Web应用程序框架。它使得开发web应用程序变得容易而轻松。 Wicket利用一个POJO data beans组件使得它可以与任何持久层技术相结合。
💻 JAVA
字号:
/* * 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.URLDecoder;import java.net.URLEncoder;import java.util.Collections;import java.util.Iterator;import java.util.Map;import java.util.TreeMap;import java.util.Map.Entry;import org.apache.wicket.Application;import org.apache.wicket.protocol.http.UnitTestSettings;import org.apache.wicket.util.string.AppendingStringBuffer;import org.apache.wicket.util.string.Strings;import org.apache.wicket.util.value.ValueMap;import org.slf4j.Logger;import org.slf4j.LoggerFactory;/** * Abstract class for mount encoders that uses paths and forward slashes. *  * @author Eelco Hillenius * @author Igor Vaynberg (ivaynberg) */public abstract class AbstractRequestTargetUrlCodingStrategy		implements			IRequestTargetUrlCodingStrategy,			IMountableRequestTargetUrlCodingStrategy{	/** log. */	private static final Logger log = LoggerFactory			.getLogger(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("Mount path cannot be null or empty");		}		this.mountPath = mountPath.startsWith("/") ? mountPath.substring(1) : mountPath;		if (this.mountPath.startsWith("resources/") || this.mountPath.equals("resources"))		{			throw new IllegalArgumentException("Mount path cannot be under '/resources'");		}	}	/**	 * @see org.apache.wicket.request.target.coding.IMountableRequestTargetUrlCodingStrategy#getMountPath()	 */	public final String getMountPath()	{		return mountPath;	}	/**	 * Encodes Map into a url fragment and append that to the provided url buffer.	 * 	 * @param url	 *            url so far	 * 	 * @param parameters	 *            Map object to be encoded	 */	protected void appendParameters(AppendingStringBuffer url, Map parameters)	{		if (parameters != null && parameters.size() > 0)		{			final Iterator entries;			if (UnitTestSettings.getSortUrlParameters())			{				entries = new TreeMap(parameters).entrySet().iterator();			}			else			{				entries = parameters.entrySet().iterator();			}			while (entries.hasNext())			{				Map.Entry entry = (Entry)entries.next();				Object value = entry.getValue();				if (value != null)				{					if (value instanceof String[])					{						String[] values = (String[])value;						for (int i = 0; i < values.length; i++)						{							appendValue(url, entry.getKey().toString(), values[i]);						}					}					else					{						appendValue(url, entry.getKey().toString(), value.toString());					}				}			}		}	}	private void appendValue(AppendingStringBuffer url, String key, String value)	{		String escapedValue = urlEncode(value);		if (!Strings.isEmpty(escapedValue))		{			if (!url.endsWith("/"))			{				url.append("/");			}			url.append(key).append("/").append(escapedValue).append("/");		}	}	/**	 * 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);		}		// Hack off any trailing slash		if (urlFragment.length() > 0 && urlFragment.endsWith("/"))		{			urlFragment = urlFragment.substring(0, urlFragment.length() - 1);		}		if (urlFragment.length() == 0)		{			return new ValueMap(urlParameters != null ? urlParameters : Collections.EMPTY_MAP);		}		// 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		ValueMap parameters = new ValueMap();		for (int i = 0; i < pairs.length; i += 2)		{			String value = pairs[i + 1];			value = urlDecode(value);			parameters.add(pairs[i], value);		}		if (urlParameters != null)		{			parameters.putAll(urlParameters);		}		return parameters;	}	/**	 * Returns a decoded value of the given value	 * 	 * @param value	 * @return Decodes the value	 */	protected String urlDecode(String value)	{		try		{			value = URLDecoder.decode(value, Application.get().getRequestCycleSettings()					.getResponseRequestEncoding());		}		catch (UnsupportedEncodingException ex)		{			log.error("error decoding parameter", ex);		}		return value;	}	/**	 * Url encodes a string	 * 	 * @param string	 *            string to be encoded	 * @return encoded string	 */	protected String urlEncode(String string)	{		try		{			return URLEncoder.encode(string, Application.get().getRequestCycleSettings()					.getResponseRequestEncoding());		}		catch (UnsupportedEncodingException e)		{			log.error(e.getMessage(), e);			return string;		}	}	public boolean matches(String path)	{		if (path.startsWith(mountPath))		{			/*			 * We need to match /mount/point or /mount/point/with/extra/path, but not			 * /mount/pointXXX			 */			String remainder = path.substring(mountPath.length());			if (remainder.length() == 0 || remainder.startsWith("/"))			{				return true;			}		}		return false;	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -