17
17
package net .sberg .openkim .gateway ;
18
18
19
19
import com .google .common .base .Preconditions ;
20
- import org .apache .james .protocols .api .Encryption ;
20
+ import io .netty .channel .ChannelInboundHandlerAdapter ;
21
+ import io .netty .channel .DefaultEventLoopGroup ;
21
22
import org .apache .james .protocols .api .Protocol ;
22
23
import org .apache .james .protocols .netty .*;
23
- import org .jboss .netty .channel .ChannelPipelineFactory ;
24
- import org .jboss .netty .channel .ChannelUpstreamHandler ;
25
- import org .jboss .netty .channel .group .ChannelGroup ;
26
- import org .jboss .netty .handler .execution .ExecutionHandler ;
27
- import org .jboss .netty .util .HashedWheelTimer ;
28
24
29
25
import javax .inject .Inject ;
30
26
import java .util .Optional ;
31
27
32
28
public class GatewayNettyServer extends AbstractAsyncServer {
33
- protected final Encryption secure ;
34
- protected final Protocol protocol ;
35
- private final ChannelHandlerFactory frameHandlerFactory ;
36
- private final HashedWheelTimer hashedWheelTimer ;
37
- private ExecutionHandler eHandler ;
38
- private ChannelUpstreamHandler coreHandler ;
39
- private int maxCurConnections ;
40
- private int maxCurConnectionsPerIP ;
41
-
42
- private GatewayNettyServer (Protocol protocol , Encryption secure , ChannelHandlerFactory frameHandlerFactory , HashedWheelTimer hashedWheelTimer ) {
43
- this .protocol = protocol ;
44
- this .secure = secure ;
45
- this .frameHandlerFactory = frameHandlerFactory ;
46
- this .hashedWheelTimer = hashedWheelTimer ;
47
- }
48
-
49
- protected ChannelUpstreamHandler createCoreHandler () {
50
- return new GatewayBasicChannelUpstreamHandler (new ProtocolMDCContextFactory .Standard (), this .protocol , this .secure );
51
- }
52
-
53
- public synchronized void bind () throws Exception {
54
- this .coreHandler = this .createCoreHandler ();
55
- super .bind ();
56
- }
57
-
58
- private ChannelHandlerFactory getFrameHandlerFactory () {
59
- return this .frameHandlerFactory ;
60
- }
61
-
62
- protected ChannelPipelineFactory createPipelineFactory (ChannelGroup group ) {
63
- return new AbstractSSLAwareChannelPipelineFactory (
64
- this .getTimeout (),
65
- this .maxCurConnections ,
66
- this .maxCurConnectionsPerIP ,
67
- group ,
68
- this .secure ,
69
- this .eHandler ,
70
- this .getFrameHandlerFactory (),
71
- this .hashedWheelTimer
72
- ) {
73
- protected ChannelUpstreamHandler createHandler () {
74
- return GatewayNettyServer .this .coreHandler ;
75
- }
76
- };
77
- }
78
-
79
29
public static class Factory {
80
- private final HashedWheelTimer hashedWheelTimer ;
81
30
private Protocol protocol ;
31
+ private boolean proxyRequired ;
82
32
private Optional <Encryption > secure ;
83
33
private Optional <ChannelHandlerFactory > frameHandlerFactory ;
84
34
85
35
@ Inject
86
- public Factory (HashedWheelTimer hashedWheelTimer ) {
87
- this .hashedWheelTimer = hashedWheelTimer ;
88
- this .secure = Optional .empty ();
89
- this .frameHandlerFactory = Optional .empty ();
36
+ public Factory () {
37
+ secure = Optional .empty ();
38
+ frameHandlerFactory = Optional .empty ();
90
39
}
91
40
92
41
public GatewayNettyServer .Factory protocol (Protocol protocol ) {
@@ -100,19 +49,82 @@ public GatewayNettyServer.Factory secure(Encryption secure) {
100
49
return this ;
101
50
}
102
51
52
+ public GatewayNettyServer .Factory proxyRequired (boolean proxyRequired ) {
53
+ this .proxyRequired = proxyRequired ;
54
+ return this ;
55
+ }
56
+
103
57
public GatewayNettyServer .Factory frameHandlerFactory (ChannelHandlerFactory frameHandlerFactory ) {
104
58
this .frameHandlerFactory = Optional .ofNullable (frameHandlerFactory );
105
59
return this ;
106
60
}
107
61
108
62
public GatewayNettyServer build () {
109
- Preconditions .checkState (this .protocol != null , "'protocol' is mandatory" );
110
- return new GatewayNettyServer (
111
- this .protocol ,
112
- this .secure .orElse (null ),
113
- this .frameHandlerFactory .orElse (new LineDelimiterBasedChannelHandlerFactory (8192 )),
114
- this .hashedWheelTimer
115
- );
63
+ Preconditions .checkState (protocol != null , "'protocol' is mandatory" );
64
+ return new GatewayNettyServer (protocol ,
65
+ secure .orElse (null ),
66
+ proxyRequired ,
67
+ frameHandlerFactory .orElse (new LineDelimiterBasedChannelHandlerFactory (AbstractChannelPipelineFactory .MAX_LINE_LENGTH )));
68
+ }
69
+ }
70
+
71
+ protected final Encryption secure ;
72
+ protected final Protocol protocol ;
73
+ private final ChannelHandlerFactory frameHandlerFactory ;
74
+ private int maxCurConnections ;
75
+ private int maxCurConnectionsPerIP ;
76
+ private boolean proxyRequired ;
77
+
78
+ private GatewayNettyServer (Protocol protocol , Encryption secure , boolean proxyRequired , ChannelHandlerFactory frameHandlerFactory ) {
79
+ this .protocol = protocol ;
80
+ this .secure = secure ;
81
+ this .proxyRequired = proxyRequired ;
82
+ this .frameHandlerFactory = frameHandlerFactory ;
83
+ }
84
+
85
+ public void setMaxConcurrentConnections (int maxCurConnections ) {
86
+ if (isBound ()) {
87
+ throw new IllegalStateException ("Server running already" );
116
88
}
89
+ this .maxCurConnections = maxCurConnections ;
90
+ }
91
+
92
+ public void setMaxConcurrentConnectionsPerIP (int maxCurConnectionsPerIP ) {
93
+ if (isBound ()) {
94
+ throw new IllegalStateException ("Server running already" );
95
+ }
96
+ this .maxCurConnectionsPerIP = maxCurConnectionsPerIP ;
97
+ }
98
+
99
+ protected ChannelInboundHandlerAdapter createCoreHandler () {
100
+ return new GatewayBasicChannelInboundHandler (new ProtocolMDCContextFactory .Standard (), protocol , secure , proxyRequired );
101
+ }
102
+
103
+ @ Override
104
+ public synchronized void bind () throws Exception {
105
+ super .bind ();
106
+ }
107
+
108
+ private ChannelHandlerFactory getFrameHandlerFactory () {
109
+ return frameHandlerFactory ;
110
+ }
111
+
112
+ @ Override
113
+ protected AbstractChannelPipelineFactory createPipelineFactory () {
114
+ return new AbstractSSLAwareChannelPipelineFactory (
115
+ getTimeout (),
116
+ maxCurConnections ,
117
+ maxCurConnectionsPerIP ,
118
+ proxyRequired ,
119
+ secure ,
120
+ getFrameHandlerFactory (),
121
+ new DefaultEventLoopGroup (16 )
122
+ ) {
123
+ @ Override
124
+ protected ChannelInboundHandlerAdapter createHandler () {
125
+ return createCoreHandler ();
126
+ }
127
+ };
128
+
117
129
}
118
130
}
0 commit comments