/
HttpServerBuilder.java
181 lines (160 loc) · 5.58 KB
/
HttpServerBuilder.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
package org.rakam.server.http;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableList.Builder;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.epoll.Epoll;
import io.netty.channel.epoll.EpollEventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.swagger.models.Operation;
import io.swagger.models.Swagger;
import io.swagger.util.PrimitiveType;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Predicate;
public class HttpServerBuilder
{
private Set<HttpService> httpServices;
private Set<WebSocketService> websocketServices;
private Swagger swagger;
private EventLoopGroup eventLoopGroup;
private ObjectMapper mapper;
private Map<Class, PrimitiveType> overridenMappings;
private Builder<PreprocessorEntry> jsonRequestPreprocessors = ImmutableList.builder();
private Builder<PostProcessorEntry> postProcessorEntryBuilder = ImmutableList.builder();
private boolean proxyProtocol;
private ExceptionHandler exceptionHandler;
private Map<String, IRequestParameterFactory> customRequestParameters;
private BiConsumer<Method, Operation> swaggerOperationConsumer;
private boolean useEpoll = true;
private long maximumBodySize = -1;
private boolean enableDebugMode;
public HttpServerBuilder setHttpServices(Set<HttpService> httpServices)
{
this.httpServices = httpServices;
return this;
}
/**
* Add processor for @JsonRequest methods.
*
* @param preprocessor preprocessor instance that will process request before the method.
* @param predicate applies preprocessor only methods that passes this predicate
* @return if true, method will not be called.
*/
public HttpServerBuilder addJsonPreprocessor(RequestPreprocessor preprocessor, Predicate<Method> predicate)
{
jsonRequestPreprocessors.add(new PreprocessorEntry(preprocessor, predicate));
return this;
}
public HttpServerBuilder addPostProcessor(ResponsePostProcessor processor)
{
postProcessorEntryBuilder.add(new PostProcessorEntry(processor));
return this;
}
public HttpServerBuilder setWebsocketServices(Set<WebSocketService> websocketServices)
{
this.websocketServices = websocketServices;
return this;
}
public HttpServerBuilder setSwagger(Swagger swagger)
{
this.swagger = swagger;
return this;
}
public HttpServerBuilder setEventLoopGroup(EventLoopGroup eventLoopGroup)
{
this.eventLoopGroup = eventLoopGroup;
return this;
}
public HttpServerBuilder setMapper(ObjectMapper mapper)
{
this.mapper = mapper;
return this;
}
public HttpServerBuilder setSwaggerOperationProcessor(BiConsumer<Method, Operation> consumer)
{
this.swaggerOperationConsumer = consumer;
return this;
}
public HttpServerBuilder setProxyProtocol(boolean proxyProtocol)
{
this.proxyProtocol = proxyProtocol;
return this;
}
public HttpServerBuilder setOverridenMappings(Map<Class, PrimitiveType> overridenMappings)
{
this.overridenMappings = overridenMappings;
return this;
}
public HttpServerBuilder setUseEpollIfPossible(boolean useEpoll)
{
this.useEpoll = useEpoll;
return this;
}
public HttpServerBuilder enableDebugMode(boolean enableDebugMode)
{
this.enableDebugMode = enableDebugMode;
return this;
}
public HttpServerBuilder setExceptionHandler(ExceptionHandler exceptionHandler)
{
this.exceptionHandler = exceptionHandler;
return this;
}
public HttpServerBuilder setMaximumBody(long bodySize)
{
if (bodySize < -1) {
throw new IllegalStateException();
}
this.maximumBodySize = bodySize;
return this;
}
public HttpServerBuilder setCustomRequestParameters(Map<String, IRequestParameterFactory> customRequestParameters)
{
this.customRequestParameters = customRequestParameters;
return this;
}
public HttpServer build()
{
if (eventLoopGroup == null) {
eventLoopGroup = useEpoll ? new EpollEventLoopGroup() : new NioEventLoopGroup();
}
if (swagger == null) {
swagger = new Swagger();
}
if (websocketServices == null) {
websocketServices = ImmutableSet.of();
}
if (customRequestParameters == null) {
customRequestParameters = ImmutableMap.of();
}
return new HttpServer(
httpServices,
websocketServices,
swagger,
eventLoopGroup,
jsonRequestPreprocessors.build(),
postProcessorEntryBuilder.build(),
mapper == null ? HttpServer.DEFAULT_MAPPER : mapper,
overridenMappings,
exceptionHandler,
customRequestParameters,
swaggerOperationConsumer,
useEpoll && Epoll.isAvailable(),
proxyProtocol,
maximumBodySize,
enableDebugMode);
}
public interface ExceptionHandler
{
void handle(RakamHttpRequest request, Throwable e);
}
public interface IRequestParameterFactory
{
IRequestParameter create(Method method);
}
}