Add serverside LIST_USERS command #246
@@ -43,6 +43,13 @@ This document describes the protocol for client-server communication in our appl
|
|||||||
- [Error Response](#error-response-5)
|
- [Error Response](#error-response-5)
|
||||||
- [Example Request](#example-request-3)
|
- [Example Request](#example-request-3)
|
||||||
- [Example Response](#example-response-3)
|
- [Example Response](#example-response-3)
|
||||||
|
- [LIST\_USERS command](#list_users-command)
|
||||||
|
- [Required pre-execution checks](#required-pre-execution-checks-4)
|
||||||
|
- [Request Parameters](#request-parameters-4)
|
||||||
|
- [Success Response](#success-response-4)
|
||||||
|
- [Error Response](#error-response-6)
|
||||||
|
- [Example Request](#example-request-4)
|
||||||
|
- [Example Response](#example-response-4)
|
||||||
|
|
||||||
<!-- Please see the comments for copy ‚ n' paste ready examples -->
|
<!-- Please see the comments for copy ‚ n' paste ready examples -->
|
||||||
|
|
||||||
@@ -287,3 +294,51 @@ LOGOUT
|
|||||||
+OK
|
+OK
|
||||||
END
|
END
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## LIST_USERS command
|
||||||
|
The `LIST_USERS` command is used to retrieve a list of all currently logged-in users.
|
||||||
|
|
||||||
|
### Required pre-execution checks
|
||||||
|
None.
|
||||||
|
|
||||||
|
### Request Parameters
|
||||||
|
No parameters.
|
||||||
|
|
||||||
|
### Success Response
|
||||||
|
| Field | Type | Description |
|
||||||
|
| :------ | :----------------- | :--------------------------------------- |
|
||||||
|
| `USERS` | `Collection<User>` | Collection of all users currently online |
|
||||||
|
|
||||||
|
| Fields of `User` | Type | Description |
|
||||||
|
| :--------------- | :---------------------------------------------------------------------- | :-------------------------------------------------------------------- |
|
||||||
|
| `USERNAME` | `String` | Username of the newly created user, can differ from the requested one |
|
||||||
|
| `ID` | [`UUID`](https://docs.oracle.com/javase/8/docs/api/java/util/UUID.html) | The ID of the created user |
|
||||||
|
|
||||||
|
|
||||||
|
### Error Response
|
||||||
|
None.
|
||||||
|
|
||||||
|
### Example Request
|
||||||
|
```
|
||||||
|
LIST_USERS
|
||||||
|
```
|
||||||
|
|
||||||
|
### Example Response
|
||||||
|
```
|
||||||
|
+OK
|
||||||
|
USERS
|
||||||
|
USER
|
||||||
|
USERNAME=Lars_001
|
||||||
|
ID=56765d0f-8cd3-4eec-91b2-7e36265c1a5d
|
||||||
|
END
|
||||||
|
USER
|
||||||
|
USERNAME=Lars_002
|
||||||
|
ID=b7bbd9b3-0d49-4c92-8306-b1c8506d2ff0
|
||||||
|
END
|
||||||
|
USER
|
||||||
|
USERNAME=Lars
|
||||||
|
ID=982bc78e-547f-495a-a821-433a3603f92c
|
||||||
|
END
|
||||||
|
END
|
||||||
|
END
|
||||||
|
```
|
||||||
@@ -3,6 +3,9 @@ package ch.unibas.dmi.dbis.cs108.casono.server;
|
|||||||
import ch.unibas.dmi.dbis.cs108.casono.server.app.commands.check_nick.CheckUsernameHandler;
|
import ch.unibas.dmi.dbis.cs108.casono.server.app.commands.check_nick.CheckUsernameHandler;
|
||||||
import ch.unibas.dmi.dbis.cs108.casono.server.app.commands.check_nick.CheckUsernameParser;
|
import ch.unibas.dmi.dbis.cs108.casono.server.app.commands.check_nick.CheckUsernameParser;
|
||||||
import ch.unibas.dmi.dbis.cs108.casono.server.app.commands.check_nick.CheckUsernameRequest;
|
import ch.unibas.dmi.dbis.cs108.casono.server.app.commands.check_nick.CheckUsernameRequest;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.casono.server.app.commands.list_users.ListUsersHandler;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.casono.server.app.commands.list_users.ListUsersParser;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.casono.server.app.commands.list_users.ListUsersRequest;
|
||||||
import ch.unibas.dmi.dbis.cs108.casono.server.app.commands.login.LoginHandler;
|
import ch.unibas.dmi.dbis.cs108.casono.server.app.commands.login.LoginHandler;
|
||||||
import ch.unibas.dmi.dbis.cs108.casono.server.app.commands.login.LoginParser;
|
import ch.unibas.dmi.dbis.cs108.casono.server.app.commands.login.LoginParser;
|
||||||
import ch.unibas.dmi.dbis.cs108.casono.server.app.commands.login.LoginRequest;
|
import ch.unibas.dmi.dbis.cs108.casono.server.app.commands.login.LoginRequest;
|
||||||
@@ -106,5 +109,9 @@ public class ServerApp {
|
|||||||
parserDispatcher.register("LOGOUT", new LogoutParser());
|
parserDispatcher.register("LOGOUT", new LogoutParser());
|
||||||
commandRouter.register(
|
commandRouter.register(
|
||||||
LogoutRequest.class, new LogoutHandler(responseDispatcher, userRegistry));
|
LogoutRequest.class, new LogoutHandler(responseDispatcher, userRegistry));
|
||||||
|
|
||||||
|
parserDispatcher.register("LIST_USERS", new ListUsersParser());
|
||||||
|
commandRouter.register(
|
||||||
|
ListUsersRequest.class, new ListUsersHandler(responseDispatcher, userRegistry));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+40
@@ -0,0 +1,40 @@
|
|||||||
|
package ch.unibas.dmi.dbis.cs108.casono.server.app.commands.list_users;
|
||||||
|
|
||||||
|
import ch.unibas.dmi.dbis.cs108.casono.server.domain.user.User;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.casono.server.domain.user.UserRegistry;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.casono.server.network.command.execution.CommandHandler;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.dispatcher.ResponseDispatcher;
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles {@link ListUsersRequest}s by retrieving all users from the {@link UserRegistry} and
|
||||||
|
* dispatching a {@link ListUsersResponse} containing the list of users.
|
||||||
|
*/
|
||||||
|
public class ListUsersHandler extends CommandHandler<ListUsersRequest> {
|
||||||
|
private final UserRegistry userRegistry;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new handler for listing all users
|
||||||
|
*
|
||||||
|
* @param responseDispatcher the dispatcher used to send the response
|
||||||
|
* @param userRegistry the registry used to look up existing users
|
||||||
|
*/
|
||||||
|
public ListUsersHandler(ResponseDispatcher responseDispatcher, UserRegistry userRegistry) {
|
||||||
|
super(responseDispatcher);
|
||||||
|
this.userRegistry = userRegistry;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Executes the list users request
|
||||||
|
*
|
||||||
|
* <p>All users are retrieved from the {@link UserRegistry} and returned in a {@link
|
||||||
|
* ListUsersResponse}.
|
||||||
|
*
|
||||||
|
* @param request the request to execute
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void execute(ListUsersRequest request) {
|
||||||
|
Collection<User> users = userRegistry.getAllUsers();
|
||||||
|
responseDispatcher.dispatch(new ListUsersResponse(request.getContext(), users));
|
||||||
|
}
|
||||||
|
}
|
||||||
+18
@@ -0,0 +1,18 @@
|
|||||||
|
package ch.unibas.dmi.dbis.cs108.casono.server.app.commands.list_users;
|
||||||
|
|
||||||
|
import ch.unibas.dmi.dbis.cs108.casono.server.network.command.parsing.CommandParser;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.PrimitiveRequest;
|
||||||
|
|
||||||
|
/** Parses a primitive request into a {@link ListUsersRequest}. */
|
||||||
|
public class ListUsersParser implements CommandParser<ListUsersRequest> {
|
||||||
|
/**
|
||||||
|
* Parses a primitive request into a ListUsersRequest.
|
||||||
|
*
|
||||||
|
* @param primitiveRequest the request to parse
|
||||||
|
* @return the created {@link ListUsersRequest}
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public ListUsersRequest parse(PrimitiveRequest primitiveRequest) {
|
||||||
|
return new ListUsersRequest(primitiveRequest.context());
|
||||||
|
}
|
||||||
|
}
|
||||||
+17
@@ -0,0 +1,17 @@
|
|||||||
|
package ch.unibas.dmi.dbis.cs108.casono.server.app.commands.list_users;
|
||||||
|
|
||||||
|
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.Request;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.RequestContext;
|
||||||
|
|
||||||
|
/** Request implementation used to retrieve all existing users from the server. */
|
||||||
|
public class ListUsersRequest extends Request {
|
||||||
|
/**
|
||||||
|
* Constructs a new ListUsersRequest with the given context
|
||||||
|
*
|
||||||
|
* @param context the {@link RequestContext} containing information for responding to the
|
||||||
|
* request
|
||||||
|
*/
|
||||||
|
public ListUsersRequest(RequestContext context) {
|
||||||
|
super(context);
|
||||||
|
}
|
||||||
|
}
|
||||||
+35
@@ -0,0 +1,35 @@
|
|||||||
|
package ch.unibas.dmi.dbis.cs108.casono.server.app.commands.list_users;
|
||||||
|
|
||||||
|
import ch.unibas.dmi.dbis.cs108.casono.server.domain.user.User;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.RequestContext;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.SuccessResponse;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.builder.ResponseBody;
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
/** Response containing a list of all active users on the server */
|
||||||
|
public class ListUsersResponse extends SuccessResponse {
|
||||||
|
/**
|
||||||
|
* Creates a new ListUsersResponse containing the given list of users
|
||||||
|
*
|
||||||
|
* @param context the {@link RequestContext} associated with the request
|
||||||
|
* @param users the collection of users currently active on the server
|
||||||
|
*/
|
||||||
|
public ListUsersResponse(RequestContext context, Collection<User> users) {
|
||||||
|
super(
|
||||||
|
context,
|
||||||
|
ResponseBody.builder()
|
||||||
|
.block(
|
||||||
|
"USERS",
|
||||||
|
users_block -> {
|
||||||
|
for (User user : users) {
|
||||||
|
users_block.block(
|
||||||
|
"USER",
|
||||||
|
user_block -> {
|
||||||
|
user_block.param("USERNAME", user.getName());
|
||||||
|
user_block.param("ID", user.getId().value());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.build());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user