From d610a1791e26c1d11413c48513ca2be05423c8d4 Mon Sep 17 00:00:00 2001 From: joshua bauer Date: Tue, 11 Apr 2017 21:01:14 -0700 Subject: [PATCH] Minor updates. --- conf/application.conf | 12 ++++- src/main/java/com/wurrly/Application.java | 2 +- .../java/com/wurrly/controllers/Users.java | 43 +++++++++--------- .../com/wurrly/modules/RoutingModule.java | 45 ++++++++++--------- .../server/handlers/HandlerGenerator.java | 24 +++++----- .../java/com/wurrly/tests/TestInvoker.java | 15 ++++--- .../com.wurrly.server.swagger.ServerExtension | 0 ....wurrly.server.swagger.ServerModelResolver | 0 8 files changed, 78 insertions(+), 63 deletions(-) rename src/main/resources/META-INF/{services => empty}/com.wurrly.server.swagger.ServerExtension (100%) rename src/main/resources/META-INF/{services => empty}/com.wurrly.server.swagger.ServerModelResolver (100%) diff --git a/conf/application.conf b/conf/application.conf index e9f9e8c..eb0bbed 100644 --- a/conf/application.conf +++ b/conf/application.conf @@ -37,7 +37,7 @@ application { # numberFormat = DecimalFormat.getInstance(${application.lang})).toPattern() # comma separated list of locale using the language tag format. Default to: Locale.getDefault() - # lang = Locale.getDefault() + lang = Locale.getDefault() # timezone, system default. set it at runtime # tz = ZoneId.systemDefault().getId() @@ -46,11 +46,21 @@ application { # example: https://my.domain.com/{0} redirect_https = "" + } api.version="v1" + +assets { + path = "/public" + dir = "./assets" + cache { + time = 500 + } +} + # images image.size.min=120 image.size.max=3200 diff --git a/src/main/java/com/wurrly/Application.java b/src/main/java/com/wurrly/Application.java index c210088..b9abed9 100644 --- a/src/main/java/com/wurrly/Application.java +++ b/src/main/java/com/wurrly/Application.java @@ -133,7 +133,7 @@ public Undertow buildServer() final DefaultResponseListener defaultResponseListener = injector.getInstance(DefaultResponseListener.class); - HandlerGenerator generator = new HandlerGenerator("com.wurrly.controllers.handlers","RouteHandlers",this.registeredControllers); + HandlerGenerator generator = new HandlerGenerator("com.wurrly.controllers.handlers","RouteHandlers"); injector.injectMembers(generator); diff --git a/src/main/java/com/wurrly/controllers/Users.java b/src/main/java/com/wurrly/controllers/Users.java index 73393ab..beb862a 100644 --- a/src/main/java/com/wurrly/controllers/Users.java +++ b/src/main/java/com/wurrly/controllers/Users.java @@ -3,6 +3,8 @@ */ package com.wurrly.controllers; +import static com.wurrly.server.ServerResponse.response; + import java.nio.ByteBuffer; import java.util.Date; import java.util.List; @@ -31,7 +33,6 @@ import com.wurrly.models.User; import com.wurrly.server.ServerRequest; import com.wurrly.server.ServerResponse; -import static com.wurrly.server.ServerResponse.response; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; @@ -48,7 +49,6 @@ public class Users { - @Inject @Named("es.index.name") protected String esIndexName; @@ -110,7 +110,7 @@ public ServerResponse userType( @Path("/form/{userId}") @Consumes("*/*") @ApiOperation(value = "Post a complex form", httpMethod = "POST", response = User.class) - public ServerResponse userForm(@ApiParam(hidden=true) final ServerRequest serverRequest, + public ServerResponse userForm( final ServerRequest serverRequest, @ApiParam(name="userId",required=true) @PathParam("userId") final Long userId, @ApiParam(name="context",required=false) @QueryParam("context") Optional context, @ApiParam(name="type",required=true) @QueryParam("type") User.UserType type, @@ -132,7 +132,7 @@ public ServerResponse userForm(@ApiParam(hidden=true) final ServerRequest server @GET @Path("/{userId}") @ApiOperation(value = "Find users by id", httpMethod = "GET", response = User.class) - public ServerResponse user(@ApiParam(hidden=true)final ServerRequest serverRequest, + public ServerResponse user( final ServerRequest serverRequest, @ApiParam(name="userId", required=true) @PathParam("userId") final Long userId, @ApiParam(name="context", required=false) @QueryParam("context") Optional context ) @@ -158,26 +158,23 @@ public ServerResponse user(@ApiParam(hidden=true)final ServerRequest serverReque //@Consumes("multipart/form-data") // @ApiImplicitParams({ @ApiImplicitParam(dataType = "com.wurrly.models.User", name = "user", paramType = "body", required = false, allowMultiple = false) }) @ApiOperation(value = "Create a user", httpMethod = "POST", response = User.class) - public ServerResponse createUser(@ApiParam(hidden=true)final ServerRequest serverRequest, @QueryParam("context") Optional context, final User user ) + public ServerResponse createUser( final ServerRequest serverRequest, @QueryParam("context") Optional context, final User user ) { -// - -// log.debug("context: " + context); -// log.debug("request: " + serverRequest); -// log.debug("file: " + user); - - - if( user != null ) - { - return response().ok().entity(user); - } - else - { - return response().exception(new Exception("No user found")); - } - - - + + ServerResponse response = null; + + if( user != null ) + { + response = response().ok().entity(user) ; + + } + else + { + response = response().exception(new Exception("No user found")) ; + + } + + return response; } @PUT diff --git a/src/main/java/com/wurrly/modules/RoutingModule.java b/src/main/java/com/wurrly/modules/RoutingModule.java index 951192a..37f8ad4 100644 --- a/src/main/java/com/wurrly/modules/RoutingModule.java +++ b/src/main/java/com/wurrly/modules/RoutingModule.java @@ -3,6 +3,7 @@ */ package com.wurrly.modules; +import java.nio.file.Paths; import java.util.HashSet; import java.util.Set; import java.util.TreeSet; @@ -18,8 +19,13 @@ import com.wurrly.Application.BaseHandlers; import com.wurrly.server.endpoints.EndpointInfo; +import io.undertow.predicate.TruePredicate; import io.undertow.server.DefaultResponseListener; +import io.undertow.server.HttpServerExchange; import io.undertow.server.RoutingHandler; +import io.undertow.server.handlers.resource.FileResourceManager; +import io.undertow.server.handlers.resource.ResourceHandler; +import io.undertow.util.Methods; /** * @author jbauer @@ -45,21 +51,34 @@ public RoutingModule(Config config) @Override protected void configure() { + + + this.binder().requestInjection(this); + RoutingHandler router = new RoutingHandler() .setFallbackHandler(BaseHandlers::notFoundHandler); - this.binder().requestInjection(this); + final String assetsPath = config.getString("assets.path"); + final String assetsDirectoryName = config.getString("assets.dir") ; + final Integer assetsCacheTime = config.getInt("assets.cache.time"); + + final FileResourceManager fileResourceManager = new FileResourceManager(Paths.get(assetsDirectoryName).toFile()); + + + router.add(Methods.GET, assetsPath + "/*", io.undertow.Handlers.rewrite("regex['" + assetsPath + "/(.*)']", "/$1", getClass().getClassLoader(), new ResourceHandler(fileResourceManager) + .setCachable(TruePredicate.instance()) + .setCacheTime(assetsCacheTime) + )); + this.bind(RoutingHandler.class).toInstance(router); this.bind(RoutingModule.class).toInstance(this); - - + try { String defaultResponseListenerClassName = config.getString("application.defaultResponseListener"); Class defaultResponseListenerClass = (Class) Class.forName(defaultResponseListenerClassName); - log.info("defaultResponseListenerClassName: " + defaultResponseListenerClassName); this.bind(DefaultResponseListener.class).to(defaultResponseListenerClass).in(Singleton.class); } catch (Exception e) { @@ -71,23 +90,7 @@ protected void configure() } - /** - * @return the registeredEndpoints - */ - public Set getRegisteredEndpoints() - { - return registeredEndpoints; - } - - /** - * @return the registeredControllers - */ - public Set> getRegisteredControllers() - { - return registeredControllers; - } - - + } diff --git a/src/main/java/com/wurrly/server/handlers/HandlerGenerator.java b/src/main/java/com/wurrly/server/handlers/HandlerGenerator.java index cd19827..7c650ef 100644 --- a/src/main/java/com/wurrly/server/handlers/HandlerGenerator.java +++ b/src/main/java/com/wurrly/server/handlers/HandlerGenerator.java @@ -382,10 +382,7 @@ else if (hasFromString) } } - @Inject - protected RoutingModule routingModule; - - + @Inject @Named("application.path") @@ -394,8 +391,14 @@ else if (hasFromString) protected String packageName; protected String className; protected String sourceString; - - protected Set> controllerClasses = null; + + @Inject + @Named("registeredEndpoints") + protected Set registeredEndpoints; + + @Inject + @Named("registeredControllers") + protected Set> registeredControllers; // public static void main(String[] args) // { @@ -422,11 +425,10 @@ else if (hasFromString) // // } - public HandlerGenerator(String packageName, String className, Set> controllerClasses) + public HandlerGenerator(String packageName, String className) { this.packageName = packageName; - this.className = className; - this.controllerClasses = controllerClasses; + this.className = className; } @@ -459,7 +461,7 @@ protected void generateRoutes() MethodSpec.Builder constructor = MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC).addAnnotation(injectClass); - for (Class clazz : this.controllerClasses) + for (Class clazz : this.registeredControllers) { String className = clazz.getSimpleName().toLowerCase(); @@ -780,7 +782,7 @@ else if (t.equals(TypeHandler.OptionalFromStringType) || t.equals(TypeHandler.Op initBuilder.addCode("$L", "\n"); - this.routingModule.getRegisteredEndpoints().add(route); + registeredEndpoints.add(route); } diff --git a/src/main/java/com/wurrly/tests/TestInvoker.java b/src/main/java/com/wurrly/tests/TestInvoker.java index 35a5ee0..003ca7c 100644 --- a/src/main/java/com/wurrly/tests/TestInvoker.java +++ b/src/main/java/com/wurrly/tests/TestInvoker.java @@ -3,11 +3,16 @@ */ package com.wurrly.tests; +import static java.lang.invoke.MethodHandles.lookup; + +import java.lang.invoke.CallSite; +import java.lang.invoke.LambdaMetafactory; import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodType; import java.lang.reflect.Method; import java.util.Optional; +import java.util.concurrent.CompletionStage; import java.util.function.BiConsumer; import java.util.function.BiFunction; import java.util.function.Consumer; @@ -19,12 +24,7 @@ import com.google.common.base.Throwables; import com.wurrly.controllers.Users; import com.wurrly.server.ServerRequest; - -import static java.lang.invoke.MethodHandles.lookup; - -import java.lang.invoke.CallSite; -import java.lang.invoke.LambdaMetafactory; - + /** * @author jbauer * @@ -64,6 +64,9 @@ interface QuadConsumer { } } + + + public static void main(String[] args) diff --git a/src/main/resources/META-INF/services/com.wurrly.server.swagger.ServerExtension b/src/main/resources/META-INF/empty/com.wurrly.server.swagger.ServerExtension similarity index 100% rename from src/main/resources/META-INF/services/com.wurrly.server.swagger.ServerExtension rename to src/main/resources/META-INF/empty/com.wurrly.server.swagger.ServerExtension diff --git a/src/main/resources/META-INF/services/com.wurrly.server.swagger.ServerModelResolver b/src/main/resources/META-INF/empty/com.wurrly.server.swagger.ServerModelResolver similarity index 100% rename from src/main/resources/META-INF/services/com.wurrly.server.swagger.ServerModelResolver rename to src/main/resources/META-INF/empty/com.wurrly.server.swagger.ServerModelResolver