feature/render-binary #1
3 changed files with 61 additions and 4 deletions
|
@ -12,7 +12,7 @@
|
||||||
</content>
|
</content>
|
||||||
<orderEntry type="inheritedJdk" />
|
<orderEntry type="inheritedJdk" />
|
||||||
<orderEntry type="sourceFolder" forTests="false" />
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
<orderEntry type="library" name="Maven: ch.fritteli.labyrinth:labyrinth-generator:0.0.1" level="project" />
|
<orderEntry type="module" module-name="labyrinth-generator" />
|
||||||
<orderEntry type="library" name="Maven: org.apache.pdfbox:pdfbox:2.0.20" level="project" />
|
<orderEntry type="library" name="Maven: org.apache.pdfbox:pdfbox:2.0.20" level="project" />
|
||||||
<orderEntry type="library" name="Maven: org.apache.pdfbox:fontbox:2.0.20" level="project" />
|
<orderEntry type="library" name="Maven: org.apache.pdfbox:fontbox:2.0.20" level="project" />
|
||||||
<orderEntry type="library" name="Maven: commons-logging:commons-logging:1.2" level="project" />
|
<orderEntry type="library" name="Maven: commons-logging:commons-logging:1.2" level="project" />
|
||||||
|
|
2
pom.xml
2
pom.xml
|
@ -18,7 +18,7 @@
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>ch.fritteli.labyrinth</groupId>
|
<groupId>ch.fritteli.labyrinth</groupId>
|
||||||
<artifactId>labyrinth-generator</artifactId>
|
<artifactId>labyrinth-generator</artifactId>
|
||||||
<version>0.0.1</version>
|
<version>0.0.2-SNAPSHOT</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.vavr</groupId>
|
<groupId>io.vavr</groupId>
|
||||||
|
|
|
@ -4,10 +4,16 @@ import ch.fritteli.labyrinth.generator.model.Labyrinth;
|
||||||
import ch.fritteli.labyrinth.generator.renderer.html.HTMLRenderer;
|
import ch.fritteli.labyrinth.generator.renderer.html.HTMLRenderer;
|
||||||
import ch.fritteli.labyrinth.generator.renderer.pdf.PDFRenderer;
|
import ch.fritteli.labyrinth.generator.renderer.pdf.PDFRenderer;
|
||||||
import ch.fritteli.labyrinth.generator.renderer.text.TextRenderer;
|
import ch.fritteli.labyrinth.generator.renderer.text.TextRenderer;
|
||||||
|
import ch.fritteli.labyrinth.generator.serialization.SerializerDeserializer;
|
||||||
import com.sun.net.httpserver.Headers;
|
import com.sun.net.httpserver.Headers;
|
||||||
import com.sun.net.httpserver.HttpExchange;
|
import com.sun.net.httpserver.HttpExchange;
|
||||||
import com.sun.net.httpserver.HttpServer;
|
import com.sun.net.httpserver.HttpServer;
|
||||||
import io.vavr.collection.*;
|
import io.vavr.collection.HashMap;
|
||||||
|
import io.vavr.collection.HashSet;
|
||||||
|
import io.vavr.collection.List;
|
||||||
|
import io.vavr.collection.Map;
|
||||||
|
import io.vavr.collection.Set;
|
||||||
|
import io.vavr.collection.Stream;
|
||||||
import io.vavr.control.Option;
|
import io.vavr.control.Option;
|
||||||
import io.vavr.control.Try;
|
import io.vavr.control.Try;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
@ -28,7 +34,12 @@ public class LabyrinthServer {
|
||||||
|
|
||||||
public LabyrinthServer(@NonNull final ServerConfig config) throws IOException {
|
public LabyrinthServer(@NonNull final ServerConfig config) throws IOException {
|
||||||
this.httpServer = HttpServer.create(new InetSocketAddress(config.getAddress(), config.getPort()), 5);
|
this.httpServer = HttpServer.create(new InetSocketAddress(config.getAddress(), config.getPort()), 5);
|
||||||
|
this.httpServer.createContext("/", exchange -> {
|
||||||
|
exchange.getResponseHeaders().add("Location", "/create");
|
||||||
|
exchange.sendResponseHeaders(302, -1);
|
||||||
|
});
|
||||||
this.httpServer.createContext("/create", this::handleCreate);
|
this.httpServer.createContext("/create", this::handleCreate);
|
||||||
|
this.httpServer.createContext("/render", this::handleRender);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Option<LabyrinthServer> createAndStartServer() {
|
public static Option<LabyrinthServer> createAndStartServer() {
|
||||||
|
@ -131,6 +142,51 @@ public class LabyrinthServer {
|
||||||
exchange.close();
|
exchange.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void handleRender(final HttpExchange exchange) throws IOException {
|
||||||
|
try {
|
||||||
|
log.debug("Handling request to {}", exchange.getRequestURI());
|
||||||
|
final String requestMethod = exchange.getRequestMethod();
|
||||||
|
if (!requestMethod.equals("POST")) {
|
||||||
|
exchange.getResponseBody().close();
|
||||||
|
exchange.sendResponseHeaders(405, -1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
final byte[] bytes = exchange.getRequestBody().readAllBytes();
|
||||||
|
@NonNull final Labyrinth labyrinth;
|
||||||
|
try {
|
||||||
|
labyrinth = SerializerDeserializer.deserialize(bytes);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
|
||||||
|
OutputType output = exchange.getRequestHeaders()
|
||||||
|
.get("Accept")
|
||||||
|
.contains("text/html") ?
|
||||||
|
OutputType.HTML :
|
||||||
|
OutputType.TEXT_PLAIN;
|
||||||
|
final byte[] render;
|
||||||
|
final Headers responseHeaders = exchange.getResponseHeaders();
|
||||||
|
try {
|
||||||
|
render = output.render(labyrinth);
|
||||||
|
} catch (Exception e) {
|
||||||
|
responseHeaders.add("Content-type", "text/plain; charset=UTF-8");
|
||||||
|
exchange.sendResponseHeaders(500, 0);
|
||||||
|
final OutputStream responseBody = exchange.getResponseBody();
|
||||||
|
responseBody.write(("Error: " + e).getBytes(StandardCharsets.UTF_8));
|
||||||
|
responseBody.flush();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
responseHeaders.add("Content-type", output.getContentType());
|
||||||
|
exchange.sendResponseHeaders(200, 0);
|
||||||
|
final OutputStream responseBody = exchange.getResponseBody();
|
||||||
|
responseBody.write(render);
|
||||||
|
responseBody.flush();
|
||||||
|
} finally {
|
||||||
|
exchange.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private enum RequestParameter {
|
private enum RequestParameter {
|
||||||
WIDTH("w", "width"),
|
WIDTH("w", "width"),
|
||||||
HEIGHT("h", "height"),
|
HEIGHT("h", "height"),
|
||||||
|
@ -154,7 +210,8 @@ public class LabyrinthServer {
|
||||||
private enum OutputType {
|
private enum OutputType {
|
||||||
TEXT_PLAIN("text/plain; charset=UTF-8", labyrinth -> TextRenderer.newInstance().render(labyrinth).getBytes(StandardCharsets.UTF_8), "t", "text"),
|
TEXT_PLAIN("text/plain; charset=UTF-8", labyrinth -> TextRenderer.newInstance().render(labyrinth).getBytes(StandardCharsets.UTF_8), "t", "text"),
|
||||||
HTML("text/html", labyrinth -> HTMLRenderer.newInstance().render(labyrinth).getBytes(StandardCharsets.UTF_8), "h", "html"),
|
HTML("text/html", labyrinth -> HTMLRenderer.newInstance().render(labyrinth).getBytes(StandardCharsets.UTF_8), "h", "html"),
|
||||||
PDF("application/pdf", labyrinth -> PDFRenderer.newInstance().render(labyrinth), "p", "pdf");
|
PDF("application/pdf", labyrinth -> PDFRenderer.newInstance().render(labyrinth), "p", "pdf"),
|
||||||
|
BINARY("application/octet-stream", SerializerDeserializer::serialize, "b", "binary");
|
||||||
@Getter
|
@Getter
|
||||||
@NonNull
|
@NonNull
|
||||||
private final String contentType;
|
private final String contentType;
|
||||||
|
|
Loading…
Reference in a new issue