You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by eh...@apache.org on 2007/03/05 05:21:14 UTC

svn commit: r514540 - /incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/WicketFilter.java

Author: ehillenius
Date: Sun Mar  4 20:21:13 2007
New Revision: 514540

URL: http://svn.apache.org/viewvc?view=rev&rev=514540
Log:
organized members

Modified:
    incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/WicketFilter.java

Modified: incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/WicketFilter.java
URL: http://svn.apache.org/viewvc/incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/WicketFilter.java?view=diff&rev=514540&r1=514539&r2=514540
==============================================================================
--- incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/WicketFilter.java (original)
+++ incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/WicketFilter.java Sun Mar  4 20:21:13 2007
@@ -69,6 +69,9 @@
 	 */
 	public static final String FILTER_MAPPING_PARAM = "filterMappingUrlPattern";
 
+	/** Log. */
+	private static final Log log = LogFactory.getLog(WicketFilter.class);
+
 	/**
 	 * The servlet path holder when the WicketSerlvet is used. So that the
 	 * filter path will be computed with the first request. Note: This variable
@@ -76,9 +79,6 @@
 	 */
 	static final String SERVLET_PATH_HOLDER = "<servlet>";
 
-	/** Log. */
-	private static final Log log = LogFactory.getLog(WicketFilter.class);
-
 	/** See javax.servlet.FilterConfig */
 	private FilterConfig filterConfig;
 
@@ -285,6 +285,51 @@
 	}
 
 	/**
+	 * Returns a relative path from an HttpServletRequest Use this to resolve a
+	 * Wicket request.
+	 * 
+	 * @param request
+	 * @return Path requested, minus query string, context path, and filterPath.
+	 *         Relative, no leading '/'.
+	 */
+	public String getRelativePath(HttpServletRequest request) {
+		String path = request.getServletPath();
+		if (servletMode)
+		{
+			path = request.getPathInfo();
+			// No path info => root.
+			if (path == null)
+			{
+				path = "";
+			}
+		}
+		filterPath = getFilterPath(request);
+		
+		if (path.length() > 0)
+		{
+			path = path.substring(1);
+		}
+		
+		// We should always be under the rootPath, except
+		// for the special case of someone landing on the
+		// home page without a trailing slash.
+		if (!path.startsWith(filterPath))
+		{
+			if (filterPath.equals(path + "/"))
+			{
+				path += "/";
+			}
+		}
+		if (path.startsWith(filterPath))
+		{
+			path = path.substring(filterPath.length());
+		}
+
+		return path;
+
+	}
+
+	/**
 	 * 
 	 * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
 	 */
@@ -363,6 +408,127 @@
 		}
 	}
 
+	private String getFilterPath(String filterName, InputStream is) throws ServletException
+	{
+		/*
+		 * Filter mappings look like this:
+		 * 
+		 * <filter-mapping> <filter-name>WicketFilter</filter-name>
+		 * <url-pattern>/*</url-pattern> <...> <filter-mapping>
+		 */
+		try
+		{
+			ArrayList urlPatterns = new ArrayList();
+			Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is);
+			is.close();
+			NodeList filterMappings = doc.getElementsByTagName("filter-mapping");
+			for (int i = 0; i < filterMappings.getLength(); i++)
+			{
+				Node filterMapping = filterMappings.item(i);
+				NodeList mappingElements = filterMapping.getChildNodes();
+
+				// We might have filter-name and url-pattern in the
+				// wrong order, even though it's a DTD-violation -
+				// many containers don't use strict parsing, so we
+				// have to deal with this.
+				String urlPattern = null;
+				boolean add = false;
+				for (int j = 0; j < mappingElements.getLength(); j++)
+				{
+					Node mappingElement = mappingElements.item(j);
+					if (mappingElement.getNodeType() != Node.ELEMENT_NODE)
+					{
+						continue;
+					}
+					if (mappingElement.getNodeName().equals("filter-name")
+							&& mappingElement.getFirstChild().getNodeValue().equals(filterName))
+					{
+						add = true;
+					}
+					if (mappingElement.getNodeName().equals("url-pattern"))
+					{
+						urlPattern = mappingElement.getFirstChild().getNodeValue();
+					}
+				}
+				if (add)
+				{
+					add = false;
+					urlPatterns.add(urlPattern);
+				}
+			}
+			// By the time we get here, we have a list of urlPatterns we match
+			// this filter against.
+			// In all likelihood, we will only have one. If we have none, we
+			// have an error.
+			// If we have more than one, we pick the first one to use for any
+			// 302 redirects that
+			// require absolute URLs.
+			if (urlPatterns.size() == 0)
+			{
+				throw new ServletException(
+						"Error initialising WicketFilter - you have no filter-mapping element with a url-pattern that uses filter: "
+								+ filterName);
+			}
+			String urlPattern = (String)urlPatterns.get(0);
+
+			// Check for leading '/' and trailing '*'.
+			if (!urlPattern.startsWith("/") || !urlPattern.endsWith("*"))
+			{
+				throw new ServletException(
+						"Filter mappings for Wicket filter must start with '/' and end with '*'.");
+			}
+
+			// Strip trailing '*' and leading '/'.
+			return urlPattern.substring(1, urlPattern.length() - 1);
+		}
+		catch (Exception e)
+		{
+			throw new ServletException("Error finding filter " + filterName + " in web.xml", e);
+		}
+	}
+
+	/**
+	 * Is this a Wicket request?
+	 * 
+	 * @param relativePath
+	 *            The relativePath
+	 * @return True if this is a Wicket request
+	 */
+	private boolean isWicketRequest(String relativePath)
+	{
+		// Special case home page
+		if (relativePath.equals("")) {
+			return true;
+		}
+				
+		// Resources
+		if (relativePath.startsWith(WebRequestCodingStrategy.RESOURCES_PATH_PREFIX)) {
+			return true;
+		}
+		
+		// Mounted page
+		return webApplication.getRequestCycleProcessor().getRequestCodingStrategy().urlCodingStrategyForPath(relativePath) != null;
+	}
+
+	/**
+	 * If the response has not already a 'lastModified' header set and if
+	 * 'lastModified' >= 0 than set the response header accordingly.
+	 * 
+	 * @param resp
+	 * @param lastModified
+	 */
+	private void maybeSetLastModified(final HttpServletResponse resp, final long lastModified)
+	{
+		if (resp.containsHeader("Last-Modified"))
+		{
+			return;
+		}
+		if (lastModified >= 0)
+		{
+			resp.setDateHeader("Last-Modified", lastModified);
+		}
+	}
+	
 	/**
 	 * Creates the web application factory instance.
 	 * 
@@ -416,7 +582,7 @@
 			}
 		}
 	}
-
+	
 	/**
 	 * @return The class loader
 	 */
@@ -425,6 +591,37 @@
 		return Thread.currentThread().getContextClassLoader();
 	}
 
+	protected String getFilterPath(HttpServletRequest request)
+	{
+		if (filterPath != null)
+		{
+			return filterPath;
+		}
+		if (servletMode)
+		{
+			return filterPath = request.getServletPath();
+		}
+		String result;
+		// Legacy migration check. TODO: Remove this after 1.3 is released and everyone's upgraded.
+		if (filterConfig.getInitParameter("filterPath") != null)
+		{
+			throw new WicketRuntimeException("\nThe filterPath init-param for WicketFilter has been removed.\n" +
+					"Please use a param called " + FILTER_MAPPING_PARAM + " with a value that exactly\n" +
+					"matches that in the <url-pattern> element of your <filter-mapping> (e.g. \"/app/*\").");
+		}
+		
+		result = filterConfig.getInitParameter(FILTER_MAPPING_PARAM);
+		if (result == null || result.equals("/*"))
+		{
+			return "";
+		}
+		else if (!result.startsWith("/") || !result.endsWith("/*"))
+		{
+			throw new WicketRuntimeException("Your " + FILTER_MAPPING_PARAM + " must start with \"/\" and end with \"/*\". It is: " + result);
+		}
+		return filterPath = result.substring(1, result.length() - 2);
+	}
+	
 	/**
 	 * Gets the last modified time stamp for the given request.
 	 * 
@@ -483,202 +680,5 @@
 			}
 		}
 		return -1;
-	}
-
-	/**
-	 * Is this a Wicket request?
-	 * 
-	 * @param relativePath
-	 *            The relativePath
-	 * @return True if this is a Wicket request
-	 */
-	private boolean isWicketRequest(String relativePath)
-	{
-		// Special case home page
-		if (relativePath.equals("")) {
-			return true;
-		}
-				
-		// Resources
-		if (relativePath.startsWith(WebRequestCodingStrategy.RESOURCES_PATH_PREFIX)) {
-			return true;
-		}
-		
-		// Mounted page
-		return webApplication.getRequestCycleProcessor().getRequestCodingStrategy().urlCodingStrategyForPath(relativePath) != null;
-	}
-	
-	/**
-	 * Returns a relative path from an HttpServletRequest Use this to resolve a
-	 * Wicket request.
-	 * 
-	 * @param request
-	 * @return Path requested, minus query string, context path, and filterPath.
-	 *         Relative, no leading '/'.
-	 */
-	public String getRelativePath(HttpServletRequest request) {
-		String path = request.getServletPath();
-		if (servletMode)
-		{
-			path = request.getPathInfo();
-			// No path info => root.
-			if (path == null)
-			{
-				path = "";
-			}
-		}
-		filterPath = getFilterPath(request);
-		
-		if (path.length() > 0)
-		{
-			path = path.substring(1);
-		}
-		
-		// We should always be under the rootPath, except
-		// for the special case of someone landing on the
-		// home page without a trailing slash.
-		if (!path.startsWith(filterPath))
-		{
-			if (filterPath.equals(path + "/"))
-			{
-				path += "/";
-			}
-		}
-		if (path.startsWith(filterPath))
-		{
-			path = path.substring(filterPath.length());
-		}
-
-		return path;
-
-	}
-	
-	protected String getFilterPath(HttpServletRequest request)
-	{
-		if (filterPath != null)
-		{
-			return filterPath;
-		}
-		if (servletMode)
-		{
-			return filterPath = request.getServletPath();
-		}
-		String result;
-		// Legacy migration check. TODO: Remove this after 1.3 is released and everyone's upgraded.
-		if (filterConfig.getInitParameter("filterPath") != null)
-		{
-			throw new WicketRuntimeException("\nThe filterPath init-param for WicketFilter has been removed.\n" +
-					"Please use a param called " + FILTER_MAPPING_PARAM + " with a value that exactly\n" +
-					"matches that in the <url-pattern> element of your <filter-mapping> (e.g. \"/app/*\").");
-		}
-		
-		result = filterConfig.getInitParameter(FILTER_MAPPING_PARAM);
-		if (result == null || result.equals("/*"))
-		{
-			return "";
-		}
-		else if (!result.startsWith("/") || !result.endsWith("/*"))
-		{
-			throw new WicketRuntimeException("Your " + FILTER_MAPPING_PARAM + " must start with \"/\" and end with \"/*\". It is: " + result);
-		}
-		return filterPath = result.substring(1, result.length() - 2);
-	}
-
-	/**
-	 * If the response has not already a 'lastModified' header set and if
-	 * 'lastModified' >= 0 than set the response header accordingly.
-	 * 
-	 * @param resp
-	 * @param lastModified
-	 */
-	private void maybeSetLastModified(final HttpServletResponse resp, final long lastModified)
-	{
-		if (resp.containsHeader("Last-Modified"))
-		{
-			return;
-		}
-		if (lastModified >= 0)
-		{
-			resp.setDateHeader("Last-Modified", lastModified);
-		}
-	}
-	
-	private String getFilterPath(String filterName, InputStream is) throws ServletException
-	{
-		/*
-		 * Filter mappings look like this:
-		 * 
-		 * <filter-mapping> <filter-name>WicketFilter</filter-name>
-		 * <url-pattern>/*</url-pattern> <...> <filter-mapping>
-		 */
-		try
-		{
-			ArrayList urlPatterns = new ArrayList();
-			Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is);
-			is.close();
-			NodeList filterMappings = doc.getElementsByTagName("filter-mapping");
-			for (int i = 0; i < filterMappings.getLength(); i++)
-			{
-				Node filterMapping = filterMappings.item(i);
-				NodeList mappingElements = filterMapping.getChildNodes();
-
-				// We might have filter-name and url-pattern in the
-				// wrong order, even though it's a DTD-violation -
-				// many containers don't use strict parsing, so we
-				// have to deal with this.
-				String urlPattern = null;
-				boolean add = false;
-				for (int j = 0; j < mappingElements.getLength(); j++)
-				{
-					Node mappingElement = mappingElements.item(j);
-					if (mappingElement.getNodeType() != Node.ELEMENT_NODE)
-					{
-						continue;
-					}
-					if (mappingElement.getNodeName().equals("filter-name")
-							&& mappingElement.getFirstChild().getNodeValue().equals(filterName))
-					{
-						add = true;
-					}
-					if (mappingElement.getNodeName().equals("url-pattern"))
-					{
-						urlPattern = mappingElement.getFirstChild().getNodeValue();
-					}
-				}
-				if (add)
-				{
-					add = false;
-					urlPatterns.add(urlPattern);
-				}
-			}
-			// By the time we get here, we have a list of urlPatterns we match
-			// this filter against.
-			// In all likelihood, we will only have one. If we have none, we
-			// have an error.
-			// If we have more than one, we pick the first one to use for any
-			// 302 redirects that
-			// require absolute URLs.
-			if (urlPatterns.size() == 0)
-			{
-				throw new ServletException(
-						"Error initialising WicketFilter - you have no filter-mapping element with a url-pattern that uses filter: "
-								+ filterName);
-			}
-			String urlPattern = (String)urlPatterns.get(0);
-
-			// Check for leading '/' and trailing '*'.
-			if (!urlPattern.startsWith("/") || !urlPattern.endsWith("*"))
-			{
-				throw new ServletException(
-						"Filter mappings for Wicket filter must start with '/' and end with '*'.");
-			}
-
-			// Strip trailing '*' and leading '/'.
-			return urlPattern.substring(1, urlPattern.length() - 1);
-		}
-		catch (Exception e)
-		{
-			throw new ServletException("Error finding filter " + filterName + " in web.xml", e);
-		}
 	}
 }