Render binary data uploaded to /render via POST.
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
parent
a63d440de5
commit
cd29c6fe75
3 changed files with 61 additions and 4 deletions
|
@ -12,7 +12,7 @@
|
|||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<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:fontbox:2.0.20" 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>
|
||||
<groupId>ch.fritteli.labyrinth</groupId>
|
||||
<artifactId>labyrinth-generator</artifactId>
|
||||
<version>0.0.1</version>
|
||||
<version>0.0.2-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<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.pdf.PDFRenderer;
|
||||
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.HttpExchange;
|
||||
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.Try;
|
||||
import lombok.Getter;
|
||||
|
@ -28,7 +34,12 @@ public class LabyrinthServer {
|
|||
|
||||
public LabyrinthServer(@NonNull final ServerConfig config) throws IOException {
|
||||
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("/render", this::handleRender);
|
||||
}
|
||||
|
||||
public static Option<LabyrinthServer> createAndStartServer() {
|
||||
|
@ -131,6 +142,51 @@ public class LabyrinthServer {
|
|||
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 {
|
||||
WIDTH("w", "width"),
|
||||
HEIGHT("h", "height"),
|
||||
|
@ -154,7 +210,8 @@ public class LabyrinthServer {
|
|||
private enum OutputType {
|
||||
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"),
|
||||
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
|
||||
@NonNull
|
||||
private final String contentType;
|
||||
|
|
Loading…
Reference in a new issue