Skip to content

Commit

Permalink
General Improvements
Browse files Browse the repository at this point in the history
Services are bound to io.sinistral.proteus.services.BaseService.
Redirects can change the request method.
Cleaned up ServerResponse class.
  • Loading branch information
noboomu committed Oct 16, 2018
1 parent 60bae20 commit f2d5091
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 69 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
55 changes: 32 additions & 23 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -206,18 +206,15 @@
<artifactId>undertow-core</artifactId>
<version>${version.undertow}</version>
</dependency>
<!-- <dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jax-rs-ri</artifactId>
<version>2.0-rc1</version>
</dependency> -->
<!-- <dependency> <groupId>org.glassfish.jersey</groupId> <artifactId>jax-rs-ri</artifactId>
<version>2.0-rc1</version> </dependency> -->
<!-- https://mvnrepository.com/artifact/javax.ws.rs/javax.ws.rs-api -->
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.1.1</version>
</dependency>

<dependency>
<groupId>com.squareup</groupId>
<artifactId>javapoet</artifactId>
Expand Down Expand Up @@ -291,34 +288,46 @@
<artifactId>jackson-databind</artifactId>
<version>${version.jackson}</version>
</dependency>

<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.12</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-core</artifactId>
<version>1.5.12</version>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>


<!-- <dependency> <groupId>io.swagger.core.v3</groupId> <artifactId>swagger-annotations</artifactId>
<version>2.0.5</version> </dependency> <dependency> <groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-core</artifactId> <version>2.0.5</version> <exclusions>
<exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId>
</exclusion> </exclusions> </dependency> <dependency> <groupId>io.swagger.parser.v3</groupId>
<artifactId>swagger-parser</artifactId> <version>2.0.5</version> <exclusions>
<exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId>
</exclusion> </exclusions> </dependency> <dependency> <groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-jaxrs2</artifactId> <version>2.0.5</version> </dependency> -->

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.12</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-core</artifactId>
<version>1.5.12</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-parser</artifactId>
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/io/sinistral/proteus/ProteusApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -77,7 +76,7 @@ public class ProteusApplication

@Inject
@Named("registeredServices")
protected Set<Class<? extends Service>> registeredServices;
protected Set<Class<? extends BaseService>> registeredServices;

@Inject
protected RoutingHandler router;
Expand Down Expand Up @@ -142,7 +141,9 @@ public void start()

log.info("Starting services...");

Set<Service> services = registeredServices.stream().map(sc -> injector.getInstance(sc)).collect(Collectors.toSet());
Set<BaseService> services = registeredServices.stream().map(sc -> injector.getInstance(sc)).collect(Collectors.toSet());

injector = injector.createChildInjector(services);

serviceManager = new ServiceManager(services);

Expand Down Expand Up @@ -316,7 +317,7 @@ public void buildServer()

}

public ProteusApplication addService(Class<? extends Service> serviceClass)
public ProteusApplication addService(Class<? extends BaseService> serviceClass)
{
registeredServices.add(serviceClass);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -44,7 +45,7 @@ public class ApplicationModule extends AbstractModule

protected Set<EndpointInfo> registeredEndpoints = new TreeSet<>();
protected Set<Class<?>> registeredControllers = new HashSet<>();
protected Set<Class<? extends Service>> registeredServices = new HashSet<>();
protected Set<Class<? extends BaseService>> registeredServices = new HashSet<>();
protected Map<String,HandlerWrapper> registeredHandlerWrappers = new HashMap<>();

protected Config config;
Expand Down Expand Up @@ -110,7 +111,7 @@ protected void configure()
{
}).annotatedWith(Names.named("registeredEndpoints")).toInstance(registeredEndpoints);

this.bind(new TypeLiteral<Set<Class<? extends Service>>>()
this.bind(new TypeLiteral<Set<Class<? extends BaseService>>>()
{
}).annotatedWith(Names.named("registeredServices")).toInstance(registeredServices);

Expand Down
102 changes: 63 additions & 39 deletions src/main/java/io/sinistral/proteus/server/ServerResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -57,6 +58,7 @@ public class ServerResponse<T>
protected T entity;
protected Throwable throwable;
// protected Class<? extends JsonContext> jsonContext;
protected HttpString method = null;
protected IoCallback ioCallback;
protected boolean hasCookies = false;
protected boolean hasHeaders = false;
Expand Down Expand Up @@ -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<T> body(ByteBuffer body)
{
Expand All @@ -223,6 +203,18 @@ public ServerResponse<T> entity(T entity)
return this;
}

public ServerResponse<T> method(HttpString method)
{
this.method = method;
return this;
}

public ServerResponse<T> method(String method)
{
this.method = Methods.fromString(method);
return this;
}

public ServerResponse<T> lastModified(Date date)
{
this.headers.put(Headers.LAST_MODIFIED, date.getTime());
Expand Down Expand Up @@ -279,21 +271,47 @@ public ServerResponse<T> 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<T> contentType(String contentType)
{
this.setContentType(contentType);
this.setContentType(contentType);
return this;
}


public ServerResponse<T> contentType(javax.ws.rs.core.MediaType mediaType)
{
this.setContentType(mediaType.toString());
this.setContentType(mediaType.toString());
return this;
}

public ServerResponse<T> contentType(MediaType mediaType)
{
this.setContentType(mediaType.contentType());
this.setContentType(mediaType.contentType());
return this;
}

Expand Down Expand Up @@ -357,7 +375,7 @@ public ServerResponse<T> redirect(String location)
public ServerResponse<T> redirectPermanently(String location)
{
this.location = location;
this.status = StatusCodes.MOVED_PERMANENTLY;;
this.status = StatusCodes.MOVED_PERMANENTLY;
return this;
}

Expand Down Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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))
Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/io/sinistral/proteus/swagger/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
margin:0;
background: #fafafa;
}

pre.microlight {
-webkit-font-smoothing: none;
}

</style>


Expand Down

0 comments on commit f2d5091

Please sign in to comment.