1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.forgerock.opendj.examples;
19
20 import static org.forgerock.opendj.ldap.LDAPListener.*;
21
22 import java.io.FileInputStream;
23 import java.io.IOException;
24
25 import javax.net.ssl.SSLContext;
26
27 import org.forgerock.opendj.ldap.Connections;
28 import org.forgerock.opendj.ldap.LdapException;
29 import org.forgerock.opendj.ldap.KeyManagers;
30 import org.forgerock.opendj.ldap.LDAPClientContext;
31 import org.forgerock.opendj.ldap.LDAPListener;
32 import org.forgerock.opendj.ldap.MemoryBackend;
33 import org.forgerock.opendj.ldap.ResultCode;
34 import org.forgerock.opendj.ldap.SSLContextBuilder;
35 import org.forgerock.opendj.ldap.ServerConnection;
36 import org.forgerock.opendj.ldap.ServerConnectionFactory;
37 import org.forgerock.opendj.ldap.TrustManagers;
38 import org.forgerock.opendj.ldif.LDIFEntryReader;
39 import org.forgerock.util.Options;
40
41 import com.forgerock.reactive.ServerConnectionFactoryAdapter;
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 public final class Server {
58
59
60
61
62
63
64
65
66 public static void main(final String[] args) {
67 if (args.length != 3 && args.length != 6) {
68 System.err.println("Usage: listenAddress listenPort ldifFile "
69 + "[keyStoreFile keyStorePassword certNickname]");
70 System.exit(1);
71 }
72
73
74 final String localAddress = args[0];
75 final int localPort = Integer.parseInt(args[1]);
76 final String ldifFileName = args[2];
77 final String keyStoreFileName = (args.length == 6) ? args[3] : null;
78 final String keyStorePassword = (args.length == 6) ? args[4] : null;
79 final String certNickname = (args.length == 6) ? args[5] : null;
80
81
82 final MemoryBackend backend;
83 try {
84 backend = new MemoryBackend(new LDIFEntryReader(new FileInputStream(ldifFileName)));
85 } catch (final IOException e) {
86 System.err.println(e.getMessage());
87 System.exit(ResultCode.CLIENT_SIDE_PARAM_ERROR.intValue());
88 return;
89 }
90
91
92 final ServerConnectionFactory<LDAPClientContext, Integer> connectionHandler =
93 Connections.newServerConnectionFactory(backend);
94
95
96 LDAPListener listener = null;
97 try {
98 final Options options = Options.defaultOptions().set(CONNECT_MAX_BACKLOG, 4096);
99
100 if (keyStoreFileName != null) {
101
102
103 final SSLContext sslContext =
104 new SSLContextBuilder().setKeyManager(
105 KeyManagers.useSingleCertificate(certNickname, KeyManagers
106 .useKeyStoreFile(keyStoreFileName, keyStorePassword
107 .toCharArray(), null))).setTrustManager(
108 TrustManagers.trustAll()).getSSLContext();
109
110 final ServerConnectionFactory<LDAPClientContext, Integer> sslWrapper =
111 new ServerConnectionFactory<LDAPClientContext, Integer>() {
112
113 @Override
114 public ServerConnection<Integer> handleAccept(final LDAPClientContext clientContext)
115 throws LdapException {
116 clientContext.enableTLS(sslContext.createSSLEngine(), false);
117 return connectionHandler.handleAccept(clientContext);
118 }
119 };
120
121 listener = new LDAPListener(localAddress, localPort,
122 new ServerConnectionFactoryAdapter(options.get(LDAP_DECODE_OPTIONS), sslWrapper), options);
123 } else {
124
125 listener = new LDAPListener(localAddress, localPort,
126 new ServerConnectionFactoryAdapter(options.get(LDAP_DECODE_OPTIONS), connectionHandler),
127 options);
128 }
129 System.out.println("Press any key to stop the server...");
130 System.in.read();
131 } catch (final Exception e) {
132 System.out.println("Error listening on " + localAddress + ":" + localPort);
133 e.printStackTrace();
134 } finally {
135 if (listener != null) {
136 listener.close();
137 }
138 }
139 }
140
141 private Server() {
142
143 }
144 }