/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.wizzardo.http;

import com.wizzardo.http.request.Request;
import com.wizzardo.http.response.Response;

import java.util.*;
import java.util.regex.Pattern;

/**
 * @author Moxa
 */
public class FiltersMapping {

    protected Map<String, List<Filter>> mappingBefore = new HashMap<String, List<Filter>>();
    protected Map<Pattern, List<Filter>> regexpMappingBefore = new LinkedHashMap<Pattern, List<Filter>>();

    protected Map<String, List<Filter>> mappingAfter = new HashMap<String, List<Filter>>();
    protected Map<Pattern, List<Filter>> regexpMappingAfter = new LinkedHashMap<Pattern, List<Filter>>();

    public FiltersMapping addBefore(String url, Filter handler) {
        return add(url, handler, mappingBefore, regexpMappingBefore);
    }

    public FiltersMapping addAfter(String url, Filter handler) {
        return add(url, handler, mappingAfter, regexpMappingAfter);
    }

    protected FiltersMapping add(String url, Filter handler, Map<String, List<Filter>> mapping, Map<Pattern, List<Filter>> regexpMapping) {
        if (url.contains("*"))
            add(Pattern.compile(url.replace("*", ".*")), handler, regexpMapping);
        else {
            List<Filter> l = mapping.get(url);
            if (l == null) {
                l = new ArrayList<>();
                mapping.put(url, l);
            }
            l.add(handler);
        }
        return this;
    }

    protected FiltersMapping add(Pattern url, Filter handler, Map<Pattern, List<Filter>> regexpMapping) {
        List<Filter> l = regexpMapping.get(url);
        if (l == null) {
            l = new ArrayList<>();
            regexpMapping.put(url, l);
        }
        l.add(handler);
        return this;
    }

    public boolean filter(Request request, Response response, Map<String, List<Filter>> mapping, Map<Pattern, List<Filter>> regexpMapping) {
        List<Filter> filters = mapping.get(request.path().toString());
        if (filters != null)
            if (!filter(mapping.get(request.path().toString()), request, response))
                return false;

        for (Map.Entry<Pattern, List<Filter>> entry : regexpMapping.entrySet()) {
            if (entry.getKey().matcher(request.path().toString()).matches()) {
                if (!filter(regexpMapping.get(entry.getKey()), request, response))
                    return false;
            }
        }

        return true;
    }

    public boolean before(Request request, Response response) {
        return filter(request, response, mappingBefore, regexpMappingBefore);
    }

    public boolean after(Request request, Response response) {
        return filter(request, response, mappingAfter, regexpMappingAfter);
    }

    protected boolean filter(List<Filter> filters, Request request, Response response) {
        for (Filter f : filters) {
            if (!f.filter(request, response))
                return false;

        }
        return true;
    }
}
@@ -225,17 +225,25 @@
 va.util.
-*
+ArrayList
 ;%0Aimport
@@ -257,21 +257,12 @@
 til.
-regex.Pattern
+List
 ;%0A%0A/
@@ -325,36 +325,35 @@
   protected 
-Map%3CString, 
+UrlMapping%3C
 List%3CFilter%3E
@@ -354,24 +354,17 @@
 ilter%3E%3E 
-mappingB
+b
 efore = 
@@ -371,342 +371,86 @@
 new 
-HashMap%3CString, List%3CFilter%3E%3E();%0A    protected Map%3CPattern, List%3CFilter%3E%3E regexpMappingBefore = new LinkedHashMap%3CPattern, List%3CFilter%3E%3E();%0A%0A    protected Map%3CString, List%3CFilter%3E%3E mappingAfter = new HashMap%3CString, List%3CFilter%3E%3E();%0A    protected Map%3CPattern, List%3CFilter%3E%3E regexpMappingAfter = new LinkedHashMap%3CPattern, List%3CFilter%3E
+UrlMapping%3C%3E();%0A    protected UrlMapping%3CList%3CFilter%3E%3E after = new UrlMapping%3C
 %3E();
@@ -554,37 +554,9 @@
 er, 
-mappingBefore, regexpMappingB
+b
 efor
@@ -668,36 +668,9 @@
 er, 
-mappingAfter, regexpMappingA
+a
 fter
@@ -744,212 +744,41 @@
 er, 
-Map%3CString, List%3CFilter%3E%3E mapping, Map%3CPattern, List%3CFilter%3E%3E regexpMapping) %7B%0A        if (url.contains(%22*%22))%0A            add(Pattern.compile(url.replace(%22*%22, %22.*%22)), handler, regexpMapping);%0A        else
+UrlMapping%3CList%3CFilter%3E%3E mapping)
  %7B%0A 
@@ -780,28 +780,24 @@
 ) %7B%0A        
-    
 List%3CFilter%3E
@@ -798,16 +798,19 @@
 ilter%3E l
+ist
  = mappi
@@ -826,33 +826,32 @@
 l);%0A        
-    
 if (l
+ist
  == null) %7B%0A
@@ -851,430 +851,68 @@
 ull)
- %7B%0A                l = new ArrayList%3C%3E();%0A                mapping.put(url, l);%0A            %7D%0A            l.add(handler);%0A        %7D%0A        return this;%0A    %7D%0A%0A    protected FiltersMapping add(Pattern url, Filter handler, Map%3CPattern, List%3CFilter%3E%3E regexpMapping) %7B%0A        List%3CFilter%3E l = regexpMapping.get(url);%0A        if (l == null) %7B%0A            l = new ArrayList%3C%3E();%0A            regexpMapping.put(url, l);%0A        %7D
+%0A            mapping.append(url, list = new ArrayList%3C%3E());%0A
 %0A   
@@ -917,16 +917,19 @@
        l
+ist
 .add(han
@@ -1029,56 +1029,19 @@
 se, 
-Map%3CString, List%3CFilter%3E%3E mapping, Map%3CPattern, 
+UrlMapping%3C
 List
@@ -1050,23 +1050,17 @@
 ilter%3E%3E 
-regexpM
+m
 apping) 
@@ -1111,34 +1111,16 @@
 (request
-.path().toString()
 );%0A     
@@ -1171,321 +1171,15 @@
 ter(
-mapping.get(request.path().toString()), request, response))%0A                return false;%0A%0A        for (Map.Entry%3CPattern, List%3CFilter%3E%3E entry : regexpMapping.entrySet()) %7B%0A            if (entry.getKey().matcher(request.path().toString()).matches()) %7B%0A                if (!filter(regexpMapping.get(entry.getKey())
+filters
 , re
@@ -1204,36 +1204,32 @@
                 
-    
 return false;%0A  
@@ -1229,32 +1229,8 @@
 lse;
-%0A            %7D%0A        %7D
 %0A%0A  
@@ -1364,37 +1364,9 @@
 se, 
-mappingBefore, regexpMappingB
+b
 efor
@@ -1484,36 +1484,9 @@
 se, 
-mappingAfter, regexpMappingA
+a
 fter

rewrite FilterMapping