Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
174 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
wro4j-core/src/main/java/ro/isdc/wro/http/WroContextFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright (c) 2008. All rights reserved. | ||
*/ | ||
package ro.isdc.wro.http; | ||
|
||
import java.io.IOException; | ||
|
||
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; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import ro.isdc.wro.config.Context; | ||
import ro.isdc.wro.http.support.ServletContextAttributeHelper; | ||
|
||
|
||
/** | ||
* This filter is responsible for setting the {@link Context} to the current request cycle. This is required if you want | ||
* to use {@link ServletContextAttributeHelper} in order to access wro related attributes from within a tag or a | ||
* servlet. Usually this filter will be mapped to all requests: | ||
* | ||
* <pre> | ||
* <filter-mapping> | ||
* <filter-name>wroContextFilter</filter-name> | ||
* <url-pattern>/*</url-pattern> | ||
* </filter-mapping> | ||
* | ||
* </pre> | ||
* | ||
* @author Alex Objelean | ||
* @created 12 May 2012 | ||
* @since 1.4.7 | ||
*/ | ||
public class WroContextFilter | ||
implements Filter { | ||
private FilterConfig filterConfig; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public void init(final FilterConfig filterConfig) | ||
throws ServletException { | ||
this.filterConfig = filterConfig; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public final void doFilter(final ServletRequest req, final ServletResponse res, final FilterChain chain) | ||
throws IOException, ServletException { | ||
final HttpServletRequest request = (HttpServletRequest) req; | ||
final HttpServletResponse response = (HttpServletResponse) res; | ||
try { | ||
Context.set(Context.webContext(request, response, this.filterConfig)); | ||
chain.doFilter(request, response); | ||
} finally { | ||
Context.unset(); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public void destroy() { | ||
Context.destroy(); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
wro4j-core/src/test/java/ro/isdc/wro/http/TestWroContextFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright (c) 2010. All rights reserved. | ||
*/ | ||
package ro.isdc.wro.http; | ||
|
||
import javax.servlet.FilterChain; | ||
import javax.servlet.FilterConfig; | ||
import javax.servlet.ServletContext; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.MockitoAnnotations; | ||
import org.mockito.invocation.InvocationOnMock; | ||
import org.mockito.stubbing.Answer; | ||
|
||
import ro.isdc.wro.config.Context; | ||
|
||
|
||
/** | ||
* Test for {@link WroFilter} class. | ||
* | ||
* @author Alex Objelean | ||
* @created Created on Jul 13, 2009 | ||
*/ | ||
public class TestWroContextFilter { | ||
private WroContextFilter victim; | ||
@Mock | ||
private FilterConfig mockFilterConfig; | ||
@Mock | ||
private ServletContext mockServletContext; | ||
@Mock | ||
private HttpServletRequest mockRequest; | ||
@Mock | ||
private HttpServletResponse mockResponse; | ||
@Mock | ||
private FilterChain mockFilterChain; | ||
|
||
@Before | ||
public void setUp() | ||
throws Exception { | ||
MockitoAnnotations.initMocks(this); | ||
victim = new WroContextFilter(); | ||
Mockito.when(mockFilterConfig.getServletContext()).thenReturn(mockServletContext); | ||
victim.init(mockFilterConfig); | ||
} | ||
|
||
@Test | ||
public void shouldInitializeContextForChainedFilters() throws Exception { | ||
Mockito.doAnswer(new Answer<Void>() { | ||
public Void answer(InvocationOnMock invocation) | ||
throws Throwable { | ||
Assert.assertTrue(Context.isContextSet()); | ||
return null; | ||
} | ||
}).when(mockFilterChain).doFilter(Mockito.any(HttpServletRequest.class), Mockito.any(HttpServletResponse.class)); | ||
victim.doFilter(mockRequest, mockResponse, mockFilterChain); | ||
Mockito.verify(mockFilterChain, Mockito.times(1)).doFilter(Mockito.any(HttpServletRequest.class), | ||
Mockito.any(HttpServletResponse.class)); | ||
//After chain processing, the context must be unset | ||
Assert.assertFalse(Context.isContextSet()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters