feature/undertow #4
1 changed files with 54 additions and 62 deletions
|
@ -6,19 +6,11 @@ import io.undertow.Undertow;
|
|||
import io.undertow.server.RoutingHandler;
|
||||
import io.undertow.server.handlers.RedirectHandler;
|
||||
import io.undertow.util.StatusCodes;
|
||||
import io.vavr.collection.HashMap;
|
||||
import io.vavr.collection.HashSet;
|
||||
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.NonNull;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.function.Function;
|
||||
|
||||
@Slf4j
|
||||
public class LabyrinthServer {
|
||||
|
@ -31,7 +23,7 @@ public class LabyrinthServer {
|
|||
return serverOption;
|
||||
}
|
||||
|
||||
public LabyrinthServer(@NonNull final ServerConfig config) {
|
||||
private LabyrinthServer(@NonNull final ServerConfig config) {
|
||||
log.info("Starting Server at http://{}:{}/", config.getAddress().getHostAddress(), config.getPort());
|
||||
final RoutingHandler routingHandler = new RoutingHandler().get("/", new RedirectHandler("/create/text"))
|
||||
.get("/create", new RedirectHandler("/create/text"))
|
||||
|
@ -49,7 +41,7 @@ public class LabyrinthServer {
|
|||
.build();
|
||||
}
|
||||
|
||||
public void start() {
|
||||
private void start() {
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(this::stop, "listener-stopper"));
|
||||
this.undertow.start();
|
||||
Try.of(() -> (InetSocketAddress) this.undertow.getListenerInfo().get(0).getAddress())
|
||||
|
@ -57,7 +49,7 @@ public class LabyrinthServer {
|
|||
.forEach(address -> log.info("Listening on http://{}:{}", address.getHostString(), address.getPort()));
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
private void stop() {
|
||||
log.info("Stopping server ...");
|
||||
this.undertow.stop();
|
||||
log.info("Server stopped.");
|
||||
|
@ -131,41 +123,41 @@ public class LabyrinthServer {
|
|||
// });
|
||||
// }
|
||||
|
||||
@NonNull
|
||||
private Map<RequestParameter, String> parseQueryString(@Nullable final String query) {
|
||||
if (query == null) {
|
||||
return HashMap.empty();
|
||||
}
|
||||
HashMap<RequestParameter, String> result = HashMap.empty();
|
||||
final String[] parts = query.split("&");
|
||||
for (final String part : parts) {
|
||||
final int split = part.indexOf('=');
|
||||
if (split == -1) {
|
||||
final Try<RequestParameter> tryKey = Try.of(() -> this.normalizeParameterName(part));
|
||||
if (tryKey.isSuccess()) {
|
||||
result = result.put(tryKey.get(), null);
|
||||
}
|
||||
} else {
|
||||
final String key = part.substring(0, split);
|
||||
final String value = part.substring(split + 1);
|
||||
final Try<RequestParameter> tryKey = Try.of(() -> this.normalizeParameterName(key));
|
||||
if (tryKey.isSuccess()) {
|
||||
result = result.put(tryKey.get(), value);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
// @NonNull
|
||||
// private Map<RequestParameter, String> parseQueryString(@Nullable final String query) {
|
||||
// if (query == null) {
|
||||
// return HashMap.empty();
|
||||
// }
|
||||
// HashMap<RequestParameter, String> result = HashMap.empty();
|
||||
// final String[] parts = query.split("&");
|
||||
// for (final String part : parts) {
|
||||
// final int split = part.indexOf('=');
|
||||
// if (split == -1) {
|
||||
// final Try<RequestParameter> tryKey = Try.of(() -> this.normalizeParameterName(part));
|
||||
// if (tryKey.isSuccess()) {
|
||||
// result = result.put(tryKey.get(), null);
|
||||
// }
|
||||
// } else {
|
||||
// final String key = part.substring(0, split);
|
||||
// final String value = part.substring(split + 1);
|
||||
// final Try<RequestParameter> tryKey = Try.of(() -> this.normalizeParameterName(key));
|
||||
// if (tryKey.isSuccess()) {
|
||||
// result = result.put(tryKey.get(), value);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// return result;
|
||||
// }
|
||||
|
||||
private RequestParameter normalizeParameterName(final String paramName) {
|
||||
return RequestParameter.parseName(paramName).get();
|
||||
}
|
||||
// private RequestParameter normalizeParameterName(final String paramName) {
|
||||
// return RequestParameter.parseName(paramName).get();
|
||||
// }
|
||||
|
||||
private <T> T getOrDefault(@NonNull final Option<String> input,
|
||||
@NonNull final Function<String, T> mapper,
|
||||
@Nullable final T defaultValue) {
|
||||
return input.toTry().map(mapper).getOrElse(defaultValue);
|
||||
}
|
||||
// private <T> T getOrDefault(@NonNull final Option<String> input,
|
||||
// @NonNull final Function<String, T> mapper,
|
||||
// @Nullable final T defaultValue) {
|
||||
// return input.toTry().map(mapper).getOrElse(defaultValue);
|
||||
// }
|
||||
|
||||
// private void handleRender(final HttpExchange exchange) {
|
||||
// this.executorService.submit(() -> {
|
||||
|
@ -211,22 +203,22 @@ public class LabyrinthServer {
|
|||
// });
|
||||
// }
|
||||
|
||||
private enum RequestParameter {
|
||||
WIDTH("w", "width"),
|
||||
HEIGHT("h", "height"),
|
||||
ID("i", "id"),
|
||||
OUTPUT("o", "output");
|
||||
private final Set<String> names;
|
||||
|
||||
RequestParameter(@NonNull final String... names) {
|
||||
this.names = HashSet.of(names);
|
||||
}
|
||||
|
||||
static Option<RequestParameter> parseName(@Nullable final String name) {
|
||||
if (name == null) {
|
||||
return Option.none();
|
||||
}
|
||||
return Stream.of(values()).find(param -> param.names.exists(name::equalsIgnoreCase));
|
||||
}
|
||||
}
|
||||
// private enum RequestParameter {
|
||||
// WIDTH("w", "width"),
|
||||
// HEIGHT("h", "height"),
|
||||
// ID("i", "id"),
|
||||
// OUTPUT("o", "output");
|
||||
// private final Set<String> names;
|
||||
//
|
||||
// RequestParameter(@NonNull final String... names) {
|
||||
// this.names = HashSet.of(names);
|
||||
// }
|
||||
//
|
||||
// static Option<RequestParameter> parseName(@Nullable final String name) {
|
||||
// if (name == null) {
|
||||
// return Option.none();
|
||||
// }
|
||||
// return Stream.of(values()).find(param -> param.names.exists(name::equalsIgnoreCase));
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue