@@ -154,27 +154,10 @@ private Document getLog(String argument) {
154154 private Document handleAdminCommand (DatabaseCommand command , Document query ) {
155155 switch (command .getCommand ()) {
156156 case LIST_DATABASES : {
157- List <Document > databases = listDatabaseNames ().stream ()
158- .sorted ()
159- .map (databaseName -> {
160- MongoDatabase database = openOrCreateDatabase (databaseName );
161- Document dbObj = new Document ("name" , database .getDatabaseName ());
162- dbObj .put ("empty" , Boolean .valueOf (database .isEmpty ()));
163- return dbObj ;
164- })
165- .toList ();
166- Document response = new Document ();
167- response .put ("databases" , databases );
168- Utils .markOkay (response );
169- return response ;
157+ return handleListDatabases ();
170158 }
171159 case FIND : {
172- String collectionName = query .get (command .getQueryValue ()).toString ();
173- if (collectionName .equals ("$cmd.sys.inprog" )) {
174- return Utils .firstBatchCursorResponse (collectionName , new Document ("inprog" , Collections .emptyList ()));
175- } else {
176- throw new NoSuchCommandException (new Document (command .getQueryValue (), collectionName ).toString ());
177- }
160+ return handleFind (command , query );
178161 }
179162 case REPL_SET_GET_STATUS : {
180163 throw new NoReplicationEnabledException ();
@@ -191,13 +174,7 @@ private Document handleAdminCommand(DatabaseCommand command, Document query) {
191174 return successResponse ();
192175 }
193176 case CONNECTION_STATUS : {
194- Document response = new Document ();
195- response .append ("authInfo" , new Document ()
196- .append ("authenticatedUsers" , Collections .emptyList ())
197- .append ("authenticatedUserRoles" , Collections .emptyList ())
198- );
199- Utils .markOkay (response );
200- return response ;
177+ return handleConnectionStatus ();
201178 }
202179 case HOST_INFO : {
203180 return handleHostInfo ();
@@ -217,12 +194,47 @@ private Document handleAdminCommand(DatabaseCommand command, Document query) {
217194 }
218195 }
219196
197+ private Document handleListDatabases () {
198+ List <Document > databases = listDatabaseNames ().stream ()
199+ .sorted ()
200+ .map (databaseName -> {
201+ MongoDatabase database = openOrCreateDatabase (databaseName );
202+ Document dbObj = new Document ("name" , database .getDatabaseName ());
203+ dbObj .put ("empty" , Boolean .valueOf (database .isEmpty ()));
204+ return dbObj ;
205+ })
206+ .toList ();
207+ Document response = new Document ();
208+ response .put ("databases" , databases );
209+ Utils .markOkay (response );
210+ return response ;
211+ }
212+
213+ private static Document handleFind (DatabaseCommand command , Document query ) {
214+ String collectionName = query .get (command .getQueryValue ()).toString ();
215+ if (collectionName .equals ("$cmd.sys.inprog" )) {
216+ return Utils .firstBatchCursorResponse (collectionName , new Document ("inprog" , Collections .emptyList ()));
217+ } else {
218+ throw new NoSuchCommandException (new Document (command .getQueryValue (), collectionName ).toString ());
219+ }
220+ }
221+
220222 private static Document successResponse () {
221223 Document response = new Document ();
222224 Utils .markOkay (response );
223225 return response ;
224226 }
225227
228+ private static Document handleConnectionStatus () {
229+ Document response = new Document ();
230+ response .append ("authInfo" , new Document ()
231+ .append ("authenticatedUsers" , Collections .emptyList ())
232+ .append ("authenticatedUserRoles" , Collections .emptyList ())
233+ );
234+ Utils .markOkay (response );
235+ return response ;
236+ }
237+
226238 private Document handleHostInfo () {
227239 Document response = new Document ();
228240 String osName = System .getProperty ("os.name" );
@@ -312,31 +324,9 @@ private MongoCollection<?> resolveCollection(String namespace) {
312324 @ Override
313325 public Document handleCommand (Channel channel , String databaseName , DatabaseCommand command , Document query ) {
314326 return switch (command .getCommand ()) {
315- case WHATS_MY_URI -> {
316- Document response = new Document ();
317- InetSocketAddress remoteAddress = (InetSocketAddress ) channel .remoteAddress ();
318- response .put ("you" , remoteAddress .getAddress ().getHostAddress () + ":" + remoteAddress .getPort ());
319- Utils .markOkay (response );
320- yield response ;
321- }
322- case IS_MASTER -> {
323- Document response = new Document ("ismaster" , Boolean .TRUE );
324- response .put ("maxBsonObjectSize" , Integer .valueOf (BsonConstants .MAX_BSON_OBJECT_SIZE ));
325- response .put ("maxWriteBatchSize" , Integer .valueOf (MongoWireProtocolHandler .MAX_WRITE_BATCH_SIZE ));
326- response .put ("maxMessageSizeBytes" , Integer .valueOf (MongoWireProtocolHandler .MAX_MESSAGE_SIZE_BYTES ));
327- response .put ("maxWireVersion" , Integer .valueOf (version .getWireVersion ()));
328- response .put ("minWireVersion" , Integer .valueOf (0 ));
329- response .put ("localTime" , Instant .now (clock ));
330- Utils .markOkay (response );
331- yield response ;
332- }
333- case BUILD_INFO -> {
334- Document response = new Document ("version" , version .toVersionString ());
335- response .put ("versionArray" , version .getVersionArray ());
336- response .put ("maxBsonObjectSize" , Integer .valueOf (BsonConstants .MAX_BSON_OBJECT_SIZE ));
337- Utils .markOkay (response );
338- yield response ;
339- }
327+ case WHATS_MY_URI -> handleWhatsMyUri (channel );
328+ case IS_MASTER -> handleIsMaster ();
329+ case BUILD_INFO -> handleBuildInfo ();
340330 case DROP_DATABASE -> handleDropDatabase (databaseName );
341331 case GET_MORE -> handleGetMore (databaseName , command .getQueryValue (), query );
342332 case KILL_CURSORS -> handleKillCursors (query );
@@ -368,6 +358,34 @@ public void closeCursors(List<Long> cursorIds) {
368358 cursorIds .forEach (cursorRegistry ::remove );
369359 }
370360
361+ private static Document handleWhatsMyUri (Channel channel ) {
362+ Document response = new Document ();
363+ InetSocketAddress remoteAddress = (InetSocketAddress ) channel .remoteAddress ();
364+ response .put ("you" , remoteAddress .getAddress ().getHostAddress () + ":" + remoteAddress .getPort ());
365+ Utils .markOkay (response );
366+ return response ;
367+ }
368+
369+ private Document handleIsMaster () {
370+ Document response = new Document ("ismaster" , Boolean .TRUE );
371+ response .put ("maxBsonObjectSize" , Integer .valueOf (BsonConstants .MAX_BSON_OBJECT_SIZE ));
372+ response .put ("maxWriteBatchSize" , Integer .valueOf (MongoWireProtocolHandler .MAX_WRITE_BATCH_SIZE ));
373+ response .put ("maxMessageSizeBytes" , Integer .valueOf (MongoWireProtocolHandler .MAX_MESSAGE_SIZE_BYTES ));
374+ response .put ("maxWireVersion" , Integer .valueOf (version .getWireVersion ()));
375+ response .put ("minWireVersion" , Integer .valueOf (0 ));
376+ response .put ("localTime" , Instant .now (clock ));
377+ Utils .markOkay (response );
378+ return response ;
379+ }
380+
381+ private Document handleBuildInfo () {
382+ Document response = new Document ("version" , version .toVersionString ());
383+ response .put ("versionArray" , version .getVersionArray ());
384+ response .put ("maxBsonObjectSize" , Integer .valueOf (BsonConstants .MAX_BSON_OBJECT_SIZE ));
385+ Utils .markOkay (response );
386+ return response ;
387+ }
388+
371389 protected Document handleKillCursors (Document query ) {
372390 List <Long > cursorIds = (List <Long >) query .get ("cursors" );
373391 List <Long > cursorsKilled = new ArrayList <>();
0 commit comments