-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpServer.java
271 lines (236 loc) · 9.09 KB
/
HttpServer.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package info.unterrainer.commons.httpserver;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import info.unterrainer.commons.httpserver.accessmanager.HttpAccessManager;
import info.unterrainer.commons.httpserver.accessmanager.UserAccessInterceptor;
import info.unterrainer.commons.httpserver.daos.CoreDaoProvider;
import info.unterrainer.commons.httpserver.enums.Attribute;
import info.unterrainer.commons.httpserver.enums.ResponseType;
import info.unterrainer.commons.httpserver.exceptions.HttpException;
import info.unterrainer.commons.httpserver.exceptions.NotFoundException;
import info.unterrainer.commons.httpserver.exceptions.UnauthorizedException;
import info.unterrainer.commons.httpserver.handlers.AppNameHandler;
import info.unterrainer.commons.httpserver.handlers.AppVersionHandler;
import info.unterrainer.commons.httpserver.handlers.DateTimeHandler;
import info.unterrainer.commons.httpserver.handlers.HealthHandler;
import info.unterrainer.commons.httpserver.handlers.PostmanCollectionHandler;
import info.unterrainer.commons.httpserver.jsons.MessageJson;
import info.unterrainer.commons.jreutils.ShutdownHook;
import info.unterrainer.commons.rdbutils.entities.BasicJpa;
import info.unterrainer.commons.serialization.jsonmapper.JsonMapper;
import info.unterrainer.commons.serialization.jsons.BasicJson;
import info.unterrainer.commons.serialization.objectmapper.ObjectMapper;
import io.javalin.Javalin;
import io.javalin.core.security.Role;
import io.javalin.http.Context;
import io.javalin.http.Handler;
import io.javalin.http.HandlerType;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import okio.BufferedSource;
import okio.GzipSource;
import okio.Okio;
@Slf4j
public class HttpServer {
private static final String VERSION_FQN = "info.unterrainer.commons.httpserver.Information";
private Javalin javalin;
private HttpServerConfiguration config;
private String applicationName;
private JsonMapper jsonMapper;
private ObjectMapper objectMapper;
private final List<HandlerGroup> handlerGroups = new ArrayList<>();
private List<HandlerInstance> handlerInstances = new ArrayList<>();
ExecutorService executorService;
List<String> appVersionFqns;
@Getter
@Setter
private UserAccessInterceptor userAccessInterceptor;
@Getter
@Setter
private List<String> enumLookupFqnForInterceptorParser;
private HttpServer() {
}
@Builder
private HttpServer(final String configPrefix, final String applicationName, final ObjectMapper objectMapper,
final JsonMapper jsonMapper, final ExecutorService executorService, final String... appVersionFqns) {
config = HttpServerConfiguration.read(configPrefix);
this.applicationName = applicationName;
this.executorService = executorService;
this.appVersionFqns = new ArrayList<>(List.of(Optional.ofNullable(appVersionFqns).orElse(new String[0])));
if (!this.appVersionFqns.contains(VERSION_FQN))
this.appVersionFqns.add(VERSION_FQN);
if (executorService == null) {
this.executorService = new ThreadPoolExecutor(200, 200, 60L, TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>());
((ThreadPoolExecutor) this.executorService).allowCoreThreadTimeOut(true);
}
if (applicationName == null)
throw new IllegalArgumentException("The application-name cannot be null");
this.jsonMapper = jsonMapper;
if (this.jsonMapper == null)
this.jsonMapper = JsonMapper.create();
this.objectMapper = objectMapper;
if (this.objectMapper == null)
this.objectMapper = new ObjectMapper();
create();
}
public void addHandlerGroup(final HandlerGroup group) {
handlerGroups.add(group);
}
public <P extends BasicJpa, J extends BasicJson, E> GenericHandlerGroupBuilder<P, J, E> handlerGroupFor(
final Class<P> jpaType, final Class<J> jsonType, final CoreDaoProvider<P, E> coreDaoProvider) {
return new GenericHandlerGroupBuilder<>(this, jpaType, jsonType, coreDaoProvider.getCoreDao());
}
private void create() {
Server server = new Server();
ServerConnector connector = new ServerConnector(server);
connector.setHost(config.host());
connector.setPort(config.port());
server.setConnectors(new ServerConnector[] { connector });
javalin = Javalin.create(c -> {
c.server(() -> server)
.accessManager(new HttpAccessManager(config.keycloakHost(), config.keycloakRealm()))
.enableCorsForAllOrigins();
}).start(config.port());
javalin.before(ctx -> ctx.attribute(Attribute.JAVALIN_SERVER, this));
javalin.before(ctx -> ctx.attribute(Attribute.RESPONSE_TYPE, ResponseType.JSON));
javalin.before(ctx -> ctx.attribute(Attribute.RESPONSE_CONTENT_TYPE, "application/json"));
javalin.before(this::unzip);
javalin.after(this::render);
javalin.error(404, ctx -> {
ctx.result(jsonMapper.toStringFrom(MessageJson.builder()
.message(NotFoundException.HTTP_STATUS + " " + NotFoundException.HTTP_TEXT)
.build())).contentType("application/json").status(NotFoundException.HTTP_STATUS);
}).exception(Exception.class, (e, ctx) -> {
handleException(e, ctx);
});
get("/", new AppNameHandler(applicationName));
get("/version", new AppVersionHandler(appVersionFqns));
get("/datetime", new DateTimeHandler());
get("/health", new HealthHandler());
get("/postman", new PostmanCollectionHandler());
registerShutdownHook();
}
private void registerShutdownHook() {
ShutdownHook.register(() -> {
executorService.shutdown();
try {
if (!executorService.awaitTermination(1, TimeUnit.SECONDS))
executorService.shutdownNow();
} catch (InterruptedException e) {
executorService.shutdownNow();
}
});
}
private void handleException(final Exception e, final Context ctx) {
int status;
String message;
if (HttpException.class.isAssignableFrom(e.getClass())) {
HttpException h = (HttpException) e;
status = h.getHttpStatus();
message = status + " " + h.getHttpText();
} else {
status = 500;
message = "500 Internal Server Error";
if (e instanceof UnauthorizedException)
log.debug(e.getMessage());
else
log.error(e.getMessage(), e);
}
ctx.result(jsonMapper.toStringFrom(MessageJson.builder().message(message).build()))
.contentType("application/json")
.status(status);
}
public void start() {
for (HandlerGroup g : handlerGroups)
g.addHandlers(this);
for (HandlerInstance hi : handlerInstances)
javalin.addHandler(hi.handlerType(), hi.path(), hi.handler(), hi.roles());
}
public HttpServer get(final String path, final Handler handler, final Role... roles) {
handlerInstances.add(HandlerInstance.builder()
.path(path)
.handlerType(HandlerType.GET)
.handler(handler)
.roles(new HashSet<>(Arrays.asList(roles)))
.build());
return this;
}
public HttpServer put(final String path, final Handler handler, final Role... roles) {
handlerInstances.add(HandlerInstance.builder()
.path(path)
.handlerType(HandlerType.PUT)
.handler(handler)
.roles(new HashSet<>(Arrays.asList(roles)))
.build());
return this;
}
public HttpServer patch(final String path, final Handler handler, final Role... roles) {
handlerInstances.add(HandlerInstance.builder()
.path(path)
.handlerType(HandlerType.PATCH)
.handler(handler)
.roles(new HashSet<>(Arrays.asList(roles)))
.build());
return this;
}
public HttpServer post(final String path, final Handler handler, final Role... roles) {
handlerInstances.add(HandlerInstance.builder()
.path(path)
.handlerType(HandlerType.POST)
.handler(handler)
.roles(new HashSet<>(Arrays.asList(roles)))
.build());
return this;
}
public HttpServer delete(final String path, final Handler handler, final Role... roles) {
handlerInstances.add(HandlerInstance.builder()
.path(path)
.handlerType(HandlerType.DELETE)
.handler(handler)
.roles(new HashSet<>(Arrays.asList(roles)))
.build());
return this;
}
private void unzip(final Context ctx) throws IOException {
String body = ctx.body();
if (body == null)
return;
if (ctx.header("Content-Encoding") == "gzip") {
BufferedSource bs = Okio.buffer(Okio.source(ctx.bodyAsInputStream()));
GzipSource gzipSource = new GzipSource(bs);
body = Okio.buffer(gzipSource).readUtf8();
}
ctx.bodyAsInputStream().reset();
ctx.attribute(Attribute.REQUEST_BODY, body);
}
private void render(final Context ctx) throws IOException {
Object dto = ctx.attribute(Attribute.RESPONSE_OBJECT);
if (dto != null)
switch ((ResponseType) ctx.attribute(Attribute.RESPONSE_TYPE)) {
case JSON:
ctx.result(jsonMapper.toStringFrom(dto));
break;
default:
ctx.result((String) dto);
}
Integer status = ctx.attribute(Attribute.RESPONSE_STATUS);
if (status != null)
ctx.status(status);
String contentType = ctx.attribute(Attribute.RESPONSE_CONTENT_TYPE);
if (contentType != null)
ctx.contentType(contentType);
}
}