Commit e6f7e71b authored by Grzegorz Pietrusza's avatar Grzegorz Pietrusza

move to seperate methods

parent cec1c3d9
......@@ -19,7 +19,6 @@ import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import objects.Flag;
import objects.Task;
import objects.Team;
import objects.User;
import org.glassfish.jersey.server.filter.RolesAllowedDynamicFeature;
import org.mongodb.morphia.Datastore;
......@@ -29,7 +28,6 @@ import repositories.TasksRepository;
import repositories.TeamsRepository;
import repositories.UsersRepository;
import java.util.List;
import java.util.UUID;
import java.util.stream.Stream;
......@@ -42,6 +40,8 @@ public class CTFApplication extends Application<ApplicationConfiguration> {
private Datastore datastore;
private UsersRepository usersRepository; //todo: refactor to injects
private TasksRepository tasksRepository; //todo: refactor to injects
private TeamsRepository teamsRepository;
private Injector injector;
@Override
public void initialize(final Bootstrap<ApplicationConfiguration> bootstrap)
......@@ -59,29 +59,18 @@ public class CTFApplication extends Application<ApplicationConfiguration> {
datastore.ensureIndexes();
}
@Override
public void run(ApplicationConfiguration applicationConfiguration, Environment environment) throws Exception {
initializeMorhpia();
Injector injector = createInjector(applicationConfiguration, datastore);
//REGISTER TEAMS
//todo: clean whole base
TeamsRepository teamsRepository = new TeamsRepository(datastore);
//todo: cleanup
private void initializeTeams(ApplicationConfiguration applicationConfiguration) {
teamsRepository.clean();
applicationConfiguration.getTeams().forEach(team -> {
team.getMembers().stream().forEach(user -> datastore.save(user));
datastore.save(team);
});
}
//GENERATE TASKS
tasksRepository.clean();
private void intializeTasks() {
int numberOfNewTasks = 20;
tasksRepository.clean();
for (int i = 0; i < numberOfNewTasks; i++) {
Task task = new Task(String.valueOf(UUID.randomUUID()), i,
i % 2 == 0 ? TaskType.CRYPTO : TaskType.WEB,
......@@ -89,15 +78,16 @@ public class CTFApplication extends Application<ApplicationConfiguration> {
);
tasksRepository.add(task);
}
}
//GENERATE TASKS END
//REGISTER RESOURCES
private void registerResources(Environment environment) {
environment.jersey().register(injector.getInstance(TeamsResource.class));
environment.jersey().register(injector.getInstance(TasksResource.class));
environment.jersey().register(injector.getInstance(ProxyResource.class));
environment.jersey().register(injector.getInstance(SolutionsResource.class));
}
private void registerAuthFeatures(Environment environment) {
//REGISTER AUTH
environment.jersey().register(new AuthDynamicFeature(
new BasicCredentialAuthFilter.Builder<User>()
......@@ -106,9 +96,19 @@ public class CTFApplication extends Application<ApplicationConfiguration> {
.setRealm("SUPER SECRET STUFF")
.buildAuthFilter()));
environment.jersey().register(RolesAllowedDynamicFeature.class);
//If you want to use @Auth to inject a custom Principal type into your resource
environment.jersey().register(new AuthValueFactoryProvider.Binder<>(User.class));
}
@Override
public void run(ApplicationConfiguration applicationConfiguration, Environment environment) throws Exception {
//todo: refactor
initializeMorhpia();
injector = createInjector(applicationConfiguration, datastore);
initializeTeams(applicationConfiguration);
intializeTasks();
registerResources(environment);
registerAuthFeatures(environment);
}
//todo: move to seperate class
......
package objects;
import com.google.common.collect.ImmutableMap;
import core.TaskType;
import org.bson.Document;
import java.util.Map;
import org.bson.types.ObjectId;
import org.mongodb.morphia.annotations.Entity;
import org.mongodb.morphia.annotations.Id;
/**
* Created by gpietrus on 20.02.2016.
*/
@Entity("tasks")
public class Task {
@Id
private ObjectId id;
private String name;
private int level;
private TaskType type;
private TaskType taskType;
private Flag flag;
public Task(Document document) {
this.name = document.get("name").toString();
this.level = (int) document.get("level");
this.type = TaskType.valueOf(document.get("type").toString());
this.flag = new Flag(document.get("flag").toString());
public Task(String name, int level, TaskType type, Flag flag) {
this.flag = flag;
this.taskType = type;
this.level = level;
this.name = name;
}
public TaskType getType() {
return type;
public Task() {
}
public Task(String name, int level, TaskType type, Flag flag) {
this.name = name;
this.level = level;
this.type = type;
this.flag = flag;
public ObjectId getId() {
return id;
}
public void setType(TaskType type) {
this.type = type;
public void setId(ObjectId id) {
this.id = id;
}
public String getName() {
......@@ -53,6 +52,14 @@ public class Task {
this.level = level;
}
public TaskType getTaskType() {
return taskType;
}
public void setTaskType(TaskType taskType) {
this.taskType = taskType;
}
public Flag getFlag() {
return flag;
}
......@@ -60,14 +67,4 @@ public class Task {
public void setFlag(Flag flag) {
this.flag = flag;
}
//todo: refactor mapping
public Map<String, Object> toMap() {
return ImmutableMap.<String, Object>builder()
.put("name", name)
.put("level", level)
.put("type", type.getType())
.put("flag", flag.getValue())
.build();
}
}
......@@ -47,11 +47,11 @@ public class TasksRepository implements Repository {
}
public void add(Task task) {
// mongoDBConnector.addDocument("tasks", new Document(task.toMap())); //todo
datastore.save(task);
}
public void clean() {
// mongoDBConnector.removeCollection("tasks"); //todo
datastore.getCollection(Task.class).drop();
}
public Map<String, Task> getUserFlagsHashes(String username) {
......
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