Skip to content

Commit

Permalink
Implement getting users info for TCP
Browse files Browse the repository at this point in the history
  • Loading branch information
mmodzelewski committed Oct 11, 2024
1 parent 1d07422 commit 7eadb89
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/main/java/rs/iggy/clients/blocking/http/HttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

class HttpClient {

private static final Logger LOG = LoggerFactory.getLogger(HttpClient.class);
private static final Logger log = LoggerFactory.getLogger(HttpClient.class);

private static final String AUTHORIZATION = "Authorization";
private final String url;
Expand Down Expand Up @@ -103,7 +103,7 @@ ClassicHttpRequest prepareDeleteRequest(String path, NameValuePair... params) {
private ClassicHttpRequest addRequestBody(ClassicRequestBuilder requestBuilder, Object body) {
try {
var encodedBody = objectMapper.writeValueAsString(body);
LOG.debug("Request body: {}", encodedBody);
log.debug("Request body: {}", encodedBody);
return requestBuilder
.setHeader("Content-Type", "application/json")
.setEntity(encodedBody)
Expand Down
82 changes: 82 additions & 0 deletions src/main/java/rs/iggy/clients/blocking/tcp/BytesDeserializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import rs.iggy.topic.CompressionAlgorithm;
import rs.iggy.topic.Topic;
import rs.iggy.topic.TopicDetails;
import rs.iggy.user.*;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.util.*;
Expand Down Expand Up @@ -259,6 +260,87 @@ static ConsumerGroupInfo readConsumerGroupInfo(ByteBuf response) {
return new ConsumerGroupInfo(streamId, topicId, groupId);
}

static UserInfoDetails readUserInfoDetails(ByteBuf response) {
var userInfo = readUserInfo(response);

Optional<Permissions> permissionsOptional = Optional.empty();
if (response.readBoolean()) {
var permissions = readPermissions(response);
permissionsOptional = Optional.of(permissions);
}

return new UserInfoDetails(userInfo, permissionsOptional);
}

static Permissions readPermissions(ByteBuf response) {
var _permissionsLength = response.readUnsignedIntLE();
var globalPermissions = readGlobalPermissions(response);
Map<Long, StreamPermissions> streamPermissionsMap = new HashMap<>();
while (response.readBoolean()) {
var streamId = response.readUnsignedIntLE();
var streamPermissions = readStreamPermissions(response);
streamPermissionsMap.put(streamId, streamPermissions);
}
return new Permissions(globalPermissions, streamPermissionsMap);
}

static StreamPermissions readStreamPermissions(ByteBuf response) {
var manageStream = response.readBoolean();
var readStream = response.readBoolean();
var manageTopics = response.readBoolean();
var readTopics = response.readBoolean();
var pollMessages = response.readBoolean();
var sendMessages = response.readBoolean();
Map<Long, TopicPermissions> topicPermissionsMap = new HashMap<>();
while (response.readBoolean()) {
var topicId = response.readUnsignedIntLE();
var topicPermissions = readTopicPermissions(response);
topicPermissionsMap.put(topicId, topicPermissions);
}
return new StreamPermissions(manageStream, readStream, manageTopics, readTopics, pollMessages, sendMessages, topicPermissionsMap);
}

static TopicPermissions readTopicPermissions(ByteBuf response) {
var manageTopic = response.readBoolean();
var readTopic = response.readBoolean();
var pollMessages = response.readBoolean();
var sendMessages = response.readBoolean();
return new TopicPermissions(manageTopic, readTopic, pollMessages, sendMessages);
}

static GlobalPermissions readGlobalPermissions(ByteBuf response) {
var manageServers = response.readBoolean();
var readServers = response.readBoolean();
var manageUsers = response.readBoolean();
var readUsers = response.readBoolean();
var manageStreams = response.readBoolean();
var readStreams = response.readBoolean();
var manageTopics = response.readBoolean();
var readTopics = response.readBoolean();
var pollMessages = response.readBoolean();
var sendMessages = response.readBoolean();
return new GlobalPermissions(manageServers,
readServers,
manageUsers,
readUsers,
manageStreams,
readStreams,
manageTopics,
readTopics,
pollMessages,
sendMessages);
}

static UserInfo readUserInfo(ByteBuf response) {
var userId = response.readUnsignedIntLE();
var createdAt = readU64AsBigInteger(response);
var statusCode = response.readByte();
var status = UserStatus.fromCode(statusCode);
var usernameLength = response.readByte();
var username = response.readCharSequence(usernameLength, StandardCharsets.UTF_8).toString();
return new UserInfo(userId, createdAt, status, username);
}

private static BigInteger readU64AsBigInteger(ByteBuf buffer) {
var bytesArray = new byte[9];
buffer.readBytes(bytesArray, 0, 8);
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/rs/iggy/clients/blocking/tcp/UsersTcpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
import rs.iggy.clients.blocking.UsersClient;
import rs.iggy.identifier.UserId;
import rs.iggy.user.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import static rs.iggy.clients.blocking.tcp.BytesDeserializer.readUserInfoDetails;
import static rs.iggy.clients.blocking.tcp.BytesSerializer.nameToBytes;
import static rs.iggy.clients.blocking.tcp.BytesSerializer.toBytes;

class UsersTcpClient implements UsersClient {

Expand All @@ -28,12 +31,19 @@ class UsersTcpClient implements UsersClient {

@Override
public UserInfoDetails getUser(UserId userId) {
throw new UnsupportedOperationException();
var payload = toBytes(userId);
var response = connection.send(GET_USER_CODE, payload);
return readUserInfoDetails(response);
}

@Override
public List<UserInfo> getUsers() {
throw new UnsupportedOperationException();
var response = connection.send(GET_USERS_CODE);
List<UserInfo> users = new ArrayList<>();
while (response.isReadable()) {
users.add(BytesDeserializer.readUserInfo(response));
}
return users;
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/rs/iggy/user/GlobalPermissions.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package rs.iggy.user;

record GlobalPermissions(
public record GlobalPermissions(
boolean manageServers,
boolean readServers,
boolean manageUsers,
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/rs/iggy/user/Permissions.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package rs.iggy.user;

import com.fasterxml.jackson.annotation.JsonSetter;
import com.fasterxml.jackson.annotation.Nulls;
import java.util.Map;
import java.util.Optional;

public record Permissions(
GlobalPermissions global,
Optional<Map<Long, StreamPermissions>> streams
@JsonSetter(nulls = Nulls.AS_EMPTY)
Map<Long, StreamPermissions> streams
) {
}
6 changes: 4 additions & 2 deletions src/main/java/rs/iggy/user/StreamPermissions.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package rs.iggy.user;

import com.fasterxml.jackson.annotation.JsonSetter;
import com.fasterxml.jackson.annotation.Nulls;
import java.util.Map;
import java.util.Optional;

public record StreamPermissions(
boolean manageStream,
Expand All @@ -10,6 +11,7 @@ public record StreamPermissions(
boolean readTopics,
boolean pollMessages,
boolean sendMessages,
Optional<Map<Long, TopicPermissions>> topics
@JsonSetter(nulls = Nulls.AS_EMPTY)
Map<Long, TopicPermissions> topics
) {
}
3 changes: 3 additions & 0 deletions src/main/java/rs/iggy/user/UserInfoDetails.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ public record UserInfoDetails(
String username,
Optional<Permissions> permissions
) {
public UserInfoDetails(UserInfo userInfo, Optional<Permissions> permissions) {
this(userInfo.id(), userInfo.createdAt(), userInfo.status(), userInfo.username(), permissions);
}
}
19 changes: 17 additions & 2 deletions src/main/java/rs/iggy/user/UserStatus.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
package rs.iggy.user;

public enum UserStatus {
Active,
Inactive
Active(1),
Inactive(2);

private final int code;

UserStatus(int code) {
this.code = code;
}

public static UserStatus fromCode(int code) {
for (UserStatus userStatus : UserStatus.values()) {
if (userStatus.code == code) {
return userStatus;
}
}
throw new IllegalArgumentException("Invalid user status: " + code);
}
}
11 changes: 11 additions & 0 deletions src/test/java/rs/iggy/clients/blocking/UsersClientBaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import rs.iggy.user.UserInfoDetails;
import static org.assertj.core.api.Assertions.assertThat;

public abstract class UsersClientBaseTest extends IntegrationTest {
Expand All @@ -23,4 +24,14 @@ void shouldLogin() {
assertThat(identityInfo.userId()).isEqualTo(1L);
}

@Test
void shouldGetUser() {
// when
login();
UserInfoDetails user = usersClient.getUser(1L);

// then
assertThat(user).isNotNull();
}

}

0 comments on commit 7eadb89

Please sign in to comment.