/*
 * Copyright 2014 Click Travel Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package com.clicktravel.cheddar.server.http.filter.flow.control;

import java.io.IOException;

import javax.annotation.Priority;
import javax.inject.Inject;
import javax.ws.rs.Priorities;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.ext.Provider;

import com.clicktravel.cheddar.server.flow.control.RateLimiterConfiguration;
import com.clicktravel.common.concurrent.RateLimiter;

/**
 * Applies a rate limit function to all (matched) requests, except for status resource (/status). This filter will block
 * until the rate limiter allows the request processing to proceed.
 */
@Provider
@Priority(Priorities.USER)
public class FlowControlledRequestFilter implements ContainerRequestFilter {

    private final RateLimiter restRequestRateLimiter;

    @Inject
    public FlowControlledRequestFilter(final RateLimiterConfiguration rateLimiterConfiguration) {
        restRequestRateLimiter = rateLimiterConfiguration.restRequestRateLimiter();
    }

    @Override
    public void filter(final ContainerRequestContext requestContext) throws IOException {
        final String path = requestContext.getUriInfo().getRequestUri().getPath();
        final boolean isStatusResource = path.matches("/status(/.*)?");
        if (!isStatusResource) {
            try {
                restRequestRateLimiter.takeToken(); // block until allowed by rate limit
            } catch (final InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
    }

}

@@ -923,16 +923,172 @@
 vider;%0A%0A
+import org.jboss.logging.Logger;%0Aimport org.springframework.beans.factory.annotation.Autowired;%0Aimport org.springframework.beans.factory.annotation.Value;%0A%0A
 import c
@@ -1156,16 +1156,16 @@
 ration;%0A
-
 import c
@@ -1523,16 +1523,210 @@
 lter %7B%0A%0A
+    @Autowired%0A    @Value(%22$%7Bflow.control.rateLimitLogging:true%7D%22)%0A    private boolean rateLimitLogging;%0A%0A    private final Logger logger = Logger.getLogger(FlowControlledRequestFilter.class);%0A%0A
     priv
@@ -2283,16 +2283,103 @@
   try %7B%0A
+                final long start = rateLimitLogging ? System.currentTimeMillis() : 0l;%0A
         
@@ -2459,16 +2459,236 @@
 e limit%0A
+                if (rateLimitLogging) %7B%0A                    logger.debug(path + %22 took %22 + ((System.currentTimeMillis() - start) / 1000)%0A                            + %22s to obtain a token to process%22);%0A                %7D%0A
         

Allow apps to turn on rest rate limit logging