package hudson.plugins.scm_sync_configuration.extensions;

import hudson.Extension;
import hudson.plugins.scm_sync_configuration.ScmSyncConfigurationDataProvider;
import hudson.plugins.scm_sync_configuration.ScmSyncConfigurationPlugin;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.concurrent.Callable;

/**
 * @author fcamblor
 * Very important class in the plugin : it is the entry point allowing to decide what files should be
 * synchronized or not during current thread execution
 */
@Extension
public class ScmSyncConfigurationFilter implements Filter {

    public void init(FilterConfig filterConfig) throws ServletException {
    }

    public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException {
        // In the beginning of every http request, we should create a new threaded transaction
        final ScmSyncConfigurationPlugin plugin = ScmSyncConfigurationPlugin.getInstance();
        if(plugin != null){
            plugin.startThreadedTransaction();

            try {
                // Providing current ServletRequest in ScmSyncConfigurationDataProvider's thread local
                // in order to be able to access it from everywhere inside this call
                ScmSyncConfigurationDataProvider.provideRequestDuring((HttpServletRequest)request, new Callable() {
                    public Object call() throws Exception {
                        try {
                            // Handling "normally" http request
                            chain.doFilter(request, response);
                        }finally{
                            // In the end of http request, we should commit current transaction
                            plugin.getTransaction().commit();
                        }

                        return null;
                    }
                });
            } catch(RuntimeException e){
                throw e;
            } catch(ServletException e){
                throw e;
            } catch(IOException e){
                throw e;
            } catch(Exception e){
                throw new ServletException(e);
            }
        }
    }

    public void destroy() {
    }
}

@@ -365,16 +365,48 @@
 allable;
+%0Aimport java.util.logging.Level;
 %0A%0A/**%0A *
@@ -656,16 +656,153 @@
 lter %7B%0A%0A
+    private static final java.util.logging.Logger LOG = java.util.logging.Logger.getLogger(ScmSyncConfigurationFilter.class.getName());%0A%0A
     publ
@@ -1161,16 +1161,50 @@
 nPlugin 
+plugin;%0A        try %7B%0A            
 plugin =
@@ -1242,24 +1242,221 @@
 Instance();%0A
+        %7D catch(Throwable t)%7B%0A            LOG.log(Level.SEVERE, %22Error when retrieving ScmSyncConfig plugin instance =%3E No filtering enabled on current request%22, t);%0A            return;%0A        %7D%0A%0A
         if(p

fix for JENKINS-15734 : avoiding strange NPE during request filtering