package com.github.digital_wonderland.sling_metrics.filter;

import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;
import com.github.digital_wonderland.sling_metrics.service.MetricService;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.sling.SlingFilter;
import org.apache.felix.scr.annotations.sling.SlingFilterScope;
import org.apache.sling.api.SlingHttpServletRequest;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.*;
import java.io.IOException;

@SlingFilter(scope = SlingFilterScope.COMPONENT,
        order = -10000,
        metatype = true,
        name = "Metrics Component Filter",
        description = "Sling Components Filter which generates Metrics for Components")
public class ComponentFilter implements javax.servlet.Filter {

    private static final Logger LOG = LoggerFactory.getLogger(ComponentFilter.class);

    @Property(label = "Enabled", boolValue = true, description = "Should the filter be enabled")
    private static final String COMPONENT_FILTER_ENABLED = "componentFilter.enabled";

    @Reference
    protected MetricService metricService;

    private boolean isEnabled = false;

    @Activate
    protected void activate(final ComponentContext context) {
        isEnabled = (Boolean) context.getProperties().get(COMPONENT_FILTER_ENABLED);
        LOG.debug("ComponentFilter.isEnabled: [{}]", isEnabled);
    }

    @Override
    public void init(final FilterConfig filterConfig) throws ServletException {
        LOG.debug("ComponentFilter init()");
    }

    @Override
    public void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse, final FilterChain filterChain) throws IOException, ServletException {
        LOG.debug("ComponentFilter doFilter()");

        if(isEnabled && metricService.isEnabled()) {
            if(servletRequest instanceof SlingHttpServletRequest) {
                final SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) servletRequest;
                final String resourceType = slingRequest.getResource().getResourceType();
                final MetricRegistry registry = metricService.getRegistry();
                final Timer.Context context = registry.timer(normalizeResourceType(resourceType)).time();
                try {
                    filterChain.doFilter(servletRequest, servletResponse);
                } finally {
                    context.stop();
                }
            } else {
                LOG.error("Metrics ComponentFilter got called for non SlingHttpServletRequest");
            }
        }
    }

    private String normalizeResourceType(final String resourceType) {
        return ("components." + resourceType).replaceAll("[\\./:]+", ".");
    }

    @Override
    public void destroy() {
        LOG.debug("ComponentFilter destroy()");
    }
}

@@ -1261,16 +1261,256 @@
 bled%22;%0A%0A
+    @Property(label = %22Component Whitelist%22, value = %22.*%22, description = %22Regular expression to filter events by white listing on the resource type%22)%0A    private static final String COMPONENT_FILTER_WHITELIST= %22componentFilter.whitelist%22;%0A%0A
     @Ref
@@ -1516,16 +1516,16 @@
 ference%0A
-
     prot
@@ -1596,24 +1596,62 @@
 d = false;%0A%0A
+    private String whitelist = %22.*%22;%0A%0A
     @Activat
@@ -1799,16 +1799,102 @@
 ABLED);%0A
+        whitelist = (String) context.getProperties().get(COMPONENT_FILTER_WHITELIST);%0A
         
@@ -2648,24 +2648,159 @@
 urceType();%0A
+                final String metricName = normalizeResourceType(resourceType);%0A                if(metricName.matches(whitelist)) %7B%0A    
             
@@ -2876,24 +2876,28 @@
             
+    
 final Timer.
@@ -2933,43 +2933,18 @@
 mer(
-normalizeResourceType(resourceType)
+metricName
 ).ti
@@ -2969,14 +2969,22 @@
     
+    
 try %7B%0A
+    
     
@@ -3050,24 +3050,28 @@
 tResponse);%0A
+    
             
@@ -3106,16 +3106,20 @@
         
+    
 context.
@@ -3122,24 +3122,46 @@
 ext.stop();%0A
+                    %7D%0A
             

add whitelisting to component filter