Search in sources :

Example 1 with DescribeClientQuotasRequestData

use of org.apache.kafka.common.message.DescribeClientQuotasRequestData in project kafka by apache.

the class ClientQuotasImage method describe.

public DescribeClientQuotasResponseData describe(DescribeClientQuotasRequestData request) {
    DescribeClientQuotasResponseData response = new DescribeClientQuotasResponseData();
    Map<String, String> exactMatch = new HashMap<>();
    Set<String> typeMatch = new HashSet<>();
    for (DescribeClientQuotasRequestData.ComponentData component : request.components()) {
        if (component.entityType().isEmpty()) {
            throw new InvalidRequestException("Invalid empty entity type.");
        } else if (exactMatch.containsKey(component.entityType()) || typeMatch.contains(component.entityType())) {
            throw new InvalidRequestException("Entity type " + component.entityType() + " cannot appear more than once in the filter.");
        }
        if (!(component.entityType().equals(IP) || component.entityType().equals(USER) || component.entityType().equals(CLIENT_ID))) {
            throw new UnsupportedVersionException("Unsupported entity type " + component.entityType());
        }
        switch(component.matchType()) {
            case MATCH_TYPE_EXACT:
                if (component.match() == null) {
                    throw new InvalidRequestException("Request specified " + "MATCH_TYPE_EXACT, but set match string to null.");
                }
                exactMatch.put(component.entityType(), component.match());
                break;
            case MATCH_TYPE_DEFAULT:
                if (component.match() != null) {
                    throw new InvalidRequestException("Request specified " + "MATCH_TYPE_DEFAULT, but also specified a match string.");
                }
                exactMatch.put(component.entityType(), null);
                break;
            case MATCH_TYPE_SPECIFIED:
                if (component.match() != null) {
                    throw new InvalidRequestException("Request specified " + "MATCH_TYPE_SPECIFIED, but also specified a match string.");
                }
                typeMatch.add(component.entityType());
                break;
            default:
                throw new InvalidRequestException("Unknown match type " + component.matchType());
        }
    }
    if (exactMatch.containsKey(IP) || typeMatch.contains(IP)) {
        if ((exactMatch.containsKey(USER) || typeMatch.contains(USER)) || (exactMatch.containsKey(CLIENT_ID) || typeMatch.contains(CLIENT_ID))) {
            throw new InvalidRequestException("Invalid entity filter component " + "combination. IP filter component should not be used with " + "user or clientId filter component.");
        }
    }
    // TODO: this is O(N). We should add indexing here to speed it up. See KAFKA-13022.
    for (Entry<ClientQuotaEntity, ClientQuotaImage> entry : entities.entrySet()) {
        ClientQuotaEntity entity = entry.getKey();
        ClientQuotaImage quotaImage = entry.getValue();
        if (matches(entity, exactMatch, typeMatch, request.strict())) {
            response.entries().add(toDescribeEntry(entity, quotaImage));
        }
    }
    return response;
}
Also used : HashMap(java.util.HashMap) DescribeClientQuotasRequestData(org.apache.kafka.common.message.DescribeClientQuotasRequestData) InvalidRequestException(org.apache.kafka.common.errors.InvalidRequestException) ClientQuotaEntity(org.apache.kafka.common.quota.ClientQuotaEntity) DescribeClientQuotasResponseData(org.apache.kafka.common.message.DescribeClientQuotasResponseData) HashSet(java.util.HashSet) UnsupportedVersionException(org.apache.kafka.common.errors.UnsupportedVersionException)

Aggregations

HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 InvalidRequestException (org.apache.kafka.common.errors.InvalidRequestException)1 UnsupportedVersionException (org.apache.kafka.common.errors.UnsupportedVersionException)1 DescribeClientQuotasRequestData (org.apache.kafka.common.message.DescribeClientQuotasRequestData)1 DescribeClientQuotasResponseData (org.apache.kafka.common.message.DescribeClientQuotasResponseData)1 ClientQuotaEntity (org.apache.kafka.common.quota.ClientQuotaEntity)1