From 4917d3016811cec1d1859d844b3f99f8bce9b7c9 Mon Sep 17 00:00:00 2001 From: Joshua Bauer Date: Mon, 31 Aug 2020 12:35:07 -0700 Subject: [PATCH] Better generic parameter handling. --- CHANGELOG.md | 5 +++ .../sinistral/proteus/ProteusApplication.java | 18 ++++++++- .../ServerDefaultResponseListener.java | 1 + .../proteus/server/handlers/TypeHandler.java | 37 +++++++++++++++++-- .../proteus/test/controllers/Tests.java | 9 +++++ .../test/server/TestControllerEndpoints.java | 37 +++++++++++++++++++ 6 files changed, 103 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 05a2d84..2a9ea1e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ Proteus Changelog. ## Unreleased ### No issue +**Improve error handling.** + + +[94f075989e28408](https://github.com/noboomu/proteus/commit/94f075989e28408) Joshua Bauer *2020-06-24 20:28:31* + **Added protocol package and custom HttpHeaders implementation.** diff --git a/proteus-core/src/main/java/io/sinistral/proteus/ProteusApplication.java b/proteus-core/src/main/java/io/sinistral/proteus/ProteusApplication.java index f7ec564..2149926 100644 --- a/proteus-core/src/main/java/io/sinistral/proteus/ProteusApplication.java +++ b/proteus-core/src/main/java/io/sinistral/proteus/ProteusApplication.java @@ -137,6 +137,8 @@ public void start() return; } + final Thread mainThread = Thread.currentThread(); + final long startTime = System.currentTimeMillis(); log.info("Configuring modules: " + registeredModules); @@ -188,11 +190,25 @@ public void healthy() public void failure(Service service) { log.error("Service failure: " + service); + + startupDuration = Duration.ofMillis(System.currentTimeMillis() - startTime); + + for (ListenerInfo info : undertow.getListenerInfo()) { + log.debug("listener info: " + info); + SocketAddress address = info.getAddress(); + + if (address != null) { + ports.add(((java.net.InetSocketAddress) address).getPort()); + } + } + + printStatus(); + + running.set(true); } }, MoreExecutors.directExecutor()); - final Thread mainThread = Thread.currentThread(); Runtime.getRuntime().addShutdownHook(new Thread(() -> { try { diff --git a/proteus-core/src/main/java/io/sinistral/proteus/server/handlers/ServerDefaultResponseListener.java b/proteus-core/src/main/java/io/sinistral/proteus/server/handlers/ServerDefaultResponseListener.java index d0d26c5..c1a6371 100644 --- a/proteus-core/src/main/java/io/sinistral/proteus/server/handlers/ServerDefaultResponseListener.java +++ b/proteus-core/src/main/java/io/sinistral/proteus/server/handlers/ServerDefaultResponseListener.java @@ -67,6 +67,7 @@ public boolean handleDefaultResponse(HttpServerExchange exchange) final Map errorMap = new HashMap<>(); + final String path = exchange.getRelativePath(); if (throwable == null) { diff --git a/proteus-core/src/main/java/io/sinistral/proteus/server/handlers/TypeHandler.java b/proteus-core/src/main/java/io/sinistral/proteus/server/handlers/TypeHandler.java index e9265ca..9258bca 100644 --- a/proteus-core/src/main/java/io/sinistral/proteus/server/handlers/TypeHandler.java +++ b/proteus-core/src/main/java/io/sinistral/proteus/server/handlers/TypeHandler.java @@ -67,6 +67,7 @@ public enum TypeHandler // StatementParameterType.LITERAL, // io.sinistral.proteus.server.Extractors.class, // StatementParameterType.LITERAL, StatementParameterType.RAW), + BeanListValueOfType("$T $L = io.sinistral.proteus.server.Extractors.model(exchange,$L)", true, StatementParameterType.TYPE, StatementParameterType.LITERAL, StatementParameterType.LITERAL), BeanListFromStringType("$T $L = io.sinistral.proteus.server.Extractors.model(exchange,$L)", true, StatementParameterType.TYPE, StatementParameterType.LITERAL, StatementParameterType.LITERAL), @@ -304,7 +305,7 @@ public static TypeHandler forType(Type type, Boolean isBeanParam) boolean isSet = type.getTypeName().contains("java.util.Set"); boolean isMap = type.getTypeName().contains("java.util.Map"); - if (!isOptional && !isArray && !isSet) { + if (!isOptional && !isArray && !isSet && !isMap) { try { Class clazz = Class.forName(type.getTypeName()); @@ -314,6 +315,7 @@ public static TypeHandler forType(Type type, Boolean isBeanParam) } catch (Exception e) { HandlerGenerator.log.error(e.getMessage(), e); + } } @@ -341,7 +343,20 @@ public static TypeHandler forType(Type type, Boolean isBeanParam) } catch (Exception e) { HandlerGenerator.log.error(e.getMessage(), e); + throw e; + } + } + + if (isMap && !isOptional) { + try { + + return ModelType; + + + } catch (Exception e) { + HandlerGenerator.log.error(e.getMessage(), e); + throw e; } } @@ -369,9 +384,10 @@ public static TypeHandler forType(Type type, Boolean isBeanParam) } catch (Exception e) { HandlerGenerator.log.error(e.getMessage(), e); + throw e; } - } else if (isArray && isOptional) { + } else if (isArray ) { try { if (type instanceof ParameterizedType) { @@ -402,9 +418,23 @@ public static TypeHandler forType(Type type, Boolean isBeanParam) } catch (Exception e) { HandlerGenerator.log.error(e.getMessage(), e); + throw e; + } + } + + else if (isMap ) { + try { + + return ModelType; + + } catch (Exception e) { + HandlerGenerator.log.error(e.getMessage(), e); + throw e; } - } else if (isSet && isOptional) { + } + + else if (isSet ) { try { if (type instanceof ParameterizedType) { @@ -435,6 +465,7 @@ public static TypeHandler forType(Type type, Boolean isBeanParam) } catch (Exception e) { HandlerGenerator.log.error(e.getMessage(), e); + throw e; } } diff --git a/proteus-core/src/test/java/io/sinistral/proteus/test/controllers/Tests.java b/proteus-core/src/test/java/io/sinistral/proteus/test/controllers/Tests.java index 4329e3a..b522a31 100644 --- a/proteus-core/src/test/java/io/sinistral/proteus/test/controllers/Tests.java +++ b/proteus-core/src/test/java/io/sinistral/proteus/test/controllers/Tests.java @@ -246,6 +246,15 @@ public ServerResponse> genericBeanSet( ServerRequest request, @BeanPa { return response( ids ).applicationJson(); } + + @POST + @Path("generic/map/bean") + @Produces((MediaType.APPLICATION_JSON)) + @Consumes(MediaType.APPLICATION_JSON) + public ServerResponse> genericBeanMap( ServerRequest request, @BeanParam Map ids ) throws Exception + { + return response( ids ).applicationJson(); + } @POST diff --git a/proteus-core/src/test/java/io/sinistral/proteus/test/server/TestControllerEndpoints.java b/proteus-core/src/test/java/io/sinistral/proteus/test/server/TestControllerEndpoints.java index 43569f1..cbc6874 100644 --- a/proteus-core/src/test/java/io/sinistral/proteus/test/server/TestControllerEndpoints.java +++ b/proteus-core/src/test/java/io/sinistral/proteus/test/server/TestControllerEndpoints.java @@ -181,6 +181,43 @@ public void genericBeanList() e.printStackTrace(); } } + + @Test + public void genericBeanMap() + { + Map randomLongs = new java.util.HashMap<>(); + + Random random = new Random(); + + Long firstNumber = null; + + for(int i = 0; i < 10; i++) + { + Long v = random.nextLong(); + + randomLongs.put(v.toString(),v); + + if(firstNumber == null) + { + firstNumber = v; + } + } + + ObjectMapper mapper = new ObjectMapper(); + + try + { + + + String body = mapper.writeValueAsString(randomLongs); + + given().contentType(ContentType.JSON).accept(ContentType.JSON).body(body).post("tests/generic/map/bean").then().statusCode(200).body(containsString(firstNumber.toString())); + + } catch (Exception e) + { + e.printStackTrace(); + } + } @Test public void optionalGenericSet()