Skip to content

Commit

Permalink
fix code after first review of PR - issue #2170
Browse files Browse the repository at this point in the history
  • Loading branch information
tgrall authored and sazzad16 committed Dec 6, 2020
1 parent f32cd78 commit 2848ec2
Show file tree
Hide file tree
Showing 9 changed files with 203 additions and 102 deletions.
17 changes: 0 additions & 17 deletions -

This file was deleted.

110 changes: 110 additions & 0 deletions src/main/java/redis/clients/jedis/AccessControlLogEntry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package redis.clients.jedis;

import java.io.Serializable;
import java.util.*;

/**
* This class holds information about an Access Control Log entry (returned by ACL LOG command)
* They can be access via getters.
* For future purpose there is also {@link #getlogEntry} method
* that returns a generic {@code Map} - in case where more info is returned from a server
*
*/
public class AccessControlLogEntry implements Serializable {

private static final long serialVersionUID = 1L;

public static final String COUNT = "count";
public static final String REASON = "reason";
public static final String CONTEXT = "context";
public static final String OBJECT = "object";
public static final String USERNAME = "username";
public static final String AGE_SECONDS = "age-seconds";
public static final String CLIENT_INFO = "client-info";

private long count;
private final String reason;
private final String context;
private final String object;
private final String username;
private final String ageSeconds;
private final Map<String,String> clientInfo;
private final Map<String,Object> logEntry;


public AccessControlLogEntry(Map<String, Object> map) {
count = (long)map.get(COUNT);
reason = (String)map.get(REASON);
context = (String)map.get(CONTEXT);
object = (String)map.get(OBJECT);
username = (String)map.get(USERNAME);
ageSeconds = (String)map.get(AGE_SECONDS);
clientInfo = getMapFromRawClientInfo((String)map.get(CLIENT_INFO));
logEntry = map;
}

public long getCount() {
return count;
}

public String getReason() {
return reason;
}

public String getContext() {
return context;
}

public String getObject() {
return object;
}

public String getUsername() {
return username;
}

public String getAgeSeconds() {
return ageSeconds;
}

public Map<String, String> getClientInfo() {
return clientInfo;
}

/**
* @return Generic map containing all key-value pairs returned by the server
*/
public Map<String,Object> getlogEntry() {
return logEntry;
}

/**
* Convert the client-info string into a Map of String.
* When the value is empty, the value in the map is set to an empty string
* The key order is maintained to reflect the string return by Redis
* @param clientInfo
* @return A Map with all client info
*/
private Map<String,String> getMapFromRawClientInfo( String clientInfo) {
Map<String,String> clientInfoMap = new LinkedHashMap<>();
List<String> myList = new ArrayList<String>(Arrays.asList(clientInfo.split(" ")));
for (String entry : myList) {
String[] kvArray = entry.split("=");
clientInfoMap.put(kvArray[0], (kvArray.length ==2)?kvArray[1]:"" );
}
return clientInfoMap;
}

@Override
public String toString() {
return "AccessControlLogEntry{" +
"count=" + count +
", reason='" + reason + '\'' +
", context='" + context + '\'' +
", object='" + object + '\'' +
", username='" + username + '\'' +
", ageSeconds='" + ageSeconds + '\'' +
", clientInfo=" + clientInfo +
'}';
}
}
14 changes: 0 additions & 14 deletions src/main/java/redis/clients/jedis/BinaryJedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -3747,20 +3747,6 @@ public List<byte[]> aclCat(byte[] category) {
return client.getBinaryMultiBulkReply();
}

@Override
public List<byte[]> aclLogBinary() {
checkIsInMultiOrPipeline();
client.aclLog();
return client.getBinaryMultiBulkReply();
}

@Override
public List<byte[]> aclLogBinary(final int limit) {
checkIsInMultiOrPipeline();
client.aclLog(limit);
return client.getBinaryMultiBulkReply();
}

@Override
public String aclLog(byte[] options) {
checkIsInMultiOrPipeline();
Expand Down
61 changes: 32 additions & 29 deletions src/main/java/redis/clients/jedis/BuilderFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -523,51 +523,54 @@ public String toString() {
};

/**
* Create a list of Map that represent for example the ACL LOG return values
* Create an Access Control Log Entry
* Result of ACL LOG command
*/
public static final Builder<List<Map<String, String>>> LIST_STRING_MAP = new Builder<List<Map<String, String>>>() {
public static final Builder<List<AccessControlLogEntry>> ACCESS_CONTROL_LOG_ENTRY_LIST = new Builder<List<AccessControlLogEntry>>() {

Map<String,Builder> mappingFunctions = createDecoderMap();

private Map<String, Builder> createDecoderMap() {

Map<String,Builder> tempMappingFunctions = new HashMap<>();
tempMappingFunctions.put(AccessControlLogEntry.COUNT ,LONG);
tempMappingFunctions.put(AccessControlLogEntry.REASON ,STRING);
tempMappingFunctions.put(AccessControlLogEntry.CONTEXT ,STRING);
tempMappingFunctions.put(AccessControlLogEntry.OBJECT ,STRING);
tempMappingFunctions.put(AccessControlLogEntry.USERNAME,STRING);
tempMappingFunctions.put(AccessControlLogEntry.AGE_SECONDS,STRING);
tempMappingFunctions.put(AccessControlLogEntry.CLIENT_INFO,STRING);

return tempMappingFunctions;
}

@Override
public List<Map<String, String>> build(Object data) {
public List<AccessControlLogEntry> build(Object data) {

if (null == data) {
return null;
}

List<ArrayList<byte[]>> objectList = (List<ArrayList<byte[]>>) data;
List<Map<String, String>> result = new ArrayList<>();
List<AccessControlLogEntry> list = new ArrayList<>();
List<Object> logEntries = (List<Object>)data;
Iterator<Object> logsArray = logEntries.iterator();

for (int i = 0; i < objectList.size() ; i++) {

final List<byte[]> flatHash = (List<byte[]>) objectList.get(i);
final Map<String, String> hash = new HashMap<>(flatHash.size()/2, 1);
final Iterator<byte[]> iterator = flatHash.iterator();
while (iterator.hasNext()) {
String key = SafeEncoder.encode(iterator.next());
String value = null;
Object o = iterator.next();
if ( o instanceof Long ) {
value = o.toString();
} else {
value = SafeEncoder.encode((byte[])o);
}
hash.put(key, value);
}
result.add(hash);
while (logsArray.hasNext()) {
List<Object> logEntryData = (List<Object>) logsArray.next();
Iterator<Object> logEntryDataIterator = logEntryData.iterator();
AccessControlLogEntry accessControlLogEntry = new AccessControlLogEntry(
createMapFromDecodingFunctions(logEntryDataIterator,mappingFunctions));
list.add(accessControlLogEntry);
}


return result;

return list;
}

@Override
public String toString() {
return "List<Map<String, String>>";
return "List<AccessControlLogEntry>";
}

};


public static final Builder<List<Long>> LONG_LIST = new Builder<List<Long>>() {
@Override
@SuppressWarnings("unchecked")
Expand Down
8 changes: 0 additions & 8 deletions src/main/java/redis/clients/jedis/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -1199,14 +1199,6 @@ public void aclCat(final String category) {
aclCat(SafeEncoder.encode(category));
}

public void aclLog() {
aclLog();
}

public void aclLog(final int limit) {
aclLog(toByteArray(limit));
}

public void aclLog(final String options) {
aclLog(SafeEncoder.encode(options));
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/redis/clients/jedis/Jedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -3773,15 +3773,15 @@ public List<String> aclCat(String category) {
}

@Override
public List<Map<String,String>> aclLog() {
public List<AccessControlLogEntry> aclLog() {
client.aclLog();
return BuilderFactory.LIST_STRING_MAP.build(client.getObjectMultiBulkReply());
return BuilderFactory.ACCESS_CONTROL_LOG_ENTRY_LIST.build(client.getObjectMultiBulkReply());
}

@Override
public List<Map<String,String>> aclLog(int limit) {
public List<AccessControlLogEntry> aclLog(int limit) {
client.aclLog(limit);
return BuilderFactory.LIST_STRING_MAP.build(client.getObjectMultiBulkReply());
return BuilderFactory.ACCESS_CONTROL_LOG_ENTRY_LIST.build(client.getObjectMultiBulkReply());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;

import redis.clients.jedis.AccessControlLogEntry;
import redis.clients.jedis.AccessControlUser;
import redis.clients.jedis.params.MigrateParams;
import redis.clients.jedis.params.ClientKillParams;
Expand Down Expand Up @@ -74,10 +75,6 @@ public interface AdvancedBinaryJedisCommands {

List<byte[]> aclCat(byte[] category);

List<byte[]> aclLogBinary();

List<byte[]> aclLogBinary(int limit);

String aclLog(byte[] options);

// TODO: Implements ACL LOAD/SAVE commands
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.List;
import java.util.Map;

import redis.clients.jedis.AccessControlLogEntry;
import redis.clients.jedis.AccessControlUser;
import redis.clients.jedis.params.MigrateParams;
import redis.clients.jedis.params.ClientKillParams;
Expand Down Expand Up @@ -75,9 +76,9 @@ public interface AdvancedJedisCommands {

List<String> aclCat(String category);

List<Map<String,String>> aclLog();
List<AccessControlLogEntry> aclLog();

List<Map<String,String>> aclLog(int limit);
List<AccessControlLogEntry> aclLog(int limit);

String aclLog(String options);

Expand Down
Loading

0 comments on commit 2848ec2

Please sign in to comment.