From 5931f6eb8ebe5c7eeb2b9275bb7fd4411153a0b0 Mon Sep 17 00:00:00 2001 From: joshua bauer Date: Fri, 15 Jun 2018 17:20:53 -0700 Subject: [PATCH] Smarter predicates. Use BufferedRequestHandler. --- .../server/handlers/HandlerGenerator.java | 3 ++- .../handlers/ServerFallbackHandler.java | 2 +- .../server/predicates/ServerPredicates.java | 19 +++++++++++++++---- .../sinistral/proteus/controllers/Tests.java | 3 +++ 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/main/java/io/sinistral/proteus/server/handlers/HandlerGenerator.java b/src/main/java/io/sinistral/proteus/server/handlers/HandlerGenerator.java index 6cc0680..58be0d6 100644 --- a/src/main/java/io/sinistral/proteus/server/handlers/HandlerGenerator.java +++ b/src/main/java/io/sinistral/proteus/server/handlers/HandlerGenerator.java @@ -65,6 +65,7 @@ import io.undertow.server.HttpHandler; import io.undertow.server.HttpServerExchange; import io.undertow.server.RoutingHandler; +import io.undertow.server.handlers.RequestBufferingHandler; import io.undertow.util.Headers; import io.undertow.util.HttpString; import net.openhft.compiler.CompilerUtils; @@ -1283,7 +1284,7 @@ else if (producesContentType.contains(MediaType.TEXT_HTML)) if(isBlocking) { - handlerName = "new io.undertow.server.handlers.BlockingHandler(" + handlerName + ")"; + handlerName = "new io.undertow.server.handlers.BlockingHandler(new io.undertow.server.handlers.RequestBufferingHandler.Wrapper(1).wrap(" + handlerName + "))"; } if (wrapAnnotation.isPresent() || typeLevelHandlerWrapperMap.size() > 0 || securityDefinitions.size() > 0) diff --git a/src/main/java/io/sinistral/proteus/server/handlers/ServerFallbackHandler.java b/src/main/java/io/sinistral/proteus/server/handlers/ServerFallbackHandler.java index 669fcb9..b98df25 100644 --- a/src/main/java/io/sinistral/proteus/server/handlers/ServerFallbackHandler.java +++ b/src/main/java/io/sinistral/proteus/server/handlers/ServerFallbackHandler.java @@ -17,7 +17,7 @@ public class ServerFallbackHandler implements HttpHandler @Override public void handleRequest(HttpServerExchange exchange) throws Exception { - exchange.setResponseCode(404); + exchange.setStatusCode(404); exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain"); exchange.getResponseSender().send("Page Not Found!!"); diff --git a/src/main/java/io/sinistral/proteus/server/predicates/ServerPredicates.java b/src/main/java/io/sinistral/proteus/server/predicates/ServerPredicates.java index 9f30714..e7da28c 100644 --- a/src/main/java/io/sinistral/proteus/server/predicates/ServerPredicates.java +++ b/src/main/java/io/sinistral/proteus/server/predicates/ServerPredicates.java @@ -19,12 +19,23 @@ */ public class ServerPredicates { - public static final Predicate JSON_PREDICATE = Predicates.contains(ExchangeAttributes.requestHeader(Headers.CONTENT_TYPE), MediaType.APPLICATION_JSON.contentType(), MediaType.APPLICATION_JSON.withCharset("utf-8")); - public static final Predicate XML_PREDICATE = Predicates.contains(ExchangeAttributes.requestHeader(Headers.CONTENT_TYPE), MediaType.APPLICATION_XML.contentType(),MediaType.APPLICATION_XML.withCharset("utf-8")); + + public static final String JSON_REGEX = "^(application\\/(json|x-javascript)|text\\/(json|x-javascript|x-json))(;.*)?$"; + + public static final String XML_REGEX = "^(application\\/(xml|xhtml\\+xml)|text\\/xml)(;.*)?$"; + + public static final Predicate JSON_PREDICATE = Predicates.regex(ExchangeAttributes.requestHeader(Headers.CONTENT_TYPE), JSON_REGEX); + public static final Predicate XML_PREDICATE = Predicates.regex(ExchangeAttributes.requestHeader(Headers.CONTENT_TYPE), XML_REGEX); + + //public static final Predicate JSON_PREDICATE = Predicates.contains(ExchangeAttributes.requestHeader(Headers.CONTENT_TYPE), MediaType.APPLICATION_JSON.contentType(), MediaType.APPLICATION_JSON.withCharset("utf-8")); + //public static final Predicate XML_PREDICATE = Predicates.contains(ExchangeAttributes.requestHeader(Headers.CONTENT_TYPE), MediaType.APPLICATION_XML.contentType(),MediaType.APPLICATION_XML.withCharset("UTF-8")); + public static final Predicate WILDCARD_PREDICATE = Predicates.contains(ExchangeAttributes.requestHeader(Headers.ACCEPT), MediaType.ANY.contentType()); public static final Predicate NO_WILDCARD_PREDICATE = Predicates.not(Predicates.contains(ExchangeAttributes.requestHeader(Headers.ACCEPT), MediaType.ANY.contentType())); - public static final Predicate ACCEPT_JSON_PREDICATE = Predicates.contains(ExchangeAttributes.requestHeader(Headers.ACCEPT), MediaType.APPLICATION_JSON.contentType(),MediaType.APPLICATION_JSON.withCharset("utf-8").toString()); - public static final Predicate ACCEPT_XML_PREDICATE = Predicates.contains(ExchangeAttributes.requestHeader(Headers.ACCEPT), MediaType.APPLICATION_XML.contentType(),MediaType.APPLICATION_XML.withCharset("utf-8").toString()); + //public static final Predicate ACCEPT_JSON_PREDICATE = Predicates.contains(ExchangeAttributes.requestHeader(Headers.ACCEPT), MediaType.APPLICATION_JSON.contentType(),MediaType.APPLICATION_JSON.withCharset("UTF-8").toString()); + public static final Predicate ACCEPT_JSON_PREDICATE = Predicates.regex(ExchangeAttributes.requestHeader(Headers.ACCEPT), JSON_REGEX); + public static final Predicate ACCEPT_XML_PREDICATE = Predicates.regex(ExchangeAttributes.requestHeader(Headers.ACCEPT), XML_REGEX);; + //public static final Predicate ACCEPT_XML_PREDICATE = Predicates.contains(ExchangeAttributes.requestHeader(Headers.ACCEPT), MediaType.APPLICATION_XML.contentType(),MediaType.APPLICATION_XML.withCharset("UTF-8").toString()); public static final Predicate ACCEPT_XML_EXCLUSIVE_PREDICATE = Predicates.and(ACCEPT_XML_PREDICATE, NO_WILDCARD_PREDICATE ); public static final Predicate MAX_CONTENT_SIZE_PREDICATE = new MaxRequestContentLengthPredicate.Builder().build(Collections.singletonMap("value", 0L)); public static final Predicate STRING_BODY_PREDICATE = Predicates.and(Predicates.or(JSON_PREDICATE,XML_PREDICATE), MAX_CONTENT_SIZE_PREDICATE ); diff --git a/src/test/java/io/sinistral/proteus/controllers/Tests.java b/src/test/java/io/sinistral/proteus/controllers/Tests.java index 6604bcf..fde4af7 100644 --- a/src/test/java/io/sinistral/proteus/controllers/Tests.java +++ b/src/test/java/io/sinistral/proteus/controllers/Tests.java @@ -39,6 +39,7 @@ import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import io.undertow.server.HttpServerExchange; +import io.undertow.server.handlers.RequestBufferingHandler; /** * @author jbauer @@ -65,6 +66,8 @@ public class Tests @ApiOperation(value = "Json serialization endpoint", httpMethod = "GET" ) public void exchangeJsonSerialize(HttpServerExchange exchange) { + + response( JsonStream.serialize(ImmutableMap.of("message", "Hello, World!")) ).applicationJson().send(exchange); }