diff options
author | James Moger <james.moger@gitblit.com> | 2014-05-23 08:17:11 -0400 |
---|---|---|
committer | James Moger <james.moger@gitblit.com> | 2014-07-03 16:57:47 -0400 |
commit | c828cf2db88956094a31a79741145688876879df (patch) | |
tree | 7f9d7e8b0fe7d91350afb79b2baf60f543712150 /src/main/java/com/gitblit/guice | |
parent | aa1361d04cfe09f90e7d8bece90c00dd6e4185bb (diff) | |
download | gitblit-c828cf2db88956094a31a79741145688876879df.tar.gz gitblit-c828cf2db88956094a31a79741145688876879df.zip |
Use Guice-Servlet rather than custom code and expose the Injector
This is a fairly functional variation of Gitblit with one notable
exception:
The security filters are not working properly.
This is a design flaw in Guice that I have reported upstream [1]. The
general idea is that Guice-Servlet filters are not properly wrapping the
ServletRequest. This has historically been a problem for Guice-Servlet
servlets but Google has fixed most of those issues. Unfortunately, all
the same flaws reported against the servlet delegation also exist in
Guice-Servlet filter delegation. :(
[1]: https://code.google.com/p/google-guice/issues/detail?id=807
Diffstat (limited to 'src/main/java/com/gitblit/guice')
-rw-r--r-- | src/main/java/com/gitblit/guice/CoreModule.java (renamed from src/main/java/com/gitblit/guice/GuiceModule.java) | 4 | ||||
-rw-r--r-- | src/main/java/com/gitblit/guice/GuiceContext.java | 86 | ||||
-rw-r--r-- | src/main/java/com/gitblit/guice/WebModule.java | 99 |
3 files changed, 101 insertions, 88 deletions
diff --git a/src/main/java/com/gitblit/guice/GuiceModule.java b/src/main/java/com/gitblit/guice/CoreModule.java index 254e0680..9b479c09 100644 --- a/src/main/java/com/gitblit/guice/GuiceModule.java +++ b/src/main/java/com/gitblit/guice/CoreModule.java @@ -47,12 +47,12 @@ import com.google.inject.AbstractModule; import com.google.inject.Provides; /** - * GuiceModule references all injectable objects. + * CoreModule references all the core business objects. * * @author James Moger * */ -public class GuiceModule extends AbstractModule { +public class CoreModule extends AbstractModule { @Override protected void configure() { diff --git a/src/main/java/com/gitblit/guice/GuiceContext.java b/src/main/java/com/gitblit/guice/GuiceContext.java deleted file mode 100644 index 4361b7bd..00000000 --- a/src/main/java/com/gitblit/guice/GuiceContext.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * Copyright 2014 gitblit.com. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.gitblit.guice; - -import javax.servlet.ServletContext; -import javax.servlet.ServletContextEvent; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.gitblit.servlet.InjectionContextListener; -import com.google.inject.AbstractModule; -import com.google.inject.Guice; -import com.google.inject.Injector; - -/** - * Guice servlet context listener is a context listener that uses Guice to - * instantiate and inject servlets, filters, and anything else you might want. - * - * @author James Moger - * - */ -public abstract class GuiceContext extends InjectionContextListener { - - public static final String INJECTOR_NAME = Injector.class.getName(); - - protected final Logger logger = LoggerFactory.getLogger(getClass()); - - protected abstract AbstractModule [] getModules(); - - protected abstract void destroyContext(ServletContext context); - - protected Injector getInjector(ServletContext context) { - Object o = context.getAttribute(INJECTOR_NAME); - if (o == null) { - logger.debug("instantiating Guice modules"); - AbstractModule [] modules = getModules(); - logger.debug("getting Guice injector"); - try { - o = Guice.createInjector(modules); - logger.debug("setting Guice injector into {} attribute", INJECTOR_NAME); - context.setAttribute(INJECTOR_NAME, o); - } catch (Throwable t) { - logger.error("an error occurred creating the Guice injector", t); - } - } - return (Injector) o; - } - - /** - * Instantiates an object. - * - * @param clazz - * @return the object - */ - @Override - protected <X> X instantiate(ServletContext context, Class<X> clazz) { - try { - Injector injector = getInjector(context); - return injector.getInstance(clazz); - } catch (Throwable t) { - logger.error(null, t); - } - return null; - } - - @Override - public final void contextDestroyed(ServletContextEvent contextEvent) { - ServletContext context = contextEvent.getServletContext(); - context.removeAttribute(INJECTOR_NAME); - destroyContext(context); - } -} diff --git a/src/main/java/com/gitblit/guice/WebModule.java b/src/main/java/com/gitblit/guice/WebModule.java new file mode 100644 index 00000000..b4f9899f --- /dev/null +++ b/src/main/java/com/gitblit/guice/WebModule.java @@ -0,0 +1,99 @@ +/* + * Copyright 2014 gitblit.com. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.gitblit.guice; + +import java.util.HashMap; +import java.util.Map; + +import com.gitblit.Constants; +import com.gitblit.servlet.BranchGraphServlet; +import com.gitblit.servlet.DownloadZipServlet; +import com.gitblit.servlet.EnforceAuthenticationFilter; +import com.gitblit.servlet.FederationServlet; +import com.gitblit.servlet.GitServlet; +import com.gitblit.servlet.LogoServlet; +import com.gitblit.servlet.PagesServlet; +import com.gitblit.servlet.ProxyFilter; +import com.gitblit.servlet.PtServlet; +import com.gitblit.servlet.RawServlet; +import com.gitblit.servlet.RobotsTxtServlet; +import com.gitblit.servlet.RpcServlet; +import com.gitblit.servlet.SparkleShareInviteServlet; +import com.gitblit.servlet.SyndicationServlet; +import com.gitblit.wicket.GitblitWicketFilter; +import com.google.common.base.Joiner; +import com.google.inject.servlet.ServletModule; + +/** + * Defines all the web servlets & filters. + * + * @author James Moger + * + */ +public class WebModule extends ServletModule { + + final static String ALL = "/*"; + + @Override + protected void configureServlets() { + // servlets + serve(fuzzy(Constants.R_PATH), fuzzy(Constants.GIT_PATH)).with(GitServlet.class); + serve(fuzzy(Constants.RAW_PATH)).with(RawServlet.class); + serve(fuzzy(Constants.PAGES)).with(PagesServlet.class); + serve(fuzzy(Constants.RPC_PATH)).with(RpcServlet.class); + serve(fuzzy(Constants.ZIP_PATH)).with(DownloadZipServlet.class); + serve(fuzzy(Constants.SYNDICATION_PATH)).with(SyndicationServlet.class); + + serve(fuzzy(Constants.FEDERATION_PATH)).with(FederationServlet.class); + serve(fuzzy(Constants.SPARKLESHARE_INVITE_PATH)).with(SparkleShareInviteServlet.class); + serve(fuzzy(Constants.BRANCH_GRAPH_PATH)).with(BranchGraphServlet.class); + serve(Constants.PT_PATH).with(PtServlet.class); + serve("/robots.txt").with(RobotsTxtServlet.class); + serve("/logo.png").with(LogoServlet.class); + + // global filters + filter(ALL).through(ProxyFilter.class); + filter(ALL).through(EnforceAuthenticationFilter.class); + + // security filters +// filter(fuzzy(Constants.R_PATH), fuzzy(Constants.GIT_PATH)).through(GitFilter.class); +// filter(fuzzy(Constants.RAW_PATH)).through(RawFilter.class); +// filter(fuzzy(Constants.PAGES)).through(PagesFilter.class); +// filter(fuzzy(Constants.RPC_PATH)).through(RpcFilter.class); +// filter(fuzzy(Constants.ZIP_PATH)).through(DownloadZipFilter.class); +// filter(fuzzy(Constants.SYNDICATION_PATH)).through(SyndicationFilter.class); + + // Wicket + String toIgnore = Joiner.on(",").join(Constants.R_PATH, Constants.GIT_PATH, Constants.RAW_PATH, + Constants.PAGES, Constants.RPC_PATH, Constants.ZIP_PATH, Constants.SYNDICATION_PATH, + Constants.FEDERATION_PATH, Constants.SPARKLESHARE_INVITE_PATH, Constants.BRANCH_GRAPH_PATH, + Constants.PT_PATH, "/robots.txt", "/logo.png"); + + Map<String, String> params = new HashMap<String, String>(); + params.put(GitblitWicketFilter.FILTER_MAPPING_PARAM, ALL); + params.put(GitblitWicketFilter.IGNORE_PATHS_PARAM, toIgnore); + filter(ALL).through(GitblitWicketFilter.class, params); + } + + private String fuzzy(String path) { + if (path.endsWith(ALL)) { + return path; + } else if (path.endsWith("/")) { + return path + "*"; + } + return path + ALL; + } +} |