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