Author: ndkhoiits
Date: 2010-11-30 03:34:43 -0500 (Tue, 30 Nov 2010)
New Revision: 5376
Modified:
portal/branches/branch-GTNPORTAL-1700/component/web/api/src/main/java/org/exoplatform/web/filter/ExtensibleFilter.java
portal/branches/branch-GTNPORTAL-1700/component/web/api/src/main/java/org/exoplatform/web/filter/GenericFilter.java
portal/branches/branch-GTNPORTAL-1700/component/web/api/src/test/java/org/exoplatform/web/filter/TestExtensibleFilter.java
Log:
GTNPORTAL-1701 Improve performance for each of request to extensible filer
Modified:
portal/branches/branch-GTNPORTAL-1700/component/web/api/src/main/java/org/exoplatform/web/filter/ExtensibleFilter.java
===================================================================
---
portal/branches/branch-GTNPORTAL-1700/component/web/api/src/main/java/org/exoplatform/web/filter/ExtensibleFilter.java 2010-11-30
08:04:31 UTC (rev 5375)
+++
portal/branches/branch-GTNPORTAL-1700/component/web/api/src/main/java/org/exoplatform/web/filter/ExtensibleFilter.java 2010-11-30
08:34:43 UTC (rev 5376)
@@ -29,7 +29,6 @@
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
-import javax.servlet.http.HttpServletRequest;
/**
* This class allows the rest of the platform to define new filters thanks to the
external
@@ -40,7 +39,7 @@
* nicolas.filotto(a)exoplatform.com
* 25 sept. 2009
*/
-public class ExtensibleFilter implements Filter
+public class ExtensibleFilter
{
/**
@@ -74,35 +73,31 @@
}
}
- /**
- * @see Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse,
javax.servlet.FilterChain)
- */
- public void doFilter(ServletRequest request, ServletResponse response, FilterChain
chain) throws IOException,
- ServletException
+ public void doFilter(ServletRequest request, ServletResponse response, FilterChain
chain, String path)
+ throws IOException, ServletException
{
- ExtensibleFilterChain efChain = new ExtensibleFilterChain(chain, filters);
+ ExtensibleFilterChain efChain = new ExtensibleFilterChain(chain, filters, path);
efChain.doFilter(request, response);
}
-
+
private static class ExtensibleFilterChain implements FilterChain
{
private final FilterChain parentChain;
private final Iterator<FilterDefinition> filters;
+
+ private final String path;
- private ExtensibleFilterChain(FilterChain parentChain, List<FilterDefinition>
filters)
+ private ExtensibleFilterChain(FilterChain parentChain, List<FilterDefinition>
filters, String path_)
{
this.parentChain = parentChain;
this.filters = filters.iterator();
+ this.path = path_;
}
public void doFilter(ServletRequest request, ServletResponse response) throws
IOException, ServletException
{
- HttpServletRequest req = (HttpServletRequest) request;
- String regex = "[/]*" + req.getContextPath() + "[/]*";
- String path = req.getRequestURI().replaceFirst(regex, "/");
-
while (filters.hasNext())
{
FilterDefinition filterDef = filters.next();
Modified:
portal/branches/branch-GTNPORTAL-1700/component/web/api/src/main/java/org/exoplatform/web/filter/GenericFilter.java
===================================================================
---
portal/branches/branch-GTNPORTAL-1700/component/web/api/src/main/java/org/exoplatform/web/filter/GenericFilter.java 2010-11-30
08:04:31 UTC (rev 5375)
+++
portal/branches/branch-GTNPORTAL-1700/component/web/api/src/main/java/org/exoplatform/web/filter/GenericFilter.java 2010-11-30
08:34:43 UTC (rev 5376)
@@ -23,11 +23,15 @@
import org.exoplatform.container.web.AbstractFilter;
import java.io.IOException;
+import java.util.regex.Pattern;
import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
/**
* This filter allows the rest of the platform to add their own filters without changing
the web.xml
@@ -41,12 +45,20 @@
public class GenericFilter extends AbstractFilter
{
+ private Pattern contextPathPattern;
/**
* @see javax.servlet.Filter#destroy()
*/
public void destroy()
{
}
+
+ @Override
+ protected void afterInit(FilterConfig config) throws ServletException
+ {
+ ServletContext servletContext = this.getServletContext();
+ contextPathPattern = Pattern.compile("[/]*" +
servletContext.getContextPath() + "[/]*");
+ }
/**
* This filter calls <code>doFilter</code> of the {@link ExtensibleFilter}
of
@@ -63,7 +75,8 @@
}
else
{
- filter.doFilter(request, response, chain);
+ String path =
contextPathPattern.matcher(((HttpServletRequest)request).getRequestURI()).replaceFirst("/");
+ filter.doFilter(request, response, chain, path);
}
}
}
Modified:
portal/branches/branch-GTNPORTAL-1700/component/web/api/src/test/java/org/exoplatform/web/filter/TestExtensibleFilter.java
===================================================================
---
portal/branches/branch-GTNPORTAL-1700/component/web/api/src/test/java/org/exoplatform/web/filter/TestExtensibleFilter.java 2010-11-30
08:04:31 UTC (rev 5375)
+++
portal/branches/branch-GTNPORTAL-1700/component/web/api/src/test/java/org/exoplatform/web/filter/TestExtensibleFilter.java 2010-11-30
08:34:43 UTC (rev 5376)
@@ -52,13 +52,14 @@
public void testDoFilter() throws IOException, ServletException
{
+ String pathRequest = "/testPath";
ExtensibleFilter exFilter = new ExtensibleFilter();
MockFilterOKTF mockFilterOKTF = new MockFilterOKTF();
MockFilterOKWTF mockFilterOKWTF = new MockFilterOKWTF();
MockFilterChain chain = new MockFilterChain();
exFilter.addFilterDefinitions(Arrays.asList(getFilterDefinition(mockFilterOKTF),
getFilterDefinition(mockFilterOKWTF)));
- exFilter.doFilter(new MockServletRequest(), null, chain);
+ exFilter.doFilter(new MockServletRequest(), null, chain, pathRequest);
assertTrue(mockFilterOKTF.start);
assertTrue(mockFilterOKTF.end);
assertTrue(mockFilterOKWTF.start);
@@ -70,7 +71,7 @@
chain = new MockFilterChain();
exFilter.addFilterDefinitions(Arrays.asList(getFilterDefinition(mockFilterOKTF),
getFilterDefinition(mockFilterOKWTF), getFilterDefinition(new
MockFilterKO())));
- exFilter.doFilter(new MockServletRequest(), null, chain);
+ exFilter.doFilter(new MockServletRequest(), null, chain, pathRequest);
assertTrue(mockFilterOKTF.start);
assertTrue(mockFilterOKTF.end);
assertTrue(mockFilterOKWTF.start);
@@ -84,7 +85,7 @@
getFilterDefinition(mockFilterOKWTF), getFilterDefinition(new
MockFilterKOIO())));
try
{
- exFilter.doFilter(new MockServletRequest(), null, chain);
+ exFilter.doFilter(new MockServletRequest(), null, chain, pathRequest);
fail("IOException is expected");
}
catch (IOException e)
@@ -103,7 +104,7 @@
getFilterDefinition(mockFilterOKWTF), getFilterDefinition(new
MockFilterKOSE())));
try
{
- exFilter.doFilter(new MockServletRequest(), null, chain);
+ exFilter.doFilter(new MockServletRequest(), null, chain, pathRequest);
fail("ServletException is expected");
}
catch (ServletException e)
@@ -122,7 +123,7 @@
getFilterDefinition(mockFilterOKWTF), getFilterDefinition(new
MockFilterKORE())));
try
{
- exFilter.doFilter(new MockServletRequest(), null, chain);
+ exFilter.doFilter(new MockServletRequest(), null, chain, pathRequest);
fail("RuntimeException is expected");
}
catch (RuntimeException e)
@@ -141,7 +142,7 @@
getFilterDefinition(mockFilterOKWTF), getFilterDefinition(new
MockFilterKOER())));
try
{
- exFilter.doFilter(new MockServletRequest(), null, chain);
+ exFilter.doFilter(new MockServletRequest(), null, chain, pathRequest);
fail("Error is expected");
}
catch (Error e)
@@ -162,7 +163,7 @@
getFilterDefinition(mockFilterOKTF2)));
try
{
- exFilter.doFilter(new MockServletRequest(), null, chain);
+ exFilter.doFilter(new MockServletRequest(), null, chain, pathRequest);
fail("IOException is expected");
}
catch (IOException e)