]> source.dussan.org Git - gitblit.git/commitdiff
Add http request filter extension point
authorDavid Ostrovsky <david@ostrovsky.org>
Mon, 10 Mar 2014 00:50:49 +0000 (01:50 +0100)
committerJames Moger <james.moger@gitblit.com>
Mon, 5 May 2014 15:16:30 +0000 (11:16 -0400)
To allow for integration of 3rd party server monitoring solutions,
Gitblit needs to expose an extension point for collecting http data.

src/main/java/WEB-INF/web.xml
src/main/java/com/gitblit/dagger/DaggerFilter.java
src/main/java/com/gitblit/extensions/HttpRequestFilter.java [new file with mode: 0644]
src/main/java/com/gitblit/servlet/AccessRestrictionFilter.java
src/main/java/com/gitblit/servlet/AuthenticationFilter.java
src/main/java/com/gitblit/servlet/EnforceAuthenticationFilter.java
src/main/java/com/gitblit/servlet/GitFilter.java
src/main/java/com/gitblit/servlet/ProxyFilter.java [new file with mode: 0644]
src/main/java/com/gitblit/servlet/RpcFilter.java
src/main/java/com/gitblit/servlet/SyndicationFilter.java
src/site/plugins_extensions.mkd

index cb483af43ef38ff678a11de960621c3710636774..3a6c449027c6d4c6dd0f293dcc0b5e5db35abaa2 100644 (file)
                <url-pattern>/robots.txt</url-pattern>\r
        </servlet-mapping>\r
 \r
+    <filter>\r
+               <filter-name>ProxyFilter</filter-name>\r
+               <filter-class>com.gitblit.servlet.ProxyFilter</filter-class>\r
+       </filter>\r
+       <filter-mapping>\r
+               <filter-name>ProxyFilter</filter-name>\r
+               <url-pattern>/*</url-pattern>\r
+       </filter-mapping>\r
+       \r
        <!-- Git Access Restriction Filter\r
                 <url-pattern> MUST match: \r
                        * GitServlet\r
         <url-pattern>/*</url-pattern>\r
     </filter-mapping>\r
     \r
-</web-app>\r
+</web-app>
\ No newline at end of file
index 1c73d4b759a8774403b42f4886ca747980af9b7f..01c07a4a783d62e25067dbeb89ab9f92695a9b5d 100644 (file)
@@ -36,10 +36,10 @@ public abstract class DaggerFilter implements Filter {
        public final void init(FilterConfig filterConfig) throws ServletException {\r
                ServletContext context = filterConfig.getServletContext();\r
                ObjectGraph objectGraph = (ObjectGraph) context.getAttribute(DaggerContext.INJECTOR_NAME);\r
-               inject(objectGraph);\r
+               inject(objectGraph, filterConfig);\r
        }\r
 \r
-       protected abstract void inject(ObjectGraph dagger);\r
+       protected abstract void inject(ObjectGraph dagger, FilterConfig filterConfig) throws ServletException;\r
 \r
        @Override\r
        public void destroy() {\r
diff --git a/src/main/java/com/gitblit/extensions/HttpRequestFilter.java b/src/main/java/com/gitblit/extensions/HttpRequestFilter.java
new file mode 100644 (file)
index 0000000..e3e330c
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * 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.extensions;
+
+import java.io.IOException;
+
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+
+import ro.fortsoft.pf4j.ExtensionPoint;
+
+/**
+ * Extension point to intercept HTTP requests passing through the server.
+ *
+ * @author David Ostrovsky
+ * @since 1.6.0
+ *
+ */
+public abstract class HttpRequestFilter implements Filter, ExtensionPoint {
+
+       @Override
+       public void init(FilterConfig config) throws ServletException {
+       }
+
+       @Override
+       public void destroy() {
+       }
+
+       @Override
+       public abstract void doFilter(ServletRequest request, ServletResponse response,
+                       FilterChain chain) throws IOException, ServletException;
+}
index e6a0169c010bb8dda73aa19380239b2583d8bded..0e6d323d0762cc2e7ced65852914bbe482f6a0a4 100644 (file)
@@ -19,6 +19,7 @@ import java.io.IOException;
 import java.text.MessageFormat;\r
 \r
 import javax.servlet.FilterChain;\r
+import javax.servlet.FilterConfig;\r
 import javax.servlet.ServletException;\r
 import javax.servlet.ServletRequest;\r
 import javax.servlet.ServletResponse;\r
@@ -54,8 +55,8 @@ public abstract class AccessRestrictionFilter extends AuthenticationFilter {
        protected IRepositoryManager repositoryManager;\r
 \r
        @Override\r
-       protected void inject(ObjectGraph dagger) {\r
-               super.inject(dagger);\r
+       protected void inject(ObjectGraph dagger, FilterConfig filterConfig) {\r
+               super.inject(dagger, filterConfig);\r
                this.runtimeManager = dagger.get(IRuntimeManager.class);\r
                this.repositoryManager = dagger.get(IRepositoryManager.class);\r
        }\r
index dd821ac70f2cf086cdde220daf330cf4f20f6425..5710a4af8f9e508684f0c1b596f535842bc2f967 100644 (file)
@@ -24,6 +24,7 @@ import java.util.HashMap;
 import java.util.Map;\r
 \r
 import javax.servlet.FilterChain;\r
+import javax.servlet.FilterConfig;\r
 import javax.servlet.ServletException;\r
 import javax.servlet.ServletRequest;\r
 import javax.servlet.ServletResponse;\r
@@ -64,7 +65,7 @@ public abstract class AuthenticationFilter extends DaggerFilter {
        protected IAuthenticationManager authenticationManager;\r
 \r
        @Override\r
-       protected void inject(ObjectGraph dagger) {\r
+       protected void inject(ObjectGraph dagger, FilterConfig filterConfig) {\r
                this.authenticationManager = dagger.get(IAuthenticationManager.class);\r
        }\r
 \r
index 5fdccb714c6bd04ce5f486d665ae3ce00a959d4f..c015021d0877669cb0c65a08676b45f3d31ef7ad 100644 (file)
@@ -19,6 +19,7 @@ import java.io.IOException;
 import java.text.MessageFormat;
 
 import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
 import javax.servlet.ServletException;
 import javax.servlet.ServletRequest;
 import javax.servlet.ServletResponse;
@@ -53,7 +54,7 @@ public class EnforceAuthenticationFilter extends DaggerFilter {
        private IAuthenticationManager authenticationManager;
 
        @Override
-       protected void inject(ObjectGraph dagger) {
+       protected void inject(ObjectGraph dagger, FilterConfig filterConfig) {
                this.settings = dagger.get(IStoredSettings.class);
                this.authenticationManager = dagger.get(IAuthenticationManager.class);
        }
index f9c062d29315338074bbc907b5b93aa450b68698..bb3d3216e9559260e8a5dce658fde612001b72c3 100644 (file)
@@ -17,6 +17,7 @@ package com.gitblit.servlet;
 \r
 import java.text.MessageFormat;\r
 \r
+import javax.servlet.FilterConfig;\r
 import javax.servlet.http.HttpServletRequest;\r
 \r
 import com.gitblit.Constants.AccessRestrictionType;\r
@@ -53,8 +54,8 @@ public class GitFilter extends AccessRestrictionFilter {
        private IFederationManager federationManager;\r
 \r
        @Override\r
-       protected void inject(ObjectGraph dagger) {\r
-               super.inject(dagger);\r
+       protected void inject(ObjectGraph dagger, FilterConfig filterConfig) {\r
+               super.inject(dagger, filterConfig);\r
                this.settings = dagger.get(IStoredSettings.class);\r
                this.federationManager = dagger.get(IFederationManager.class);\r
        }\r
diff --git a/src/main/java/com/gitblit/servlet/ProxyFilter.java b/src/main/java/com/gitblit/servlet/ProxyFilter.java
new file mode 100644 (file)
index 0000000..bfdc87f
--- /dev/null
@@ -0,0 +1,76 @@
+/*
+ * 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.io.IOException;
+import java.util.Iterator;
+import java.util.List;
+
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+
+import com.gitblit.dagger.DaggerFilter;
+import com.gitblit.extensions.HttpRequestFilter;
+import com.gitblit.manager.IPluginManager;
+
+import dagger.ObjectGraph;
+
+/**
+ * A request filter than allows regsitered extension request filters to access
+ * request data.  The intended purpose is for server monitoring plugins.
+ *
+ * @author David Ostrovsky
+ * @since 1.6.0
+ */
+public class ProxyFilter extends DaggerFilter {
+       private List<HttpRequestFilter> filters;
+
+       @Override
+       protected void inject(ObjectGraph dagger, FilterConfig filterConfig) throws ServletException {
+               IPluginManager pluginManager = dagger.get(IPluginManager.class);
+               filters = pluginManager.getExtensions(HttpRequestFilter.class);
+               for (HttpRequestFilter f : filters) {
+                       f.init(filterConfig);
+               }
+       }
+
+       @Override
+       public void doFilter(ServletRequest req, ServletResponse res, final FilterChain last)
+                       throws IOException, ServletException {
+               final Iterator<HttpRequestFilter> itr = filters.iterator();
+               new FilterChain() {
+                       @Override
+                       public void doFilter(ServletRequest req, ServletResponse res) throws IOException,
+                                       ServletException {
+                               if (itr.hasNext()) {
+                                       itr.next().doFilter(req, res, this);
+                               } else {
+                                       last.doFilter(req, res);
+                               }
+                       }
+               }.doFilter(req, res);
+       }
+
+       @Override
+       public void destroy() {
+               for (HttpRequestFilter f : filters) {
+                       f.destroy();
+               }
+       }
+}
index e0b1a233f913b0bfe54a646d9cacd8b4c3da3ade..23bf956e6fe9a37b95377a2d8901b619c556c4df 100644 (file)
@@ -19,6 +19,7 @@ import java.io.IOException;
 import java.text.MessageFormat;\r
 \r
 import javax.servlet.FilterChain;\r
+import javax.servlet.FilterConfig;\r
 import javax.servlet.ServletException;\r
 import javax.servlet.ServletRequest;\r
 import javax.servlet.ServletResponse;\r
@@ -53,8 +54,8 @@ public class RpcFilter extends AuthenticationFilter {
        private IRuntimeManager runtimeManager;\r
 \r
        @Override\r
-       protected void inject(ObjectGraph dagger) {\r
-               super.inject(dagger);\r
+       protected void inject(ObjectGraph dagger, FilterConfig filterConfig) {\r
+               super.inject(dagger, filterConfig);\r
                this.settings = dagger.get(IStoredSettings.class);\r
                this.runtimeManager = dagger.get(IRuntimeManager.class);\r
        }\r
index 67a845eaf1d883758b34faae9130e3e971b73c61..78da47e92a2a5d324c0eec8bc97c6b3d39164957 100644 (file)
@@ -19,6 +19,7 @@ import java.io.IOException;
 import java.text.MessageFormat;\r
 \r
 import javax.servlet.FilterChain;\r
+import javax.servlet.FilterConfig;\r
 import javax.servlet.ServletException;\r
 import javax.servlet.ServletRequest;\r
 import javax.servlet.ServletResponse;\r
@@ -50,8 +51,8 @@ public class SyndicationFilter extends AuthenticationFilter {
        private IProjectManager projectManager;\r
 \r
        @Override\r
-       protected void inject(ObjectGraph dagger) {\r
-               super.inject(dagger);\r
+       protected void inject(ObjectGraph dagger, FilterConfig filterConfig) {\r
+               super.inject(dagger, filterConfig);\r
                this.runtimeManager = dagger.get(IRuntimeManager.class);\r
                this.repositoryManager = dagger.get(IRepositoryManager.class);\r
                this.projectManager = dagger.get(IProjectManager.class);\r
index 60f8b47dd36f74f685ed96eb0a4b47672a7a52b2..e8b53d11914391e6bb06d84fca1cbd8967eb7bc9 100644 (file)
@@ -185,3 +185,23 @@ public class MyTicketHook extends TicketHook {
 }
 ```
 
+### Request Filter
+
+*SINCE 1.6.0*
+
+You can provide your own custom request filter by subclassing the *HttpRequestFilter* class.
+
+```java
+import com.gitblit.extensions.HttpRequestFilter;
+import ro.fortsoft.pf4j.Extension;
+
+@Extension
+public class MyRequestFilter extends HttpRequestFilter {
+
+    @Override
+    public void doFilter(ServletRequest request, ServletResponse response,
+            FilterChain chain) throws IOException, ServletException {
+    }
+}
+```
+