Skip to content

Commit de49178

Browse files
committed
0.18.0: - umstieg auf apache james 3.8.0 bibliotheken
1 parent c979429 commit de49178

16 files changed

+199
-152
lines changed

pom.xml

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</parent>
1111
<groupId>net.sberg</groupId>
1212
<artifactId>openkim</artifactId>
13-
<version>0.17.2</version>
13+
<version>0.18.0</version>
1414
<name>openkim</name>
1515
<description>Open KIM Client Modul</description>
1616

@@ -66,7 +66,7 @@
6666
<dependency>
6767
<groupId>org.apache.james.protocols</groupId>
6868
<artifactId>protocols-netty</artifactId>
69-
<version>3.7.2</version>
69+
<version>3.8.0</version>
7070
<exclusions>
7171
<!-- exclude version 1.9 (with Vulnerabilities), newer version 1.10.0 is set later-->
7272
<exclusion>
@@ -78,12 +78,12 @@
7878
<dependency>
7979
<groupId>org.apache.james</groupId>
8080
<artifactId>metrics-logger</artifactId>
81-
<version>3.7.2</version>
81+
<version>3.8.0</version>
8282
</dependency>
8383
<dependency>
8484
<groupId>org.apache.james.protocols</groupId>
8585
<artifactId>protocols-smtp</artifactId>
86-
<version>3.7.2</version>
86+
<version>3.8.0</version>
8787
<exclusions>
8888
<exclusion>
8989
<groupId>org.apache.james</groupId>
@@ -94,7 +94,7 @@
9494
<dependency>
9595
<groupId>org.apache.james.protocols</groupId>
9696
<artifactId>protocols-pop3</artifactId>
97-
<version>3.7.2</version>
97+
<version>3.8.0</version>
9898
</dependency>
9999
<dependency>
100100
<groupId>org.bouncycastle</groupId>

src/main/java/net/sberg/openkim/common/FileUtils.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public static final File writeToFileDirectory(Message msg, String prefix, String
119119
return f;
120120
}
121121

122-
public static final File writeToFileDirectory(ByteArrayOutputStream byteArrayOutputStream, String prefix, String storageFolder) throws Exception {
122+
public static final File writeToFileDirectory(byte[] bytes, String prefix, String storageFolder) throws Exception {
123123
File f = new File(storageFolder);
124124
if (!f.exists()) {
125125
f.mkdirs();
@@ -128,7 +128,7 @@ public static final File writeToFileDirectory(ByteArrayOutputStream byteArrayOut
128128
f = new File(whereToSave);
129129
f.delete();
130130
OutputStream out = new FileOutputStream(new File(whereToSave));
131-
out.write(byteArrayOutputStream.toByteArray());
131+
out.write(bytes);
132132
out.flush();
133133
out.close();
134134
return f;

src/main/java/net/sberg/openkim/gateway/GatewayBasicChannelUpstreamHandler.java src/main/java/net/sberg/openkim/gateway/GatewayBasicChannelInboundHandler.java

+9-8
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,27 @@
1616
*/
1717
package net.sberg.openkim.gateway;
1818

19+
import io.netty.channel.ChannelHandlerContext;
1920
import net.sberg.openkim.gateway.pop3.Pop3GatewaySession;
2021
import net.sberg.openkim.gateway.smtp.SmtpGatewaySession;
21-
import org.apache.james.protocols.api.Encryption;
2222
import org.apache.james.protocols.api.Protocol;
2323
import org.apache.james.protocols.api.ProtocolSession;
24-
import org.apache.james.protocols.netty.BasicChannelUpstreamHandler;
24+
import org.apache.james.protocols.netty.BasicChannelInboundHandler;
25+
import org.apache.james.protocols.netty.Encryption;
2526
import org.apache.james.protocols.netty.ProtocolMDCContextFactory;
26-
import org.jboss.netty.channel.ChannelHandlerContext;
2727
import org.slf4j.Logger;
2828
import org.slf4j.LoggerFactory;
2929

30-
public class GatewayBasicChannelUpstreamHandler extends BasicChannelUpstreamHandler {
30+
public class GatewayBasicChannelInboundHandler extends BasicChannelInboundHandler {
3131

32-
private static final Logger log = LoggerFactory.getLogger(GatewayBasicChannelUpstreamHandler.class);
32+
private static final Logger log = LoggerFactory.getLogger(GatewayBasicChannelInboundHandler.class);
3333

34-
public GatewayBasicChannelUpstreamHandler(ProtocolMDCContextFactory mdcContextFactory, Protocol protocol, Encryption secure) {
35-
super(mdcContextFactory, protocol, secure);
34+
public GatewayBasicChannelInboundHandler(ProtocolMDCContextFactory mdcContextFactory, Protocol protocol, Encryption secure, boolean proxyRequired) {
35+
super(mdcContextFactory, protocol, secure, proxyRequired);
3636
}
3737

3838
protected void cleanup(ChannelHandlerContext ctx) {
39-
ProtocolSession session = (ProtocolSession) ctx.getAttachment();
39+
ProtocolSession session = (ProtocolSession) ctx.channel().attr(SESSION_ATTRIBUTE_KEY).getAndSet(null);
4040
if (session != null) {
4141
if (session instanceof SmtpGatewaySession) {
4242
try {
@@ -67,5 +67,6 @@ protected void cleanup(ChannelHandlerContext ctx) {
6767
session.resetState();
6868
session = null;
6969
}
70+
ctx.close();
7071
}
7172
}

src/main/java/net/sberg/openkim/gateway/GatewayKeystoreService.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
*/
1717
package net.sberg.openkim.gateway;
1818

19+
import io.netty.handler.ssl.util.SelfSignedCertificate;
1920
import net.sberg.openkim.common.FileUtils;
2021
import net.sberg.openkim.common.ICommonConstants;
21-
import org.jboss.netty.handler.ssl.util.SelfSignedCertificate;
2222
import org.slf4j.Logger;
2323
import org.slf4j.LoggerFactory;
2424
import org.springframework.beans.factory.annotation.Value;

src/main/java/net/sberg/openkim/gateway/GatewayNettyServer.java

+76-64
Original file line numberDiff line numberDiff line change
@@ -17,76 +17,25 @@
1717
package net.sberg.openkim.gateway;
1818

1919
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;
2122
import org.apache.james.protocols.api.Protocol;
2223
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;
2824

2925
import javax.inject.Inject;
3026
import java.util.Optional;
3127

3228
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-
7929
public static class Factory {
80-
private final HashedWheelTimer hashedWheelTimer;
8130
private Protocol protocol;
31+
private boolean proxyRequired;
8232
private Optional<Encryption> secure;
8333
private Optional<ChannelHandlerFactory> frameHandlerFactory;
8434

8535
@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();
9039
}
9140

9241
public GatewayNettyServer.Factory protocol(Protocol protocol) {
@@ -100,19 +49,82 @@ public GatewayNettyServer.Factory secure(Encryption secure) {
10049
return this;
10150
}
10251

52+
public GatewayNettyServer.Factory proxyRequired(boolean proxyRequired) {
53+
this.proxyRequired = proxyRequired;
54+
return this;
55+
}
56+
10357
public GatewayNettyServer.Factory frameHandlerFactory(ChannelHandlerFactory frameHandlerFactory) {
10458
this.frameHandlerFactory = Optional.ofNullable(frameHandlerFactory);
10559
return this;
10660
}
10761

10862
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");
11688
}
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+
117129
}
118130
}

src/main/java/net/sberg/openkim/gateway/pop3/Pop3Gateway.java

+41-8
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,35 @@
1818

1919
import jakarta.annotation.PostConstruct;
2020
import jakarta.annotation.PreDestroy;
21+
import net.sberg.openkim.common.EnumMailConnectionSecurity;
22+
import net.sberg.openkim.common.ICommonConstants;
2123
import net.sberg.openkim.gateway.GatewayNettyServer;
2224
import net.sberg.openkim.konfiguration.Konfiguration;
2325
import net.sberg.openkim.konfiguration.KonfigurationService;
2426
import net.sberg.openkim.log.LogService;
2527
import net.sberg.openkim.pipeline.PipelineService;
26-
import org.apache.james.protocols.api.Encryption;
28+
import org.apache.james.protocols.api.ClientAuth;
2729
import org.apache.james.protocols.api.Protocol;
2830
import org.apache.james.protocols.api.handler.WiringException;
29-
import org.jboss.netty.util.HashedWheelTimer;
31+
import org.apache.james.protocols.netty.Encryption;
3032
import org.slf4j.Logger;
3133
import org.slf4j.LoggerFactory;
3234
import org.springframework.beans.factory.annotation.Autowired;
35+
import org.springframework.beans.factory.annotation.Value;
3336
import org.springframework.stereotype.Service;
3437

38+
import javax.net.ssl.KeyManagerFactory;
39+
import javax.net.ssl.SSLContext;
40+
import java.io.File;
41+
import java.io.FileInputStream;
3542
import java.net.InetSocketAddress;
43+
import java.security.KeyStore;
3644

3745
@Service
3846
public class Pop3Gateway {
3947

4048
private static final Logger log = LoggerFactory.getLogger(Pop3Gateway.class);
4149

42-
private HashedWheelTimer hashedWheelTimer;
4350
private GatewayNettyServer server;
4451

4552
@Autowired
@@ -48,6 +55,8 @@ public class Pop3Gateway {
4855
private PipelineService pipelineService;
4956
@Autowired
5057
private KonfigurationService konfigurationService;
58+
@Value("${gatewaykeystore.password}")
59+
private String keyStorePwd;
5160

5261
private boolean startSucces = false;
5362

@@ -86,11 +95,7 @@ private void start() throws Exception {
8695
log.info("***POP3 Gateway activated***");
8796
}
8897

89-
if (hashedWheelTimer == null) {
90-
hashedWheelTimer = new HashedWheelTimer();
91-
}
92-
93-
server = new GatewayNettyServer.Factory(hashedWheelTimer)
98+
server = new GatewayNettyServer.Factory()
9499
.protocol(createProtocol(konfiguration)).secure(buildSSLContext(konfiguration))
95100
.build();
96101
server.setTimeout(konfiguration.getPop3GatewayIdleTimeoutInSeconds());
@@ -108,6 +113,34 @@ private void start() throws Exception {
108113

109114
private Encryption buildSSLContext(Konfiguration konfiguration) throws Exception {
110115
Encryption encryption = null;
116+
if (!konfiguration.getPop3GatewayConnectionSec().equals(EnumMailConnectionSecurity.NONE)) {
117+
FileInputStream fis = null;
118+
try {
119+
KeyStore ks = KeyStore.getInstance("PKCS12", "BC");
120+
fis = new FileInputStream(new File(ICommonConstants.BASE_DIR+ICommonConstants.OPENKIM_SERVER_KEYSTORE_FILENAME));
121+
ks.load(fis, keyStorePwd.toCharArray());
122+
123+
// Set up key manager factory to use our key store
124+
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
125+
kmf.init(ks, keyStorePwd.toCharArray());
126+
127+
// Initialize the SSLContext to work with our key managers.
128+
SSLContext context = SSLContext.getInstance("TLS");
129+
context.init(kmf.getKeyManagers(), null, null);
130+
if (konfiguration.getPop3GatewayConnectionSec().equals(EnumMailConnectionSecurity.STARTTLS)) {
131+
encryption = Encryption.createStartTls(context, null, null, ClientAuth.NONE);
132+
}
133+
else {
134+
encryption = Encryption.createTls(context, null, null, ClientAuth.NONE);
135+
}
136+
137+
} finally {
138+
if (fis != null) {
139+
fis.close();
140+
}
141+
return encryption;
142+
}
143+
}
111144
return encryption;
112145
}
113146

0 commit comments

Comments
 (0)