Skip to content

Commit

Permalink
Minor updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
noboomu committed Apr 12, 2017
1 parent 9e6a02d commit d610a17
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 63 deletions.
12 changes: 11 additions & 1 deletion conf/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/wurrly/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
43 changes: 20 additions & 23 deletions src/main/java/com/wurrly/controllers/Users.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -48,7 +49,6 @@
public class Users
{


@Inject
@Named("es.index.name")
protected String esIndexName;
Expand Down Expand Up @@ -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<String> context,
@ApiParam(name="type",required=true) @QueryParam("type") User.UserType type,
Expand All @@ -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<String> context
)
Expand All @@ -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<String> context, final User user )
public ServerResponse createUser( final ServerRequest serverRequest, @QueryParam("context") Optional<String> 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
Expand Down
45 changes: 24 additions & 21 deletions src/main/java/com/wurrly/modules/RoutingModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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<? extends DefaultResponseListener> defaultResponseListenerClass = (Class<? extends DefaultResponseListener>) Class.forName(defaultResponseListenerClassName);
log.info("defaultResponseListenerClassName: " + defaultResponseListenerClassName);
this.bind(DefaultResponseListener.class).to(defaultResponseListenerClass).in(Singleton.class);
} catch (Exception e)
{
Expand All @@ -71,23 +90,7 @@ protected void configure()

}

/**
* @return the registeredEndpoints
*/
public Set<EndpointInfo> getRegisteredEndpoints()
{
return registeredEndpoints;
}

/**
* @return the registeredControllers
*/
public Set<Class<?>> getRegisteredControllers()
{
return registeredControllers;

}




}
24 changes: 13 additions & 11 deletions src/main/java/com/wurrly/server/handlers/HandlerGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -382,10 +382,7 @@ else if (hasFromString)
}
}

@Inject
protected RoutingModule routingModule;




@Inject
@Named("application.path")
Expand All @@ -394,8 +391,14 @@ else if (hasFromString)
protected String packageName;
protected String className;
protected String sourceString;

protected Set<Class<?>> controllerClasses = null;

@Inject
@Named("registeredEndpoints")
protected Set<EndpointInfo> registeredEndpoints;

@Inject
@Named("registeredControllers")
protected Set<Class<?>> registeredControllers;

// public static void main(String[] args)
// {
Expand All @@ -422,11 +425,10 @@ else if (hasFromString)
//
// }

public HandlerGenerator(String packageName, String className, Set<Class<?>> controllerClasses)
public HandlerGenerator(String packageName, String className)
{
this.packageName = packageName;
this.className = className;
this.controllerClasses = controllerClasses;
this.className = className;

}

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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);

}

Expand Down
15 changes: 9 additions & 6 deletions src/main/java/com/wurrly/tests/TestInvoker.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
*
Expand Down Expand Up @@ -64,6 +64,9 @@ interface QuadConsumer<R, T, U, V> {
}
}






public static void main(String[] args)
Expand Down

0 comments on commit d610a17

Please sign in to comment.