From f2d50912fd06078e6b21d44fc1034c2e5f50563f Mon Sep 17 00:00:00 2001 From: joshua bauer Date: Tue, 16 Oct 2018 14:54:07 -0700 Subject: [PATCH] General Improvements Services are bound to io.sinistral.proteus.services.BaseService. Redirects can change the request method. Cleaned up ServerResponse class. --- README.md | 4 + pom.xml | 55 ++++++---- .../sinistral/proteus/ProteusApplication.java | 11 +- .../proteus/modules/ApplicationModule.java | 5 +- .../proteus/server/ServerResponse.java | 102 +++++++++++------- .../io/sinistral/proteus/swagger/index.html | 5 + 6 files changed, 113 insertions(+), 69 deletions(-) diff --git a/README.md b/README.md index a9834d0..2a99120 100644 --- a/README.md +++ b/README.md @@ -111,6 +111,10 @@ Controller methods arguments support the following [JAX-RS annotations](https:// * ```javax.ws.rs.BeanParam``` * Binds and attempts to convert the request body to an instance of the method parameter. +* @DefaultParam + * ```javax.ws.rs.DefaultParam``` + * Sets the default value of a method parameter. + ## Return Types #### Performance diff --git a/pom.xml b/pom.xml index 0b84392..0ed311b 100644 --- a/pom.xml +++ b/pom.xml @@ -206,18 +206,15 @@ undertow-core ${version.undertow} - + - - javax.ws.rs - javax.ws.rs-api - 2.1.1 - - + + javax.ws.rs + javax.ws.rs-api + 2.1.1 + + com.squareup javapoet @@ -291,15 +288,11 @@ jackson-databind ${version.jackson} + - io.swagger - swagger-annotations - 1.5.12 - - - io.swagger - swagger-core - 1.5.12 + ch.qos.logback + logback-classic + 1.2.3 org.slf4j @@ -307,10 +300,27 @@ + + + + - ch.qos.logback - logback-classic - 1.2.3 + io.swagger + swagger-annotations + 1.5.12 + + + io.swagger + swagger-core + 1.5.12 org.slf4j @@ -318,7 +328,6 @@ - io.swagger swagger-parser diff --git a/src/main/java/io/sinistral/proteus/ProteusApplication.java b/src/main/java/io/sinistral/proteus/ProteusApplication.java index e8d2f9f..5ff2c6d 100644 --- a/src/main/java/io/sinistral/proteus/ProteusApplication.java +++ b/src/main/java/io/sinistral/proteus/ProteusApplication.java @@ -46,16 +46,15 @@ import io.sinistral.proteus.server.endpoints.EndpointInfo; import io.sinistral.proteus.server.handlers.HandlerGenerator; import io.sinistral.proteus.server.handlers.ServerDefaultHttpHandler; +import io.sinistral.proteus.services.BaseService; import io.sinistral.proteus.utilities.SecurityOps; import io.sinistral.proteus.utilities.TablePrinter; -import io.undertow.Handlers; import io.undertow.Undertow; import io.undertow.Undertow.ListenerInfo; import io.undertow.UndertowOptions; import io.undertow.server.HttpHandler; import io.undertow.server.HttpServerExchange; import io.undertow.server.RoutingHandler; -import io.undertow.server.protocol.http2.Http2UpgradeHandler; import io.undertow.util.Headers; import io.undertow.util.Methods; @@ -77,7 +76,7 @@ public class ProteusApplication @Inject @Named("registeredServices") - protected Set> registeredServices; + protected Set> registeredServices; @Inject protected RoutingHandler router; @@ -142,7 +141,9 @@ public void start() log.info("Starting services..."); - Set services = registeredServices.stream().map(sc -> injector.getInstance(sc)).collect(Collectors.toSet()); + Set services = registeredServices.stream().map(sc -> injector.getInstance(sc)).collect(Collectors.toSet()); + + injector = injector.createChildInjector(services); serviceManager = new ServiceManager(services); @@ -316,7 +317,7 @@ public void buildServer() } - public ProteusApplication addService(Class serviceClass) + public ProteusApplication addService(Class serviceClass) { registeredServices.add(serviceClass); return this; diff --git a/src/main/java/io/sinistral/proteus/modules/ApplicationModule.java b/src/main/java/io/sinistral/proteus/modules/ApplicationModule.java index 00e55e8..cb00df9 100644 --- a/src/main/java/io/sinistral/proteus/modules/ApplicationModule.java +++ b/src/main/java/io/sinistral/proteus/modules/ApplicationModule.java @@ -29,6 +29,7 @@ import io.sinistral.proteus.server.Extractors; import io.sinistral.proteus.server.ServerResponse; import io.sinistral.proteus.server.endpoints.EndpointInfo; +import io.sinistral.proteus.services.BaseService; import io.undertow.server.DefaultResponseListener; import io.undertow.server.HandlerWrapper; import io.undertow.server.HttpHandler; @@ -44,7 +45,7 @@ public class ApplicationModule extends AbstractModule protected Set registeredEndpoints = new TreeSet<>(); protected Set> registeredControllers = new HashSet<>(); - protected Set> registeredServices = new HashSet<>(); + protected Set> registeredServices = new HashSet<>(); protected Map registeredHandlerWrappers = new HashMap<>(); protected Config config; @@ -110,7 +111,7 @@ protected void configure() { }).annotatedWith(Names.named("registeredEndpoints")).toInstance(registeredEndpoints); - this.bind(new TypeLiteral>>() + this.bind(new TypeLiteral>>() { }).annotatedWith(Names.named("registeredServices")).toInstance(registeredServices); diff --git a/src/main/java/io/sinistral/proteus/server/ServerResponse.java b/src/main/java/io/sinistral/proteus/server/ServerResponse.java index 5fc276b..70fadf7 100644 --- a/src/main/java/io/sinistral/proteus/server/ServerResponse.java +++ b/src/main/java/io/sinistral/proteus/server/ServerResponse.java @@ -30,6 +30,7 @@ import io.undertow.util.HeaderValues; import io.undertow.util.Headers; import io.undertow.util.HttpString; +import io.undertow.util.Methods; import io.undertow.util.StatusCodes; /** @@ -57,6 +58,7 @@ public class ServerResponse protected T entity; protected Throwable throwable; // protected Class jsonContext; + protected HttpString method = null; protected IoCallback ioCallback; protected boolean hasCookies = false; protected boolean hasHeaders = false; @@ -179,29 +181,7 @@ public void setStatus(Response.Status status) this.status = status.getStatusCode(); } - /** - * @param contentType - * the contentType to set - */ - protected void setContentType(String contentType) - { - this.contentType = contentType; - - if (this.contentType.equals(javax.ws.rs.core.MediaType.APPLICATION_JSON)) - { - if (!this.preprocessed) - { - this.processJson = true; - } - } - else if (this.contentType.equals(javax.ws.rs.core.MediaType.APPLICATION_XML)) - { - if (!this.preprocessed) - { - this.processXml = true; - } - } - } + public ServerResponse body(ByteBuffer body) { @@ -223,6 +203,18 @@ public ServerResponse entity(T entity) return this; } + public ServerResponse method(HttpString method) + { + this.method = method; + return this; + } + + public ServerResponse method(String method) + { + this.method = Methods.fromString(method); + return this; + } + public ServerResponse lastModified(Date date) { this.headers.put(Headers.LAST_MODIFIED, date.getTime()); @@ -279,21 +271,47 @@ public ServerResponse cookie(String cookieName, Cookie cookie) return this; } + + /** + * @param contentType + * the contentType to set + */ + protected void setContentType(String contentType) + { + this.contentType = contentType; + + if (this.contentType.equals(javax.ws.rs.core.MediaType.APPLICATION_JSON)) + { + if (!this.preprocessed) + { + this.processJson = true; + } + } + else if (this.contentType.equals(javax.ws.rs.core.MediaType.APPLICATION_XML)) + { + if (!this.preprocessed) + { + this.processXml = true; + } + } + } + public ServerResponse contentType(String contentType) { - this.setContentType(contentType); + this.setContentType(contentType); return this; } + public ServerResponse contentType(javax.ws.rs.core.MediaType mediaType) { - this.setContentType(mediaType.toString()); + this.setContentType(mediaType.toString()); return this; } public ServerResponse contentType(MediaType mediaType) { - this.setContentType(mediaType.contentType()); + this.setContentType(mediaType.contentType()); return this; } @@ -357,7 +375,7 @@ public ServerResponse redirect(String location) public ServerResponse redirectPermanently(String location) { this.location = location; - this.status = StatusCodes.MOVED_PERMANENTLY;; + this.status = StatusCodes.MOVED_PERMANENTLY; return this; } @@ -543,16 +561,13 @@ public void send(final HttpServerExchange exchange) throws RuntimeException public void send(final HttpHandler handler, final HttpServerExchange exchange) throws RuntimeException { - - - + final boolean hasBody = this.body != null; final boolean hasEntity = this.entity != null; final boolean hasError = this.throwable != null; exchange.setStatusCode(this.status); - if (hasError) { @@ -562,15 +577,26 @@ public void send(final HttpHandler handler, final HttpServerExchange exchange) t } if(this.location != null) - { - exchange.getResponseHeaders().put(Headers.LOCATION, this.location); + { + exchange.getResponseHeaders().put(Headers.LOCATION, this.location); } - if(this.status == StatusCodes.TEMPORARY_REDIRECT || this.status == StatusCodes.PERMANENT_REDIRECT) + if(this.status == StatusCodes.FOUND || this.status == StatusCodes.MOVED_PERMANENTLY || this.status == StatusCodes.TEMPORARY_REDIRECT || this.status == StatusCodes.PERMANENT_REDIRECT ) { - exchange.endExchange(); + if( (this.status == StatusCodes.FOUND || this.status == StatusCodes.MOVED_PERMANENTLY) && (this.method != null) ) + { + exchange.setRequestMethod(this.method); + } + + exchange.endExchange(); return; + } + + if (this.contentType != null) + { + exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, this.contentType); } + if (this.hasHeaders) { @@ -591,11 +617,9 @@ public void send(final HttpHandler handler, final HttpServerExchange exchange) t exchange.getResponseCookies().putAll(this.cookies); } + - if (this.contentType != null) - { - exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, this.contentType); - } + else if (!this.processJson && !this.processXml) { if (ServerPredicates.ACCEPT_JSON_PREDICATE.resolve(exchange)) diff --git a/src/main/resources/io/sinistral/proteus/swagger/index.html b/src/main/resources/io/sinistral/proteus/swagger/index.html index 7e96476..1f13f97 100644 --- a/src/main/resources/io/sinistral/proteus/swagger/index.html +++ b/src/main/resources/io/sinistral/proteus/swagger/index.html @@ -25,6 +25,11 @@ margin:0; background: #fafafa; } + + pre.microlight { + -webkit-font-smoothing: none; + } +