-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathHttpInitializer.java
52 lines (44 loc) · 2.1 KB
/
HttpInitializer.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
package org.logstash.plugins.inputs.http;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpContentDecompressor;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.ssl.SslHandler;
import org.logstash.plugins.inputs.http.util.SslHandlerProvider;
import java.util.concurrent.ThreadPoolExecutor;
/**
* Created by joaoduarte on 11/10/2017.
*/
public class HttpInitializer extends ChannelInitializer<SocketChannel> {
private final IMessageHandler messageHandler;
private SslHandlerProvider sslHandlerProvider;
private final int maxContentLength;
private final HttpResponseStatus responseStatus;
private final String responseBody;
private final ThreadPoolExecutor executorGroup;
public HttpInitializer(IMessageHandler messageHandler, ThreadPoolExecutor executorGroup,
int maxContentLength, HttpResponseStatus responseStatus, String responseBody) {
this.messageHandler = messageHandler;
this.executorGroup = executorGroup;
this.maxContentLength = maxContentLength;
this.responseStatus = responseStatus;
this.responseBody = responseBody;
}
protected void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline pipeline = socketChannel.pipeline();
if(sslHandlerProvider != null) {
SslHandler sslHandler = sslHandlerProvider.getSslHandler(socketChannel.alloc());
pipeline.addLast(sslHandler);
}
pipeline.addLast(new HttpServerCodec());
pipeline.addLast(new HttpContentDecompressor());
pipeline.addLast(new HttpObjectAggregator(maxContentLength));
pipeline.addLast(new HttpServerHandler(messageHandler.copy(), executorGroup, responseStatus, responseBody));
}
public void enableSSL(SslHandlerProvider sslHandlerProvider) {
this.sslHandlerProvider = sslHandlerProvider;
}
}