Skip to content

Commit 5516754

Browse files
author
Igor Polevoy
committed
#349 Different instances of the same filter result in just one last instance of a filter configured
1 parent f567799 commit 5516754

File tree

3 files changed

+25
-18
lines changed

3 files changed

+25
-18
lines changed

activeweb/src/main/java/org/javalite/activeweb/Configuration.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ enum Params {
6060

6161
private static boolean filtersInjected = false;
6262

63-
private static Map<Class, FilterMetadata> filterMetadataMap = new HashMap<>();
63+
private static Map<HttpSupport, FilterMetadata> filterMetadataMap = new HashMap<>();
6464

6565
static{
6666
try {
@@ -314,10 +314,10 @@ protected static void injectFilters() {
314314
//no need to synchronize here, since FilterConfig objects will be created
315315
//by a single thread when the app is bootstrapped.
316316
static FilterMetadata getFilterMetadata(HttpSupportFilter filter){
317-
FilterMetadata config = filterMetadataMap.get(filter.getClass());
317+
FilterMetadata config = filterMetadataMap.get(filter);
318318
if(config == null){
319319
config = new FilterMetadata();
320-
filterMetadataMap.put(filter.getClass(), config);
320+
filterMetadataMap.put(filter, config);
321321
}
322322
return config;
323323
}

activeweb/src/main/java/org/javalite/activeweb/ControllerRunner.java

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ protected void run(Route route, boolean integrateViews) throws Exception {
5353

5454
String actionMethod = Inflector.camelize(route.getActionName().replace('-', '_'), false);
5555
if (checkActionMethod(route.getController(), actionMethod)) {
56-
//Configuration.getTemplateManager().
5756
injectController(route.getController());
57+
LOGGER.debug("Executing: " + route.getController() + "#" + actionMethod);
5858
executeAction(route.getController(), actionMethod);
5959
}
6060
}
@@ -207,11 +207,9 @@ private boolean checkActionMethod(AppController controller, String actionMethod)
207207
private boolean exceptionHandled(Exception e, Route route) throws Exception{
208208
for(HttpSupportFilter filter: Configuration.getFilters()){
209209
if(Configuration.getFilterMetadata(filter).matches(route)){
210-
if (Configuration.logRequestParams()) {
211-
LOGGER.debug("Executing filter: " + filter.getClass().getName() + "#before");
212-
}
210+
LOGGER.debug("Executing filter: " + filter.getClass().getName() + "#exceptionHandled");
213211
filter.onException(e);
214-
if(RequestContext.getControllerResponse() != null){ // this filter sent a response, breaking the loop
212+
if (RequestContext.getControllerResponse() != null){ // this filter sent a response, breaking the loop
215213
break;
216214
}
217215
}
@@ -227,16 +225,10 @@ private boolean exceptionHandled(Exception e, Route route) throws Exception{
227225
*/
228226
private void filterBefore(Route route) {
229227
try {
230-
List<HttpSupportFilter> filters = Configuration.getFilters();
231228

232229
for(HttpSupportFilter filter: Configuration.getFilters()){
233-
if (Configuration.logRequestParams()) {
234-
LOGGER.debug("Executing filter: " + filter.getClass().getName() + "#before");
235-
}
236230
if(Configuration.getFilterMetadata(filter).matches(route)){
237-
if (Configuration.logRequestParams()) {
238-
LOGGER.debug("Executing filter: " + filter.getClass().getName() + "#after");
239-
}
231+
LOGGER.debug("Executing filter: " + filter.getClass().getName() + "#before");
240232
filter.before();
241233
}
242234
if (RequestContext.getControllerResponse() != null){
@@ -256,9 +248,7 @@ private void filterAfter(Route route) {
256248
for (int i = filters.size() - 1; i >= 0; i--) {
257249
HttpSupportFilter filter = filters.get(i);
258250
if(Configuration.getFilterMetadata(filter).matches(route)){
259-
if (Configuration.logRequestParams()) {
260-
LOGGER.debug("Executing filter: " + filter.getClass().getName() + "#after");
261-
}
251+
LOGGER.debug("Executing filter: " + filter.getClass().getName() + "#after");
262252
filters.get(i).after();
263253
}
264254
}

activeweb/src/test/java/org/javalite/activeweb/AbstractControllerConfigSpec.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.javalite.activeweb;
1717

18+
import org.javalite.activeweb.controller_filters.DBConnectionFilter;
1819
import org.javalite.activeweb.controller_filters.HttpSupportFilter;
1920
import org.javalite.activeweb.mock.*;
2021
import org.javalite.common.Util;
@@ -221,5 +222,21 @@ public void shouldTriggerFiltersInOrderOfDefinition() throws IOException, Servle
221222
the(getLine(7)).shouldBeEqual("GlobalFilter2 after");
222223
the(getLine(8)).shouldBeEqual("GlobalFilter1 after");
223224
}
225+
226+
@Test
227+
public void shouldAllowMultipleInstancesOfFilterRegistered() {
228+
config = new AbstractControllerConfig() {
229+
public void init(AppContext context) {
230+
add(new DBConnectionFilter());
231+
add(new DBConnectionFilter("another")).to(LibraryController.class);
232+
}
233+
};
234+
config.init(new AppContext());
235+
config.completeInit();
236+
List<HttpSupportFilter> filters = Configuration.getFilters();
237+
FilterMetadata filter1Metadata = Configuration.getFilterMetadata(filters.get(0));
238+
FilterMetadata filter2Metadata = Configuration.getFilterMetadata(filters.get(1));
239+
the(filter1Metadata).shouldNotBeTheSameAs(filter2Metadata);
240+
}
224241
}
225242

0 commit comments

Comments
 (0)