1 /* |
1 /* |
2 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. |
7 * published by the Free Software Foundation. |
44 Server s = new Server(); |
43 Server s = new Server(); |
45 s.start(); |
44 s.start(); |
46 Thread.sleep(3000); |
45 Thread.sleep(3000); |
47 |
46 |
48 // Setup JNDI parameters |
47 // Setup JNDI parameters |
49 Hashtable env = new Hashtable(); |
48 Hashtable<String, Object> env = new Hashtable<>(); |
50 env.put(Context.INITIAL_CONTEXT_FACTORY, |
49 env.put(Context.INITIAL_CONTEXT_FACTORY, |
51 "com.sun.jndi.ldap.LdapCtxFactory"); |
50 "com.sun.jndi.ldap.LdapCtxFactory"); |
52 env.put(Context.PROVIDER_URL, "ldap://localhost:" + s.getPortNumber()); |
51 env.put(Context.PROVIDER_URL, "ldap://localhost:" + s.getPort()); |
53 |
52 |
54 try { |
53 try (s) { |
55 |
54 |
56 // Create initial context |
55 // Create initial context |
57 System.out.println("Client: connecting..."); |
56 System.out.println("Client: connecting..."); |
58 DirContext ctx = new InitialDirContext(env); |
57 DirContext ctx = new InitialDirContext(env); |
59 |
58 |
71 System.err.println("Failed: caught an unexpected Exception - " + e); |
70 System.err.println("Failed: caught an unexpected Exception - " + e); |
72 throw e; |
71 throw e; |
73 } |
72 } |
74 } |
73 } |
75 |
74 |
76 static class Server extends Thread { |
75 static class Server extends BaseLdapServer { |
77 |
76 |
78 private int serverPort = 0; |
|
79 private byte[] bindResponse = { |
77 private byte[] bindResponse = { |
80 0x30, 0x0C, 0x02, 0x01, 0x01, 0x61, 0x07, 0x0A, |
78 0x30, 0x0C, 0x02, 0x01, 0x01, 0x61, 0x07, 0x0A, |
81 0x01, 0x00, 0x04, 0x00, 0x04, 0x00 |
79 0x01, 0x00, 0x04, 0x00, 0x04, 0x00 |
82 }; |
80 }; |
83 private byte[] searchResponse = { |
81 private byte[] searchResponse = { |
84 0x30, 0x0C, 0x02, 0x01, 0x02, 0x65, 0x07, 0x0A, |
82 0x30, 0x0C, 0x02, 0x01, 0x02, 0x65, 0x07, 0x0A, |
85 0x01, 0x02, 0x04, 0x00, 0x04, 0x00 |
83 0x01, 0x02, 0x04, 0x00, 0x04, 0x00 |
86 }; |
84 }; |
87 |
85 |
88 Server() { |
86 Server() throws IOException { |
89 } |
87 setDebugLevel(DebugLevel.FULL); |
90 |
88 setCommonRequestHandler((msg, out) -> { |
91 public int getPortNumber() { |
89 switch (msg.getOperation()) { |
92 return serverPort; |
90 case BIND_REQUEST: |
93 } |
91 // Write an LDAP BindResponse |
94 |
92 debug("writing response..."); |
95 public void run() { |
93 out.write(bindResponse); |
96 try { |
94 break; |
97 ServerSocket serverSock = new ServerSocket(0); |
95 case SEARCH_REQUEST: |
98 serverPort = serverSock.getLocalPort(); |
96 // Write an LDAP SearchResponse |
99 System.out.println("Server: listening on port " + serverPort); |
97 debug("writing response..."); |
100 |
98 out.write(searchResponse); |
101 Socket socket = serverSock.accept(); |
99 break; |
102 System.out.println("Server: connection accepted"); |
100 default: |
103 |
101 break; |
104 InputStream in = socket.getInputStream(); |
|
105 OutputStream out = socket.getOutputStream(); |
|
106 |
|
107 // Read the LDAP BindRequest |
|
108 System.out.println("Server: reading request..."); |
|
109 while (in.read() != -1) { |
|
110 in.skip(in.available()); |
|
111 break; |
|
112 } |
102 } |
113 |
103 }); |
114 // Write an LDAP BindResponse |
|
115 System.out.println("Server: writing response..."); |
|
116 out.write(bindResponse); |
|
117 out.flush(); |
|
118 |
|
119 // Read the LDAP SearchRequest |
|
120 System.out.println("Server: reading request..."); |
|
121 while (in.read() != -1) { |
|
122 in.skip(in.available()); |
|
123 break; |
|
124 } |
|
125 |
|
126 // Write an LDAP SearchResponse |
|
127 System.out.println("Server: writing response..."); |
|
128 out.write(searchResponse); |
|
129 out.flush(); |
|
130 |
|
131 in.close(); |
|
132 out.close(); |
|
133 socket.close(); |
|
134 serverSock.close(); |
|
135 |
|
136 } catch (IOException e) { |
|
137 // ignore |
|
138 } |
|
139 } |
104 } |
140 } |
105 } |
141 } |
106 } |