]> source.dussan.org Git - gitblit.git/commitdiff
Instantiate and register all servlets and filters from code (servlet 3)
authorJames Moger <james.moger@gitblit.com>
Mon, 18 Nov 2013 22:38:34 +0000 (17:38 -0500)
committerJames Moger <james.moger@gitblit.com>
Tue, 26 Nov 2013 21:07:04 +0000 (16:07 -0500)
Change-Id: I6009e8e157232feab40ec275547a59e2cea23950

12 files changed:
src/main/java/WEB-INF/web.xml
src/main/java/com/gitblit/DownloadZipFilter.java
src/main/java/com/gitblit/EnforceAuthenticationFilter.java
src/main/java/com/gitblit/GitBlit.java
src/main/java/com/gitblit/GitFilter.java
src/main/java/com/gitblit/InjectionContextListener.java [new file with mode: 0644]
src/main/java/com/gitblit/PagesFilter.java
src/main/java/com/gitblit/RpcFilter.java
src/main/java/com/gitblit/SyndicationFilter.java
src/main/java/com/gitblit/SyndicationServlet.java
src/main/java/com/gitblit/git/GitServlet.java
src/main/java/com/gitblit/wicket/GitblitWicketFilter.java

index 6e24326b2004edc95d02f030c77b90ee28b5aa7e..6ce5daf83a188b14e32e16107a2486d3e4305a49 100644 (file)
        </display-name>\r
 \r
        <!-- PARAMS --> \r
-        \r
-       <!-- Gitblit Context Listener --><!-- STRIP      \r
-       <listener>\r
-               <listener-class>com.gitblit.GitBlit</listener-class>\r
-       </listener>STRIP -->    \r
-       \r
-       \r
-       <!-- Git Servlet\r
-                <url-pattern> MUST match: \r
-                       * GitFilter\r
-                       * com.gitblit.Constants.GIT_PATH\r
-                       * Wicket Filter ignorePaths parameter -->\r
-       <servlet>\r
-               <servlet-name>GitServlet</servlet-name>\r
-               <servlet-class>com.gitblit.git.GitServlet</servlet-class>\r
-       </servlet>\r
-       <servlet-mapping>\r
-               <servlet-name>GitServlet</servlet-name>         \r
-               <url-pattern>/git/*</url-pattern>\r
-       </servlet-mapping>\r
-\r
-       \r
-       <!-- SparkleShare Invite Servlet\r
-                <url-pattern> MUST match: \r
-                       * com.gitblit.Constants.SPARKLESHARE_INVITE_PATH\r
-                       * Wicket Filter ignorePaths parameter -->\r
-       <servlet>\r
-               <servlet-name>SparkleShareInviteServlet</servlet-name>\r
-               <servlet-class>com.gitblit.SparkleShareInviteServlet</servlet-class>\r
-       </servlet>\r
-       <servlet-mapping>\r
-               <servlet-name>SparkleShareInviteServlet</servlet-name>          \r
-               <url-pattern>/sparkleshare/*</url-pattern>\r
-       </servlet-mapping>\r
-\r
-       \r
-       <!-- Syndication Servlet\r
-                <url-pattern> MUST match: \r
-                       * SyndicationFilter\r
-                       * com.gitblit.Constants.SYNDICATION_PATH\r
-                       * Wicket Filter ignorePaths parameter -->\r
-       <servlet>\r
-               <servlet-name>SyndicationServlet</servlet-name>\r
-               <servlet-class>com.gitblit.SyndicationServlet</servlet-class>           \r
-       </servlet>\r
-       <servlet-mapping>\r
-               <servlet-name>SyndicationServlet</servlet-name>\r
-               <url-pattern>/feed/*</url-pattern>\r
-       </servlet-mapping>\r
-       \r
-       \r
-       <!-- Zip Servlet\r
-                <url-pattern> MUST match: \r
-                       * ZipServlet\r
-                       * com.gitblit.Constants.ZIP_PATH\r
-                       * Wicket Filter ignorePaths parameter -->\r
-       <servlet>\r
-               <servlet-name>ZipServlet</servlet-name>\r
-               <servlet-class>com.gitblit.DownloadZipServlet</servlet-class>           \r
-       </servlet>\r
-       <servlet-mapping>\r
-               <servlet-name>ZipServlet</servlet-name>\r
-               <url-pattern>/zip/*</url-pattern>\r
-       </servlet-mapping>\r
-       \r
-       \r
-       <!-- Federation Servlet\r
-                <url-pattern> MUST match: \r
-                       * com.gitblit.Constants.FEDERATION_PATH          \r
-                       * Wicket Filter ignorePaths parameter -->\r
-       <servlet>\r
-               <servlet-name>FederationServlet</servlet-name>\r
-               <servlet-class>com.gitblit.FederationServlet</servlet-class>            \r
-       </servlet>\r
-       <servlet-mapping>\r
-               <servlet-name>FederationServlet</servlet-name>\r
-               <url-pattern>/federation/*</url-pattern>\r
-       </servlet-mapping>      \r
-       \r
-       \r
-       <!-- Rpc Servlet\r
-                <url-pattern> MUST match: \r
-                       * com.gitblit.Constants.RPC_PATH                 \r
-                       * Wicket Filter ignorePaths parameter -->\r
-       <servlet>\r
-               <servlet-name>RpcServlet</servlet-name>\r
-               <servlet-class>com.gitblit.RpcServlet</servlet-class>           \r
-       </servlet>\r
-       <servlet-mapping>\r
-               <servlet-name>RpcServlet</servlet-name>\r
-               <url-pattern>/rpc/*</url-pattern>\r
-       </servlet-mapping>      \r
-\r
-\r
-       <!-- Pages Servlet\r
-                <url-pattern> MUST match: \r
-                       * PagesFilter\r
-                       * com.gitblit.Constants.PAGES_PATH\r
-                       * Wicket Filter ignorePaths parameter -->\r
-       <servlet>\r
-               <servlet-name>PagesServlet</servlet-name>\r
-               <servlet-class>com.gitblit.PagesServlet</servlet-class>\r
-       </servlet>\r
-       <servlet-mapping>\r
-               <servlet-name>PagesServlet</servlet-name>               \r
-               <url-pattern>/pages/*</url-pattern>\r
-       </servlet-mapping>      \r
-\r
-       \r
-       <!-- Logo Servlet\r
-                <url-pattern> MUST match: \r
-                       * Wicket Filter ignorePaths parameter -->\r
-       <servlet>\r
-               <servlet-name>LogoServlet</servlet-name>\r
-               <servlet-class>com.gitblit.LogoServlet</servlet-class>\r
-       </servlet>\r
-       <servlet-mapping>\r
-               <servlet-name>LogoServlet</servlet-name>                \r
-               <url-pattern>/logo.png</url-pattern>\r
-       </servlet-mapping>\r
-\r
-       <!-- Branch Graph Servlet\r
-                <url-pattern> MUST match: \r
-                       * Wicket Filter ignorePaths parameter -->\r
-       <servlet>\r
-               <servlet-name>BranchGraphServlet</servlet-name>\r
-               <servlet-class>com.gitblit.BranchGraphServlet</servlet-class>\r
-       </servlet>\r
-       <servlet-mapping>\r
-               <servlet-name>BranchGraphServlet</servlet-name>         \r
-               <url-pattern>/graph/*</url-pattern>\r
-       </servlet-mapping>\r
-\r
-       <!-- Robots.txt Servlet\r
-                <url-pattern> MUST match: \r
-                       * Wicket Filter ignorePaths parameter -->\r
-       <servlet>\r
-               <servlet-name>RobotsTxtServlet</servlet-name>\r
-               <servlet-class>com.gitblit.RobotsTxtServlet</servlet-class>\r
-       </servlet>\r
-       <servlet-mapping>\r
-               <servlet-name>RobotsTxtServlet</servlet-name>           \r
-               <url-pattern>/robots.txt</url-pattern>\r
-       </servlet-mapping>\r
-\r
-       \r
-       <!-- Git Access Restriction Filter\r
-                <url-pattern> MUST match: \r
-                       * GitServlet\r
-                       * com.gitblit.Constants.GIT_PATH\r
-                       * Wicket Filter ignorePaths parameter -->\r
-       <filter>\r
-               <filter-name>GitFilter</filter-name>\r
-               <filter-class>com.gitblit.GitFilter</filter-class>\r
-       </filter>\r
-       <filter-mapping>\r
-               <filter-name>GitFilter</filter-name>\r
-               <url-pattern>/git/*</url-pattern>\r
-       </filter-mapping>\r
-       \r
-       \r
-       <!-- Syndication Restriction Filter\r
-                <url-pattern> MUST match: \r
-                       * SyndicationServlet\r
-                       * com.gitblit.Constants.SYNDICATION_PATH\r
-                       * Wicket Filter ignorePaths parameter -->\r
-       <filter>\r
-               <filter-name>SyndicationFilter</filter-name>\r
-               <filter-class>com.gitblit.SyndicationFilter</filter-class>\r
-       </filter>\r
-       <filter-mapping>\r
-               <filter-name>SyndicationFilter</filter-name>\r
-               <url-pattern>/feed/*</url-pattern>\r
-       </filter-mapping>\r
-       \r
-       \r
-       <!-- Download Zip Restriction Filter\r
-                <url-pattern> MUST match: \r
-                       * DownloadZipServlet\r
-                       * com.gitblit.Constants.ZIP_PATH\r
-                       * Wicket Filter ignorePaths parameter -->\r
-       <filter>\r
-               <filter-name>ZipFilter</filter-name>\r
-               <filter-class>com.gitblit.DownloadZipFilter</filter-class>\r
-       </filter>\r
-       <filter-mapping>\r
-               <filter-name>ZipFilter</filter-name>\r
-               <url-pattern>/zip/*</url-pattern>\r
-       </filter-mapping>\r
-\r
-               \r
-       <!-- Rpc Restriction Filter\r
-                <url-pattern> MUST match: \r
-                       * RpcServlet\r
-                       * com.gitblit.Constants.RPC_PATH\r
-                       * Wicket Filter ignorePaths parameter -->\r
-       <filter>\r
-               <filter-name>RpcFilter</filter-name>\r
-               <filter-class>com.gitblit.RpcFilter</filter-class>\r
-       </filter>\r
-       <filter-mapping>\r
-               <filter-name>RpcFilter</filter-name>\r
-               <url-pattern>/rpc/*</url-pattern>\r
-       </filter-mapping>\r
-\r
-\r
-       <!-- Pges Restriction Filter\r
-                <url-pattern> MUST match: \r
-                       * PagesServlet\r
-                       * com.gitblit.Constants.PAGES_PATH\r
-                       * Wicket Filter ignorePaths parameter -->\r
-       <filter>\r
-               <filter-name>PagesFilter</filter-name>\r
-               <filter-class>com.gitblit.PagesFilter</filter-class>\r
-       </filter>\r
-       <filter-mapping>\r
-               <filter-name>PagesFilter</filter-name>\r
-               <url-pattern>/pages/*</url-pattern>\r
-       </filter-mapping>\r
-       \r
-       <filter>\r
-               <filter-name>EnforceAuthenticationFilter</filter-name>\r
-               <filter-class>com.gitblit.EnforceAuthenticationFilter</filter-class>\r
-       </filter>\r
-       <filter-mapping>\r
-        <filter-name>EnforceAuthenticationFilter</filter-name>\r
-        <url-pattern>/*</url-pattern>\r
-    </filter-mapping>\r
-\r
-\r
-       <!-- Wicket Filter -->\r
-    <filter>\r
-        <filter-name>wicketFilter</filter-name>\r
-        <filter-class>\r
-            com.gitblit.wicket.GitblitWicketFilter\r
-        </filter-class>\r
-        <init-param>\r
-            <param-name>applicationClassName</param-name>\r
-            <param-value>com.gitblit.wicket.GitBlitWebApp</param-value>\r
-        </init-param>\r
-        <init-param>\r
-            <param-name>ignorePaths</param-name>\r
-            <!-- Paths should match \r
-               * SyndicationFilter <url-pattern>\r
-               * SyndicationServlet <url-pattern>\r
-               * com.gitblit.Constants.SYNDICATION_PATH\r
-               * GitFilter <url-pattern>\r
-               * GitServlet <url-pattern>\r
-               * com.gitblit.Constants.GIT_PATH\r
-               * SparkleshareInviteServlet <url-pattern>\r
-               * com.gitblit.Constants.SPARKLESHARE_INVITE_PATH\r
-               * Zipfilter <url-pattern>\r
-               * ZipServlet <url-pattern>\r
-               * com.gitblit.Constants.ZIP_PATH\r
-               * FederationServlet <url-pattern>\r
-               * RpcFilter <url-pattern>\r
-               * RpcServlet <url-pattern>\r
-               * PagesFilter <url-pattern>\r
-               * PagesServlet <url-pattern>\r
-               * com.gitblit.Constants.PAGES_PATH -->\r
-            <param-value>git/,feed/,zip/,federation/,rpc/,pages/,robots.txt,logo.png,graph/,sparkleshare/</param-value>\r
-        </init-param>\r
-    </filter>\r
-    <filter-mapping>\r
-        <filter-name>wicketFilter</filter-name>\r
-        <url-pattern>/*</url-pattern>\r
-    </filter-mapping>\r
 </web-app>
\ No newline at end of file
index fb57af53a5153a6ec3817bb2db8a4ae9cd17e4a8..e52d9d790ca3a0d5dc778982751cb4f7d362cebd 100644 (file)
@@ -29,6 +29,9 @@ import com.gitblit.models.UserModel;
  */\r
 public class DownloadZipFilter extends AccessRestrictionFilter {\r
 \r
+       public DownloadZipFilter() {\r
+       }\r
+\r
        /**\r
         * Extract the repository name from the url.\r
         *\r
index 93057b452ca6b87324b6599799883236395716f8..12b0759539c1bc783e1d84e7af0c101ade3fde5f 100644 (file)
@@ -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)
         */
index 5fbc876b115334e92c2182a16dc4e13b461a0bd0..31dedf1355e8963b83dcb2569d6aa006722a08a5 100644 (file)
@@ -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<FederationModel> federationRegistrations = Collections
@@ -205,8 +201,6 @@ public class GitBlit implements ServletContextListener,
 
        private final ObjectCache<String> projectRepositoriesMarkdownCache = new ObjectCache<String>();
 
-       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<String, String> params = new HashMap<String, String>();
+               params.put(GitblitWicketFilter.FILTER_MAPPING_PARAM, "/*");
+               params.put(GitblitWicketFilter.IGNORE_PATHS_PARAM, toIgnore);
+               filter(context, "/*", GitblitWicketFilter.class, params);
+       }
 }
index f52963f74e6e94706c3b0a6070b424af791bc3d9..a06c7e52ce94f827cdaa8cd4a1cb032e7bf64513 100644 (file)
@@ -42,6 +42,9 @@ public class GitFilter extends AccessRestrictionFilter {
        protected static final String[] suffixes = { gitReceivePack, gitUploadPack, "/info/refs", "/HEAD",\r
                        "/objects" };\r
 \r
+       public GitFilter() {\r
+       }\r
+\r
        /**\r
         * Extract the repository name from the url.\r
         *\r
diff --git a/src/main/java/com/gitblit/InjectionContextListener.java b/src/main/java/com/gitblit/InjectionContextListener.java
new file mode 100644 (file)
index 0000000..712ae64
--- /dev/null
@@ -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<String> registeredPaths = new ArrayList<String>();
+
+       protected final List<String> 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<? extends Servlet> 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<? extends Servlet> servletClass, Map<String, String> 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<? extends Servlet> servletClass) {
+               serve(context, route, servletClass, (Class<Filter>) 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<? extends Servlet> servletClass, Map<String, String> 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<? extends Servlet> servletClass, Class<? extends Filter> 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<? extends Filter> 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<? extends Filter> filterClass, Map<String, String> 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> X instantiate(ServletContext context, Class<X> clazz) {
+               try {
+                       return clazz.newInstance();
+               } catch (Throwable t) {
+                       logger.error(null, t);
+               }
+               return null;
+       }
+}
index a42d8a4a826722e4a4f47b3025bacdf0bc3eaef3..4c9568dcb0de3ec2ce514026f1c018b958fe3d19 100644 (file)
@@ -31,6 +31,9 @@ import com.gitblit.models.UserModel;
  */\r
 public class PagesFilter extends AccessRestrictionFilter {\r
 \r
+       public PagesFilter() {\r
+       }\r
+\r
        /**\r
         * Extract the repository name from the url.\r
         *\r
index 161af9d59d89d87bdac743f568276a1658179138..b1384ad39d9568bfe2f94c9d37815e636a0d60cf 100644 (file)
@@ -44,6 +44,9 @@ import com.gitblit.models.UserModel;
  */\r
 public class RpcFilter extends AuthenticationFilter {\r
 \r
+       public RpcFilter() {\r
+       }\r
+\r
        /**\r
         * doFilter does the actual work of preprocessing the request to ensure that\r
         * the user may proceed.\r
index ab854bb770207179365def9f3f722ee011db3c8e..ad908fa8853e091515eaf6c08aa7de20a55d8395 100644 (file)
@@ -43,6 +43,9 @@ import com.gitblit.models.UserModel;
  */\r
 public class SyndicationFilter extends AuthenticationFilter {\r
 \r
+       public SyndicationFilter() {\r
+       }\r
+\r
        /**\r
         * Extract the repository name from the url.\r
         *\r
index ae50c3e43d46127b3f3cd8dde930e6c96ad71f79..11089ee1f5619c2b473774ed3eec4e501c7171d8 100644 (file)
@@ -59,6 +59,9 @@ public class SyndicationServlet extends HttpServlet {
 \r
        private transient Logger logger = LoggerFactory.getLogger(SyndicationServlet.class);\r
 \r
+       public SyndicationServlet() {\r
+       }\r
+\r
        /**\r
         * Create a feed link for the specified repository and branch/tag/commit id.\r
         *\r
index 0233cd90c2a0e1690515152d91fae1eee35eaa2c..23ea3434e752980bb15bc78aeff3b84e580f06bd 100644 (file)
@@ -33,6 +33,9 @@ public class GitServlet extends org.eclipse.jgit.http.server.GitServlet {
 \r
        private static final long serialVersionUID = 1L;\r
 \r
+       public GitServlet() {\r
+       }\r
+\r
        @Override\r
        public void init(ServletConfig config) throws ServletException {\r
                IRepositoryManager repositoryManager = GitBlit.getManager(IRepositoryManager.class);\r
index fbe68fe48a34e21f5412943bc3336ffb85bc4ab8..bc65d77da111bd4dea98ecf0d02a3dd365e11912 100644 (file)
@@ -19,6 +19,8 @@ import java.util.Date;
 \r
 import javax.servlet.http.HttpServletRequest;\r
 \r
+import org.apache.wicket.protocol.http.IWebApplicationFactory;\r
+import org.apache.wicket.protocol.http.WebApplication;\r
 import org.apache.wicket.protocol.http.WicketFilter;\r
 import org.apache.wicket.util.string.Strings;\r
 import org.eclipse.jgit.lib.Repository;\r
@@ -45,6 +47,19 @@ import com.gitblit.utils.StringUtils;
  */\r
 public class GitblitWicketFilter extends WicketFilter {\r
 \r
+       public GitblitWicketFilter() {\r
+       }\r
+\r
+       @Override\r
+       protected IWebApplicationFactory getApplicationFactory() {\r
+               return new IWebApplicationFactory() {\r
+                       @Override\r
+                       public WebApplication createApplication(WicketFilter filter) {\r
+                               return new GitBlitWebApp();\r
+                       }\r
+               };\r
+       }\r
+\r
        /**\r
         * Determines the last-modified date of the requested resource.\r
         *\r
@@ -54,8 +69,9 @@ public class GitblitWicketFilter extends WicketFilter {
        @Override\r
        protected long getLastModified(final HttpServletRequest servletRequest) {\r
                final String pathInfo = getRelativePath(servletRequest);\r
-               if (Strings.isEmpty(pathInfo))\r
+               if (Strings.isEmpty(pathInfo)) {\r
                        return -1;\r
+               }\r
                long lastModified = super.getLastModified(servletRequest);\r
                if (lastModified > -1) {\r
                        return lastModified;\r