Small refactoring.
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Manuel Friedli 2020-10-11 02:25:56 +02:00
parent 916b163ea8
commit 11580f2927
3 changed files with 19 additions and 13 deletions

View file

@ -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" />

View file

@ -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>

View file

@ -7,12 +7,7 @@ import ch.fritteli.labyrinth.generator.renderer.text.TextRenderer;
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.HashMap; import io.vavr.collection.*;
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;
@ -82,7 +77,7 @@ public class LabyrinthServer {
public void start() { public void start() {
Runtime.getRuntime().addShutdownHook(new Thread(this::stop, "listener-stopper")); Runtime.getRuntime().addShutdownHook(new Thread(this::stop, "listener-stopper"));
this.httpServer.start(); this.httpServer.start();
log.info("Listening on {}", this.httpServer.getAddress()); log.info("Listening on http://{}:{}", this.httpServer.getAddress().getHostString(), this.httpServer.getAddress().getPort());
} }
public void stop() { public void stop() {
@ -101,8 +96,8 @@ public class LabyrinthServer {
return; return;
} }
final Map<RequestParameter, String> requestParams = this.parseQueryString(exchange.getRequestURI().getQuery()); final Map<RequestParameter, String> requestParams = this.parseQueryString(exchange.getRequestURI().getQuery());
final int width = this.getOrDefault(requestParams.get(RequestParameter.WIDTH), Integer::valueOf, 1); final int width = this.getOrDefault(requestParams.get(RequestParameter.WIDTH), Integer::valueOf, 5);
final int height = this.getOrDefault(requestParams.get(RequestParameter.HEIGHT), Integer::valueOf, 1); final int height = this.getOrDefault(requestParams.get(RequestParameter.HEIGHT), Integer::valueOf, 5);
final Option<Long> idOption = requestParams.get(RequestParameter.ID).toTry().map(Long::valueOf).toOption(); final Option<Long> idOption = requestParams.get(RequestParameter.ID).toTry().map(Long::valueOf).toOption();
final Option<OutputType> outputOption = requestParams.get(RequestParameter.OUTPUT).flatMap(OutputType::ofString); final Option<OutputType> outputOption = requestParams.get(RequestParameter.OUTPUT).flatMap(OutputType::ofString);
final Headers responseHeaders = exchange.getResponseHeaders(); final Headers responseHeaders = exchange.getResponseHeaders();
@ -115,10 +110,21 @@ public class LabyrinthServer {
exchange.close(); exchange.close();
return; return;
} }
final Labyrinth labyrinth = new Labyrinth(width, height, id);
final byte[] render;
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();
exchange.close();
return;
}
responseHeaders.add("Content-type", output.getContentType()); responseHeaders.add("Content-type", output.getContentType());
exchange.sendResponseHeaders(200, 0); exchange.sendResponseHeaders(200, 0);
final Labyrinth labyrinth = new Labyrinth(width, height, id);
final byte[] render = output.render(labyrinth);
final OutputStream responseBody = exchange.getResponseBody(); final OutputStream responseBody = exchange.getResponseBody();
responseBody.write(render); responseBody.write(render);
responseBody.flush(); responseBody.flush();