Author: ndkhoiits
Date: 2010-11-30 04:05:47 -0500 (Tue, 30 Nov 2010)
New Revision: 5380
Modified:
exo/portal/branches/3.1.x/component/web/src/main/java/org/exoplatform/web/filter/ExtensibleFilter.java
exo/portal/branches/3.1.x/component/web/src/main/java/org/exoplatform/web/filter/GenericFilter.java
exo/portal/branches/3.1.x/component/web/src/test/java/org/exoplatform/web/filter/TestExtensibleFilter.java
Log:
EXOGTN-195 Improve performance for each of request to Extensible filter
Modified:
exo/portal/branches/3.1.x/component/web/src/main/java/org/exoplatform/web/filter/ExtensibleFilter.java
===================================================================
---
exo/portal/branches/3.1.x/component/web/src/main/java/org/exoplatform/web/filter/ExtensibleFilter.java 2010-11-30
08:49:27 UTC (rev 5379)
+++
exo/portal/branches/3.1.x/component/web/src/main/java/org/exoplatform/web/filter/ExtensibleFilter.java 2010-11-30
09:05:47 UTC (rev 5380)
@@ -40,7 +40,7 @@
* nicolas.filotto(a)exoplatform.com
* 25 sept. 2009
*/
-public class ExtensibleFilter implements Filter
+public class ExtensibleFilter
{
/**
@@ -77,10 +77,10 @@
/**
* @see org.exoplatform.web.filter.Filter#doFilter(javax.servlet.ServletRequest,
javax.servlet.ServletResponse, javax.servlet.FilterChain)
*/
- public void doFilter(ServletRequest request, ServletResponse response, FilterChain
chain) throws IOException,
+ 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);
}
@@ -90,19 +90,18 @@
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:
exo/portal/branches/3.1.x/component/web/src/main/java/org/exoplatform/web/filter/GenericFilter.java
===================================================================
---
exo/portal/branches/3.1.x/component/web/src/main/java/org/exoplatform/web/filter/GenericFilter.java 2010-11-30
08:49:27 UTC (rev 5379)
+++
exo/portal/branches/3.1.x/component/web/src/main/java/org/exoplatform/web/filter/GenericFilter.java 2010-11-30
09:05:47 UTC (rev 5380)
@@ -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
@@ -40,13 +44,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 +74,8 @@
}
else
{
- filter.doFilter(request, response, chain);
+ String path =
contextPathPattern.matcher(((HttpServletRequest)request).getRequestURI()).replaceFirst("/");
+ filter.doFilter(request, response, chain, path);
}
}
}
Modified:
exo/portal/branches/3.1.x/component/web/src/test/java/org/exoplatform/web/filter/TestExtensibleFilter.java
===================================================================
---
exo/portal/branches/3.1.x/component/web/src/test/java/org/exoplatform/web/filter/TestExtensibleFilter.java 2010-11-30
08:49:27 UTC (rev 5379)
+++
exo/portal/branches/3.1.x/component/web/src/test/java/org/exoplatform/web/filter/TestExtensibleFilter.java 2010-11-30
09:05:47 UTC (rev 5380)
@@ -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)