/
JaxRsRoute.java
137 lines (111 loc) · 4.83 KB
/
JaxRsRoute.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package net.trajano.ms.engine;
import static java.util.Collections.singletonMap;
import java.net.URI;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import org.glassfish.jersey.internal.MapPropertiesDelegate;
import org.glassfish.jersey.server.ApplicationHandler;
import org.glassfish.jersey.server.ContainerRequest;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.ServerProperties;
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
import io.swagger.jaxrs.config.BeanConfig;
import io.swagger.jaxrs.listing.SwaggerSerializers;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
import net.trajano.ms.engine.internal.ApiListingResource;
import net.trajano.ms.engine.internal.VertxBlockingInputStream;
import net.trajano.ms.engine.internal.VertxBufferInputStream;
import net.trajano.ms.engine.internal.VertxSecurityContext;
import net.trajano.ms.engine.internal.VertxWebResponseWriter;
public class JaxRsRoute implements
Handler<RoutingContext> {
/**
* Constructs a new route for the given router to a JAX-RS application.
*
* @param router
* @param applicationClass
*/
public static void route(final Vertx vertx,
final Router router,
final Class<? extends Application> applicationClass) {
new JaxRsRoute(vertx, router, applicationClass);
}
private volatile ApplicationHandler appHandler;
private final URI baseUri;
private final Vertx vertx;
private JaxRsRoute(final Vertx vertx,
final Router router,
final Class<? extends Application> applicationClass) {
this.vertx = vertx;
final ResourceConfig resourceConfig = ResourceConfig.forApplicationClass(applicationClass);
resourceConfig.register(JacksonJaxbJsonProvider.class);
resourceConfig.register(ApiListingResource.class);
resourceConfig.register(SwaggerSerializers.class);
final String resourcePackage = applicationClass.getPackage().getName();
resourceConfig.addProperties(singletonMap(ServerProperties.PROVIDER_PACKAGES, resourcePackage));
resourceConfig.addProperties(singletonMap(ServerProperties.TRACING, "ALL"));
final ApplicationPath annotation = applicationClass.getAnnotation(ApplicationPath.class);
if (annotation != null) {
baseUri = URI.create(annotation.value() + "/").normalize();
} else {
baseUri = URI.create("/");
}
final BeanConfig beanConfig = new BeanConfig();
beanConfig.setResourcePackage(resourcePackage);
beanConfig.setScan(true);
beanConfig.setBasePath(baseUri.getPath());
beanConfig.scanAndRead();
beanConfig.getSwagger();
appHandler = new ApplicationHandler(resourceConfig);
router.route(baseUri.getPath() + "*").handler(this);
}
@Override
public void handle(final RoutingContext context) {
final HttpServerRequest event = context.request();
final URI requestUri = URI.create(event.absoluteURI());
final ContainerRequest request = new ContainerRequest(baseUri, requestUri, event.method().name(), new VertxSecurityContext(event), new MapPropertiesDelegate());
event.headers().entries().forEach(entry -> {
request.getHeaders().add(entry.getKey(), entry.getValue());
});
request.setWriter(new VertxWebResponseWriter(event.response()));
final String contentLengthString = event.getHeader("Content-Length");
final int contentLength;
if (contentLengthString != null) {
contentLength = Integer.parseInt(contentLengthString);
} else {
contentLength = 0;
}
if (contentLength == 0) {
final Buffer body = Buffer.buffer();
event
.endHandler(aVoid -> {
request.setEntityStream(new VertxBufferInputStream(body));
appHandler.handle(request);
});
} else if (contentLength < 1024) {
event
.bodyHandler(body -> {
request.setEntityStream(new VertxBufferInputStream(body));
appHandler.handle(request);
});
} else {
final VertxBlockingInputStream is = new VertxBlockingInputStream(event);
event
.handler(buffer -> {
is.populate(buffer);
})
.endHandler(aVoid -> is.end());
vertx.executeBlocking(future -> {
request.setEntityStream(is);
appHandler.handle(request);
future.complete();
}, false, result -> {
});
}
}
}