[jboss-svn-commits] JBL Code SVN: r15386 - in labs/shotoku/trunk: shotoku-cache/cache-admin/src/java/org/jboss/shotoku/web and 8 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Wed Sep 26 14:47:57 EDT 2007


Author: adamw
Date: 2007-09-26 14:47:57 -0400 (Wed, 26 Sep 2007)
New Revision: 15386

Added:
   labs/shotoku/trunk/shotoku-web/
   labs/shotoku/trunk/shotoku-web/src/
   labs/shotoku/trunk/shotoku-web/src/java/
   labs/shotoku/trunk/shotoku-web/src/java/org/
   labs/shotoku/trunk/shotoku-web/src/java/org/jboss/
   labs/shotoku/trunk/shotoku-web/src/java/org/jboss/shotoku/
   labs/shotoku/trunk/shotoku-web/src/java/org/jboss/shotoku/web/
   labs/shotoku/trunk/shotoku-web/src/java/org/jboss/shotoku/web/ResourcesFilter.java
Modified:
   labs/shotoku/trunk/shotoku-cache/cache-admin/src/java/org/jboss/shotoku/web/WebFilesystemFilter.java
   labs/shotoku/trunk/shotoku-cache/cache-admin/src/web/WEB-INF/web.xml
Log:
New resources filter

Modified: labs/shotoku/trunk/shotoku-cache/cache-admin/src/java/org/jboss/shotoku/web/WebFilesystemFilter.java
===================================================================
--- labs/shotoku/trunk/shotoku-cache/cache-admin/src/java/org/jboss/shotoku/web/WebFilesystemFilter.java	2007-09-26 18:40:40 UTC (rev 15385)
+++ labs/shotoku/trunk/shotoku-cache/cache-admin/src/java/org/jboss/shotoku/web/WebFilesystemFilter.java	2007-09-26 18:47:57 UTC (rev 15386)
@@ -63,16 +63,6 @@
 	private String basePath;
 
 	/**
-	 * Directory, through which the filter is invoked (when files are requested).
-	 */
-	private String repoAccessDir;
-
-	/**
-	 * Length of repoAccessDir
-	 */
-	private int repoAccessDirLength;
-
-	/**
 	 * Real directory in the filesystem from which to read the files.
 	 */
 	private String realDir;
@@ -96,9 +86,6 @@
 	}
 
 	public void init(FilterConfig conf) {
-		repoAccessDir = conf.getInitParameter("repoAccessDir");
-		repoAccessDirLength = repoAccessDir.length();
-
 		realDir = conf.getInitParameter("realDir");
 
 		// Constructing the base path.
@@ -114,17 +101,9 @@
 			// Constructing the path of the requested path as relative to the
 			// content's repository root.
 			String requestURI = httpRequest.getRequestURI();
-			int indexOfRepoAccessDir = requestURI.indexOf(repoAccessDir);
-			if (indexOfRepoAccessDir == -1) {
-				chain.doFilter(request, response);
-				return;
-			}
 
-			String requestedFile = requestURI.substring(indexOfRepoAccessDir
-					+ repoAccessDirLength + 1);
-
 			// We have to replace .jsf with .jsp.
-			String realRequestedFile = requestedFile;
+			String realRequestedFile = requestURI;
 			if (realRequestedFile.endsWith(".jsf")) {
 				realRequestedFile = realRequestedFile.replace(".jsf", ".jsp");
 			}
@@ -157,7 +136,7 @@
 
 			request.getRequestDispatcher(
 					File.separator + COPIED_TO_REPO_DIR + File.separator
-							+ requestedFile).include(request, response);
+							+ requestURI).include(request, response);
 		} else {
 			response.setContentType("text/html");
 			response.getWriter().write("Unsupported request class");

Modified: labs/shotoku/trunk/shotoku-cache/cache-admin/src/web/WEB-INF/web.xml
===================================================================
--- labs/shotoku/trunk/shotoku-cache/cache-admin/src/web/WEB-INF/web.xml	2007-09-26 18:40:40 UTC (rev 15385)
+++ labs/shotoku/trunk/shotoku-cache/cache-admin/src/web/WEB-INF/web.xml	2007-09-26 18:47:57 UTC (rev 15386)
@@ -62,13 +62,9 @@
         <filter-name>filesFromRepoFilter</filter-name>
         <filter-class>org.jboss.shotoku.cache.admin.WebFilesystemFilter</filter-class>
         <init-param>
-            <param-name>repoAccessDir</param-name>
-            <param-value>pages</param-value>
-        </init-param>
-        <init-param>
             <param-name>realDir</param-name>
-            <! <param-value>/home/adamw/shotoku/shotoku-cache/cache-admin/src/web/pages</param-value> >
-            <param-value>/Users/adamwarski/shotoku/shotoku-cache/cache-admin/src/web/pages</param-value>
+            <! <param-value>/home/adamw/shotoku/shotoku-cache/cache-admin/src/web</param-value> >
+            <param-value>/Users/adamwarski/shotoku/shotoku-cache/cache-admin/src/web</param-value>
         </init-param>
     </filter>
 

Added: labs/shotoku/trunk/shotoku-web/src/java/org/jboss/shotoku/web/ResourcesFilter.java
===================================================================
--- labs/shotoku/trunk/shotoku-web/src/java/org/jboss/shotoku/web/ResourcesFilter.java	                        (rev 0)
+++ labs/shotoku/trunk/shotoku-web/src/java/org/jboss/shotoku/web/ResourcesFilter.java	2007-09-26 18:47:57 UTC (rev 15386)
@@ -0,0 +1,199 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat                                               *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual                    *
+ * contributors as indicated by the @authors tag. See the                     *
+ * copyright.txt in the distribution for a full listing of                    *
+ * individual contributors.                                                   *
+ *                                                                            *
+ * This is free software; you can redistribute it and/or modify it            *
+ * under the terms of the GNU Lesser General Public License as                *
+ * published by the Free Software Foundation; either version 2.1 of           *
+ * the License, or (at your option) any later version.                        *
+ *                                                                            *
+ * This software is distributed in the hope that it will be useful,           *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU           *
+ * Lesser General Public License for more details.                            *
+ *                                                                            *
+ * You should have received a copy of the GNU Lesser General Public           *
+ * License along with this software; if not, write to the Free                *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA         *
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.                   *
+ ******************************************************************************/
+package org.jboss.shotoku.web;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
+
+/**
+ * A filter, which reads resources from the filesystem and makes them visible to the
+ * application as deployed files --- useful for development. Specifically,
+ * the <code>sourceBasePath</code> init-parameter value
+ * is prepended to the path. The file referenced by the path is then included
+ * in the request. To specify for which file extensions the filter is enabled,
+ * set the <code>extensions</code> init parameter. If not set, it defaults to:
+ * <code>jsp,css,html,htm,gif,jpg,jpeg,png,txt</code>.
+ * 
+ * @author <a href="mailto:adam at warski.org">Adam Warski</a>
+ */
+public class ResourcesFilter implements Filter {
+	/**
+	 * Name of a subdirectory of the exploded deployment to which files
+	 * will be copied.
+	 */
+	private final static String DEST_RES_DIR = "copied-resources";
+	
+	/**
+	 * A list of extensions, which are filtered by default, if nothing is
+	 * specified in the filter configuration.
+	 */
+	private final static String DEFAULT_EXTENSIONS = "jsp,css,html,htm,gif,jpg,jpeg,png,txt";
+
+	/**
+	 * Base path to a directory where files will
+	 * be copied; it's a subdirectory of a deployment directory created by the
+	 * app server.
+	 */
+	private String destBasePath;
+
+	/**
+	 * Directory in the filesystem from which to read the files.
+	 */
+	private String sourceBasePath;
+	
+	/**
+	 * A set of <code>java.lang.String</code>s, which are extensions, that are filtered.
+	 */
+	private Set extensions;
+
+	/**
+	 * Transfers all bytes from the given input stream to the given output
+	 * stream.
+	 *
+	 * @param is
+	 *            Input stream to read from.
+	 * @param os
+	 *            Output stream to write to.
+	 * @throws IOException In case of an IO exception.
+	 */
+	private void transfer(InputStream is, OutputStream os) throws IOException {
+		byte[] buffer = new byte[1024];
+		int read;
+		while ((read = is.read(buffer)) != -1) {
+			os.write(buffer, 0, read);
+		}
+	}
+
+	public void init(FilterConfig conf) {
+		sourceBasePath = conf.getInitParameter("sourceBasePath");
+		destBasePath = conf.getServletContext().getRealPath("") + File.separator + DEST_RES_DIR;
+		
+		extensions = new HashSet();
+		String filteredExtensionsString = conf.getInitParameter("extensions");
+		
+		if (filteredExtensionsString == null) {
+			filteredExtensionsString = DEFAULT_EXTENSIONS;
+		}
+		
+		String[] tokens = filteredExtensionsString.split(",");
+		
+		for (int i=0; i<tokens.length; i++) {
+			extensions.add(tokens[i]);
+		}
+	}
+	
+	private String safeToString(Object o) {
+		if (o == null) {
+			return null;
+		}
+		
+		return o.toString();
+	}
+	
+	private boolean checkExtension(String path) {
+		int dotIndex = path.lastIndexOf('.');
+		
+		if (dotIndex != -1) {
+			String extension = path.substring(dotIndex + 1);
+			return extensions.contains(extension);
+		} else {
+			return false;
+		}
+	}
+
+	public void doFilter(ServletRequest request, ServletResponse response,
+			FilterChain chain) throws IOException, ServletException {
+		if (request instanceof HttpServletRequest) {
+			HttpServletRequest httpRequest = (HttpServletRequest) request;
+			
+			/* Getting the name of the requested resource; first checking if
+			 * it is an included, then forwarded resource. Finally, checking
+			 * the request uri itself. */
+			String requestedResource;
+			requestedResource = safeToString(httpRequest.getAttribute("javax.servlet.include.servlet_path"));
+			
+			if (requestedResource == null) {
+				requestedResource = httpRequest.getServletPath();
+			}
+			
+			// JSF check - we have to replace .jsf with .jsp.
+			String realRequestedResource = requestedResource;
+			if (realRequestedResource.endsWith(".jsf")) {
+				realRequestedResource = realRequestedResource.replace(".jsf", ".jsp");
+			}
+			
+			// Filtering only some extensions and preventing a loop.
+			if (requestedResource.startsWith("/" + DEST_RES_DIR) || !checkExtension(realRequestedResource)) {
+				chain.doFilter(request, response);
+				return;
+			}
+
+			File sourceFile = new File(sourceBasePath + realRequestedResource);
+			File destFile = new File(destBasePath + realRequestedResource);
+
+			InputStream in = null;
+			OutputStream out = null;
+
+			try {
+				destFile.getParentFile().mkdirs();
+				destFile.setLastModified(System.currentTimeMillis());
+
+				in = new FileInputStream(sourceFile);
+				out = new FileOutputStream(destFile);
+
+				transfer(in, out);
+			} finally {
+				if (in != null) {
+					in.close();
+				}
+
+				if (out != null) {
+					out.close();
+				}
+			}
+
+			request.getRequestDispatcher("/" + DEST_RES_DIR + requestedResource).include(request, response);
+		} else {
+			response.setContentType("text/html");
+			response.getWriter().write("Unsupported request class");
+		}
+	}
+
+	public void destroy() {
+
+	}
+}




More information about the jboss-svn-commits mailing list