Commit 005af2dd authored by Grzegorz Pietrusza's avatar Grzegorz Pietrusza

refacotr tasks.getText

parent cad8e3d0
package com.telephoners.krakyournet.ctf.beans.tasks;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.telephoners.krakyournet.ctf.beans.User;
import com.telephoners.krakyournet.ctf.helpers.PublicProperty;
import com.telephoners.krakyournet.ctf.beans.Flag;
import org.bson.types.ObjectId;
import org.mongodb.morphia.annotations.Entity;
import org.mongodb.morphia.annotations.Id;
import java.io.IOException;
import java.util.List;
@Entity("tasks")
......@@ -32,6 +34,8 @@ public abstract class Task
{
}
public abstract String getTextForUser(User user) throws IOException;
public String getName()
{
return name;
......
......@@ -2,6 +2,7 @@ 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 org.mongodb.morphia.annotations.Entity;
import java.util.List;
......@@ -22,7 +23,7 @@ public class TextTask extends Task
{
}
public String getText()
public String getTextForUser(User user)
{
return text;
}
......
......@@ -2,8 +2,17 @@ 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.StreamUtils;
import org.apache.http.HttpEntity;
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 java.io.IOException;
import java.io.InputStream;
import java.util.List;
@Entity("tasks")
......@@ -22,6 +31,11 @@ public class WebTask extends Task
{
}
public String getTextForUser(User user) throws IOException
{
return StreamUtils.readStream(proxyRequest(getUrl(), user));
}
public String getUrl()
{
return url;
......@@ -31,4 +45,14 @@ public class WebTask extends Task
{
this.url = url;
}
private InputStream proxyRequest(String url, User user) throws IOException
{
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpget = new HttpGet(url);
httpget.setHeader("CTF-User", user.getName());
CloseableHttpResponse execute = httpClient.execute(httpget);
HttpEntity entity = execute.getEntity();
return entity.getContent();
}
}
package com.telephoners.krakyournet.ctf.helpers;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.stream.Collectors;
public class StreamUtils
{
public static String readStream(InputStream input) throws IOException
{
try (BufferedReader buffer = new BufferedReader(new InputStreamReader(input))) {
return buffer.lines().collect(Collectors.joining("\n"));
}
}
}
......@@ -46,36 +46,6 @@ public class TaskResource
if (task == null) {
return Response.status(Response.Status.BAD_REQUEST).build();
}
String taskText = null;
//todo: refactor, so ugly;(
if(task instanceof WebTask) {
taskText = readStream(proxyRequest(((WebTask) task).getUrl(), user));
}
if(task instanceof TextTask) {
taskText = ((TextTask) task).getText();
}
//todo!!!!!
return Response.ok().entity(taskText).build();
}
//todo: remove proxy resource
private InputStream proxyRequest(String url, User user) throws IOException
{
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpget = new HttpGet(url);
httpget.setHeader("CTF-User", user.getName());
CloseableHttpResponse execute = httpClient.execute(httpget);
HttpEntity entity = execute.getEntity();
return entity.getContent();
}
private String readStream(InputStream input) throws IOException
{
try (BufferedReader buffer = new BufferedReader(new InputStreamReader(input))) {
return buffer.lines().collect(Collectors.joining("\n"));
}
return Response.ok().entity(task.getTextForUser(user)).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