]> source.dussan.org Git - gitblit.git/commitdiff
Allow filter extensions to define init parameters in IStoredSettings
authorJames Moger <james.moger@gitblit.com>
Mon, 21 Apr 2014 20:15:58 +0000 (16:15 -0400)
committerJames Moger <james.moger@gitblit.com>
Mon, 5 May 2014 15:16:30 +0000 (11:16 -0400)
src/main/java/com/gitblit/servlet/FilterRuntimeConfig.java [new file with mode: 0644]
src/main/java/com/gitblit/servlet/ProxyFilter.java

diff --git a/src/main/java/com/gitblit/servlet/FilterRuntimeConfig.java b/src/main/java/com/gitblit/servlet/FilterRuntimeConfig.java
new file mode 100644 (file)
index 0000000..9f0c0ac
--- /dev/null
@@ -0,0 +1,71 @@
+/*
+ * Copyright 2014 gitblit.com.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.gitblit.servlet;
+
+import java.util.Enumeration;
+
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletContext;
+
+import com.gitblit.IStoredSettings;
+import com.gitblit.manager.IRuntimeManager;
+
+/**
+ * Wraps a filter config and will prefer a setting retrieved from IStoredSettings
+ * if one is available.
+ *
+ * @author James Moger
+ * @since 1.6.0
+ */
+public class FilterRuntimeConfig implements FilterConfig {
+
+       final IRuntimeManager runtime;
+       final IStoredSettings settings;
+       final String namespace;
+       final FilterConfig config;
+
+       public FilterRuntimeConfig(IRuntimeManager runtime, String namespace, FilterConfig config) {
+               this.runtime = runtime;
+               this.settings = runtime.getSettings();
+               this.namespace = namespace;
+               this.config = config;
+       }
+
+       @Override
+       public String getFilterName() {
+               return config.getFilterName();
+       }
+
+       @Override
+       public ServletContext getServletContext() {
+               return config.getServletContext();
+       }
+
+       @Override
+       public String getInitParameter(String name) {
+               String key = namespace + "." + name;
+               if (settings.hasSettings(key)) {
+                       String value = settings.getString(key, null);
+                       return value;
+               }
+               return config.getInitParameter(name);
+       }
+
+       @Override
+       public Enumeration<String> getInitParameterNames() {
+               return config.getInitParameterNames();
+       }
+}
index bfdc87f3997f0f54344e3d1652e76248ca1d30ee..46f59de92fd40fe3e266ea82c74dde5a31345fe8 100644 (file)
@@ -25,14 +25,17 @@ import javax.servlet.ServletException;
 import javax.servlet.ServletRequest;
 import javax.servlet.ServletResponse;
 
+import ro.fortsoft.pf4j.PluginWrapper;
+
 import com.gitblit.dagger.DaggerFilter;
 import com.gitblit.extensions.HttpRequestFilter;
 import com.gitblit.manager.IPluginManager;
+import com.gitblit.manager.IRuntimeManager;
 
 import dagger.ObjectGraph;
 
 /**
- * A request filter than allows regsitered extension request filters to access
+ * A request filter than allows registered extension request filters to access
  * request data.  The intended purpose is for server monitoring plugins.
  *
  * @author David Ostrovsky
@@ -43,10 +46,17 @@ public class ProxyFilter extends DaggerFilter {
 
        @Override
        protected void inject(ObjectGraph dagger, FilterConfig filterConfig) throws ServletException {
+               IRuntimeManager runtimeManager = dagger.get(IRuntimeManager.class);
                IPluginManager pluginManager = dagger.get(IPluginManager.class);
+
                filters = pluginManager.getExtensions(HttpRequestFilter.class);
                for (HttpRequestFilter f : filters) {
-                       f.init(filterConfig);
+                       // wrap the filter config for Gitblit settings retrieval
+                       PluginWrapper pluginWrapper = pluginManager.whichPlugin(f.getClass());
+                       FilterConfig runtimeConfig = new FilterRuntimeConfig(runtimeManager,
+                                       pluginWrapper.getPluginId(), filterConfig);
+
+                       f.init(runtimeConfig);
                }
        }