author | xyin |
Mon, 06 May 2019 14:23:32 +0800 | |
branch | JDK-8210696-branch |
changeset 57351 | b9e5f8090688 |
parent 57345 | ff884a2f247b |
permissions | -rw-r--r-- |
57345 | 1 |
/* |
2 |
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
22 |
*/ |
|
23 |
||
24 |
import java.io.IOException; |
|
25 |
import java.io.OutputStream; |
|
26 |
import java.net.ServerSocket; |
|
27 |
import java.nio.file.Paths; |
|
28 |
import java.util.ArrayList; |
|
29 |
import java.util.Arrays; |
|
30 |
import java.util.HashMap; |
|
31 |
import java.util.List; |
|
32 |
import java.util.Map; |
|
33 |
import java.util.Scanner; |
|
34 |
import java.util.regex.MatchResult; |
|
35 |
||
36 |
/* |
|
37 |
* An enhanced dummy LDAP server which can playback captured LDAP messages. |
|
38 |
* |
|
39 |
* Loads a sequence of LDAP messages from a capture file into its cache. |
|
40 |
* It listens for LDAP requests, finds a match in its cache and sends the |
|
41 |
* corresponding LDAP responses. |
|
42 |
* |
|
43 |
* The capture file contains an LDAP protocol exchange in the hexadecimal |
|
44 |
* dump format emitted by sun.misc.HexDumpEncoder: |
|
45 |
* |
|
46 |
* xxxx: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff ................ |
|
47 |
* |
|
48 |
* Typically, LDAP protocol exchange is generated by running the LDAP client |
|
49 |
* application program against a real LDAP server and setting the JNDI/LDAP |
|
50 |
* environment property: com.sun.jndi.ldap.trace.ber to activate LDAP message |
|
51 |
* tracing. |
|
52 |
*/ |
|
53 |
public class LdapPlaybackServer extends BaseLdapServer { |
|
54 |
||
55 |
/* |
|
56 |
* A cache of LDAP requests and responses. |
|
57 |
* Messages with the same ID are stored in a list. |
|
58 |
* The first element in the list is the LDAP request, |
|
59 |
* the remaining elements are the LDAP responses. |
|
60 |
*/ |
|
61 |
private final Map<Integer, List<byte[]>> cache = new HashMap<>(); |
|
62 |
||
63 |
public LdapPlaybackServer(ServerSocket serverSocket, String fileName) { |
|
64 |
super(serverSocket); |
|
65 |
setDebugLevel(DebugLevel.CUSTOMIZE, this.getClass()); |
|
66 |
setCommonRequestHandler(this::handleRequest); |
|
57351
b9e5f8090688
JDK-8210696-branch: fix loadCaptureFile exception swallowing
xyin
parents:
57345
diff
changeset
|
67 |
try { |
b9e5f8090688
JDK-8210696-branch: fix loadCaptureFile exception swallowing
xyin
parents:
57345
diff
changeset
|
68 |
debug("Loading LDAP cache from: " + fileName); |
b9e5f8090688
JDK-8210696-branch: fix loadCaptureFile exception swallowing
xyin
parents:
57345
diff
changeset
|
69 |
loadCaptureFile(fileName); |
b9e5f8090688
JDK-8210696-branch: fix loadCaptureFile exception swallowing
xyin
parents:
57345
diff
changeset
|
70 |
} catch (IOException e) { |
b9e5f8090688
JDK-8210696-branch: fix loadCaptureFile exception swallowing
xyin
parents:
57345
diff
changeset
|
71 |
throw new RuntimeException("ERROR: failed to load LDAP cache", e); |
b9e5f8090688
JDK-8210696-branch: fix loadCaptureFile exception swallowing
xyin
parents:
57345
diff
changeset
|
72 |
} |
57345 | 73 |
} |
74 |
||
75 |
/* |
|
76 |
* Load a capture file containing an LDAP protocol exchange in the |
|
77 |
* hexadecimal dump format emitted by sun.misc.HexDumpEncoder: |
|
78 |
* |
|
79 |
* xxxx: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff ................ |
|
80 |
*/ |
|
81 |
private void loadCaptureFile(String filename) throws IOException { |
|
82 |
StringBuilder hexString = new StringBuilder(); |
|
83 |
String pattern = "(....): (..) (..) (..) (..) (..) (..) (..) (..) (..) (..) (..) (..) (..) (..) (..) (..).*"; |
|
84 |
String preLineNum = ""; |
|
85 |
||
86 |
try (Scanner fileScanner = new Scanner(Paths.get(filename))) { |
|
87 |
while (fileScanner.hasNextLine()) { |
|
88 |
||
89 |
try (Scanner lineScanner = new Scanner( |
|
90 |
fileScanner.nextLine())) { |
|
91 |
if (lineScanner.findInLine(pattern) == null) { |
|
92 |
preLineNum = ""; |
|
93 |
continue; |
|
94 |
} |
|
95 |
MatchResult result = lineScanner.match(); |
|
96 |
for (int i = 1; i <= result.groupCount(); i++) { |
|
97 |
String digits = result.group(i); |
|
98 |
if (digits.length() == 4) { |
|
99 |
if (digits.equals("0000") && !preLineNum |
|
100 |
.equalsIgnoreCase( |
|
101 |
"FFF0")) { // start-of-message |
|
102 |
if (hexString.length() > 0) { |
|
103 |
addToCache(hexString.toString()); |
|
104 |
hexString = new StringBuilder(); |
|
105 |
} |
|
106 |
} |
|
107 |
preLineNum = digits; |
|
108 |
continue; |
|
109 |
} else if (digits.equals(" ")) { // short message |
|
110 |
continue; |
|
111 |
} |
|
112 |
hexString.append(digits); |
|
113 |
} |
|
114 |
} |
|
115 |
} |
|
116 |
} |
|
117 |
if (!hexString.toString().isEmpty()) { |
|
118 |
addToCache(hexString.toString()); |
|
119 |
} |
|
120 |
} |
|
121 |
||
122 |
/* |
|
123 |
* Add an LDAP encoding to the cache (by messageID key). |
|
124 |
*/ |
|
125 |
private void addToCache(String hexString) { |
|
126 |
LdapMessage message = new LdapMessage(hexString); |
|
127 |
byte[] encoding = message.getMessages(); |
|
128 |
int messageID = message.getMessageID(); |
|
129 |
List<byte[]> encodings = cache.get(messageID); |
|
130 |
if (encodings == null) { |
|
131 |
encodings = new ArrayList<>(); |
|
132 |
} |
|
133 |
debug(" adding LDAP " + message.getOperation() + " with message ID " |
|
134 |
+ messageID + " to the cache"); |
|
135 |
encodings.add(encoding); |
|
136 |
cache.put(messageID, encodings); |
|
137 |
} |
|
138 |
||
139 |
@Override |
|
140 |
public void stopServer() { |
|
141 |
debug("force stopping server"); |
|
142 |
super.stopServer(); |
|
143 |
} |
|
144 |
||
145 |
private void handleRequest(LdapMessage request, OutputStream out) |
|
146 |
throws IOException { |
|
147 |
int messageID = request.getMessageID(); |
|
148 |
debug("received LDAP " + request.getOperation() + " [message ID " |
|
149 |
+ messageID + "]"); |
|
150 |
||
151 |
List<byte[]> encodings = cache.get(messageID); |
|
152 |
if (encodings == null || (!Arrays |
|
153 |
.equals(request.getMessages(), encodings.get(0)))) { |
|
154 |
throw new RuntimeException( |
|
155 |
"LDAPServer: ERROR: received an LDAP " + request |
|
156 |
.getOperation() + " (ID=" + messageID |
|
157 |
+ ") not present in cache"); |
|
158 |
} |
|
159 |
||
160 |
for (int i = 1; i < encodings.size(); i++) { |
|
161 |
// skip the request (at index 0) |
|
162 |
byte[] response = encodings.get(i); |
|
163 |
out.write(response, 0, response.length); |
|
164 |
LdapMessage responseMsg = new LdapMessage(response); |
|
165 |
debug("Sent LDAP " + responseMsg.getOperation() + " [message ID " |
|
166 |
+ responseMsg.getMessageID() + "]"); |
|
167 |
} |
|
168 |
} |
|
169 |
||
170 |
@Override |
|
171 |
public void run() { |
|
172 |
try { |
|
173 |
debug("listening on port " + getPort()); |
|
174 |
super.run(); |
|
175 |
} catch (Exception e) { |
|
176 |
debug("ERROR: " + e); |
|
177 |
e.printStackTrace(); |
|
178 |
} |
|
179 |
} |
|
180 |
} |