diff --git a/CHANGELOG.md b/CHANGELOG.md index 7dd66a7..a0acbfd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,31 @@ Proteus Changelog. ## Unreleased ### No issue +**Cleanup JsonViewWrapper.** + + +[dc95a16a710813a](https://github.com/noboomu/proteus/commit/dc95a16a710813a) Joshua Bauer *2020-01-31 01:04:26* + +**Improvements to JsonViewWrapper.** + + +[ead870ae6ad73d9](https://github.com/noboomu/proteus/commit/ead870ae6ad73d9) Joshua Bauer *2020-01-31 01:02:12* + +**Update readme.** + + +[7bfd78a09ba642d](https://github.com/noboomu/proteus/commit/7bfd78a09ba642d) Joshua Bauer *2020-01-31 00:39:35* + +**Improve JSON view handling.** + + +[e713dcdbaa67453](https://github.com/noboomu/proteus/commit/e713dcdbaa67453) Joshua Bauer *2020-01-31 00:20:00* + +**Improve OpenAPI preprocessing.** + + +[82a22bcbe66f93f](https://github.com/noboomu/proteus/commit/82a22bcbe66f93f) Joshua Bauer *2020-01-30 23:30:24* + **Allow byte arrays in response bodies.** diff --git a/README.md b/README.md index 5f7dfd8..3b7762a 100644 --- a/README.md +++ b/README.md @@ -9,11 +9,11 @@ An extremely __lightweight, flexible, and high performance__ [Undertow](http://u - JAX-RS compliant - Easy on the developer and the metal - Blazing fast!!! -[The latest Techempower benchmarks](https://www.techempower.com/benchmarks/) demonstrate __proteus__ outperforming 99% of all other web frameworks +[The latest Techempower benchmarks](https://www.techempower.com/benchmarks/) demonstrate __proteus__ outperforming the top Java web frameworks -![Top 5 in Java Frameworks for Fortunes](https://github.com/noboomu/proteus-example/blob/master/src/main/resources/images/benchmark1.png?raw=true) +![Top in Java Frameworks for JSON](https://github.com/noboomu/proteus-example/blob/master/src/main/resources/images/benchmark1.png?raw=true) -![Top 5 in Java Frameworks for JSON](https://github.com/noboomu/proteus-example/blob/master/src/main/resources/images/benchmark2.png?raw=true) +![Top in Java Frameworks for Plaintext](https://github.com/noboomu/proteus-example/blob/master/src/main/resources/images/benchmark2.png?raw=true) TL;DR --------------- diff --git a/pom.xml b/pom.xml index cff112b..b6e3830 100644 --- a/pom.xml +++ b/pom.xml @@ -356,6 +356,8 @@ + + \ No newline at end of file diff --git a/proteus-core/src/main/java/io/sinistral/proteus/wrappers/JsonViewWrapper.java b/proteus-core/src/main/java/io/sinistral/proteus/wrappers/JsonViewWrapper.java index 00d76d4..39048a0 100644 --- a/proteus-core/src/main/java/io/sinistral/proteus/wrappers/JsonViewWrapper.java +++ b/proteus-core/src/main/java/io/sinistral/proteus/wrappers/JsonViewWrapper.java @@ -1,6 +1,7 @@ package io.sinistral.proteus.wrappers; import com.google.inject.Inject; +import com.google.inject.Singleton; import io.undertow.server.HandlerWrapper; import io.undertow.server.HttpHandler; import io.undertow.util.AttachmentKey; @@ -12,45 +13,51 @@ import java.util.HashMap; import java.util.Map; import java.util.Optional; +import java.util.concurrent.ConcurrentHashMap; +@Singleton public class JsonViewWrapper implements HandlerWrapper { - public static AttachmentKey JSON_VIEW_KEY = AttachmentKey.create(Class.class); + public static AttachmentKey> JSON_VIEW_KEY = AttachmentKey.create(Class.class); private static final Logger logger = LoggerFactory.getLogger(JsonViewWrapper.class.getName()); @Named("jackson.jsonView.className") @Inject(optional = true) - private static String VIEW_CLASS_NAME; + private static String VIEW_CLASS_NAME = null; @Named("jackson.jsonView.defaultViewClass") @Inject(optional = true) - private static String DEFAULT_VIEW_CLASS = null; + private static String DEFAULT_VIEW_CLASS_NAME = null; @Named("jackson.jsonView.queryParameterName") @Inject(optional = true) private static String QUERY_PARAMETER_NAME = "context"; - private static Map CLASS_MAP; + private Map CLASS_MAP = new ConcurrentHashMap<>(); + + private Class defaultViewClass = null; public JsonViewWrapper() { super(); - if (CLASS_MAP == null && VIEW_CLASS_NAME != null) { + if ( VIEW_CLASS_NAME != null) { try { - Class clazz = Class.forName(VIEW_CLASS_NAME); - - CLASS_MAP = new HashMap<>(); + Class clazz = Class.forName(VIEW_CLASS_NAME); - final Class[] contexts = clazz.getClasses(); + final Class[] contexts = clazz.getClasses(); - for (Class c : contexts) { + for (Class c : contexts) { CLASS_MAP.put(c.getSimpleName().toLowerCase(), c); } + if(DEFAULT_VIEW_CLASS_NAME != null) + { + defaultViewClass = Class.forName(DEFAULT_VIEW_CLASS_NAME); + } } catch (Exception e) { @@ -69,17 +76,17 @@ public HttpHandler wrap(HttpHandler handler) String className = Optional.ofNullable(exchange.getQueryParameters().get(QUERY_PARAMETER_NAME)) .filter(q -> q.size() > 0) - .map(Deque::getFirst) - .orElse(DEFAULT_VIEW_CLASS); - + .map(Deque::getFirst).orElse(null); if(className != null) { - Class viewClass = CLASS_MAP.get(className.toLowerCase()); + Class viewClass = CLASS_MAP.getOrDefault(className, defaultViewClass); exchange.putAttachment(JSON_VIEW_KEY, viewClass); } - - + else if(defaultViewClass != null) + { + exchange.putAttachment(JSON_VIEW_KEY, defaultViewClass); + } } handler.handleRequest(exchange);