From d69097b03a703cf150af6a1c139d9eafaa07782a Mon Sep 17 00:00:00 2001 From: joshua bauer Date: Mon, 10 Apr 2017 12:17:36 -0700 Subject: [PATCH] Added more dependencies. Renamed endpointInfo. Added bytes benchmark. --- pom.xml | 24 ++- src/main/java/com/wurrly/Application.java | 4 +- .../com/wurrly/modules/RoutingModule.java | 10 +- .../com/wurrly/server/ServerResponse.java | 180 ++++++++++++++++-- .../EndpointInfo.java} | 18 +- .../server/handlers/HandlerGenerator.java | 6 +- .../handlers/benchmark/BenchmarkHandlers.java | 31 ++- .../com/wurrly/services/SwaggerService.java | 10 +- 8 files changed, 239 insertions(+), 44 deletions(-) rename src/main/java/com/wurrly/server/{route/RouteInfo.java => endpoints/EndpointInfo.java} (91%) diff --git a/pom.xml b/pom.xml index 5d68929..7688b90 100644 --- a/pom.xml +++ b/pom.xml @@ -30,19 +30,19 @@ com.fasterxml.jackson.core jackson-annotations - 2.8.7 + [2.8.7,) com.fasterxml.jackson.core jackson-core - 2.8.7 + [2.8.7,) com.fasterxml.jackson.core jackson-databind - 2.8.7 + [2.8.7,) io.swagger @@ -80,7 +80,7 @@ com.squareup.okhttp3 okhttp - 3.6.0 + [3.6.0,) com.squareup.okhttp3 @@ -90,7 +90,7 @@ com.squareup.okhttp3 okcurl - 3.6.0 + [3.6.0,) com.squareup.retrofit2 @@ -135,12 +135,12 @@ com.fasterxml.jackson.module jackson-module-afterburner - 2.8.7 + [2.8.7,) com.fasterxml.jackson.datatype jackson-datatype-jdk8 - 2.8.7 + [2.8.7,) org.glassfish.jersey @@ -226,5 +226,15 @@ jansi 1.15 + + org.hibernate + hibernate-validator + 5.4.1.Final + + + com.graphql-java + graphql-java + 2.3.0 + \ No newline at end of file diff --git a/src/main/java/com/wurrly/Application.java b/src/main/java/com/wurrly/Application.java index ad7ea9c..23ea46e 100644 --- a/src/main/java/com/wurrly/Application.java +++ b/src/main/java/com/wurrly/Application.java @@ -26,9 +26,9 @@ import com.wurrly.controllers.Users; import com.wurrly.modules.ConfigModule; import com.wurrly.modules.RoutingModule; +import com.wurrly.server.endpoints.EndpointInfo; import com.wurrly.server.handlers.HandlerGenerator; import com.wurrly.server.handlers.benchmark.BenchmarkHandlers; -import com.wurrly.server.route.RouteInfo; import com.wurrly.services.SwaggerService; import io.undertow.Undertow; @@ -133,7 +133,7 @@ public Undertow buildServer() StringBuilder sb = new StringBuilder(); - Set routingInfo = routingModule.getRegisteredRoutes(); //injector.getInstance(Key.get(new TypeLiteral>() {},Names.named("routeInfo"))); + Set routingInfo = routingModule.getRegisteredEndpoints(); //injector.getInstance(Key.get(new TypeLiteral>() {},Names.named("routeInfo"))); routingInfo.stream().forEachOrdered( r -> sb.append(r.toString()).append("\n")); diff --git a/src/main/java/com/wurrly/modules/RoutingModule.java b/src/main/java/com/wurrly/modules/RoutingModule.java index 109b312..f807885 100644 --- a/src/main/java/com/wurrly/modules/RoutingModule.java +++ b/src/main/java/com/wurrly/modules/RoutingModule.java @@ -17,7 +17,7 @@ import com.google.inject.TypeLiteral; import com.google.inject.name.Names; import com.wurrly.Application.BaseHandlers; -import com.wurrly.server.route.RouteInfo; +import com.wurrly.server.endpoints.EndpointInfo; import io.undertow.server.RoutingHandler; @@ -30,7 +30,7 @@ public class RoutingModule extends AbstractModule { private static Logger log = LoggerFactory.getLogger(RoutingModule.class.getCanonicalName()); - protected Set registeredRoutes = new TreeSet<>(); + protected Set registeredEndpoints = new TreeSet<>(); protected Set> registeredControllers = new HashSet<>(); @Override @@ -47,11 +47,11 @@ protected void configure() } /** - * @return the registeredRoutes + * @return the registeredEndpoints */ - public Set getRegisteredRoutes() + public Set getRegisteredEndpoints() { - return registeredRoutes; + return registeredEndpoints; } /** diff --git a/src/main/java/com/wurrly/server/ServerResponse.java b/src/main/java/com/wurrly/server/ServerResponse.java index 205e079..f13d066 100644 --- a/src/main/java/com/wurrly/server/ServerResponse.java +++ b/src/main/java/com/wurrly/server/ServerResponse.java @@ -7,9 +7,14 @@ import java.util.HashMap; import java.util.Map; +import io.undertow.io.IoCallback; +import io.undertow.server.HttpHandler; import io.undertow.server.HttpServerExchange; import io.undertow.server.handlers.Cookie; import io.undertow.util.HeaderMap; +import io.undertow.util.HeaderValues; +import io.undertow.util.Headers; +import io.undertow.util.HttpString; import io.undertow.util.StatusCodes; /** @@ -18,36 +23,187 @@ */ public class ServerResponse { - private ByteBuffer buffer; - private int status = StatusCodes.OK; - private HeaderMap headers = new HeaderMap(); - private Map cookies = new HashMap<>(); + private ByteBuffer body; + + private int status = -1; + private final HeaderMap headers = new HeaderMap(); + private final Map cookies = new HashMap<>(); + private String contentType; + private IoCallback callback; public ServerResponse() { } - - public ByteBuffer buffer() - { - return this.buffer; + public ByteBuffer getBody() + { + return body; } - - public int status() + + public int getStatus() { return this.status; } - public Map cookies() + public Map getCookies() { return this.cookies; } - public HeaderMap headers() + public HeaderMap getHeaders() { return this.headers; } + /** + * @return the contentType + */ + public String getContentType() + { + return contentType; + } + + /** + * @return the callback + */ + public IoCallback getCallback() + { + return callback; + } + + /** + * @param callback the callback to set + */ + public void setCallback(IoCallback callback) + { + this.callback = callback; + } + + /** + * @param body the body to set + */ + public void setBody(ByteBuffer body) + { + this.body = body; + } + + /** + * @param status the status to set + */ + public void setStatus(int status) + { + this.status = status; + } + + /** + * @param contentType the contentType to set + */ + public void setContentType(String contentType) + { + this.contentType = contentType; + } + + public void send( final HttpHandler currentHandler, final HttpServerExchange exchange ) + { + long itr = this.headers.fastIterateNonEmpty(); + + while( itr != -1L ) + { + final HeaderValues values = this.headers.fiCurrent(itr); + + exchange.getResponseHeaders().putAll(values.getHeaderName(), values); + + this.headers.fiNextNonEmpty(itr); + } + + exchange.getResponseCookies().putAll(this.cookies); + + exchange.setStatusCode( this.status != -1 ? this.status : StatusCodes.ACCEPTED); + + exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, this.contentType); + + if( this.callback != null ) + { + exchange.getResponseSender().send(this.body,this.callback); + } + else + { + exchange.getResponseSender().send(this.body); + } + + } + + /** + * Creates builder to build {@link ServerResponse}. + * @return created builder + */ + public static Builder builder() + { + return new Builder(); + } + + /** + * Builder to build {@link ServerResponse}. + */ + public static final class Builder + { + private ServerResponse response = new ServerResponse(); + + private Builder() + { + } + + public Builder withBody(ByteBuffer body) + { + this.response.body = body; + return this; + } + + public Builder withBody(String body) + { + this.response.body = ByteBuffer.wrap(body.getBytes()); + return this; + } + + + public Builder withStatus(int status) + { + this.response.status = status; + return this; + } + + public Builder withHeader(HttpString headerName, String value) + { + this.response.headers.put(headerName, value); + return this; + } + + public Builder withCookie(String cookieName, Cookie cookie) + { + this.response.getCookies().put(cookieName, cookie); + return this; + } + + public Builder withContentType(String contentType) + { + this.response.contentType = contentType; + return this; + } + + public Builder withCallback(IoCallback callback) + { + this.response.callback = callback; + return this; + } + + public ServerResponse build() + { + return this.response; + } + } + + + } diff --git a/src/main/java/com/wurrly/server/route/RouteInfo.java b/src/main/java/com/wurrly/server/endpoints/EndpointInfo.java similarity index 91% rename from src/main/java/com/wurrly/server/route/RouteInfo.java rename to src/main/java/com/wurrly/server/endpoints/EndpointInfo.java index c2eb815..26da81e 100644 --- a/src/main/java/com/wurrly/server/route/RouteInfo.java +++ b/src/main/java/com/wurrly/server/endpoints/EndpointInfo.java @@ -1,7 +1,7 @@ /** * */ -package com.wurrly.server.route; +package com.wurrly.server.endpoints; import org.apache.commons.lang3.builder.CompareToBuilder; import org.apache.commons.lang3.builder.HashCodeBuilder; @@ -12,7 +12,7 @@ * @author jbauer * */ -public class RouteInfo implements Comparable +public class EndpointInfo implements Comparable { private HttpString method; private String pathTemplate; @@ -22,7 +22,7 @@ public class RouteInfo implements Comparable private String controllerName = "anonymous"; - private RouteInfo(Builder builder) + private EndpointInfo(Builder builder) { this.method = builder.method; this.pathTemplate = builder.pathTemplate; @@ -32,7 +32,7 @@ private RouteInfo(Builder builder) this.controllerName = builder.controllerName; } - public RouteInfo() + public EndpointInfo() { } @@ -146,7 +146,7 @@ public int hashCode() toHashCode(); } - public int compareTo(RouteInfo other) { + public int compareTo(EndpointInfo other) { return new CompareToBuilder() .append(this.controllerName, other.controllerName) @@ -166,7 +166,7 @@ public String toString() } /** - * Creates builder to build {@link RouteInfo}. + * Creates builder to build {@link EndpointInfo}. * @return created builder */ @@ -177,7 +177,7 @@ public static Builder builder() /** - * Builder to build {@link RouteInfo}. + * Builder to build {@link EndpointInfo}. */ public static final class Builder @@ -229,9 +229,9 @@ public Builder withControllerName(String controllerName) return this; } - public RouteInfo build() + public EndpointInfo build() { - return new RouteInfo(this); + return new EndpointInfo(this); } } diff --git a/src/main/java/com/wurrly/server/handlers/HandlerGenerator.java b/src/main/java/com/wurrly/server/handlers/HandlerGenerator.java index 371ff5e..76ef45e 100644 --- a/src/main/java/com/wurrly/server/handlers/HandlerGenerator.java +++ b/src/main/java/com/wurrly/server/handlers/HandlerGenerator.java @@ -48,7 +48,7 @@ import com.wurrly.modules.RoutingModule; import com.wurrly.server.Extractors; import com.wurrly.server.ServerRequest; -import com.wurrly.server.route.RouteInfo; +import com.wurrly.server.endpoints.EndpointInfo; import io.swagger.annotations.Api; import io.undertow.server.HttpServerExchange; @@ -515,7 +515,7 @@ public void addClassMethodHandlers(TypeSpec.Builder typeBuilder, Class clazz) for (Method m : clazz.getDeclaredMethods()) { - RouteInfo route = new RouteInfo(); + EndpointInfo route = new EndpointInfo(); route.setControllerName(clazz.getSimpleName()); @@ -770,7 +770,7 @@ else if (t.equals(TypeHandler.OptionalFromStringType) || t.equals(TypeHandler.Op initBuilder.addCode("$L", "\n"); - this.routingModule.getRegisteredRoutes().add(route); + this.routingModule.getRegisteredEndpoints().add(route); } diff --git a/src/main/java/com/wurrly/server/handlers/benchmark/BenchmarkHandlers.java b/src/main/java/com/wurrly/server/handlers/benchmark/BenchmarkHandlers.java index 94415f2..7f4d359 100644 --- a/src/main/java/com/wurrly/server/handlers/benchmark/BenchmarkHandlers.java +++ b/src/main/java/com/wurrly/server/handlers/benchmark/BenchmarkHandlers.java @@ -4,13 +4,16 @@ package com.wurrly.server.handlers.benchmark; import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; import java.nio.file.Path; import java.nio.file.Paths; +import java.nio.file.StandardOpenOption; import java.util.function.Supplier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.common.io.Files; import com.j256.simplemagic.ContentType; import com.jsoniter.output.JsonStream; @@ -66,7 +69,6 @@ public void handleRequest(HttpServerExchange exchange) throws Exception } } ); - handler.add(Methods.GET, "/video.mp4", new HttpHandler(){ @Override @@ -90,6 +92,33 @@ public void handleRequest(HttpServerExchange exchange) throws Exception hdlr.handleRequest(exchange); + + } + } ); + + handler.add(Methods.GET, "/bytes.mp4", new HttpHandler(){ + + @Override + public void handleRequest(HttpServerExchange exchange) throws Exception + { + // TODO Auto-generated method stub + +// if (exchange.isInIoThread()) { +// exchange.dispatch(this); +// return; +// } +// + Path filePath = Paths.get("./assets/video.mp4"); + + + byte[] bytes = Files.toByteArray(filePath.toFile()); + + exchange.getResponseHeaders().put(Headers.CONTENT_LENGTH, bytes.length); + exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, ContentType.MP4A.getMimeType()); + + exchange.getResponseSender().send(ByteBuffer.wrap(bytes)); + + diff --git a/src/main/java/com/wurrly/services/SwaggerService.java b/src/main/java/com/wurrly/services/SwaggerService.java index 1664f51..a745158 100644 --- a/src/main/java/com/wurrly/services/SwaggerService.java +++ b/src/main/java/com/wurrly/services/SwaggerService.java @@ -24,8 +24,8 @@ import com.mitchellbosecke.pebble.template.PebbleTemplate; import com.typesafe.config.Config; import com.wurrly.modules.RoutingModule; +import com.wurrly.server.endpoints.EndpointInfo; import com.wurrly.server.handlers.HandlerGenerator; -import com.wurrly.server.route.RouteInfo; import com.wurrly.server.swagger.ServerParameterExtension; import com.wurrly.utilities.JsonMapper; @@ -248,7 +248,7 @@ public void handleRequest(HttpServerExchange exchange) throws Exception }); - routingModule.getRegisteredRoutes().add(RouteInfo.builder().withConsumes("*/*").withPathTemplate(pathTemplate).withControllerName("Swagger").withMethod(Methods.GET).withProduces(ContentType.JSON.getMimeType()).build()); + routingModule.getRegisteredEndpoints().add(EndpointInfo.builder().withConsumes("*/*").withPathTemplate(pathTemplate).withControllerName("Swagger").withMethod(Methods.GET).withProduces(ContentType.JSON.getMimeType()).build()); pathTemplate = this.swaggerBasePath; @@ -267,7 +267,7 @@ public void handleRequest(HttpServerExchange exchange) throws Exception }); - routingModule.getRegisteredRoutes().add(RouteInfo.builder().withConsumes("*/*").withProduces("text/html").withPathTemplate(pathTemplate).withControllerName("Swagger").withMethod(Methods.GET).build()); + routingModule.getRegisteredEndpoints().add(EndpointInfo.builder().withConsumes("*/*").withProduces("text/html").withPathTemplate(pathTemplate).withControllerName("Swagger").withMethod(Methods.GET).build()); ClassPathResourceManager resourceManager = new ClassPathResourceManager(this.getClass().getClassLoader()); @@ -292,7 +292,7 @@ public void handleRequest(HttpServerExchange exchange) throws Exception }); - routingModule.getRegisteredRoutes().add(RouteInfo.builder().withConsumes("*/*").withProduces("text/css").withPathTemplate(pathTemplate).withControllerName("Swagger").withMethod(Methods.GET).build()); + routingModule.getRegisteredEndpoints().add(EndpointInfo.builder().withConsumes("*/*").withProduces("text/css").withPathTemplate(pathTemplate).withControllerName("Swagger").withMethod(Methods.GET).build()); try @@ -321,7 +321,7 @@ public void handleRequest(HttpServerExchange exchange) throws Exception - routingModule.getRegisteredRoutes().add(RouteInfo.builder().withConsumes("*/*").withProduces("*/*").withPathTemplate(pathTemplate).withControllerName("Swagger").withMethod(Methods.GET).build()); + routingModule.getRegisteredEndpoints().add(EndpointInfo.builder().withConsumes("*/*").withProduces("*/*").withPathTemplate(pathTemplate).withControllerName("Swagger").withMethod(Methods.GET).build());