package com.github.aesteve.vertx.nubes.handlers.impl;

import com.github.aesteve.vertx.nubes.Config;
import com.github.aesteve.vertx.nubes.context.ClientAccesses;
import com.github.aesteve.vertx.nubes.context.RateLimit;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
import io.vertx.core.shareddata.LocalMap;
import io.vertx.ext.web.RoutingContext;

public class RateLimitationHandler implements Handler<RoutingContext> {

  private final RateLimit rateLimit;

  public static RateLimitationHandler create(Config config) {
    return new RateLimitationHandler(config.getRateLimit());
  }

  private RateLimitationHandler(RateLimit rateLimit) {
    this.rateLimit = rateLimit;
  }

  @Override
  public void handle(RoutingContext context) {
    Vertx vertx = context.vertx();
    LocalMap<Object, Object> rateLimitations = vertx.sharedData().getLocalMap("mvc.rateLimitation");
    String clientIp = context.request().remoteAddress().host();
    JsonObject json = (JsonObject) rateLimitations.get(clientIp);
    ClientAccesses accesses;
    if (json == null) {
      accesses = new ClientAccesses();
    } else {
      accesses = ClientAccesses.fromJsonObject(json);
    }
    accesses.newAccess();
    rateLimitations.put(clientIp, accesses.toJsonObject());
    if (accesses.isOverLimit(rateLimit)) {
      context.fail(420);
    } else {
      context.next();
    }
  }

}

@@ -503,16 +503,108 @@
 Limit;%0A%0A
+  private RateLimitationHandler(RateLimit rateLimit) %7B%0A    this.rateLimit = rateLimit;%0A  %7D%0A%0A
   public
@@ -727,100 +727,8 @@
  %7D%0A%0A
-  private RateLimitationHandler(RateLimit rateLimit) %7B%0A    this.rateLimit = rateLimit;%0A  %7D%0A%0A
   @O

rearrange code