Commit 4c82e88b authored by Grzegorz Pietrusza's avatar Grzegorz Pietrusza

refactoring stub

parent ce9a0225
......@@ -2,13 +2,11 @@ package com.telephoners.krakyournet.ctf.beans.tasks;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.telephoners.krakyournet.ctf.beans.Flag;
import com.telephoners.krakyournet.ctf.beans.User;
import com.telephoners.krakyournet.ctf.helpers.PublicProperty;
import org.bson.types.ObjectId;
import org.mongodb.morphia.annotations.Entity;
import org.mongodb.morphia.annotations.Id;
import javax.ws.rs.container.ContainerRequestContext;
import java.io.IOException;
import java.util.List;
......@@ -38,7 +36,7 @@ public abstract class Task
{
}
public abstract TaskResponse getTaskResponse(User user, String path, ContainerRequestContext containerRequestContext) throws IOException;
public abstract TaskResponse getTaskResponse(TaskRequestContext taskRequestContext) throws IOException;
public String getName()
{
......
package com.telephoners.krakyournet.ctf.beans.tasks;
import com.telephoners.krakyournet.ctf.beans.User;
public class TaskRequestContext
{
private String httpMethod; //todo: use class
private User user;
private String path;
private String body;
public TaskRequestContext withHttpMethod(String httpMethod)
{
this.httpMethod = httpMethod;
return this;
}
public TaskRequestContext withUser(User user)
{
this.user = user;
return this;
}
public TaskRequestContext withPath(String path)
{
this.path = path;
return this;
}
public TaskRequestContext withBody(String body) {
this.body = body;
return this;
}
public String getHttpMethod()
{
return httpMethod;
}
public User getUser()
{
return user;
}
public String getPath()
{
return path;
}
public String getBody()
{
return body;
}
}
package com.telephoners.krakyournet.ctf.beans.tasks;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.google.common.base.Joiner;
import com.telephoners.krakyournet.ctf.beans.Flag;
import com.telephoners.krakyournet.ctf.beans.User;
import com.telephoners.krakyournet.ctf.helpers.StreamUtils;
import org.apache.http.Header;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.mongodb.morphia.annotations.Entity;
import javax.ws.rs.container.ContainerRequestContext;
import java.io.IOException;
import java.util.List;
......@@ -34,15 +31,27 @@ public class WebTask extends Task
}
@Override
public TaskResponse getTaskResponse(TaskRequestContext taskRequestContext) throws IOException
{
String url = getUrl() + taskRequestContext.getPath();
//todo: header in
CloseableHttpResponse response = proxyRequest(url, taskRequestContext.getUser(), null);
String text = StreamUtils.readStream(response.getEntity().getContent());
//todo: header out
TaskResponse taskResponse = new TaskResponse(text, null);//todo: build with
return taskResponse;
}
/*
public TaskResponse getTaskResponse(User user, String path, ContainerRequestContext context) throws IOException
{
String url = getUrl() + path;
String kynHeaderValue = context.getHeaderString(KYN_HEADER_NAME);
CloseableHttpResponse response = proxyRequest(url, user, kynHeaderValue);
String text = StreamUtils.readStream(response.getEntity().getContent());
Header kynHeader = response.getFirstHeader(KYN_HEADER_NAME);
return new TaskResponse(text, kynHeader != null ? kynHeader.getValue() : null); //todo: needs refactorig
}
*/
public String getUrl()
{
......
......@@ -2,7 +2,9 @@ package com.telephoners.krakyournet.ctf.resources;
import com.telephoners.krakyournet.ctf.beans.User;
import com.telephoners.krakyournet.ctf.beans.tasks.Task;
import com.telephoners.krakyournet.ctf.beans.tasks.TaskRequestContext;
import com.telephoners.krakyournet.ctf.beans.tasks.TaskResponse;
import com.telephoners.krakyournet.ctf.helpers.StreamUtils;
import com.telephoners.krakyournet.ctf.repositories.TasksRepository;
import io.dropwizard.auth.Auth;
import org.glassfish.jersey.server.ContainerRequest;
......@@ -10,6 +12,7 @@ import org.glassfish.jersey.server.ContainerRequest;
import javax.inject.Inject;
import javax.inject.Singleton;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.container.ContainerRequestContext;
......@@ -30,29 +33,72 @@ public class TaskResource
this.tasksRepository = tasksRepository;
}
//todo: name
private TaskResponse getTaskResponse(int taskLevel, TaskRequestContext taskRequestContext) throws IOException
{
Task task = tasksRepository.getByLevel(taskLevel);
return task.getTaskResponse(taskRequestContext);
}
/*
@Path("{task_level}/{path: .*}")
@GET
public Response getTask(@Auth User user,
public Response getTaskGet(@Auth User user,
final @PathParam("task_level") int taskLevel,
final @PathParam("path") String path,
@Context ContainerRequestContext containerRequestContext) throws IOException
{
Task task = tasksRepository.getByLevel(taskLevel);
//todo: refactor, path not necessary in textTasks
ContainerRequest context = (ContainerRequest) containerRequestContext;
String query = context.getRequestUri().getQuery();
String fullPath = path;
if(query != null) { //todo: refactor
fullPath = fullPath + "?" + query;
}
TaskResponse taskResponse = task.getTaskResponse(user, fullPath, containerRequestContext);
Response.ResponseBuilder responseBuilder = Response.ok();
responseBuilder.entity(taskResponse.getText());
String kynHeaderValue = taskResponse.getKynHeader();
if(kynHeaderValue != null) {
if (kynHeaderValue != null) {
responseBuilder.header(KYN_HEADER_NAME, kynHeaderValue);
}
return responseBuilder.build();
}
*/
@Path("{task_level}/{path: .*}")
@GET
public Response getTaskGet(@Auth User user,
final @PathParam("task_level") int taskLevel,
final @PathParam("path") String path,
@Context ContainerRequestContext containerRequestContext) throws IOException
{
String fullPath = path;
String query = ((ContainerRequest)containerRequestContext).getRequestUri().getQuery();
if(query != null) {
fullPath += query;
}
TaskRequestContext taskRequestContext = new TaskRequestContext()
.withHttpMethod("POST")
.withUser(user)
.withPath(fullPath);
TaskResponse taskResponse = getTaskResponse(taskLevel, taskRequestContext);
//todo: headers
return Response.ok().entity(taskResponse.getText()).build();
}
@Path("{task_level}/{path: .*}")
@POST
public Response getTaskPost(@Auth User user,
final @PathParam("task_level") int taskLevel,
final @PathParam("path") String path,
@Context ContainerRequestContext containerRequestContext) throws IOException
{
String fullPath = path;
String query = ((ContainerRequest)containerRequestContext).getRequestUri().getQuery();
if(query != null) {
fullPath += query;
}
String body = StreamUtils.readStream(containerRequestContext.getEntityStream());
//todo: TaskContextFrom
TaskRequestContext taskRequestContext = new TaskRequestContext()
.withHttpMethod("POST")
.withUser(user)
.withPath(fullPath)
.withBody(body);
TaskResponse taskResponse = getTaskResponse(taskLevel, taskRequestContext);
//todo: hedaers
return Response.ok().entity(taskResponse.getText()).build();
}
}
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