Search in sources :

Example 1 with TooManyRequestsException

use of com.b2international.commons.exceptions.TooManyRequestsException in project snow-owl by b2ihealthcare.

the class RateLimitingRequest method execute.

@Override
public R execute(ServiceProvider context) {
    final User user = context.service(User.class);
    if (user != User.SYSTEM) {
        // rate limit only non-system user requests
        final String username = user.getUsername();
        final RateLimitConsumption consumption = context.service(RateLimiter.class).consume(username);
        if (consumption.isConsumed()) {
            context.service(ResponseHeaders.class).set("X-Rate-Limit-Remaining", Long.toString(consumption.getRemainingTokens()));
        } else {
            throw new TooManyRequestsException(consumption.getSecondsToWait());
        }
    }
    return next(context);
}
Also used : TooManyRequestsException(com.b2international.commons.exceptions.TooManyRequestsException) User(com.b2international.snowowl.core.identity.User) ResponseHeaders(com.b2international.snowowl.core.events.util.ResponseHeaders)

Aggregations

TooManyRequestsException (com.b2international.commons.exceptions.TooManyRequestsException)1 ResponseHeaders (com.b2international.snowowl.core.events.util.ResponseHeaders)1 User (com.b2international.snowowl.core.identity.User)1