Skip to content

Commit

Permalink
Generation and compilation functional.
Browse files Browse the repository at this point in the history
  • Loading branch information
noboomu committed Apr 3, 2017
1 parent 714520a commit d2838dc
Show file tree
Hide file tree
Showing 9 changed files with 1,056 additions and 645 deletions.
65 changes: 36 additions & 29 deletions src/com/wurrly/BaseServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,21 @@
import com.google.inject.Injector;
import com.jsoniter.DecodingMode;
import com.jsoniter.JsonIterator;
import com.jsoniter.ReflectionDecoderFactory;
import com.jsoniter.annotation.JsoniterAnnotationSupport;
import com.jsoniter.output.EncodingMode;
import com.jsoniter.output.JsonStream;
import com.jsoniter.spi.JsoniterSpi;
import com.wurrly.controllers.Users;
import com.wurrly.models.User;
import com.wurrly.modules.DIModule;
import com.wurrly.server.GeneratedRouteHandler;
import com.wurrly.server.ServerRequest;
import com.wurrly.utilities.HandleGenerator;
import com.wurrly.tests.RestRouteGenerator;

import io.undertow.Undertow;
import io.undertow.UndertowOptions;
import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;
import io.undertow.server.RoutingHandler;
import io.undertow.util.Headers;
import io.undertow.util.HttpString;
import io.undertow.util.Methods;

/**
Expand Down Expand Up @@ -125,7 +122,7 @@ public static void main(String[] args)
Injector injector = Guice.createInjector(new DIModule());


Users usersController = injector.getInstance(Users.class);
// Users usersController = injector.getInstance(Users.class);

// injector.injectMembers(usersController);

Expand All @@ -136,32 +133,42 @@ public static void main(String[] args)

RoutingHandler router = new RoutingHandler().setFallbackHandler(BaseHandlers::notFoundHandler);

RestRouteGenerator generator = new RestRouteGenerator("com.wurrly.controllers.handlers","RouteHandlers");
generator.generateRoutes();

Class<? extends GeneratedRouteHandler> handlerClass = generator.compileRoutes();

Logger.debug("New class: " + handlerClass);

GeneratedRouteHandler routeHandler = injector.getInstance(handlerClass);

routeHandler.addRouteHandlers(router);

// HttpHandler getUserHandler = null;
// GetUsersHandler getUserHandler = new GetUsersHandler(usersController);

for( Method m : Users.class.getDeclaredMethods() )
{
System.out.println("method: " + m);

if( m.isSynthetic() || !m.getDeclaringClass().equals(Users.class))
{
System.out.println("m " + m + " is shady");
continue;
}

HttpString httpMethod = HandleGenerator.extractHttpMethod.apply(m);
String pathTemplate = HandleGenerator.extractPathTemplate.apply(m);
HttpHandler handler = HandleGenerator.generateHandler(usersController, m, httpMethod.equals(Methods.POST));

Logger.info("\nFUNCTION: " + m + "\n\tMETHOD: " + httpMethod + "\n\tPATH: " + pathTemplate);


router.add(httpMethod, pathTemplate, handler );

// router.addAll(Handlers.path().addPrefixPath(pathTemplate, handler));
System.out.println("handler: " + handler);

}
// for( Method m : Users.class.getDeclaredMethods() )
// {
// System.out.println("method: " + m);
//
// if( m.isSynthetic() || !m.getDeclaringClass().equals(Users.class))
// {
// System.out.println("m " + m + " is shady");
// continue;
// }
//
// HttpString httpMethod = HandleGenerator.extractHttpMethod.apply(m);
// String pathTemplate = HandleGenerator.extractPathTemplate.apply(m);
// HttpHandler handler = HandleGenerator.generateHandler(usersController, m, httpMethod.equals(Methods.POST));
//
// Logger.info("\nFUNCTION: " + m + "\n\tMETHOD: " + httpMethod + "\n\tPATH: " + pathTemplate);
//
//
// router.add(httpMethod, pathTemplate, handler );
//
// System.out.println("handler: " + handler);
//
// }

// final HttpHandler createUserPostHandler = new HttpHandler() {
// @Override
Expand Down
16 changes: 8 additions & 8 deletions src/com/wurrly/controllers/Users.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
package com.wurrly.controllers;

import java.nio.ByteBuffer;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.UUID;

import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.FormParam;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -27,7 +27,6 @@
import com.google.inject.Singleton;
import com.google.inject.name.Named;
import com.jsoniter.any.Any;
import com.jsoniter.output.JsonStream;
import com.typesafe.config.Config;
import com.wurrly.models.User;
import com.wurrly.server.ServerRequest;
Expand All @@ -41,6 +40,7 @@
@Api(tags="users",produces="application/json", consumes="application/json")
@Path("/api/users")
@Produces(("application/json"))
@Consumes(("application/json"))
@Singleton
public class Users
{
Expand All @@ -64,10 +64,10 @@ public Users()
@GET
@Path("/{userId}/type")
@ApiOperation(value = "Find users by id with type", nickname = "user", httpMethod = "GET", response = User.class)
public Any userType(final ServerRequest serverRequest, @PathParam("userId") final Long userId, @QueryParam("context") Optional<String> context, @QueryParam("type") User.UserType type)
public Any userType(final ServerRequest serverRequest, @PathParam("userId") final Long userId, @QueryParam("context") Optional<String> context, @QueryParam("type") User.UserType type, @QueryParam("uuid") UUID uuid)
{
//
// log.debug("esIndexName: " + esIndexName);
log.debug("uuid: " + uuid);
// log.debug("configuration: " + configuration);

// log.debug("context: " + context);
Expand Down
43 changes: 40 additions & 3 deletions src/com/wurrly/server/Extractors.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import java.nio.channels.FileChannel;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Deque;
import java.util.Optional;
import java.text.SimpleDateFormat;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.Deque;

import com.jsoniter.JsonIterator;
import com.jsoniter.any.Any;
Expand Down Expand Up @@ -44,6 +46,20 @@ public static final <T> java.util.Optional<T> typed(final HttpServerExchange exc
}
});
}

public static final <T> java.util.Optional<T> typed(final HttpServerExchange exchange, final Class<T> type )
{
return jsonIterator(exchange).map(i -> {
try
{
return i.read(type);
} catch (Exception e)
{
return null;
}
});
}


public static final java.util.Optional<Any> any(final HttpServerExchange exchange )
{
Expand Down Expand Up @@ -89,17 +105,33 @@ public static final java.util.Optional<String> string(final HttpServerExchange e
{
return java.util.Optional.ofNullable(exchange.getQueryParameters().get(name)).map(Deque::getFirst);
}

public static final java.util.Optional<String> header(final HttpServerExchange exchange, final String name)
{
return java.util.Optional.ofNullable(exchange.getRequestHeaders().get(name)).map(Deque::getFirst);
}

public static final java.util.Optional<Path> filePath(final HttpServerExchange exchange, final String name)
{
return java.util.Optional.ofNullable(exchange.getAttachment(FormDataParser.FORM_DATA).get(name)).map(Deque::getFirst).map(FormValue::getPath);
}
}

public static Date date(final HttpServerExchange exchange,final String name) throws Throwable {

return Date.from( ZonedDateTime.parse( string(exchange,name) ).toInstant() );

}

public static final <T> T typed(final HttpServerExchange exchange, final TypeLiteral<T> type ) throws Exception
{
return jsonIterator(exchange).read(type);
}

public static final <T> T typed(final HttpServerExchange exchange, final Class<T> type ) throws Exception
{
return jsonIterator(exchange).read(type);
}

public static final Any any(final HttpServerExchange exchange )
{
Expand Down Expand Up @@ -153,6 +185,11 @@ public static final String string(final HttpServerExchange exchange, final Strin
{
return exchange.getQueryParameters().get(name).getFirst();
}

public static final String header(final HttpServerExchange exchange, final String name)
{
return exchange.getRequestHeaders().get(name).getFirst();
}

public static final Long longValue(final HttpServerExchange exchange, final String name)
{
Expand All @@ -169,7 +206,7 @@ public static final Boolean booleanValue(final HttpServerExchange exchange, fina
return Boolean.parseBoolean(string(exchange, name));
}

public static final <E extends Enum<E>> E enumValue(final HttpServerExchange exchange, final String name, Class<E> clazz)
public static final <E extends Enum<E>> E enumValue(final HttpServerExchange exchange, Class<E> clazz,final String name)
{
return Enum.valueOf(clazz, string(exchange, name));
}
Expand Down
15 changes: 15 additions & 0 deletions src/com/wurrly/server/GeneratedRouteHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
*
*/
package com.wurrly.server;

import io.undertow.server.RoutingHandler;

/**
* @author jbauer
*
*/
public interface GeneratedRouteHandler
{
public void addRouteHandlers(final RoutingHandler router);
}
Loading

0 comments on commit d2838dc

Please sign in to comment.