Commit 24890a47 authored by Grzegorz Pietrusza's avatar Grzegorz Pietrusza

use reflections to iterate over registered resources

parent e3772e66
package com.telephoners.krakyournet.ctf; package com.telephoners.krakyournet.ctf;
import com.bazaarvoice.dropwizard.webjars.WebJarBundle; import com.bazaarvoice.dropwizard.webjars.WebJarBundle;
import com.google.common.collect.ImmutableSet;
import com.google.common.reflect.ClassPath;
import com.google.inject.Guice; import com.google.inject.Guice;
import com.google.inject.Injector; import com.google.inject.Injector;
import com.telephoners.krakyournet.ctf.auth.ExampleAuthenticator; import com.telephoners.krakyournet.ctf.auth.ExampleAuthenticator;
import com.telephoners.krakyournet.ctf.beans.User;
import com.telephoners.krakyournet.ctf.commands.PurgeDatabaseCommand; import com.telephoners.krakyournet.ctf.commands.PurgeDatabaseCommand;
import com.telephoners.krakyournet.ctf.commands.RegisterTasksCommand; import com.telephoners.krakyournet.ctf.commands.RegisterTasksCommand;
import com.telephoners.krakyournet.ctf.commands.RegisterTeamsCommand; import com.telephoners.krakyournet.ctf.commands.RegisterTeamsCommand;
import com.telephoners.krakyournet.ctf.core.ApplicationConfiguration; import com.telephoners.krakyournet.ctf.core.ApplicationConfiguration;
import com.telephoners.krakyournet.ctf.logging.LoggingFilter; import com.telephoners.krakyournet.ctf.logging.LoggingFilter;
import com.telephoners.krakyournet.ctf.modules.ApplicationModule; import com.telephoners.krakyournet.ctf.modules.ApplicationModule;
import com.telephoners.krakyournet.ctf.beans.User;
import com.telephoners.krakyournet.ctf.repositories.UsersRepository; import com.telephoners.krakyournet.ctf.repositories.UsersRepository;
import com.telephoners.krakyournet.ctf.resources.*;
import io.dropwizard.Application; import io.dropwizard.Application;
import io.dropwizard.assets.AssetsBundle; import io.dropwizard.assets.AssetsBundle;
import io.dropwizard.auth.AuthDynamicFeature; import io.dropwizard.auth.AuthDynamicFeature;
...@@ -24,6 +25,7 @@ import io.dropwizard.setup.Bootstrap; ...@@ -24,6 +25,7 @@ import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment; import io.dropwizard.setup.Environment;
import org.glassfish.jersey.server.filter.RolesAllowedDynamicFeature; import org.glassfish.jersey.server.filter.RolesAllowedDynamicFeature;
import java.io.IOException;
import java.util.stream.Stream; import java.util.stream.Stream;
public class CTFApplication extends Application<ApplicationConfiguration> public class CTFApplication extends Application<ApplicationConfiguration>
...@@ -42,16 +44,13 @@ public class CTFApplication extends Application<ApplicationConfiguration> ...@@ -42,16 +44,13 @@ public class CTFApplication extends Application<ApplicationConfiguration>
.forEach(bootstrap::addBundle); .forEach(bootstrap::addBundle);
} }
private void registerResources(Environment environment) private void registerResources(Environment environment) throws IOException
{ {
//todo: use reflections to iterate over resources
JerseyEnvironment jersey = environment.jersey(); JerseyEnvironment jersey = environment.jersey();
jersey.register(injector.getInstance(TeamsResource.class)); final ClassPath classPath = ClassPath.from(this.getClass().getClassLoader());
jersey.register(injector.getInstance(TasksResource.class)); ImmutableSet<ClassPath.ClassInfo> resourceClasses = classPath.getTopLevelClasses("com.telephoners.krakyournet.ctf.resources");
jersey.register(injector.getInstance(ProxyResource.class)); resourceClasses.stream()
jersey.register(injector.getInstance(SolutionsResource.class)); .forEach(classInfo -> jersey.register(injector.getInstance(classInfo.load())));
jersey.register(injector.getInstance(TaskResource.class));
jersey.register(injector.getInstance(UtilResource.class));
} }
private void registerAuthFeatures(Environment environment) private void registerAuthFeatures(Environment environment)
......
...@@ -9,9 +9,7 @@ import javax.inject.Inject; ...@@ -9,9 +9,7 @@ import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@Singleton @Singleton
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment