From 11642275bab78a22da2f85ed06eb2246f8444f4f Mon Sep 17 00:00:00 2001 From: James Moger Date: Mon, 18 Nov 2013 17:38:34 -0500 Subject: Instantiate and register all servlets and filters from code (servlet 3) Change-Id: I6009e8e157232feab40ec275547a59e2cea23950 --- src/main/java/WEB-INF/web.xml | 267 --------------------- src/main/java/com/gitblit/DownloadZipFilter.java | 3 + .../com/gitblit/EnforceAuthenticationFilter.java | 3 + src/main/java/com/gitblit/GitBlit.java | 59 +++-- src/main/java/com/gitblit/GitFilter.java | 3 + .../java/com/gitblit/InjectionContextListener.java | 241 +++++++++++++++++++ src/main/java/com/gitblit/PagesFilter.java | 3 + src/main/java/com/gitblit/RpcFilter.java | 3 + src/main/java/com/gitblit/SyndicationFilter.java | 3 + src/main/java/com/gitblit/SyndicationServlet.java | 3 + src/main/java/com/gitblit/git/GitServlet.java | 3 + .../com/gitblit/wicket/GitblitWicketFilter.java | 18 +- 12 files changed, 323 insertions(+), 286 deletions(-) create mode 100644 src/main/java/com/gitblit/InjectionContextListener.java diff --git a/src/main/java/WEB-INF/web.xml b/src/main/java/WEB-INF/web.xml index 6e24326b..6ce5daf8 100644 --- a/src/main/java/WEB-INF/web.xml +++ b/src/main/java/WEB-INF/web.xml @@ -35,271 +35,4 @@ - - - - - - - GitServlet - com.gitblit.git.GitServlet - - - GitServlet - /git/* - - - - - - SparkleShareInviteServlet - com.gitblit.SparkleShareInviteServlet - - - SparkleShareInviteServlet - /sparkleshare/* - - - - - - SyndicationServlet - com.gitblit.SyndicationServlet - - - SyndicationServlet - /feed/* - - - - - - ZipServlet - com.gitblit.DownloadZipServlet - - - ZipServlet - /zip/* - - - - - - FederationServlet - com.gitblit.FederationServlet - - - FederationServlet - /federation/* - - - - - - RpcServlet - com.gitblit.RpcServlet - - - RpcServlet - /rpc/* - - - - - - PagesServlet - com.gitblit.PagesServlet - - - PagesServlet - /pages/* - - - - - - LogoServlet - com.gitblit.LogoServlet - - - LogoServlet - /logo.png - - - - - BranchGraphServlet - com.gitblit.BranchGraphServlet - - - BranchGraphServlet - /graph/* - - - - - RobotsTxtServlet - com.gitblit.RobotsTxtServlet - - - RobotsTxtServlet - /robots.txt - - - - - - GitFilter - com.gitblit.GitFilter - - - GitFilter - /git/* - - - - - - SyndicationFilter - com.gitblit.SyndicationFilter - - - SyndicationFilter - /feed/* - - - - - - ZipFilter - com.gitblit.DownloadZipFilter - - - ZipFilter - /zip/* - - - - - - RpcFilter - com.gitblit.RpcFilter - - - RpcFilter - /rpc/* - - - - - - PagesFilter - com.gitblit.PagesFilter - - - PagesFilter - /pages/* - - - - EnforceAuthenticationFilter - com.gitblit.EnforceAuthenticationFilter - - - EnforceAuthenticationFilter - /* - - - - - - wicketFilter - - com.gitblit.wicket.GitblitWicketFilter - - - applicationClassName - com.gitblit.wicket.GitBlitWebApp - - - ignorePaths - - 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/DownloadZipFilter.java b/src/main/java/com/gitblit/DownloadZipFilter.java index fb57af53..e52d9d79 100644 --- a/src/main/java/com/gitblit/DownloadZipFilter.java +++ b/src/main/java/com/gitblit/DownloadZipFilter.java @@ -29,6 +29,9 @@ import com.gitblit.models.UserModel; */ public class DownloadZipFilter extends AccessRestrictionFilter { + public DownloadZipFilter() { + } + /** * Extract the repository name from the url. * diff --git a/src/main/java/com/gitblit/EnforceAuthenticationFilter.java b/src/main/java/com/gitblit/EnforceAuthenticationFilter.java index 93057b45..12b07595 100644 --- a/src/main/java/com/gitblit/EnforceAuthenticationFilter.java +++ b/src/main/java/com/gitblit/EnforceAuthenticationFilter.java @@ -46,6 +46,9 @@ public class EnforceAuthenticationFilter implements Filter { protected transient Logger logger = LoggerFactory.getLogger(getClass()); + public EnforceAuthenticationFilter() { + } + /* * @see javax.servlet.Filter#init(javax.servlet.FilterConfig) */ diff --git a/src/main/java/com/gitblit/GitBlit.java b/src/main/java/com/gitblit/GitBlit.java index 5fbc876b..31dedf13 100644 --- a/src/main/java/com/gitblit/GitBlit.java +++ b/src/main/java/com/gitblit/GitBlit.java @@ -66,7 +66,7 @@ import javax.naming.InitialContext; import javax.naming.NamingException; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; -import javax.servlet.ServletContextListener; +import javax.servlet.annotation.WebListener; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -83,7 +83,6 @@ import org.eclipse.jgit.storage.file.WindowCacheConfig; import org.eclipse.jgit.util.FS; import org.eclipse.jgit.util.FileUtils; import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import com.gitblit.Constants.AccessPermission; import com.gitblit.Constants.AccessRestrictionType; @@ -100,6 +99,7 @@ import com.gitblit.fanout.FanoutNioService; import com.gitblit.fanout.FanoutService; import com.gitblit.fanout.FanoutSocketService; import com.gitblit.git.GitDaemon; +import com.gitblit.git.GitServlet; import com.gitblit.manager.IFederationManager; import com.gitblit.manager.IGitblitManager; import com.gitblit.manager.INotificationManager; @@ -144,6 +144,7 @@ import com.gitblit.utils.StringUtils; import com.gitblit.utils.TimeUtils; import com.gitblit.utils.X509Utils.X509Metadata; import com.gitblit.wicket.GitBlitWebSession; +import com.gitblit.wicket.GitblitWicketFilter; import com.gitblit.wicket.WicketUtils; import com.google.gson.Gson; import com.google.gson.JsonIOException; @@ -153,21 +154,18 @@ import com.google.gson.reflect.TypeToken; /** * GitBlit is the servlet context listener singleton that acts as the core for * the web ui and the servlets. This class is either directly instantiated by - * the GitBlitServer class (Gitblit GO) or is reflectively instantiated from the - * definition in the web.xml file (Gitblit WAR). + * the GitBlitServer class (Gitblit GO) or is reflectively instantiated by the + * servlet 3 container (Gitblit WAR or Express). * * This class is the central logic processor for Gitblit. All settings, user * object, and repository object operations pass through this class. * - * Repository Resolution. There are two pathways for finding repositories. One - * pathway, for web ui display and repository authentication & authorization, is - * within this class. The other pathway is through the standard GitServlet. - * * @author James Moger * */ -public class GitBlit implements ServletContextListener, - IRuntimeManager, +@WebListener +public class GitBlit extends InjectionContextListener + implements IRuntimeManager, INotificationManager, IUserManager, ISessionManager, @@ -180,8 +178,6 @@ public class GitBlit implements ServletContextListener, private final IStoredSettings goSettings; - private final Logger logger = LoggerFactory.getLogger(GitBlit.class); - private final ScheduledExecutorService scheduledExecutor = Executors.newScheduledThreadPool(10); private final List federationRegistrations = Collections @@ -205,8 +201,6 @@ public class GitBlit implements ServletContextListener, private final ObjectCache projectRepositoriesMarkdownCache = new ObjectCache(); - private ServletContext servletContext; - private File baseFolder; private File repositoriesFolder; @@ -3718,11 +3712,9 @@ public class GitBlit implements ServletContextListener, * @see ServletContextListener.contextInitialize(ServletContextEvent) */ @Override - public void contextInitialized(ServletContextEvent contextEvent) { - servletContext = contextEvent.getServletContext(); + protected void beforeServletInjection(ServletContext context) { if (settings == null) { // Gitblit is running in a servlet container - ServletContext context = contextEvent.getServletContext(); WebXmlSettings webxmlSettings = new WebXmlSettings(context); String contextRealPath = context.getRealPath("/"); File contextFolder = (contextRealPath != null) ? new File(contextRealPath) : null; @@ -3807,7 +3799,7 @@ public class GitBlit implements ServletContextListener, } settingsModel = loadSettingModels(); - serverStatus.servletContainer = servletContext.getServerInfo(); + serverStatus.servletContainer = context.getServerInfo(); } protected void extractResources(ServletContext context, String path, File toDir) { @@ -4059,4 +4051,35 @@ public class GitBlit implements ServletContextListener, setCookie(response, null); userService.logout(user); } + + /** + * 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.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); + } } diff --git a/src/main/java/com/gitblit/GitFilter.java b/src/main/java/com/gitblit/GitFilter.java index f52963f7..a06c7e52 100644 --- a/src/main/java/com/gitblit/GitFilter.java +++ b/src/main/java/com/gitblit/GitFilter.java @@ -42,6 +42,9 @@ public class GitFilter extends AccessRestrictionFilter { protected static final String[] suffixes = { gitReceivePack, gitUploadPack, "/info/refs", "/HEAD", "/objects" }; + public GitFilter() { + } + /** * Extract the repository name from the url. * diff --git a/src/main/java/com/gitblit/InjectionContextListener.java b/src/main/java/com/gitblit/InjectionContextListener.java new file mode 100644 index 00000000..712ae643 --- /dev/null +++ b/src/main/java/com/gitblit/InjectionContextListener.java @@ -0,0 +1,241 @@ +/* + * 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; + +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/PagesFilter.java b/src/main/java/com/gitblit/PagesFilter.java index a42d8a4a..4c9568dc 100644 --- a/src/main/java/com/gitblit/PagesFilter.java +++ b/src/main/java/com/gitblit/PagesFilter.java @@ -31,6 +31,9 @@ import com.gitblit.models.UserModel; */ public class PagesFilter extends AccessRestrictionFilter { + public PagesFilter() { + } + /** * Extract the repository name from the url. * diff --git a/src/main/java/com/gitblit/RpcFilter.java b/src/main/java/com/gitblit/RpcFilter.java index 161af9d5..b1384ad3 100644 --- a/src/main/java/com/gitblit/RpcFilter.java +++ b/src/main/java/com/gitblit/RpcFilter.java @@ -44,6 +44,9 @@ import com.gitblit.models.UserModel; */ public class RpcFilter extends AuthenticationFilter { + public RpcFilter() { + } + /** * doFilter does the actual work of preprocessing the request to ensure that * the user may proceed. diff --git a/src/main/java/com/gitblit/SyndicationFilter.java b/src/main/java/com/gitblit/SyndicationFilter.java index ab854bb7..ad908fa8 100644 --- a/src/main/java/com/gitblit/SyndicationFilter.java +++ b/src/main/java/com/gitblit/SyndicationFilter.java @@ -43,6 +43,9 @@ import com.gitblit.models.UserModel; */ public class SyndicationFilter extends AuthenticationFilter { + public SyndicationFilter() { + } + /** * Extract the repository name from the url. * diff --git a/src/main/java/com/gitblit/SyndicationServlet.java b/src/main/java/com/gitblit/SyndicationServlet.java index ae50c3e4..11089ee1 100644 --- a/src/main/java/com/gitblit/SyndicationServlet.java +++ b/src/main/java/com/gitblit/SyndicationServlet.java @@ -59,6 +59,9 @@ public class SyndicationServlet extends HttpServlet { private transient Logger logger = LoggerFactory.getLogger(SyndicationServlet.class); + public SyndicationServlet() { + } + /** * Create a feed link for the specified repository and branch/tag/commit id. * diff --git a/src/main/java/com/gitblit/git/GitServlet.java b/src/main/java/com/gitblit/git/GitServlet.java index 0233cd90..23ea3434 100644 --- a/src/main/java/com/gitblit/git/GitServlet.java +++ b/src/main/java/com/gitblit/git/GitServlet.java @@ -33,6 +33,9 @@ public class GitServlet extends org.eclipse.jgit.http.server.GitServlet { private static final long serialVersionUID = 1L; + public GitServlet() { + } + @Override public void init(ServletConfig config) throws ServletException { IRepositoryManager repositoryManager = GitBlit.getManager(IRepositoryManager.class); diff --git a/src/main/java/com/gitblit/wicket/GitblitWicketFilter.java b/src/main/java/com/gitblit/wicket/GitblitWicketFilter.java index fbe68fe4..bc65d77d 100644 --- a/src/main/java/com/gitblit/wicket/GitblitWicketFilter.java +++ b/src/main/java/com/gitblit/wicket/GitblitWicketFilter.java @@ -19,6 +19,8 @@ import java.util.Date; import javax.servlet.http.HttpServletRequest; +import org.apache.wicket.protocol.http.IWebApplicationFactory; +import org.apache.wicket.protocol.http.WebApplication; import org.apache.wicket.protocol.http.WicketFilter; import org.apache.wicket.util.string.Strings; import org.eclipse.jgit.lib.Repository; @@ -45,6 +47,19 @@ import com.gitblit.utils.StringUtils; */ public class GitblitWicketFilter extends WicketFilter { + public GitblitWicketFilter() { + } + + @Override + protected IWebApplicationFactory getApplicationFactory() { + return new IWebApplicationFactory() { + @Override + public WebApplication createApplication(WicketFilter filter) { + return new GitBlitWebApp(); + } + }; + } + /** * Determines the last-modified date of the requested resource. * @@ -54,8 +69,9 @@ public class GitblitWicketFilter extends WicketFilter { @Override protected long getLastModified(final HttpServletRequest servletRequest) { final String pathInfo = getRelativePath(servletRequest); - if (Strings.isEmpty(pathInfo)) + if (Strings.isEmpty(pathInfo)) { return -1; + } long lastModified = super.getLastModified(servletRequest); if (lastModified > -1) { return lastModified; -- cgit v1.2.3