Skip to content

Commit

Permalink
Improved README. Handle duplicate end-point handler names.
Browse files Browse the repository at this point in the history
  • Loading branch information
noboomu committed May 10, 2017
1 parent eab3c46 commit 66381c2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,17 @@ A great deal of inspiration came from working with the following excellent proje
```
- By default, passing a String to the response helper will convert it into a ByteBuffer
- For other types of responses the following demonstrates the preferred style:
```java
@GET
@Path("/world")
@Produces((MediaType.APPLICATION_JSON))
@ApiOperation(value = "Return a world JSON object", httpMethod = "GET", response=World.class )
public io.sinistral.proteus.server.ServerResponse<World> getWorld(Integer id, Integer randomNumber )
{
return io.sinistral.proteus.server.ServerResponse.response(new World(id,randomNumber));
}
```
- The entity can be set separately as well:
```java
@GET
@Path("/world")
Expand All @@ -96,6 +107,26 @@ A great deal of inspiration came from working with the following excellent proje
return io.sinistral.proteus.server.ServerResponse.response().entity(new World(id,randomNumber));
}
```
- CompletableFuture<ServerResponse<T>> is also supported:
```java
@GET
@Path("/future/user")
@ApiOperation(value = "Future user endpoint", httpMethod = "GET" )
public CompletableFuture<ServerResponse<User>> futureUser()
{
return CompletableFuture.completedFuture(response( new User(123L) ).applicationJson() );
}
```
- In this case a handler will be generated the following source code:
```java
public void handleRequest(final io.undertow.server.HttpServerExchange exchange) throws java.lang.Exception {
CompletableFuture<ServerResponse<User>> response = examplesController.futureUser();
response.thenAccept( r -> r.applicationJson().send(this,exchange) )
.exceptionally( ex -> {
throw new java.util.concurrent.CompletionException(ex);
} );
}
```

##### Endpoint Arguments
- a ```io.sinistral.proteus.server.ServerRequest``` can be added as an endpoint argument if the user wishes to access request properties that are not included in the argument list
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -607,6 +607,8 @@ protected void addClassMethodHandlers(TypeSpec.Builder typeBuilder, Class<?> cla
ClassName httpHandlerClass = ClassName.get("io.undertow.server", "HttpHandler");

String controllerName = clazz.getSimpleName().toLowerCase() + "Controller";

HashSet<String> handlerNameSet = new HashSet<>();

MethodSpec.Builder initBuilder = MethodSpec.methodBuilder("get").addModifiers(Modifier.PUBLIC).returns(RoutingHandler.class).addStatement("final $T router = new $T()", io.undertow.server.RoutingHandler.class, io.undertow.server.RoutingHandler.class);

Expand Down Expand Up @@ -792,6 +794,15 @@ else if (t.equals(HttpServerExchange.class) || t.equals(ServerRequest.class))
endpointInfo.setControllerMethod(m.getName());

String handlerName = String.format("%c%s%sHandler", Character.toLowerCase(clazz.getSimpleName().charAt(0)), clazz.getSimpleName().substring(1, clazz.getSimpleName().length()), StringUtils.capitalize(m.getName()));

int nameIndex = 1;

while(handlerNameSet.contains(handlerName))
{
handlerName = handlerName + "_" + nameIndex++;
}

handlerNameSet.add(handlerName);

TypeSpec.Builder handlerClassBuilder = TypeSpec.anonymousClassBuilder("").addSuperinterface(httpHandlerClass);

Expand Down

0 comments on commit 66381c2

Please sign in to comment.