From 65d5bb3f1cbfb42be5a3c4aa077b7ab2504082af Mon Sep 17 00:00:00 2001 From: James Moger Date: Wed, 11 Dec 2013 23:12:12 -0500 Subject: [PATCH] Rollback to Servlet 2.5 Combining Dagger and Servlet 3 works really well on stock Tomcat and Jetty but it is a troublesome combination on JEE containers with their own ideas on how to instantiate classes. JBoss AS 7 has been particularly nasty and it is just simpler to scaleback and stay with Servlet 2.5 than it is to fight all permuations of containers. Instead of using constructor DI, the servlets and filters each have an inject(ObjectGaph) method which is automatically called during initialization. Each servlet or filter is responsible for retrieving the required dependency from the graph. The Dagger object graph is created in the context listener and stuffed into the context as an attribute. Change-Id: Ib5714584fe73e2a6b9c6fda12af080a43356cbda --- releases.moxie | 1 + src/main/java/WEB-INF/web.xml | 273 ++++++++++++++++++ src/main/java/com/gitblit/DaggerModule.java | 179 +----------- ...ontextListener.java => DaggerContext.java} | 27 +- .../java/com/gitblit/dagger/DaggerFilter.java | 47 +++ .../com/gitblit/dagger/DaggerServlet.java | 44 +++ .../gitblit/dagger/DaggerWicketFilter.java | 45 +++ src/main/java/com/gitblit/git/GitServlet.java | 49 ---- .../servlet/AccessRestrictionFilter.java | 19 +- .../gitblit/servlet/AuthenticationFilter.java | 28 +- .../gitblit/servlet/BranchGraphServlet.java | 28 +- .../gitblit/servlet/DownloadZipFilter.java | 11 - .../gitblit/servlet/DownloadZipServlet.java | 22 +- .../servlet/EnforceAuthenticationFilter.java | 28 +- .../gitblit/servlet/FederationServlet.java | 28 +- .../java/com/gitblit/servlet/GitFilter.java | 28 +- .../java/com/gitblit/servlet/GitServlet.java | 112 +++++++ .../com/gitblit/servlet/GitblitContext.java | 68 ++--- .../servlet/InjectionContextListener.java | 241 ---------------- .../java/com/gitblit/servlet/JsonServlet.java | 4 +- .../java/com/gitblit/servlet/LogoServlet.java | 14 +- .../java/com/gitblit/servlet/PagesFilter.java | 10 - .../com/gitblit/servlet/PagesServlet.java | 23 +- .../com/gitblit/servlet/RobotsTxtServlet.java | 14 +- .../java/com/gitblit/servlet/RpcFilter.java | 19 +- .../java/com/gitblit/servlet/RpcServlet.java | 16 +- .../servlet/SparkleShareInviteServlet.java | 32 +- .../gitblit/servlet/SyndicationFilter.java | 25 +- .../gitblit/servlet/SyndicationServlet.java | 28 +- .../gitblit/wicket/GitblitWicketFilter.java | 30 +- 30 files changed, 721 insertions(+), 772 deletions(-) rename src/main/java/com/gitblit/dagger/{DaggerContextListener.java => DaggerContext.java} (77%) create mode 100644 src/main/java/com/gitblit/dagger/DaggerFilter.java create mode 100644 src/main/java/com/gitblit/dagger/DaggerServlet.java create mode 100644 src/main/java/com/gitblit/dagger/DaggerWicketFilter.java delete mode 100644 src/main/java/com/gitblit/git/GitServlet.java create mode 100644 src/main/java/com/gitblit/servlet/GitServlet.java delete mode 100644 src/main/java/com/gitblit/servlet/InjectionContextListener.java diff --git a/releases.moxie b/releases.moxie index b16ee716..4b7a10af 100644 --- a/releases.moxie +++ b/releases.moxie @@ -62,6 +62,7 @@ r20: { - updated to Lucene 4.6.0 - updated to BouncyCastle 1.49 - replaced MarkdownPapers with pegdown 1.4.2 + - added Dagger 1.1.0 - added Eclipse WikiText libraries for processing confluence, mediawiki, textile, tracwiki, and twiki settings: - { name: 'git.createRepositoriesShared', defaultValue: 'false' } diff --git a/src/main/java/WEB-INF/web.xml b/src/main/java/WEB-INF/web.xml index 6ce5daf8..c082b944 100644 --- a/src/main/java/WEB-INF/web.xml +++ b/src/main/java/WEB-INF/web.xml @@ -35,4 +35,277 @@ + + + + + + + + GitServlet + com.gitblit.servlet.GitServlet + + + GitServlet + /git/* + + + GitServlet2 + com.gitblit.servlet.GitServlet + + + GitServlet2 + /r/* + + + + + + SparkleShareInviteServlet + com.gitblit.servlet.SparkleShareInviteServlet + + + SparkleShareInviteServlet + /sparkleshare/* + + + + + + SyndicationServlet + com.gitblit.servlet.SyndicationServlet + + + SyndicationServlet + /feed/* + + + + + + ZipServlet + com.gitblit.servlet.DownloadZipServlet + + + ZipServlet + /zip/* + + + + + + FederationServlet + com.gitblit.servlet.FederationServlet + + + FederationServlet + /federation/* + + + + + + RpcServlet + com.gitblit.servlet.RpcServlet + + + RpcServlet + /rpc/* + + + + + + PagesServlet + com.gitblit.servlet.PagesServlet + + + PagesServlet + /pages/* + + + + + + LogoServlet + com.gitblit.servlet.LogoServlet + + + LogoServlet + /logo.png + + + + + BranchGraphServlet + com.gitblit.servlet.BranchGraphServlet + + + BranchGraphServlet + /graph/* + + + + + RobotsTxtServlet + com.gitblit.servlet.RobotsTxtServlet + + + RobotsTxtServlet + /robots.txt + + + + + + GitFilter + com.gitblit.servlet.GitFilter + + + GitFilter + /git/* + + + + + + SyndicationFilter + com.gitblit.servlet.SyndicationFilter + + + SyndicationFilter + /feed/* + + + + + + ZipFilter + com.gitblit.servlet.DownloadZipFilter + + + ZipFilter + /zip/* + + + + + + RpcFilter + com.gitblit.servlet.RpcFilter + + + RpcFilter + /rpc/* + + + + + + PagesFilter + com.gitblit.servlet.PagesFilter + + + PagesFilter + /pages/* + + + + EnforceAuthenticationFilter + com.gitblit.servlet.EnforceAuthenticationFilter + + + EnforceAuthenticationFilter + /* + + + + + + wicketFilter + + com.gitblit.wicket.GitblitWicketFilter + + + ignorePaths + + r/,git/,feed/,zip/,federation/,rpc/,pages/,robots.txt,logo.png,graph/,sparkleshare/ + + + + wicketFilter + /* + + \ No newline at end of file diff --git a/src/main/java/com/gitblit/DaggerModule.java b/src/main/java/com/gitblit/DaggerModule.java index b12510c9..5ae8b253 100644 --- a/src/main/java/com/gitblit/DaggerModule.java +++ b/src/main/java/com/gitblit/DaggerModule.java @@ -17,7 +17,6 @@ package com.gitblit; import javax.inject.Singleton; -import com.gitblit.git.GitServlet; import com.gitblit.manager.AuthenticationManager; import com.gitblit.manager.FederationManager; import com.gitblit.manager.IAuthenticationManager; @@ -33,23 +32,7 @@ import com.gitblit.manager.ProjectManager; import com.gitblit.manager.RepositoryManager; import com.gitblit.manager.RuntimeManager; import com.gitblit.manager.UserManager; -import com.gitblit.servlet.BranchGraphServlet; -import com.gitblit.servlet.DownloadZipFilter; -import com.gitblit.servlet.DownloadZipServlet; -import com.gitblit.servlet.EnforceAuthenticationFilter; -import com.gitblit.servlet.FederationServlet; -import com.gitblit.servlet.GitFilter; -import com.gitblit.servlet.LogoServlet; -import com.gitblit.servlet.PagesFilter; -import com.gitblit.servlet.PagesServlet; -import com.gitblit.servlet.RobotsTxtServlet; -import com.gitblit.servlet.RpcFilter; -import com.gitblit.servlet.RpcServlet; -import com.gitblit.servlet.SparkleShareInviteServlet; -import com.gitblit.servlet.SyndicationFilter; -import com.gitblit.servlet.SyndicationServlet; import com.gitblit.wicket.GitBlitWebApp; -import com.gitblit.wicket.GitblitWicketFilter; import dagger.Module; import dagger.Provides; @@ -77,24 +60,8 @@ import dagger.Provides; // the monolithic manager IGitblit.class, - // filters & servlets - GitServlet.class, - GitFilter.class, - PagesServlet.class, - PagesFilter.class, - RpcServlet.class, - RpcFilter.class, - DownloadZipServlet.class, - DownloadZipFilter.class, - SyndicationServlet.class, - SyndicationFilter.class, - FederationServlet.class, - SparkleShareInviteServlet.class, - BranchGraphServlet.class, - RobotsTxtServlet.class, - LogoServlet.class, - EnforceAuthenticationFilter.class, - GitblitWicketFilter.class + // the Gitblit Wicket app + GitBlitWebApp.class } ) public class DaggerModule { @@ -194,146 +161,4 @@ public class DaggerModule { federationManager, gitblit); } - - @Provides @Singleton GitblitWicketFilter provideGitblitWicketFilter(GitBlitWebApp webapp) { - return new GitblitWicketFilter(webapp); - } - - @Provides GitServlet provideGitServlet(IGitblit gitblit) { - return new GitServlet(gitblit); - } - - @Provides GitFilter provideGitFilter( - IRuntimeManager runtimeManager, - IUserManager userManager, - IAuthenticationManager authenticationManager, - IRepositoryManager repositoryManager, - IFederationManager federationManager) { - - return new GitFilter( - runtimeManager, - userManager, - authenticationManager, - repositoryManager, - federationManager); - } - - @Provides @Singleton PagesServlet providePagesServlet( - IRuntimeManager runtimeManager, - IRepositoryManager repositoryManager) { - - return new PagesServlet(runtimeManager, repositoryManager); - } - - @Provides @Singleton PagesFilter providePagesFilter( - IRuntimeManager runtimeManager, - IAuthenticationManager authenticationManager, - IRepositoryManager repositoryManager) { - - return new PagesFilter( - runtimeManager, - authenticationManager, - repositoryManager); - } - - @Provides @Singleton RpcServlet provideRpcServlet(IGitblit gitblit) { - return new RpcServlet(gitblit); - } - - @Provides @Singleton RpcFilter provideRpcFilter( - IRuntimeManager runtimeManager, - IAuthenticationManager authenticationManager) { - - return new RpcFilter(runtimeManager, authenticationManager); - } - - @Provides @Singleton DownloadZipServlet provideDownloadZipServlet( - IRuntimeManager runtimeManager, - IRepositoryManager repositoryManager) { - - return new DownloadZipServlet(runtimeManager, repositoryManager); - } - - @Provides @Singleton DownloadZipFilter provideDownloadZipFilter( - IRuntimeManager runtimeManager, - IAuthenticationManager authenticationManager, - IRepositoryManager repositoryManager) { - - return new DownloadZipFilter( - runtimeManager, - authenticationManager, - repositoryManager); - } - - @Provides @Singleton SyndicationServlet provideSyndicationServlet( - IRuntimeManager runtimeManager, - IRepositoryManager repositoryManager, - IProjectManager projectManager) { - - return new SyndicationServlet( - runtimeManager, - repositoryManager, - projectManager); - } - - @Provides @Singleton SyndicationFilter provideSyndicationFilter( - IRuntimeManager runtimeManager, - IAuthenticationManager authenticationManager, - IRepositoryManager repositoryManager, - IProjectManager projectManager) { - - return new SyndicationFilter( - runtimeManager, - authenticationManager, - repositoryManager, - projectManager); - } - - @Provides @Singleton FederationServlet provideFederationServlet( - IRuntimeManager runtimeManager, - IUserManager userManager, - IRepositoryManager repositoryManager, - IFederationManager federationManager) { - - return new FederationServlet( - runtimeManager, - userManager, - repositoryManager, - federationManager); - } - - @Provides @Singleton SparkleShareInviteServlet provideSparkleshareInviteServlet( - IRuntimeManager runtimeManager, - IUserManager userManager, - IAuthenticationManager authenticationManager, - IRepositoryManager repositoryManager) { - - return new SparkleShareInviteServlet( - runtimeManager, - userManager, - authenticationManager, - repositoryManager); - } - - @Provides @Singleton BranchGraphServlet provideBranchGraphServlet( - IRuntimeManager runtimeManager, - IRepositoryManager repositoryManager) { - - return new BranchGraphServlet(runtimeManager, repositoryManager); - } - - @Provides @Singleton RobotsTxtServlet provideRobotsTxtServlet(IRuntimeManager runtimeManager) { - return new RobotsTxtServlet(runtimeManager); - } - - @Provides @Singleton LogoServlet provideLogoServlet(IRuntimeManager runtimeManager) { - return new LogoServlet(runtimeManager); - } - - @Provides @Singleton EnforceAuthenticationFilter provideEnforceAuthenticationFilter( - IRuntimeManager runtimeManager, - IAuthenticationManager authenticationManager) { - - return new EnforceAuthenticationFilter(runtimeManager, authenticationManager); - } } \ No newline at end of file diff --git a/src/main/java/com/gitblit/dagger/DaggerContextListener.java b/src/main/java/com/gitblit/dagger/DaggerContext.java similarity index 77% rename from src/main/java/com/gitblit/dagger/DaggerContextListener.java rename to src/main/java/com/gitblit/dagger/DaggerContext.java index 6a04e4a7..28ee25d3 100644 --- a/src/main/java/com/gitblit/dagger/DaggerContextListener.java +++ b/src/main/java/com/gitblit/dagger/DaggerContext.java @@ -17,8 +17,10 @@ package com.gitblit.dagger; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; -import com.gitblit.servlet.InjectionContextListener; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import dagger.ObjectGraph; @@ -29,9 +31,11 @@ import dagger.ObjectGraph; * @author James Moger * */ -public abstract class DaggerContextListener extends InjectionContextListener { +public abstract class DaggerContext implements ServletContextListener { - protected static final String INJECTOR_NAME = ObjectGraph.class.getName(); + public static final String INJECTOR_NAME = ObjectGraph.class.getName(); + + protected final Logger logger = LoggerFactory.getLogger(getClass()); protected abstract Object [] getModules(); @@ -54,23 +58,6 @@ public abstract class DaggerContextListener extends InjectionContextListener { return (ObjectGraph) o; } - /** - * Instantiates an object. - * - * @param clazz - * @return the object - */ - @Override - protected X instantiate(ServletContext context, Class clazz) { - try { - ObjectGraph injector = getInjector(context); - return injector.get(clazz); - } catch (Throwable t) { - logger.error(null, t); - } - return null; - } - @Override public final void contextDestroyed(ServletContextEvent contextEvent) { ServletContext context = contextEvent.getServletContext(); diff --git a/src/main/java/com/gitblit/dagger/DaggerFilter.java b/src/main/java/com/gitblit/dagger/DaggerFilter.java new file mode 100644 index 00000000..1c73d4b7 --- /dev/null +++ b/src/main/java/com/gitblit/dagger/DaggerFilter.java @@ -0,0 +1,47 @@ +/* + * Copyright 2013 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.dagger; + +import javax.servlet.Filter; +import javax.servlet.FilterConfig; +import javax.servlet.ServletContext; +import javax.servlet.ServletException; + +import dagger.ObjectGraph; + +/** + * Uses Dagger to manually inject dependencies into a servlet filter. + * This class is useful for servlet containers that offer CDI and are + * confused by Dagger. + * + * @author James Moger + * + */ +public abstract class DaggerFilter implements Filter { + + @Override + public final void init(FilterConfig filterConfig) throws ServletException { + ServletContext context = filterConfig.getServletContext(); + ObjectGraph objectGraph = (ObjectGraph) context.getAttribute(DaggerContext.INJECTOR_NAME); + inject(objectGraph); + } + + protected abstract void inject(ObjectGraph dagger); + + @Override + public void destroy() { + } +} diff --git a/src/main/java/com/gitblit/dagger/DaggerServlet.java b/src/main/java/com/gitblit/dagger/DaggerServlet.java new file mode 100644 index 00000000..88331a43 --- /dev/null +++ b/src/main/java/com/gitblit/dagger/DaggerServlet.java @@ -0,0 +1,44 @@ +/* + * Copyright 2013 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.dagger; + +import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; + +import dagger.ObjectGraph; + +/** + * Uses Dagger to manually inject dependencies into a servlet. + * This class is useful for servlet containers that offer CDI and are + * confused by Dagger. + * + * @author James Moger + * + */ +public abstract class DaggerServlet extends HttpServlet { + + private static final long serialVersionUID = 1L; + + @Override + public final void init() throws ServletException { + ServletContext context = getServletContext(); + ObjectGraph objectGraph = (ObjectGraph) context.getAttribute(DaggerContext.INJECTOR_NAME); + inject(objectGraph); + } + + protected abstract void inject(ObjectGraph dagger); +} diff --git a/src/main/java/com/gitblit/dagger/DaggerWicketFilter.java b/src/main/java/com/gitblit/dagger/DaggerWicketFilter.java new file mode 100644 index 00000000..c2fd4d67 --- /dev/null +++ b/src/main/java/com/gitblit/dagger/DaggerWicketFilter.java @@ -0,0 +1,45 @@ +/* + * Copyright 2013 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.dagger; + +import javax.servlet.FilterConfig; +import javax.servlet.ServletContext; +import javax.servlet.ServletException; + +import org.apache.wicket.protocol.http.WicketFilter; + +import dagger.ObjectGraph; + +/** + * Uses Dagger to manually inject dependencies into a Wicket filter. + * This class is useful for servlet containers that offer CDI and are + * confused by Dagger. + * + * @author James Moger + * + */ +public abstract class DaggerWicketFilter extends WicketFilter { + + @Override + public final void init(FilterConfig filterConfig) throws ServletException { + ServletContext context = filterConfig.getServletContext(); + ObjectGraph objectGraph = (ObjectGraph) context.getAttribute(DaggerContext.INJECTOR_NAME); + inject(objectGraph); + super.init(filterConfig); + } + + protected abstract void inject(ObjectGraph dagger); +} diff --git a/src/main/java/com/gitblit/git/GitServlet.java b/src/main/java/com/gitblit/git/GitServlet.java deleted file mode 100644 index cf54e6d3..00000000 --- a/src/main/java/com/gitblit/git/GitServlet.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright 2011 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.git; - -import javax.servlet.ServletConfig; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; - -import com.gitblit.manager.IGitblit; - -/** - * The GitServlet provides http/https access to Git repositories. - * Access to this servlet is protected by the GitFilter. - * - * @author James Moger - * - */ -public class GitServlet extends org.eclipse.jgit.http.server.GitServlet { - - private static final long serialVersionUID = 1L; - - private final IGitblit gitblit; - - public GitServlet(IGitblit gitblit) { - super(); - this.gitblit = gitblit; - } - - @Override - public void init(ServletConfig config) throws ServletException { - setRepositoryResolver(new RepositoryResolver(gitblit)); - setUploadPackFactory(new GitblitUploadPackFactory(gitblit)); - setReceivePackFactory(new GitblitReceivePackFactory(gitblit)); - super.init(config); - } -} diff --git a/src/main/java/com/gitblit/servlet/AccessRestrictionFilter.java b/src/main/java/com/gitblit/servlet/AccessRestrictionFilter.java index d6acdbb6..e6a0169c 100644 --- a/src/main/java/com/gitblit/servlet/AccessRestrictionFilter.java +++ b/src/main/java/com/gitblit/servlet/AccessRestrictionFilter.java @@ -27,11 +27,12 @@ import javax.servlet.http.HttpServletResponse; import com.gitblit.manager.IRepositoryManager; import com.gitblit.manager.IRuntimeManager; -import com.gitblit.manager.IAuthenticationManager; import com.gitblit.models.RepositoryModel; import com.gitblit.models.UserModel; import com.gitblit.utils.StringUtils; +import dagger.ObjectGraph; + /** * The AccessRestrictionFilter is an AuthenticationFilter that confirms that the * requested repository can be accessed by the anonymous or named user. @@ -48,17 +49,15 @@ import com.gitblit.utils.StringUtils; */ public abstract class AccessRestrictionFilter extends AuthenticationFilter { - protected final IRuntimeManager runtimeManager; + protected IRuntimeManager runtimeManager; - protected final IRepositoryManager repositoryManager; + protected IRepositoryManager repositoryManager; - protected AccessRestrictionFilter( - IRuntimeManager runtimeManager, - IAuthenticationManager authenticationManager, - IRepositoryManager repositoryManager) { - super(authenticationManager); - this.runtimeManager = runtimeManager; - this.repositoryManager = repositoryManager; + @Override + protected void inject(ObjectGraph dagger) { + super.inject(dagger); + 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 34478662..97ca438f 100644 --- a/src/main/java/com/gitblit/servlet/AuthenticationFilter.java +++ b/src/main/java/com/gitblit/servlet/AuthenticationFilter.java @@ -21,9 +21,7 @@ import java.util.Enumeration; import java.util.HashMap; import java.util.Map; -import javax.servlet.Filter; import javax.servlet.FilterChain; -import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; @@ -36,11 +34,14 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.gitblit.Constants; +import com.gitblit.dagger.DaggerFilter; import com.gitblit.manager.IAuthenticationManager; import com.gitblit.models.UserModel; import com.gitblit.utils.DeepCopier; import com.gitblit.utils.StringUtils; +import dagger.ObjectGraph; + /** * The AuthenticationFilter is a servlet filter that preprocesses requests that * match its url pattern definition in the web.xml file. @@ -50,7 +51,7 @@ import com.gitblit.utils.StringUtils; * @author James Moger * */ -public abstract class AuthenticationFilter implements Filter { +public abstract class AuthenticationFilter extends DaggerFilter { protected static final String CHALLENGE = "Basic realm=\"" + Constants.NAME + "\""; @@ -58,10 +59,11 @@ public abstract class AuthenticationFilter implements Filter { protected transient Logger logger = LoggerFactory.getLogger(getClass()); - protected final IAuthenticationManager authenticationManager; + protected IAuthenticationManager authenticationManager; - protected AuthenticationFilter(IAuthenticationManager authenticationManager) { - this.authenticationManager = authenticationManager; + @Override + protected void inject(ObjectGraph dagger) { + this.authenticationManager = dagger.get(IAuthenticationManager.class); } /** @@ -137,20 +139,6 @@ public abstract class AuthenticationFilter implements Filter { } } - /** - * @see javax.servlet.Filter#init(javax.servlet.FilterConfig) - */ - @Override - public void init(final FilterConfig config) throws ServletException { - } - - /** - * @see javax.servlet.Filter#destroy() - */ - @Override - public void destroy() { - } - /** * Wraps a standard HttpServletRequest and overrides user principal methods. */ diff --git a/src/main/java/com/gitblit/servlet/BranchGraphServlet.java b/src/main/java/com/gitblit/servlet/BranchGraphServlet.java index 1f6d3a87..0abe347f 100644 --- a/src/main/java/com/gitblit/servlet/BranchGraphServlet.java +++ b/src/main/java/com/gitblit/servlet/BranchGraphServlet.java @@ -37,7 +37,6 @@ import java.util.TreeSet; import javax.imageio.ImageIO; import javax.servlet.ServletException; -import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -53,18 +52,20 @@ import org.eclipse.jgit.revwalk.RevCommit; import com.gitblit.Constants; import com.gitblit.IStoredSettings; import com.gitblit.Keys; +import com.gitblit.dagger.DaggerServlet; import com.gitblit.manager.IRepositoryManager; -import com.gitblit.manager.IRuntimeManager; import com.gitblit.utils.JGitUtils; import com.gitblit.utils.StringUtils; +import dagger.ObjectGraph; + /** * Handles requests for branch graphs * * @author James Moger * */ -public class BranchGraphServlet extends HttpServlet { +public class BranchGraphServlet extends DaggerServlet { private static final long serialVersionUID = 1L; @@ -77,21 +78,22 @@ public class BranchGraphServlet extends HttpServlet { private final Stroke[] strokeCache; - private final IStoredSettings settings; - - private final IRepositoryManager repositoryManager; + private IStoredSettings settings; - public BranchGraphServlet( - IRuntimeManager runtimeManager, - IRepositoryManager repositoryManager) { + private IRepositoryManager repositoryManager; + public BranchGraphServlet() { super(); - this.settings = runtimeManager.getSettings(); - this.repositoryManager = repositoryManager; - strokeCache = new Stroke[4]; - for (int i = 1; i < strokeCache.length; i++) + for (int i = 1; i < strokeCache.length; i++) { strokeCache[i] = new BasicStroke(i); + } + } + + @Override + protected void inject(ObjectGraph dagger) { + this.settings = dagger.get(IStoredSettings.class); + this.repositoryManager = dagger.get(IRepositoryManager.class); } /** diff --git a/src/main/java/com/gitblit/servlet/DownloadZipFilter.java b/src/main/java/com/gitblit/servlet/DownloadZipFilter.java index 30bb9cb9..0c7b3e56 100644 --- a/src/main/java/com/gitblit/servlet/DownloadZipFilter.java +++ b/src/main/java/com/gitblit/servlet/DownloadZipFilter.java @@ -16,9 +16,6 @@ package com.gitblit.servlet; import com.gitblit.Constants.AccessRestrictionType; -import com.gitblit.manager.IAuthenticationManager; -import com.gitblit.manager.IRepositoryManager; -import com.gitblit.manager.IRuntimeManager; import com.gitblit.models.RepositoryModel; import com.gitblit.models.UserModel; @@ -32,14 +29,6 @@ import com.gitblit.models.UserModel; */ public class DownloadZipFilter extends AccessRestrictionFilter { - public DownloadZipFilter( - IRuntimeManager runtimeManager, - IAuthenticationManager authenticationManager, - IRepositoryManager repositoryManager) { - - super(runtimeManager, authenticationManager, repositoryManager); - } - /** * Extract the repository name from the url. * diff --git a/src/main/java/com/gitblit/servlet/DownloadZipServlet.java b/src/main/java/com/gitblit/servlet/DownloadZipServlet.java index 437a9c26..6a64778c 100644 --- a/src/main/java/com/gitblit/servlet/DownloadZipServlet.java +++ b/src/main/java/com/gitblit/servlet/DownloadZipServlet.java @@ -21,7 +21,6 @@ import java.text.ParseException; import java.util.Date; import javax.servlet.ServletException; -import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletResponse; import org.eclipse.jgit.lib.Repository; @@ -32,13 +31,15 @@ import org.slf4j.LoggerFactory; import com.gitblit.Constants; import com.gitblit.IStoredSettings; import com.gitblit.Keys; +import com.gitblit.dagger.DaggerServlet; import com.gitblit.manager.IRepositoryManager; -import com.gitblit.manager.IRuntimeManager; import com.gitblit.utils.CompressionUtils; import com.gitblit.utils.JGitUtils; import com.gitblit.utils.MarkdownUtils; import com.gitblit.utils.StringUtils; +import dagger.ObjectGraph; + /** * Streams out a zip file from the specified repository for any tree path at any * revision. @@ -46,15 +47,15 @@ import com.gitblit.utils.StringUtils; * @author James Moger * */ -public class DownloadZipServlet extends HttpServlet { +public class DownloadZipServlet extends DaggerServlet { private static final long serialVersionUID = 1L; private transient Logger logger = LoggerFactory.getLogger(DownloadZipServlet.class); - private final IStoredSettings settings; + private IStoredSettings settings; - private final IRepositoryManager repositoryManager; + private IRepositoryManager repositoryManager; public static enum Format { zip(".zip"), tar(".tar"), gz(".tar.gz"), xz(".tar.xz"), bzip2(".tar.bzip2"); @@ -75,13 +76,10 @@ public class DownloadZipServlet extends HttpServlet { } } - public DownloadZipServlet( - IRuntimeManager runtimeManager, - IRepositoryManager repositoryManager) { - - super(); - this.settings = runtimeManager.getSettings(); - this.repositoryManager = repositoryManager; + @Override + protected void inject(ObjectGraph dagger) { + this.settings = dagger.get(IStoredSettings.class); + this.repositoryManager = dagger.get(IRepositoryManager.class); } /** diff --git a/src/main/java/com/gitblit/servlet/EnforceAuthenticationFilter.java b/src/main/java/com/gitblit/servlet/EnforceAuthenticationFilter.java index b129c0ca..5fdccb71 100644 --- a/src/main/java/com/gitblit/servlet/EnforceAuthenticationFilter.java +++ b/src/main/java/com/gitblit/servlet/EnforceAuthenticationFilter.java @@ -18,9 +18,7 @@ import java.io.IOException; import java.text.MessageFormat; -import javax.servlet.Filter; import javax.servlet.FilterChain; -import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; @@ -32,10 +30,12 @@ import org.slf4j.LoggerFactory; import com.gitblit.IStoredSettings; import com.gitblit.Keys; +import com.gitblit.dagger.DaggerFilter; import com.gitblit.manager.IAuthenticationManager; -import com.gitblit.manager.IRuntimeManager; import com.gitblit.models.UserModel; +import dagger.ObjectGraph; + /** * This filter enforces authentication via HTTP Basic Authentication, if the settings indicate so. * It looks at the settings "web.authenticateViewPages" and "web.enforceHttpBasicAuthentication"; if @@ -44,28 +44,18 @@ import com.gitblit.models.UserModel; * @author Laurens Vrijnsen * */ -public class EnforceAuthenticationFilter implements Filter { +public class EnforceAuthenticationFilter extends DaggerFilter { protected transient Logger logger = LoggerFactory.getLogger(getClass()); - private final IStoredSettings settings; - - private final IAuthenticationManager authenticationManager; - - public EnforceAuthenticationFilter( - IRuntimeManager runtimeManager, - IAuthenticationManager authenticationManager) { + private IStoredSettings settings; - super(); - this.settings = runtimeManager.getSettings(); - this.authenticationManager = authenticationManager; - } + private IAuthenticationManager authenticationManager; - /* - * @see javax.servlet.Filter#init(javax.servlet.FilterConfig) - */ @Override - public void init(FilterConfig filterConfig) throws ServletException { + protected void inject(ObjectGraph dagger) { + this.settings = dagger.get(IStoredSettings.class); + this.authenticationManager = dagger.get(IAuthenticationManager.class); } /* diff --git a/src/main/java/com/gitblit/servlet/FederationServlet.java b/src/main/java/com/gitblit/servlet/FederationServlet.java index 8d352feb..372292de 100644 --- a/src/main/java/com/gitblit/servlet/FederationServlet.java +++ b/src/main/java/com/gitblit/servlet/FederationServlet.java @@ -32,7 +32,6 @@ import com.gitblit.IStoredSettings; import com.gitblit.Keys; import com.gitblit.manager.IFederationManager; import com.gitblit.manager.IRepositoryManager; -import com.gitblit.manager.IRuntimeManager; import com.gitblit.manager.IUserManager; import com.gitblit.models.FederationModel; import com.gitblit.models.FederationProposal; @@ -44,6 +43,8 @@ import com.gitblit.utils.HttpUtils; import com.gitblit.utils.StringUtils; import com.gitblit.utils.TimeUtils; +import dagger.ObjectGraph; + /** * Handles federation requests. * @@ -54,25 +55,20 @@ public class FederationServlet extends JsonServlet { private static final long serialVersionUID = 1L; - private final IStoredSettings settings; - - private final IUserManager userManager; + private IStoredSettings settings; - private final IRepositoryManager repositoryManager; + private IUserManager userManager; - private final IFederationManager federationManager; + private IRepositoryManager repositoryManager; - public FederationServlet( - IRuntimeManager runtimeManager, - IUserManager userManager, - IRepositoryManager repositoryManager, - IFederationManager federationManager) { + private IFederationManager federationManager; - super(); - this.settings = runtimeManager.getSettings(); - this.userManager = userManager; - this.repositoryManager = repositoryManager; - this.federationManager = federationManager; + @Override + protected void inject(ObjectGraph dagger) { + this.settings = dagger.get(IStoredSettings.class); + this.userManager = dagger.get(IUserManager.class); + this.repositoryManager = dagger.get(IRepositoryManager.class); + this.federationManager = dagger.get(IFederationManager.class); } /** diff --git a/src/main/java/com/gitblit/servlet/GitFilter.java b/src/main/java/com/gitblit/servlet/GitFilter.java index 58a45ca2..402f72d3 100644 --- a/src/main/java/com/gitblit/servlet/GitFilter.java +++ b/src/main/java/com/gitblit/servlet/GitFilter.java @@ -24,15 +24,13 @@ import com.gitblit.Constants.AuthorizationControl; import com.gitblit.GitBlitException; import com.gitblit.IStoredSettings; import com.gitblit.Keys; -import com.gitblit.manager.IAuthenticationManager; import com.gitblit.manager.IFederationManager; -import com.gitblit.manager.IRepositoryManager; -import com.gitblit.manager.IRuntimeManager; -import com.gitblit.manager.IUserManager; import com.gitblit.models.RepositoryModel; import com.gitblit.models.UserModel; import com.gitblit.utils.StringUtils; +import dagger.ObjectGraph; + /** * The GitFilter is an AccessRestrictionFilter which ensures that Git client * requests for push, clone, or view restricted repositories are authenticated @@ -50,23 +48,15 @@ public class GitFilter extends AccessRestrictionFilter { protected static final String[] suffixes = { gitReceivePack, gitUploadPack, "/info/refs", "/HEAD", "/objects" }; - private final IStoredSettings settings; - - private final IUserManager userManager; + private IStoredSettings settings; - private final IFederationManager federationManager; + private IFederationManager federationManager; - public GitFilter( - IRuntimeManager runtimeManager, - IUserManager userManager, - IAuthenticationManager authenticationManager, - IRepositoryManager repositoryManager, - IFederationManager federationManager) { - - super(runtimeManager, authenticationManager, repositoryManager); - this.settings = runtimeManager.getSettings(); - this.userManager = userManager; - this.federationManager = federationManager; + @Override + protected void inject(ObjectGraph dagger) { + super.inject(dagger); + this.settings = dagger.get(IStoredSettings.class); + this.federationManager = dagger.get(IFederationManager.class); } /** diff --git a/src/main/java/com/gitblit/servlet/GitServlet.java b/src/main/java/com/gitblit/servlet/GitServlet.java new file mode 100644 index 00000000..93fe31d0 --- /dev/null +++ b/src/main/java/com/gitblit/servlet/GitServlet.java @@ -0,0 +1,112 @@ +/* + * Copyright 2011 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 static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND; + +import java.io.IOException; +import java.util.Enumeration; + +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletConfig; +import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.eclipse.jgit.http.server.GitFilter; + +import com.gitblit.dagger.DaggerContext; +import com.gitblit.git.GitblitReceivePackFactory; +import com.gitblit.git.GitblitUploadPackFactory; +import com.gitblit.git.RepositoryResolver; +import com.gitblit.manager.IGitblit; + +import dagger.ObjectGraph; + +/** + * The GitServlet provides http/https access to Git repositories. + * Access to this servlet is protected by the GitFilter. + * + * @author James Moger + * + */ +public class GitServlet extends HttpServlet { + + private static final long serialVersionUID = 1L; + + private final GitFilter gitFilter; + + public GitServlet() { + gitFilter = new GitFilter(); + } + + @Override + public void init(final ServletConfig config) throws ServletException { + ServletContext context = config.getServletContext(); + ObjectGraph dagger = (ObjectGraph) context.getAttribute(DaggerContext.INJECTOR_NAME); + IGitblit gitblit = dagger.get(IGitblit.class); + gitFilter.setRepositoryResolver(new RepositoryResolver(gitblit)); + gitFilter.setUploadPackFactory(new GitblitUploadPackFactory(gitblit)); + gitFilter.setReceivePackFactory(new GitblitReceivePackFactory(gitblit)); + + gitFilter.init(new FilterConfig() { + @Override + public String getFilterName() { + return gitFilter.getClass().getName(); + } + + @Override + public String getInitParameter(String name) { + return config.getInitParameter(name); + } + + @Override + public Enumeration getInitParameterNames() { + return config.getInitParameterNames(); + } + + @Override + public ServletContext getServletContext() { + return config.getServletContext(); + } + }); + + init(); + } + + @Override + protected void service(HttpServletRequest req, HttpServletResponse res) + throws ServletException, IOException { + gitFilter.doFilter(req, res, new FilterChain() { + @Override + public void doFilter(ServletRequest request, + ServletResponse response) throws IOException, + ServletException { + ((HttpServletResponse) response).sendError(SC_NOT_FOUND); + } + }); + } + + @Override + public void destroy() { + gitFilter.destroy(); + } +} diff --git a/src/main/java/com/gitblit/servlet/GitblitContext.java b/src/main/java/com/gitblit/servlet/GitblitContext.java index ca9a67c1..682b590c 100644 --- a/src/main/java/com/gitblit/servlet/GitblitContext.java +++ b/src/main/java/com/gitblit/servlet/GitblitContext.java @@ -23,15 +23,13 @@ import java.io.InputStream; import java.io.OutputStream; import java.text.MessageFormat; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.servlet.ServletContext; -import javax.servlet.annotation.WebListener; +import javax.servlet.ServletContextEvent; import com.gitblit.Constants; import com.gitblit.DaggerModule; @@ -39,8 +37,7 @@ import com.gitblit.FileSettings; import com.gitblit.IStoredSettings; import com.gitblit.Keys; import com.gitblit.WebXmlSettings; -import com.gitblit.dagger.DaggerContextListener; -import com.gitblit.git.GitServlet; +import com.gitblit.dagger.DaggerContext; import com.gitblit.manager.IAuthenticationManager; import com.gitblit.manager.IFederationManager; import com.gitblit.manager.IGitblit; @@ -52,25 +49,21 @@ import com.gitblit.manager.IRuntimeManager; import com.gitblit.manager.IUserManager; import com.gitblit.utils.ContainerUtils; import com.gitblit.utils.StringUtils; -import com.gitblit.wicket.GitblitWicketFilter; import dagger.ObjectGraph; /** * This class is the main entry point for the entire webapp. It is a singleton * created manually by Gitblit GO or dynamically by the WAR/Express servlet - * container. This class instantiates and starts all managers followed by - * instantiating and registering all servlets and filters. - * - * Leveraging Servlet 3 and Dagger static dependency injection allows Gitblit to - * be modular and completely code-driven rather then relying on the fragility of - * a web.xml descriptor and the static & monolithic design previously used. + * container. This class instantiates and starts all managers. Servlets and + * filters are instantiated defined in web.xml and instantiated by the servlet + * container, but those servlets and filters use Dagger to manually inject their + * dependencies. * * @author James Moger * */ -@WebListener -public class GitblitContext extends DaggerContextListener { +public class GitblitContext extends DaggerContext { private static GitblitContext gitblit; @@ -126,10 +119,21 @@ public class GitblitContext extends DaggerContextListener { } /** - * Prepare runtime settings and start all manager instances. + * Configure Gitblit from the web.xml, if no configuration has already been + * specified. + * + * @see ServletContextListener.contextInitialize(ServletContextEvent) */ @Override - protected void beforeServletInjection(ServletContext context) { + public final void contextInitialized(ServletContextEvent contextEvent) { + ServletContext context = contextEvent.getServletContext(); + configureContext(context); + } + + /** + * Prepare runtime settings and start all manager instances. + */ + protected void configureContext(ServletContext context) { ObjectGraph injector = getInjector(context); // create the runtime settings object @@ -193,38 +197,6 @@ public class GitblitContext extends DaggerContextListener { logger.info("----[{}]----", clazz.getName()); } - /** - * Instantiate and inject all filters and servlets into the container using - * the servlet 3 specification. - */ - @Override - protected void injectServlets(ServletContext context) { - // access restricted servlets - serve(context, Constants.R_PATH, GitServlet.class, GitFilter.class); - serve(context, Constants.GIT_PATH, GitServlet.class, GitFilter.class); - serve(context, Constants.PAGES, PagesServlet.class, PagesFilter.class); - serve(context, Constants.RPC_PATH, RpcServlet.class, RpcFilter.class); - serve(context, Constants.ZIP_PATH, DownloadZipServlet.class, DownloadZipFilter.class); - serve(context, Constants.SYNDICATION_PATH, SyndicationServlet.class, SyndicationFilter.class); - - // servlets - serve(context, Constants.FEDERATION_PATH, FederationServlet.class); - serve(context, Constants.SPARKLESHARE_INVITE_PATH, SparkleShareInviteServlet.class); - serve(context, Constants.BRANCH_GRAPH_PATH, BranchGraphServlet.class); - file(context, "/robots.txt", RobotsTxtServlet.class); - file(context, "/logo.png", LogoServlet.class); - - // optional force basic authentication - filter(context, "/*", EnforceAuthenticationFilter.class, null); - - // Wicket - String toIgnore = StringUtils.flattenStrings(getRegisteredPaths(), ","); - Map params = new HashMap(); - params.put(GitblitWicketFilter.FILTER_MAPPING_PARAM, "/*"); - params.put(GitblitWicketFilter.IGNORE_PATHS_PARAM, toIgnore); - filter(context, "/*", GitblitWicketFilter.class, params); - } - /** * Gitblit is being shutdown either because the servlet container is * shutting down or because the servlet container is re-deploying Gitblit. diff --git a/src/main/java/com/gitblit/servlet/InjectionContextListener.java b/src/main/java/com/gitblit/servlet/InjectionContextListener.java deleted file mode 100644 index b0e10985..00000000 --- a/src/main/java/com/gitblit/servlet/InjectionContextListener.java +++ /dev/null @@ -1,241 +0,0 @@ -/* - * Copyright 2013 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.ArrayList; -import java.util.EnumSet; -import java.util.List; -import java.util.Map; - -import javax.servlet.DispatcherType; -import javax.servlet.Filter; -import javax.servlet.FilterRegistration; -import javax.servlet.Servlet; -import javax.servlet.ServletContext; -import javax.servlet.ServletContextEvent; -import javax.servlet.ServletContextListener; -import javax.servlet.ServletRegistration; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Injection context listener instantiates and injects servlets, filters, and - * anything else you might want into a servlet context. This class provides - * convenience methods for servlet & filter registration and also tracks - * registered paths. - * - * @author James Moger - * - */ -public abstract class InjectionContextListener implements ServletContextListener { - - protected final Logger logger = LoggerFactory.getLogger(getClass()); - - private final List registeredPaths = new ArrayList(); - - protected final List getRegisteredPaths() { - return registeredPaths; - } - - /** - * Hook for subclasses to manipulate context initialization before - * standard initialization procedure. - * - * @param context - */ - protected void beforeServletInjection(ServletContext context) { - // NOOP - } - - /** - * Hook for subclasses to instantiate and inject servlets and filters - * into the servlet context. - * - * @param context - */ - protected abstract void injectServlets(ServletContext context); - - /** - * Hook for subclasses to manipulate context initialization after - * servlet registration. - * - * @param context - */ - protected void afterServletInjection(ServletContext context) { - // NOOP - } - - /** - * Configure Gitblit from the web.xml, if no configuration has already been - * specified. - * - * @see ServletContextListener.contextInitialize(ServletContextEvent) - */ - @Override - public final void contextInitialized(ServletContextEvent contextEvent) { - ServletContext context = contextEvent.getServletContext(); - beforeServletInjection(context); - injectServlets(context); - afterServletInjection(context); - } - - - /** - * Registers a file path. - * - * @param context - * @param file - * @param servletClass - */ - protected void file(ServletContext context, String file, Class servletClass) { - file(context, file, servletClass, null); - } - - /** - * Registers a file path with init parameters. - * - * @param context - * @param file - * @param servletClass - * @param initParams - */ - protected void file(ServletContext context, String file, Class servletClass, Map initParams) { - Servlet servlet = instantiate(context, servletClass); - ServletRegistration.Dynamic d = context.addServlet(sanitize(servletClass.getSimpleName() + file), servlet); - d.addMapping(file); - if (initParams != null) { - d.setInitParameters(initParams); - } - registeredPaths.add(file); - } - - /** - * Serves a path (trailing wildcard will be appended). - * - * @param context - * @param route - * @param servletClass - */ - protected void serve(ServletContext context, String route, Class servletClass) { - serve(context, route, servletClass, (Class) null); - } - - /** - * Serves a path (trailing wildcard will be appended) with init parameters. - * - * @param context - * @param route - * @param servletClass - * @param initParams - */ - protected void serve(ServletContext context, String route, Class servletClass, Map initParams) { - Servlet servlet = instantiate(context, servletClass); - ServletRegistration.Dynamic d = context.addServlet(sanitize(servletClass.getSimpleName() + route), servlet); - d.addMapping(route + "*"); - if (initParams != null) { - d.setInitParameters(initParams); - } - registeredPaths.add(route); - } - - /** - * Serves a path (trailing wildcard will be appended) and also maps a filter - * to that path. - * - * @param context - * @param route - * @param servletClass - * @param filterClass - */ - protected void serve(ServletContext context, String route, Class servletClass, Class filterClass) { - Servlet servlet = instantiate(context, servletClass); - ServletRegistration.Dynamic d = context.addServlet(sanitize(servletClass.getSimpleName() + route), servlet); - d.addMapping(route + "*"); - if (filterClass != null) { - filter(context, route + "*", filterClass); - } - registeredPaths.add(route); - } - - /** - * Registers a path filter. - * - * @param context - * @param route - * @param filterClass - */ - protected void filter(ServletContext context, String route, Class filterClass) { - filter(context, route, filterClass, null); - } - - /** - * Registers a path filter with init parameters. - * - * @param context - * @param route - * @param filterClass - * @param initParams - */ - protected void filter(ServletContext context, String route, Class filterClass, Map initParams) { - Filter filter = instantiate(context, filterClass); - FilterRegistration.Dynamic d = context.addFilter(sanitize(filterClass.getSimpleName() + route), filter); - d.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, route); - if (initParams != null) { - d.setInitParameters(initParams); - } - } - - /** - * Limit the generated servlet/filter names to alpha-numeric values with a - * handful of acceptable other characters. - * - * @param name - * @return a sanitized name - */ - protected String sanitize(String name) { - StringBuilder sb = new StringBuilder(); - for (char c : name.toCharArray()) { - if (Character.isLetterOrDigit(c)) { - sb.append(c); - } else if ('-' == c) { - sb.append(c); - } else if ('*' == c) { - sb.append("all"); - } else if ('.' == c) { - sb.append('.'); - } else { - sb.append('_'); - } - } - return sb.toString(); - } - - /** - * Instantiates an object. - * - * @param clazz - * @return the object - */ - protected X instantiate(ServletContext context, Class clazz) { - try { - return clazz.newInstance(); - } catch (Throwable t) { - logger.error(null, t); - } - return null; - } -} diff --git a/src/main/java/com/gitblit/servlet/JsonServlet.java b/src/main/java/com/gitblit/servlet/JsonServlet.java index abc0f292..4378c8a6 100644 --- a/src/main/java/com/gitblit/servlet/JsonServlet.java +++ b/src/main/java/com/gitblit/servlet/JsonServlet.java @@ -21,7 +21,6 @@ import java.lang.reflect.Type; import java.text.MessageFormat; import javax.servlet.ServletException; -import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -29,6 +28,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.gitblit.Constants; +import com.gitblit.dagger.DaggerServlet; import com.gitblit.utils.JsonUtils; import com.gitblit.utils.StringUtils; @@ -38,7 +38,7 @@ import com.gitblit.utils.StringUtils; * @author James Moger * */ -public abstract class JsonServlet extends HttpServlet { +public abstract class JsonServlet extends DaggerServlet { private static final long serialVersionUID = 1L; diff --git a/src/main/java/com/gitblit/servlet/LogoServlet.java b/src/main/java/com/gitblit/servlet/LogoServlet.java index e39cc695..96f34afd 100644 --- a/src/main/java/com/gitblit/servlet/LogoServlet.java +++ b/src/main/java/com/gitblit/servlet/LogoServlet.java @@ -23,30 +23,32 @@ import java.io.OutputStream; import javax.servlet.ServletContext; import javax.servlet.ServletException; -import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.gitblit.Keys; +import com.gitblit.dagger.DaggerServlet; import com.gitblit.manager.IRuntimeManager; +import dagger.ObjectGraph; + /** * Handles requests for logo.png * * @author James Moger * */ -public class LogoServlet extends HttpServlet { +public class LogoServlet extends DaggerServlet { private static final long serialVersionUID = 1L; private static final long lastModified = System.currentTimeMillis(); - private final IRuntimeManager runtimeManager; + private IRuntimeManager runtimeManager; - public LogoServlet(IRuntimeManager runtimeManager) { - super(); - this.runtimeManager = runtimeManager; + @Override + protected void inject(ObjectGraph dagger) { + this.runtimeManager = dagger.get(IRuntimeManager.class); } @Override diff --git a/src/main/java/com/gitblit/servlet/PagesFilter.java b/src/main/java/com/gitblit/servlet/PagesFilter.java index fd7b10fe..9e009163 100644 --- a/src/main/java/com/gitblit/servlet/PagesFilter.java +++ b/src/main/java/com/gitblit/servlet/PagesFilter.java @@ -18,9 +18,6 @@ package com.gitblit.servlet; import org.eclipse.jgit.lib.Repository; import com.gitblit.Constants.AccessRestrictionType; -import com.gitblit.manager.IAuthenticationManager; -import com.gitblit.manager.IRepositoryManager; -import com.gitblit.manager.IRuntimeManager; import com.gitblit.models.RepositoryModel; import com.gitblit.models.UserModel; @@ -33,13 +30,6 @@ import com.gitblit.models.UserModel; */ public class PagesFilter extends AccessRestrictionFilter { - public PagesFilter(IRuntimeManager runtimeManager, - IAuthenticationManager authenticationManager, - IRepositoryManager repositoryManager) { - - super(runtimeManager, authenticationManager, repositoryManager); - } - /** * Extract the repository name from the url. * diff --git a/src/main/java/com/gitblit/servlet/PagesServlet.java b/src/main/java/com/gitblit/servlet/PagesServlet.java index 8b91b486..74c1cb2d 100644 --- a/src/main/java/com/gitblit/servlet/PagesServlet.java +++ b/src/main/java/com/gitblit/servlet/PagesServlet.java @@ -23,10 +23,8 @@ import java.util.List; import java.util.Set; import java.util.TreeSet; -import javax.inject.Inject; import javax.servlet.ServletContext; import javax.servlet.ServletException; -import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -39,8 +37,8 @@ import org.slf4j.LoggerFactory; import com.gitblit.Constants; import com.gitblit.IStoredSettings; import com.gitblit.Keys; +import com.gitblit.dagger.DaggerServlet; import com.gitblit.manager.IRepositoryManager; -import com.gitblit.manager.IRuntimeManager; import com.gitblit.models.PathModel; import com.gitblit.models.RefModel; import com.gitblit.utils.ArrayUtils; @@ -51,29 +49,28 @@ import com.gitblit.utils.StringUtils; import com.gitblit.wicket.MarkupProcessor; import com.gitblit.wicket.MarkupProcessor.MarkupDocument; +import dagger.ObjectGraph; + /** * Serves the content of a gh-pages branch. * * @author James Moger * */ -public class PagesServlet extends HttpServlet { +public class PagesServlet extends DaggerServlet { private static final long serialVersionUID = 1L; private transient Logger logger = LoggerFactory.getLogger(PagesServlet.class); - private final IStoredSettings settings; - - private final IRepositoryManager repositoryManager; + private IStoredSettings settings; - public PagesServlet( - IRuntimeManager runtimeManager, - IRepositoryManager repositoryManager) { + private IRepositoryManager repositoryManager; - super(); - this.settings = runtimeManager.getSettings(); - this.repositoryManager = repositoryManager; + @Override + protected void inject(ObjectGraph dagger) { + this.settings = dagger.get(IStoredSettings.class); + this.repositoryManager = dagger.get(IRepositoryManager.class); } /** diff --git a/src/main/java/com/gitblit/servlet/RobotsTxtServlet.java b/src/main/java/com/gitblit/servlet/RobotsTxtServlet.java index 94cc832a..9bd3b3c3 100644 --- a/src/main/java/com/gitblit/servlet/RobotsTxtServlet.java +++ b/src/main/java/com/gitblit/servlet/RobotsTxtServlet.java @@ -19,29 +19,31 @@ import java.io.File; import java.io.IOException; import javax.servlet.ServletException; -import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.gitblit.Keys; +import com.gitblit.dagger.DaggerServlet; import com.gitblit.manager.IRuntimeManager; import com.gitblit.utils.FileUtils; +import dagger.ObjectGraph; + /** * Handles requests for robots.txt * * @author James Moger * */ -public class RobotsTxtServlet extends HttpServlet { +public class RobotsTxtServlet extends DaggerServlet { private static final long serialVersionUID = 1L; - private final IRuntimeManager runtimeManager; + private IRuntimeManager runtimeManager; - public RobotsTxtServlet(IRuntimeManager runtimeManager) { - super(); - this.runtimeManager = runtimeManager; + @Override + protected void inject(ObjectGraph dagger) { + this.runtimeManager = dagger.get(IRuntimeManager.class); } @Override diff --git a/src/main/java/com/gitblit/servlet/RpcFilter.java b/src/main/java/com/gitblit/servlet/RpcFilter.java index fc838724..e0b1a233 100644 --- a/src/main/java/com/gitblit/servlet/RpcFilter.java +++ b/src/main/java/com/gitblit/servlet/RpcFilter.java @@ -28,10 +28,11 @@ import javax.servlet.http.HttpServletResponse; import com.gitblit.Constants.RpcRequest; import com.gitblit.IStoredSettings; import com.gitblit.Keys; -import com.gitblit.manager.IAuthenticationManager; import com.gitblit.manager.IRuntimeManager; import com.gitblit.models.UserModel; +import dagger.ObjectGraph; + /** * The RpcFilter is a servlet filter that secures the RpcServlet. * @@ -47,17 +48,15 @@ import com.gitblit.models.UserModel; */ public class RpcFilter extends AuthenticationFilter { - private final IStoredSettings settings; - - private final IRuntimeManager runtimeManager; + private IStoredSettings settings; - public RpcFilter( - IRuntimeManager runtimeManager, - IAuthenticationManager authenticationManager) { + private IRuntimeManager runtimeManager; - super(authenticationManager); - this.settings = runtimeManager.getSettings(); - this.runtimeManager = runtimeManager; + @Override + protected void inject(ObjectGraph dagger) { + super.inject(dagger); + this.settings = dagger.get(IStoredSettings.class); + this.runtimeManager = dagger.get(IRuntimeManager.class); } /** diff --git a/src/main/java/com/gitblit/servlet/RpcServlet.java b/src/main/java/com/gitblit/servlet/RpcServlet.java index a5d892fe..e7b3ed2c 100644 --- a/src/main/java/com/gitblit/servlet/RpcServlet.java +++ b/src/main/java/com/gitblit/servlet/RpcServlet.java @@ -47,6 +47,8 @@ import com.gitblit.utils.JGitUtils; import com.gitblit.utils.RpcUtils; import com.gitblit.utils.StringUtils; +import dagger.ObjectGraph; + /** * Handles remote procedure calls. * @@ -59,16 +61,14 @@ public class RpcServlet extends JsonServlet { public static final int PROTOCOL_VERSION = 6; - private final IStoredSettings settings; - - private final IGitblit gitblit; + private IStoredSettings settings; - public RpcServlet(IGitblit gitblit) { + private IGitblit gitblit; - super(); - - this.settings = gitblit.getSettings(); - this.gitblit = gitblit; + @Override + protected void inject(ObjectGraph dagger) { + this.settings = dagger.get(IStoredSettings.class); + this.gitblit = dagger.get(IGitblit.class); } /** diff --git a/src/main/java/com/gitblit/servlet/SparkleShareInviteServlet.java b/src/main/java/com/gitblit/servlet/SparkleShareInviteServlet.java index 709e22ef..d7f00c67 100644 --- a/src/main/java/com/gitblit/servlet/SparkleShareInviteServlet.java +++ b/src/main/java/com/gitblit/servlet/SparkleShareInviteServlet.java @@ -19,50 +19,46 @@ import java.io.IOException; import java.text.MessageFormat; import javax.servlet.ServletException; -import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.gitblit.Constants; import com.gitblit.IStoredSettings; import com.gitblit.Keys; +import com.gitblit.dagger.DaggerServlet; import com.gitblit.manager.IAuthenticationManager; import com.gitblit.manager.IRepositoryManager; -import com.gitblit.manager.IRuntimeManager; import com.gitblit.manager.IUserManager; import com.gitblit.models.RepositoryModel; import com.gitblit.models.UserModel; import com.gitblit.utils.StringUtils; +import dagger.ObjectGraph; + /** * Handles requests for Sparkleshare Invites * * @author James Moger * */ -public class SparkleShareInviteServlet extends HttpServlet { +public class SparkleShareInviteServlet extends DaggerServlet { private static final long serialVersionUID = 1L; - private final IStoredSettings settings; - - private final IUserManager userManager; + private IStoredSettings settings; - private final IAuthenticationManager authenticationManager; + private IUserManager userManager; - private final IRepositoryManager repositoryManager; + private IAuthenticationManager authenticationManager; - public SparkleShareInviteServlet( - IRuntimeManager runtimeManager, - IUserManager userManager, - IAuthenticationManager authenticationManager, - IRepositoryManager repositoryManager) { + private IRepositoryManager repositoryManager; - super(); - this.settings = runtimeManager.getSettings(); - this.userManager = userManager; - this.authenticationManager = authenticationManager; - this.repositoryManager = repositoryManager; + @Override + protected void inject(ObjectGraph dagger) { + this.settings = dagger.get(IStoredSettings.class); + this.userManager = dagger.get(IUserManager.class); + this.authenticationManager = dagger.get(IAuthenticationManager.class); + this.repositoryManager = dagger.get(IRepositoryManager.class); } @Override diff --git a/src/main/java/com/gitblit/servlet/SyndicationFilter.java b/src/main/java/com/gitblit/servlet/SyndicationFilter.java index e4e5e1d6..67a845ea 100644 --- a/src/main/java/com/gitblit/servlet/SyndicationFilter.java +++ b/src/main/java/com/gitblit/servlet/SyndicationFilter.java @@ -26,7 +26,6 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.gitblit.Constants.AccessRestrictionType; -import com.gitblit.manager.IAuthenticationManager; import com.gitblit.manager.IProjectManager; import com.gitblit.manager.IRepositoryManager; import com.gitblit.manager.IRuntimeManager; @@ -34,6 +33,8 @@ import com.gitblit.models.ProjectModel; import com.gitblit.models.RepositoryModel; import com.gitblit.models.UserModel; +import dagger.ObjectGraph; + /** * The SyndicationFilter is an AuthenticationFilter which ensures that feed * requests for projects or view-restricted repositories have proper authentication @@ -44,20 +45,16 @@ import com.gitblit.models.UserModel; */ public class SyndicationFilter extends AuthenticationFilter { - private final IRuntimeManager runtimeManager; - private final IRepositoryManager repositoryManager; - private final IProjectManager projectManager; - - public SyndicationFilter( - IRuntimeManager runtimeManager, - IAuthenticationManager authenticationManager, - IRepositoryManager repositoryManager, - IProjectManager projectManager) { + private IRuntimeManager runtimeManager; + private IRepositoryManager repositoryManager; + private IProjectManager projectManager; - super(authenticationManager); - this.runtimeManager = runtimeManager; - this.repositoryManager = repositoryManager; - this.projectManager = projectManager; + @Override + protected void inject(ObjectGraph dagger) { + super.inject(dagger); + this.runtimeManager = dagger.get(IRuntimeManager.class); + this.repositoryManager = dagger.get(IRepositoryManager.class); + this.projectManager = dagger.get(IProjectManager.class); } /** diff --git a/src/main/java/com/gitblit/servlet/SyndicationServlet.java b/src/main/java/com/gitblit/servlet/SyndicationServlet.java index a9555f7d..8acd0192 100644 --- a/src/main/java/com/gitblit/servlet/SyndicationServlet.java +++ b/src/main/java/com/gitblit/servlet/SyndicationServlet.java @@ -22,8 +22,6 @@ import java.util.Collections; import java.util.List; import java.util.Map; -import javax.servlet.http.HttpServlet; - import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.revwalk.RevCommit; @@ -33,9 +31,9 @@ import org.slf4j.LoggerFactory; import com.gitblit.Constants; import com.gitblit.IStoredSettings; import com.gitblit.Keys; +import com.gitblit.dagger.DaggerServlet; import com.gitblit.manager.IProjectManager; import com.gitblit.manager.IRepositoryManager; -import com.gitblit.manager.IRuntimeManager; import com.gitblit.models.FeedEntryModel; import com.gitblit.models.ProjectModel; import com.gitblit.models.RefModel; @@ -48,6 +46,8 @@ import com.gitblit.utils.MessageProcessor; import com.gitblit.utils.StringUtils; import com.gitblit.utils.SyndicationUtils; +import dagger.ObjectGraph; + /** * SyndicationServlet generates RSS 2.0 feeds and feed links. * @@ -56,27 +56,23 @@ import com.gitblit.utils.SyndicationUtils; * @author James Moger * */ -public class SyndicationServlet extends HttpServlet { +public class SyndicationServlet extends DaggerServlet { private static final long serialVersionUID = 1L; private transient Logger logger = LoggerFactory.getLogger(SyndicationServlet.class); - private final IStoredSettings settings; + private IStoredSettings settings; - private final IRepositoryManager repositoryManager; + private IRepositoryManager repositoryManager; - private final IProjectManager projectManager; + private IProjectManager projectManager; - public SyndicationServlet( - IRuntimeManager runtimeManager, - IRepositoryManager repositoryManager, - IProjectManager projectManager) { - - super(); - this.settings = runtimeManager.getSettings(); - this.repositoryManager = repositoryManager; - this.projectManager = projectManager; + @Override + protected void inject(ObjectGraph dagger) { + this.settings = dagger.get(IStoredSettings.class); + this.repositoryManager = dagger.get(IRepositoryManager.class); + this.projectManager = dagger.get(IProjectManager.class); } /** diff --git a/src/main/java/com/gitblit/wicket/GitblitWicketFilter.java b/src/main/java/com/gitblit/wicket/GitblitWicketFilter.java index 9e554708..7865fb3b 100644 --- a/src/main/java/com/gitblit/wicket/GitblitWicketFilter.java +++ b/src/main/java/com/gitblit/wicket/GitblitWicketFilter.java @@ -28,6 +28,7 @@ import org.eclipse.jgit.revwalk.RevCommit; import com.gitblit.IStoredSettings; import com.gitblit.Keys; +import com.gitblit.dagger.DaggerWicketFilter; import com.gitblit.manager.IProjectManager; import com.gitblit.manager.IRepositoryManager; import com.gitblit.manager.IRuntimeManager; @@ -36,6 +37,8 @@ import com.gitblit.models.RepositoryModel; import com.gitblit.utils.JGitUtils; import com.gitblit.utils.StringUtils; +import dagger.ObjectGraph; + /** * * Customization of the WicketFilter to allow smart browser-side caching of @@ -44,26 +47,25 @@ import com.gitblit.utils.StringUtils; * @author James Moger * */ -public class GitblitWicketFilter extends WicketFilter { - - private final IStoredSettings settings; +public class GitblitWicketFilter extends DaggerWicketFilter { - private final IRuntimeManager runtimeManager; + private IStoredSettings settings; - private final IRepositoryManager repositoryManager; + private IRuntimeManager runtimeManager; - private final IProjectManager projectManager; + private IRepositoryManager repositoryManager; - private final GitBlitWebApp webapp; + private IProjectManager projectManager; - public GitblitWicketFilter(GitBlitWebApp webapp) { + private GitBlitWebApp webapp; - super(); - this.settings = webapp.settings(); - this.runtimeManager = webapp.runtime(); - this.repositoryManager = webapp.repositories(); - this.projectManager = webapp.projects(); - this.webapp = webapp; + @Override + protected void inject(ObjectGraph dagger) { + this.settings = dagger.get(IStoredSettings.class); + this.runtimeManager = dagger.get(IRuntimeManager.class); + this.repositoryManager = dagger.get(IRepositoryManager.class); + this.projectManager = dagger.get(IProjectManager.class); + this.webapp = dagger.get(GitBlitWebApp.class); } @Override -- 2.39.5