[jboss-svn-commits] JBL Code SVN: r11313 - labs/jbosslabs/trunk/portal-extensions/forge-portal-attr/src/java/org/jboss/forge/portal.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Tue Apr 24 16:34:09 EDT 2007


Author: adamw
Date: 2007-04-24 16:34:09 -0400 (Tue, 24 Apr 2007)
New Revision: 11313

Modified:
   labs/jbosslabs/trunk/portal-extensions/forge-portal-attr/src/java/org/jboss/forge/portal/DownloadsFilter.java
Log:
Downloads fix

Modified: labs/jbosslabs/trunk/portal-extensions/forge-portal-attr/src/java/org/jboss/forge/portal/DownloadsFilter.java
===================================================================
--- labs/jbosslabs/trunk/portal-extensions/forge-portal-attr/src/java/org/jboss/forge/portal/DownloadsFilter.java	2007-04-24 20:08:00 UTC (rev 11312)
+++ labs/jbosslabs/trunk/portal-extensions/forge-portal-attr/src/java/org/jboss/forge/portal/DownloadsFilter.java	2007-04-24 20:34:09 UTC (rev 11313)
@@ -39,117 +39,113 @@
 import org.jboss.forge.common.projects.project.Category;
 import org.jboss.forge.common.projects.project.Downloads;
 import org.jboss.forge.common.projects.project.File;
+import org.jboss.forge.common.projects.project.Files;
 import org.jboss.logging.Logger;
 
 /**
  * DownloadsFilter.java
- * 
+ *
  * @author <a href="mailto:tomasz.szymanski at jboss.com">Tomasz Szymanski</a>
  */
 
 public class DownloadsFilter implements Filter {
 
-	private static final String PROJ_DOWNLOAD = "[\\w\\d-]+/downloads/([\\w\\d-]+/)*[\\w\\d-]+(\\.[\\w\\d-]+)*";
+    private static final String PROJ_DOWNLOAD = "[\\w\\d-]+/downloads/([\\w\\d-]+/)*[\\w\\d-]+(\\.[\\w\\d-]+)*";
 
-	private static final Logger log = Logger.getLogger(DownloadsFilter.class);
+    private static final Logger log = Logger.getLogger(DownloadsFilter.class);
 
-	public void destroy() {
+    public void destroy() {
 
-	}
+    }
 
     public boolean process(HttpServletRequest htReq, ServletResponse response) throws IOException {
         String uri = htReq.getRequestURI();
 
-			if (uri.startsWith("/auth")) {
-				uri = uri.substring("/auth".length());
-			} else if (uri.startsWith("/authsec")) {
-				uri = uri.substring("/authsec".length());
-			}
+        if (uri.startsWith("/auth")) {
+            uri = uri.substring("/auth".length());
+        } else if (uri.startsWith("/authsec")) {
+            uri = uri.substring("/authsec".length());
+        }
 
-			if (uri.startsWith("/")) {
-				uri = uri.substring(1);
-			}
+        if (uri.startsWith("/")) {
+            uri = uri.substring(1);
+        }
 
-			//log.info(uri + " " + uri.matches(PROJ_DOWNLOAD));
+        //log.info(uri + " " + uri.matches(PROJ_DOWNLOAD));
 
-			if (uri.matches(PROJ_DOWNLOAD)) {
-				String projectId = uri.substring(0, uri.indexOf('/'));
+        if (uri.matches(PROJ_DOWNLOAD)) {
+            String projectId = uri.substring(0, uri.indexOf('/'));
 
-				try {
-					Downloads downl = LabsServices.getProjectsService()
-							.getProjectDownloads(projectId);
+            try {
+                Downloads downl = LabsServices.getProjectsService().getProjectDownloads(projectId);
 
-					String[] tokens = uri.split("/");
+                if (downl == null) {
+                    return true;
+                }
 
-					//log.info("tokens lenght: "+tokens.length);
+                Category cat;
 
-					Category cat = null;
+                String[] tokens = uri.split("/", 3);
 
-					// first check if url is leading to file
+                if (tokens.length < 3) {
+                    return true;
+                }
 
-					//there are categories
-					if (tokens.length > 3) {
-						for (int i = 2; i < tokens.length - 1; i++) {
-							if (cat == null) {
-								cat = downl.getCategories().getCategory(tokens[i]);
-							} else {
-								cat = cat.getCategories().getCategory(tokens[i]);
-							}
+                String current = tokens[2];
+                tokens = current.split("/", 2);
+                cat = downl.getCategories().getCategory(tokens[0]);
+                Category newCat = cat;
 
-							if (cat == null) {
-								// url is invalid (no such categories) - forward
-								// request
+                while (newCat != null) {
+                    if (tokens.length < 2) {
+                        return true;
+                    }
 
-								return true;
-							}
-						}
+                    current = tokens[1];
+                    tokens = current.split("/", 2);
+                    cat = newCat;
 
-                        if (cat == null || cat.getFiles() == null) {
-                            return true;
-                        }
+                    if (cat.getCategories() == null) {
+                        newCat = null;
+                    } else {
+                        newCat = cat.getCategories().getCategory(tokens[0]);
+                    }
+                }
 
-                        String fileURI = getUri(tokens[tokens.length - 1], cat.getFiles().getFile(), uri);
+                String fileURI;
+                if (cat == null) {
+                    if (downl.getFiles() == null) {
+                        return true;
+                    }
 
-						//log.info("FILE URI: "+fileURI);
+                    fileURI = getUri(current, downl.getFiles(), uri);
+                } else {
+                    if (cat.getFiles() == null) {
+                        return true;
+                    }
 
-						if (fileURI != null) {
-							//htReq.getRequestDispatcher(fileURI).forward(
-							//		request, response);
-							((HttpServletResponse)response).sendRedirect(fileURI);
-							return false;
-						}
-					} else {
-						//no categories just check file
-                                               
-                        if (downl == null || downl.getFiles() == null) {
-                            return true;
-                        }
+                    fileURI = getUri(current, cat.getFiles(), uri);
+                }
 
-                        String fileURI = getUri(tokens[tokens.length - 1],
-								downl.getFiles().getFile(), uri);
+                if (fileURI != null) {
+                    ((HttpServletResponse)response).sendRedirect(fileURI);
 
-						//log.info("FILE URI: "+fileURI);
+                    return false;
+                } else {
+                    return true;
+                }
+            } catch (ServiceRetrievalException e) {
+                log.error(e);
+            }
+        }
 
-						if (fileURI != null) {
-							//htReq.getRequestDispatcher(fileURI).forward(
-							//		request, response);
-							((HttpServletResponse)response).sendRedirect(fileURI);
-
-							return false;
-						}
-					}
-				} catch (ServiceRetrievalException e) {
-					log.error(e);
-				}
-			}
-
         return true;
     }
 
     public void doFilter(ServletRequest request, ServletResponse response,
-			FilterChain chain) throws IOException, ServletException {
-		if (request instanceof HttpServletRequest) {
-			HttpServletRequest htReq = (HttpServletRequest) request;
+                         FilterChain chain) throws IOException, ServletException {
+        if (request instanceof HttpServletRequest) {
+            HttpServletRequest htReq = (HttpServletRequest) request;
 
             if (process(htReq, response)) {
                 chain.doFilter(request, response);
@@ -157,25 +153,28 @@
         } else {
             chain.doFilter(request, response);
         }
-	}
+    }
 
-	private String getUri(String fileName, List<? extends File> files,
-			String uri) {
-		//log.info("Execute: "+fileName);
-		for (File f : files) {
-			//log.info("another file: "+f.getId());
-			if (f.getId().equals(fileName)) {
-				// it's a file. include.
+    private String getUri(String fileName, Files files,
+                          String uri) {
+        File f = files.getFile(fileName);
 
-				return "/file-access/default/members/" + uri;
-			}
-		}
+        if (f != null) {
+            return "/file-access/default/members/" + uri;
+        }
 
-		return null;
-	}
+        /*for (File f : files) {
+            if (f.getId().equals(fileName)) {
 
-	public void init(FilterConfig arg0) throws ServletException {
+                return "/file-access/default/members/" + uri;
+            }
+        }*/
 
-	}
+        return null;
+    }
 
+    public void init(FilterConfig arg0) throws ServletException {
+
+    }
+
 }




More information about the jboss-svn-commits mailing list