Skip to content

Commit

Permalink
Blocking and Debug Support
Browse files Browse the repository at this point in the history
Support isBlocking when more than one consume type specified.
Added @debug annotation.
  • Loading branch information
noboomu committed Oct 3, 2018
1 parent 7e481ec commit c4308a5
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.sinistral</groupId>
<artifactId>proteus-core</artifactId>
<version>0.3.3-SNAPSHOT</version>
<version>0.3.4-SNAPSHOT</version>
<name>proteus core</name>
<description>Proteus is an extremely light, fast, and flexible Java REST API framework built atop Undertow.</description>
<url>http://github.com/noboomu/proteus</url>
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/io/sinistral/proteus/annotations/Debug.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
*
*/
package io.sinistral.proteus.annotations;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

/**
* Indicates that this route should use a RequestDumpingHandler
*/
@Retention(RUNTIME)
@Target({ TYPE, METHOD })
public @interface Debug
{
boolean value() default true;
}


Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,15 @@
import javax.ws.rs.HeaderParam;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;



import org.apache.commons.lang3.StringUtils;
import org.reflections.Reflections;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.type.TypeReference;
import com.google.inject.Inject;
import com.google.inject.name.Named;
import com.fasterxml.jackson.core.type.TypeReference;
import com.squareup.javapoet.AnnotationSpec;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.CodeBlock;
Expand All @@ -53,6 +51,7 @@
import com.squareup.javapoet.TypeSpec;

import io.sinistral.proteus.annotations.Blocking;
import io.sinistral.proteus.annotations.Debug;
import io.sinistral.proteus.server.Extractors;
import io.sinistral.proteus.server.ServerRequest;
import io.sinistral.proteus.server.ServerResponse;
Expand All @@ -63,7 +62,8 @@
import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;
import io.undertow.server.RoutingHandler;
import io.undertow.server.handlers.RequestBufferingHandler;
import io.undertow.server.handlers.form.FormEncodedDataDefinition;
import io.undertow.server.handlers.form.MultiPartParserDefinition;
import io.undertow.util.Headers;
import io.undertow.util.HttpString;
import net.openhft.compiler.CompilerUtils;
Expand Down Expand Up @@ -393,13 +393,21 @@ else if (t.equals(HttpServerExchange.class) || t.equals(ServerRequest.class))
String consumesContentType = "*/*";

Boolean isBlocking = false;
Boolean isDebug = false;

Optional<Blocking> blockingAnnotation = Optional.ofNullable(m.getAnnotation(Blocking.class));

if (blockingAnnotation.isPresent())
{
isBlocking = blockingAnnotation.get().value();
}

Optional<Debug> debugAnnotation = Optional.ofNullable(m.getAnnotation(Debug.class));

if (debugAnnotation.isPresent())
{
isDebug = debugAnnotation.get().value();
}

Optional<javax.ws.rs.Produces> producesAnnotation = Optional.ofNullable(m.getAnnotation(javax.ws.rs.Produces.class));

Expand Down Expand Up @@ -467,8 +475,8 @@ else if (t.equals(HttpServerExchange.class) || t.equals(ServerRequest.class))
endpointInfo.setConsumes(consumesContentType);

//The handler for these two inputs types is blocking, so we set the flag
if (endpointInfo.getConsumes().equals("application/x-www-form-urlencoded")
|| endpointInfo.getConsumes().equals("multipart/form-data")) {
if (endpointInfo.getConsumes().contains(FormEncodedDataDefinition.APPLICATION_X_WWW_FORM_URLENCODED)
|| endpointInfo.getConsumes().contains(MultiPartParserDefinition.MULTIPART_FORM_DATA)) {
isBlocking = true;
}

Expand Down Expand Up @@ -768,7 +776,7 @@ else if (producesContentType.contains(MediaType.TEXT_HTML))
}
else
{
methodBuilder.addStatement("exchange.getResponseSender().send(com.jsoniter.output.JsonStream.serialize($L))", "response");
methodBuilder.addStatement("exchange.getResponseSender().send($L.toString())", "response");
}

}
Expand Down Expand Up @@ -827,9 +835,19 @@ else if (producesContentType.contains(MediaType.TEXT_HTML))
securityDefinitions.addAll(typeLevelSecurityDefinitions);
}

if (isBlocking)
if (isBlocking && isDebug)
{
handlerName = "new io.undertow.server.handlers.BlockingHandler(new io.undertow.server.handlers.RequestBufferingHandler.Wrapper(1).wrap(" + handlerName + "))";
handlerName = "new io.undertow.server.handlers.RequestDumpingHandler(new io.undertow.server.handlers.BlockingHandler(new io.undertow.server.handlers.RequestBufferingHandler.Wrapper(8).wrap(" + handlerName + ")))";
}
else if( isBlocking )
{
handlerName = "new io.undertow.server.handlers.BlockingHandler(new io.undertow.server.handlers.RequestBufferingHandler.Wrapper(8).wrap(" + handlerName + "))";

}
else if( isDebug )
{
handlerName = "new io.undertow.server.handlers.RequestDumpingHandler(" + handlerName + ")";

}

if (wrapAnnotation.isPresent() || typeLevelHandlerWrapperMap.size() > 0 || securityDefinitions.size() > 0)
Expand Down
36 changes: 36 additions & 0 deletions src/test/java/io/sinistral/proteus/test/controllers/Tests.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.google.inject.Singleton;

import io.sinistral.proteus.annotations.Blocking;
import io.sinistral.proteus.annotations.Debug;
import io.sinistral.proteus.server.ServerRequest;
import io.sinistral.proteus.server.ServerResponse;
import io.sinistral.proteus.test.models.User;
Expand Down Expand Up @@ -269,6 +270,41 @@ public ServerResponse<ByteBuffer> responseUploadByteBuffer(ServerRequest request

}

@GET
@Path("/response/debug")
@Debug
@ApiOperation(value = "Debug endpoint", httpMethod = "GET" )
public ServerResponse<Map<String,String>> debugEndpoint(ServerRequest request)
{
try
{
Map<String,String> map = ImmutableMap.of("message", "Hello, World!");

return response( map ).applicationJson();
} catch(Exception e)
{
return response().badRequest(e);
}
}


@GET
@Path("/response/debug/blocking")
@Debug
@Blocking
@ApiOperation(value = "Debug blocking endpoint", httpMethod = "GET" )
public ServerResponse<Map<String,String>> debugBlockingEndpoint(ServerRequest request)
{
try
{
Map<String,String> map = ImmutableMap.of("message", "Hello, World!");

return response( map ).applicationJson();
} catch(Exception e)
{
return response().badRequest(e);
}
}

@GET
@Path("/response/future/user")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,20 @@ public void testSwaggerDocs()
{
given().accept(ContentType.JSON).log().uri().when().get("swagger.json").then().statusCode(200).and().body("basePath", is("/v1"));
}

@Test
public void testDebugEndpoint()
{
given().accept(ContentType.JSON).log().uri().when().get("tests/response/debug").then().statusCode(200);
}

@Test
public void testDebugBlockingEndpoint()
{
given().accept(ContentType.JSON).log().uri().when().get("tests/response/debug/blocking").then().statusCode(200);
}


@Test
public void exchangeUserJson()
{
Expand Down

0 comments on commit c4308a5

Please sign in to comment.