summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Ostrovsky <david@ostrovsky.org>2014-03-10 01:50:49 +0100
committerJames Moger <james.moger@gitblit.com>2014-05-05 11:16:30 -0400
commitec24568f14ee38b264c86133ce8ebcc35a39034b (patch)
tree053fc0a9c67a81ca7f7290f2e1e8f7f9ea5276e1
parent3920e1f852b0417fd1217095d7ec12cbd6f41cb8 (diff)
downloadgitblit-ec24568f14ee38b264c86133ce8ebcc35a39034b.tar.gz
gitblit-ec24568f14ee38b264c86133ce8ebcc35a39034b.zip
Add http request filter extension point
To allow for integration of 3rd party server monitoring solutions, Gitblit needs to expose an extension point for collecting http data.
-rw-r--r--src/main/java/WEB-INF/web.xml11
-rw-r--r--src/main/java/com/gitblit/dagger/DaggerFilter.java4
-rw-r--r--src/main/java/com/gitblit/extensions/HttpRequestFilter.java49
-rw-r--r--src/main/java/com/gitblit/servlet/AccessRestrictionFilter.java5
-rw-r--r--src/main/java/com/gitblit/servlet/AuthenticationFilter.java3
-rw-r--r--src/main/java/com/gitblit/servlet/EnforceAuthenticationFilter.java3
-rw-r--r--src/main/java/com/gitblit/servlet/GitFilter.java5
-rw-r--r--src/main/java/com/gitblit/servlet/ProxyFilter.java76
-rw-r--r--src/main/java/com/gitblit/servlet/RpcFilter.java5
-rw-r--r--src/main/java/com/gitblit/servlet/SyndicationFilter.java5
-rw-r--r--src/site/plugins_extensions.mkd20
11 files changed, 173 insertions, 13 deletions
diff --git a/src/main/java/WEB-INF/web.xml b/src/main/java/WEB-INF/web.xml
index cb483af4..3a6c4490 100644
--- a/src/main/java/WEB-INF/web.xml
+++ b/src/main/java/WEB-INF/web.xml
@@ -214,6 +214,15 @@
<url-pattern>/robots.txt</url-pattern>
</servlet-mapping>
+ <filter>
+ <filter-name>ProxyFilter</filter-name>
+ <filter-class>com.gitblit.servlet.ProxyFilter</filter-class>
+ </filter>
+ <filter-mapping>
+ <filter-name>ProxyFilter</filter-name>
+ <url-pattern>/*</url-pattern>
+ </filter-mapping>
+
<!-- Git Access Restriction Filter
<url-pattern> MUST match:
* GitServlet
@@ -353,4 +362,4 @@
<url-pattern>/*</url-pattern>
</filter-mapping>
-</web-app>
+</web-app> \ No newline at end of file
diff --git a/src/main/java/com/gitblit/dagger/DaggerFilter.java b/src/main/java/com/gitblit/dagger/DaggerFilter.java
index 1c73d4b7..01c07a4a 100644
--- a/src/main/java/com/gitblit/dagger/DaggerFilter.java
+++ b/src/main/java/com/gitblit/dagger/DaggerFilter.java
@@ -36,10 +36,10 @@ public abstract class DaggerFilter implements Filter {
public final void init(FilterConfig filterConfig) throws ServletException {
ServletContext context = filterConfig.getServletContext();
ObjectGraph objectGraph = (ObjectGraph) context.getAttribute(DaggerContext.INJECTOR_NAME);
- inject(objectGraph);
+ inject(objectGraph, filterConfig);
}
- protected abstract void inject(ObjectGraph dagger);
+ protected abstract void inject(ObjectGraph dagger, FilterConfig filterConfig) throws ServletException;
@Override
public void destroy() {
diff --git a/src/main/java/com/gitblit/extensions/HttpRequestFilter.java b/src/main/java/com/gitblit/extensions/HttpRequestFilter.java
new file mode 100644
index 00000000..e3e330cb
--- /dev/null
+++ b/src/main/java/com/gitblit/extensions/HttpRequestFilter.java
@@ -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;
+}
diff --git a/src/main/java/com/gitblit/servlet/AccessRestrictionFilter.java b/src/main/java/com/gitblit/servlet/AccessRestrictionFilter.java
index e6a0169c..0e6d323d 100644
--- a/src/main/java/com/gitblit/servlet/AccessRestrictionFilter.java
+++ b/src/main/java/com/gitblit/servlet/AccessRestrictionFilter.java
@@ -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;
@@ -54,8 +55,8 @@ public abstract class AccessRestrictionFilter extends AuthenticationFilter {
protected IRepositoryManager repositoryManager;
@Override
- protected void inject(ObjectGraph dagger) {
- super.inject(dagger);
+ protected void inject(ObjectGraph dagger, FilterConfig filterConfig) {
+ super.inject(dagger, filterConfig);
this.runtimeManager = dagger.get(IRuntimeManager.class);
this.repositoryManager = dagger.get(IRepositoryManager.class);
}
diff --git a/src/main/java/com/gitblit/servlet/AuthenticationFilter.java b/src/main/java/com/gitblit/servlet/AuthenticationFilter.java
index dd821ac7..5710a4af 100644
--- a/src/main/java/com/gitblit/servlet/AuthenticationFilter.java
+++ b/src/main/java/com/gitblit/servlet/AuthenticationFilter.java
@@ -24,6 +24,7 @@ import java.util.HashMap;
import java.util.Map;
import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
@@ -64,7 +65,7 @@ public abstract class AuthenticationFilter extends DaggerFilter {
protected IAuthenticationManager authenticationManager;
@Override
- protected void inject(ObjectGraph dagger) {
+ protected void inject(ObjectGraph dagger, FilterConfig filterConfig) {
this.authenticationManager = dagger.get(IAuthenticationManager.class);
}
diff --git a/src/main/java/com/gitblit/servlet/EnforceAuthenticationFilter.java b/src/main/java/com/gitblit/servlet/EnforceAuthenticationFilter.java
index 5fdccb71..c015021d 100644
--- a/src/main/java/com/gitblit/servlet/EnforceAuthenticationFilter.java
+++ b/src/main/java/com/gitblit/servlet/EnforceAuthenticationFilter.java
@@ -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);
}
diff --git a/src/main/java/com/gitblit/servlet/GitFilter.java b/src/main/java/com/gitblit/servlet/GitFilter.java
index f9c062d2..bb3d3216 100644
--- a/src/main/java/com/gitblit/servlet/GitFilter.java
+++ b/src/main/java/com/gitblit/servlet/GitFilter.java
@@ -17,6 +17,7 @@ package com.gitblit.servlet;
import java.text.MessageFormat;
+import javax.servlet.FilterConfig;
import javax.servlet.http.HttpServletRequest;
import com.gitblit.Constants.AccessRestrictionType;
@@ -53,8 +54,8 @@ public class GitFilter extends AccessRestrictionFilter {
private IFederationManager federationManager;
@Override
- protected void inject(ObjectGraph dagger) {
- super.inject(dagger);
+ protected void inject(ObjectGraph dagger, FilterConfig filterConfig) {
+ super.inject(dagger, filterConfig);
this.settings = dagger.get(IStoredSettings.class);
this.federationManager = dagger.get(IFederationManager.class);
}
diff --git a/src/main/java/com/gitblit/servlet/ProxyFilter.java b/src/main/java/com/gitblit/servlet/ProxyFilter.java
new file mode 100644
index 00000000..bfdc87f3
--- /dev/null
+++ b/src/main/java/com/gitblit/servlet/ProxyFilter.java
@@ -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();
+ }
+ }
+}
diff --git a/src/main/java/com/gitblit/servlet/RpcFilter.java b/src/main/java/com/gitblit/servlet/RpcFilter.java
index e0b1a233..23bf956e 100644
--- a/src/main/java/com/gitblit/servlet/RpcFilter.java
+++ b/src/main/java/com/gitblit/servlet/RpcFilter.java
@@ -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,8 +54,8 @@ public class RpcFilter extends AuthenticationFilter {
private IRuntimeManager runtimeManager;
@Override
- protected void inject(ObjectGraph dagger) {
- super.inject(dagger);
+ protected void inject(ObjectGraph dagger, FilterConfig filterConfig) {
+ super.inject(dagger, filterConfig);
this.settings = dagger.get(IStoredSettings.class);
this.runtimeManager = dagger.get(IRuntimeManager.class);
}
diff --git a/src/main/java/com/gitblit/servlet/SyndicationFilter.java b/src/main/java/com/gitblit/servlet/SyndicationFilter.java
index 67a845ea..78da47e9 100644
--- a/src/main/java/com/gitblit/servlet/SyndicationFilter.java
+++ b/src/main/java/com/gitblit/servlet/SyndicationFilter.java
@@ -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;
@@ -50,8 +51,8 @@ public class SyndicationFilter extends AuthenticationFilter {
private IProjectManager projectManager;
@Override
- protected void inject(ObjectGraph dagger) {
- super.inject(dagger);
+ protected void inject(ObjectGraph dagger, FilterConfig filterConfig) {
+ super.inject(dagger, filterConfig);
this.runtimeManager = dagger.get(IRuntimeManager.class);
this.repositoryManager = dagger.get(IRepositoryManager.class);
this.projectManager = dagger.get(IProjectManager.class);
diff --git a/src/site/plugins_extensions.mkd b/src/site/plugins_extensions.mkd
index 60f8b47d..e8b53d11 100644
--- a/src/site/plugins_extensions.mkd
+++ b/src/site/plugins_extensions.mkd
@@ -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 {
+ }
+}
+```
+