PageRenderTime 576ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/com/commafeed/CommaFeedApplication.java

https://gitlab.com/gmi7/commafeed
Java | 174 lines | 136 code | 26 blank | 12 comment | 3 complexity | 87faea5646d1e516a67edaa0ef8c9e66 MD5 | raw file
  1. package com.commafeed;
  2. import java.io.IOException;
  3. import java.util.Date;
  4. import java.util.EnumSet;
  5. import java.util.Set;
  6. import java.util.concurrent.ScheduledExecutorService;
  7. import javax.servlet.DispatcherType;
  8. import javax.servlet.FilterChain;
  9. import javax.servlet.ServletException;
  10. import javax.servlet.ServletRequest;
  11. import javax.servlet.ServletResponse;
  12. import javax.servlet.http.HttpServletRequest;
  13. import org.eclipse.jetty.server.session.SessionHandler;
  14. import org.hibernate.cfg.AvailableSettings;
  15. import com.commafeed.backend.feed.FeedRefreshTaskGiver;
  16. import com.commafeed.backend.feed.FeedRefreshUpdater;
  17. import com.commafeed.backend.feed.FeedRefreshWorker;
  18. import com.commafeed.backend.model.AbstractModel;
  19. import com.commafeed.backend.model.Feed;
  20. import com.commafeed.backend.model.FeedCategory;
  21. import com.commafeed.backend.model.FeedEntry;
  22. import com.commafeed.backend.model.FeedEntryContent;
  23. import com.commafeed.backend.model.FeedEntryStatus;
  24. import com.commafeed.backend.model.FeedEntryTag;
  25. import com.commafeed.backend.model.FeedSubscription;
  26. import com.commafeed.backend.model.User;
  27. import com.commafeed.backend.model.UserRole;
  28. import com.commafeed.backend.model.UserSettings;
  29. import com.commafeed.backend.service.StartupService;
  30. import com.commafeed.backend.service.UserService;
  31. import com.commafeed.backend.task.ScheduledTask;
  32. import com.commafeed.frontend.auth.SecurityCheckFactoryProvider;
  33. import com.commafeed.frontend.resource.AdminREST;
  34. import com.commafeed.frontend.resource.CategoryREST;
  35. import com.commafeed.frontend.resource.EntryREST;
  36. import com.commafeed.frontend.resource.FeedREST;
  37. import com.commafeed.frontend.resource.PubSubHubbubCallbackREST;
  38. import com.commafeed.frontend.resource.ServerREST;
  39. import com.commafeed.frontend.resource.UserREST;
  40. import com.commafeed.frontend.servlet.AnalyticsServlet;
  41. import com.commafeed.frontend.servlet.CustomCssServlet;
  42. import com.commafeed.frontend.servlet.LogoutServlet;
  43. import com.commafeed.frontend.servlet.NextUnreadServlet;
  44. import com.commafeed.frontend.session.SessionHelperFactoryProvider;
  45. import com.google.inject.Guice;
  46. import com.google.inject.Injector;
  47. import com.google.inject.Key;
  48. import com.google.inject.TypeLiteral;
  49. import io.dropwizard.Application;
  50. import io.dropwizard.assets.AssetsBundle;
  51. import io.dropwizard.db.DataSourceFactory;
  52. import io.dropwizard.forms.MultiPartBundle;
  53. import io.dropwizard.hibernate.HibernateBundle;
  54. import io.dropwizard.migrations.MigrationsBundle;
  55. import io.dropwizard.server.DefaultServerFactory;
  56. import io.dropwizard.servlets.CacheBustingFilter;
  57. import io.dropwizard.setup.Bootstrap;
  58. import io.dropwizard.setup.Environment;
  59. public class CommaFeedApplication extends Application<CommaFeedConfiguration> {
  60. public static final String USERNAME_ADMIN = "admin";
  61. public static final String USERNAME_DEMO = "demo";
  62. public static final Date STARTUP_TIME = new Date();
  63. private HibernateBundle<CommaFeedConfiguration> hibernateBundle;
  64. @Override
  65. public String getName() {
  66. return "CommaFeed";
  67. }
  68. @Override
  69. public void initialize(Bootstrap<CommaFeedConfiguration> bootstrap) {
  70. bootstrap.addBundle(hibernateBundle = new HibernateBundle<CommaFeedConfiguration>(AbstractModel.class, Feed.class,
  71. FeedCategory.class, FeedEntry.class, FeedEntryContent.class, FeedEntryStatus.class, FeedEntryTag.class,
  72. FeedSubscription.class, User.class, UserRole.class, UserSettings.class) {
  73. @Override
  74. public DataSourceFactory getDataSourceFactory(CommaFeedConfiguration configuration) {
  75. DataSourceFactory factory = configuration.getDataSourceFactory();
  76. // keep using old id generator for backward compatibility
  77. factory.getProperties().put(AvailableSettings.USE_NEW_ID_GENERATOR_MAPPINGS, "false");
  78. factory.getProperties().put(AvailableSettings.STATEMENT_BATCH_SIZE, "50");
  79. factory.getProperties().put(AvailableSettings.BATCH_VERSIONED_DATA, "true");
  80. return factory;
  81. }
  82. });
  83. bootstrap.addBundle(new MigrationsBundle<CommaFeedConfiguration>() {
  84. @Override
  85. public DataSourceFactory getDataSourceFactory(CommaFeedConfiguration configuration) {
  86. return configuration.getDataSourceFactory();
  87. }
  88. });
  89. bootstrap.addBundle(new AssetsBundle("/assets/", "/", "index.html"));
  90. bootstrap.addBundle(new MultiPartBundle());
  91. }
  92. @Override
  93. public void run(CommaFeedConfiguration config, Environment environment) throws Exception {
  94. // guice init
  95. Injector injector = Guice.createInjector(new CommaFeedModule(hibernateBundle.getSessionFactory(), config, environment.metrics()));
  96. // session management
  97. environment.servlets().setSessionHandler(new SessionHandler(config.getSessionManagerFactory().build()));
  98. // support for "@SecurityCheck User user" injection
  99. environment.jersey().register(new SecurityCheckFactoryProvider.Binder(injector.getInstance(UserService.class)));
  100. // support for "@Context SessionHelper sessionHelper" injection
  101. environment.jersey().register(new SessionHelperFactoryProvider.Binder());
  102. // REST resources
  103. environment.jersey().setUrlPattern("/rest/*");
  104. ((DefaultServerFactory) config.getServerFactory()).setJerseyRootPath("/rest/*");
  105. environment.jersey().register(injector.getInstance(AdminREST.class));
  106. environment.jersey().register(injector.getInstance(CategoryREST.class));
  107. environment.jersey().register(injector.getInstance(EntryREST.class));
  108. environment.jersey().register(injector.getInstance(FeedREST.class));
  109. environment.jersey().register(injector.getInstance(PubSubHubbubCallbackREST.class));
  110. environment.jersey().register(injector.getInstance(ServerREST.class));
  111. environment.jersey().register(injector.getInstance(UserREST.class));
  112. // Servlets
  113. environment.servlets().addServlet("next", injector.getInstance(NextUnreadServlet.class)).addMapping("/next");
  114. environment.servlets().addServlet("logout", injector.getInstance(LogoutServlet.class)).addMapping("/logout");
  115. environment.servlets().addServlet("customCss", injector.getInstance(CustomCssServlet.class)).addMapping("/custom_css.css");
  116. environment.servlets().addServlet("analytics.js", injector.getInstance(AnalyticsServlet.class)).addMapping("/analytics.js");
  117. // Scheduled tasks
  118. Set<ScheduledTask> tasks = injector.getInstance(Key.get(new TypeLiteral<Set<ScheduledTask>>() {
  119. }));
  120. ScheduledExecutorService executor = environment.lifecycle().scheduledExecutorService("task-scheduler", true).threads(tasks.size())
  121. .build();
  122. for (ScheduledTask task : tasks) {
  123. task.register(executor);
  124. }
  125. // database init/changelogs
  126. environment.lifecycle().manage(injector.getInstance(StartupService.class));
  127. // background feed fetching
  128. environment.lifecycle().manage(injector.getInstance(FeedRefreshTaskGiver.class));
  129. environment.lifecycle().manage(injector.getInstance(FeedRefreshWorker.class));
  130. environment.lifecycle().manage(injector.getInstance(FeedRefreshUpdater.class));
  131. // cache configuration
  132. // prevent caching on REST resources, except for favicons
  133. environment.servlets().addFilter("cache-filter", new CacheBustingFilter() {
  134. @Override
  135. public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
  136. String path = ((HttpServletRequest) request).getRequestURI();
  137. if (path.contains("/feed/favicon")) {
  138. chain.doFilter(request, response);
  139. } else {
  140. super.doFilter(request, response, chain);
  141. }
  142. }
  143. }).addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "/rest/*");
  144. }
  145. public static void main(String[] args) throws Exception {
  146. new CommaFeedApplication().run(args);
  147. }
  148. }