You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

InjectionContextListener.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Copyright 2013 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit.servlet;
  17. import java.util.ArrayList;
  18. import java.util.EnumSet;
  19. import java.util.List;
  20. import java.util.Map;
  21. import javax.servlet.DispatcherType;
  22. import javax.servlet.Filter;
  23. import javax.servlet.FilterRegistration;
  24. import javax.servlet.Servlet;
  25. import javax.servlet.ServletContext;
  26. import javax.servlet.ServletContextEvent;
  27. import javax.servlet.ServletContextListener;
  28. import javax.servlet.ServletRegistration;
  29. import org.slf4j.Logger;
  30. import org.slf4j.LoggerFactory;
  31. /**
  32. * Injection context listener instantiates and injects servlets, filters, and
  33. * anything else you might want into a servlet context. This class provides
  34. * convenience methods for servlet & filter registration and also tracks
  35. * registered paths.
  36. *
  37. * @author James Moger
  38. *
  39. */
  40. public abstract class InjectionContextListener implements ServletContextListener {
  41. protected final Logger logger = LoggerFactory.getLogger(getClass());
  42. private final List<String> registeredPaths = new ArrayList<String>();
  43. protected final List<String> getRegisteredPaths() {
  44. return registeredPaths;
  45. }
  46. /**
  47. * Hook for subclasses to manipulate context initialization before
  48. * standard initialization procedure.
  49. *
  50. * @param context
  51. */
  52. protected void beforeServletInjection(ServletContext context) {
  53. // NOOP
  54. }
  55. /**
  56. * Hook for subclasses to instantiate and inject servlets and filters
  57. * into the servlet context.
  58. *
  59. * @param context
  60. */
  61. protected abstract void injectServlets(ServletContext context);
  62. /**
  63. * Hook for subclasses to manipulate context initialization after
  64. * servlet registration.
  65. *
  66. * @param context
  67. */
  68. protected void afterServletInjection(ServletContext context) {
  69. // NOOP
  70. }
  71. /**
  72. * Configure Gitblit from the web.xml, if no configuration has already been
  73. * specified.
  74. *
  75. * @see ServletContextListener.contextInitialize(ServletContextEvent)
  76. */
  77. @Override
  78. public final void contextInitialized(ServletContextEvent contextEvent) {
  79. ServletContext context = contextEvent.getServletContext();
  80. beforeServletInjection(context);
  81. injectServlets(context);
  82. afterServletInjection(context);
  83. }
  84. /**
  85. * Registers a file path.
  86. *
  87. * @param context
  88. * @param file
  89. * @param servletClass
  90. */
  91. protected void file(ServletContext context, String file, Class<? extends Servlet> servletClass) {
  92. file(context, file, servletClass, null);
  93. }
  94. /**
  95. * Registers a file path with init parameters.
  96. *
  97. * @param context
  98. * @param file
  99. * @param servletClass
  100. * @param initParams
  101. */
  102. protected void file(ServletContext context, String file, Class<? extends Servlet> servletClass, Map<String, String> initParams) {
  103. Servlet servlet = instantiate(context, servletClass);
  104. ServletRegistration.Dynamic d = context.addServlet(sanitize(servletClass.getSimpleName() + file), servlet);
  105. d.addMapping(file);
  106. if (initParams != null) {
  107. d.setInitParameters(initParams);
  108. }
  109. registeredPaths.add(file);
  110. }
  111. /**
  112. * Serves a path (trailing wildcard will be appended).
  113. *
  114. * @param context
  115. * @param route
  116. * @param servletClass
  117. */
  118. protected void serve(ServletContext context, String route, Class<? extends Servlet> servletClass) {
  119. serve(context, route, servletClass, (Class<Filter>) null);
  120. }
  121. /**
  122. * Serves a path (trailing wildcard will be appended) with init parameters.
  123. *
  124. * @param context
  125. * @param route
  126. * @param servletClass
  127. * @param initParams
  128. */
  129. protected void serve(ServletContext context, String route, Class<? extends Servlet> servletClass, Map<String, String> initParams) {
  130. Servlet servlet = instantiate(context, servletClass);
  131. ServletRegistration.Dynamic d = context.addServlet(sanitize(servletClass.getSimpleName() + route), servlet);
  132. d.addMapping(route + "*");
  133. if (initParams != null) {
  134. d.setInitParameters(initParams);
  135. }
  136. registeredPaths.add(route);
  137. }
  138. /**
  139. * Serves a path (trailing wildcard will be appended) and also maps a filter
  140. * to that path.
  141. *
  142. * @param context
  143. * @param route
  144. * @param servletClass
  145. * @param filterClass
  146. */
  147. protected void serve(ServletContext context, String route, Class<? extends Servlet> servletClass, Class<? extends Filter> filterClass) {
  148. Servlet servlet = instantiate(context, servletClass);
  149. ServletRegistration.Dynamic d = context.addServlet(sanitize(servletClass.getSimpleName() + route), servlet);
  150. d.addMapping(route + "*");
  151. if (filterClass != null) {
  152. filter(context, route + "*", filterClass);
  153. }
  154. registeredPaths.add(route);
  155. }
  156. /**
  157. * Registers a path filter.
  158. *
  159. * @param context
  160. * @param route
  161. * @param filterClass
  162. */
  163. protected void filter(ServletContext context, String route, Class<? extends Filter> filterClass) {
  164. filter(context, route, filterClass, null);
  165. }
  166. /**
  167. * Registers a path filter with init parameters.
  168. *
  169. * @param context
  170. * @param route
  171. * @param filterClass
  172. * @param initParams
  173. */
  174. protected void filter(ServletContext context, String route, Class<? extends Filter> filterClass, Map<String, String> initParams) {
  175. Filter filter = instantiate(context, filterClass);
  176. FilterRegistration.Dynamic d = context.addFilter(sanitize(filterClass.getSimpleName() + route), filter);
  177. d.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, route);
  178. if (initParams != null) {
  179. d.setInitParameters(initParams);
  180. }
  181. }
  182. /**
  183. * Limit the generated servlet/filter names to alpha-numeric values with a
  184. * handful of acceptable other characters.
  185. *
  186. * @param name
  187. * @return a sanitized name
  188. */
  189. protected String sanitize(String name) {
  190. StringBuilder sb = new StringBuilder();
  191. for (char c : name.toCharArray()) {
  192. if (Character.isLetterOrDigit(c)) {
  193. sb.append(c);
  194. } else if ('-' == c) {
  195. sb.append(c);
  196. } else if ('*' == c) {
  197. sb.append("all");
  198. } else if ('.' == c) {
  199. sb.append('.');
  200. } else {
  201. sb.append('_');
  202. }
  203. }
  204. return sb.toString();
  205. }
  206. /**
  207. * Instantiates an object.
  208. *
  209. * @param clazz
  210. * @return the object
  211. */
  212. protected <X> X instantiate(ServletContext context, Class<X> clazz) {
  213. try {
  214. return clazz.newInstance();
  215. } catch (Throwable t) {
  216. logger.error(null, t);
  217. }
  218. return null;
  219. }
  220. }