Search in sources :

Example 1 with TbRateLimitsException

use of org.thingsboard.server.common.msg.tools.TbRateLimitsException in project thingsboard by thingsboard.

the class RateLimitProcessingFilter method doFilter.

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    SecurityUser user = getCurrentUser();
    if (user != null && !user.isSystemAdmin()) {
        if (perTenantLimitsEnabled) {
            TbRateLimits rateLimits = perTenantLimits.computeIfAbsent(user.getTenantId(), id -> new TbRateLimits(perTenantLimitsConfiguration));
            if (!rateLimits.tryConsume()) {
                errorResponseHandler.handle(new TbRateLimitsException(EntityType.TENANT), (HttpServletResponse) response);
                return;
            }
        }
        if (perCustomerLimitsEnabled && user.isCustomerUser()) {
            TbRateLimits rateLimits = perCustomerLimits.computeIfAbsent(user.getCustomerId(), id -> new TbRateLimits(perCustomerLimitsConfiguration));
            if (!rateLimits.tryConsume()) {
                errorResponseHandler.handle(new TbRateLimitsException(EntityType.CUSTOMER), (HttpServletResponse) response);
                return;
            }
        }
    }
    chain.doFilter(request, response);
}
Also used : SecurityUser(org.thingsboard.server.service.security.model.SecurityUser) TbRateLimits(org.thingsboard.server.common.msg.tools.TbRateLimits) TbRateLimitsException(org.thingsboard.server.common.msg.tools.TbRateLimitsException)

Example 2 with TbRateLimitsException

use of org.thingsboard.server.common.msg.tools.TbRateLimitsException in project thingsboard by thingsboard.

the class DefaultTransportService method checkLimits.

private boolean checkLimits(TransportProtos.SessionInfoProto sessionInfo, Object msg, TransportServiceCallback<?> callback, int dataPoints) {
    if (log.isTraceEnabled()) {
        log.trace("[{}] Processing msg: {}", toSessionId(sessionInfo), msg);
    }
    TenantId tenantId = TenantId.fromUUID(new UUID(sessionInfo.getTenantIdMSB(), sessionInfo.getTenantIdLSB()));
    DeviceId deviceId = new DeviceId(new UUID(sessionInfo.getDeviceIdMSB(), sessionInfo.getDeviceIdLSB()));
    EntityType rateLimitedEntityType = rateLimitService.checkLimits(tenantId, deviceId, dataPoints);
    if (rateLimitedEntityType == null) {
        return true;
    } else {
        if (callback != null) {
            callback.onError(new TbRateLimitsException(rateLimitedEntityType));
        }
        return false;
    }
}
Also used : EntityType(org.thingsboard.server.common.data.EntityType) TenantId(org.thingsboard.server.common.data.id.TenantId) DeviceId(org.thingsboard.server.common.data.id.DeviceId) UUID(java.util.UUID) TbRateLimitsException(org.thingsboard.server.common.msg.tools.TbRateLimitsException)

Aggregations

TbRateLimitsException (org.thingsboard.server.common.msg.tools.TbRateLimitsException)2 UUID (java.util.UUID)1 EntityType (org.thingsboard.server.common.data.EntityType)1 DeviceId (org.thingsboard.server.common.data.id.DeviceId)1 TenantId (org.thingsboard.server.common.data.id.TenantId)1 TbRateLimits (org.thingsboard.server.common.msg.tools.TbRateLimits)1 SecurityUser (org.thingsboard.server.service.security.model.SecurityUser)1