/*
 * Copyright 2012-2018 the original author or authors.
 *
 * 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.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.config.repository;

import com.ecwid.consul.v1.ConsulClient;
import com.ecwid.consul.v1.kv.model.GetValue;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.config.Rate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

import static org.springframework.util.StringUtils.hasText;

/**
 * Consul rate limiter configuration.
 *
 * @author Liel Chayoun
 * @author Marcos Barbero
 * @since 2017-08-15
 */
public class ConsulRateLimiter extends AbstractRateLimiter {

    private static Logger log = LoggerFactory.getLogger(ConsulRateLimiter.class);

    private final ConsulClient consulClient;
    private final ObjectMapper objectMapper;

    public ConsulRateLimiter(RateLimiterErrorHandler rateLimiterErrorHandler,
        ConsulClient consulClient, ObjectMapper objectMapper) {
        super(rateLimiterErrorHandler);
        this.consulClient = consulClient;
        this.objectMapper = objectMapper;
    }

    @Override
    protected Rate getRate(String key) {
        Rate rate = null;
        GetValue value = this.consulClient.getKVValue(key).getValue();
        if (value != null && value.getDecodedValue() != null) {
            try {
                rate = this.objectMapper.readValue(value.getDecodedValue(), Rate.class);
            } catch (IOException e) {
                log.error("Failed to deserialize Rate", e);
            }
        }
        return rate;
    }

    @Override
    protected void saveRate(Rate rate) {
        String value = "";
        try {
            value = this.objectMapper.writeValueAsString(rate);
        } catch (JsonProcessingException e) {
            log.error("Failed to serialize Rate", e);
        }

        if (hasText(value)) {
            this.consulClient.setKVValue(rate.getKey(), value);
        }
    }

}

@@ -1054,16 +1054,79 @@
 ption;%0A%0A
+import static org.apache.commons.lang3.StringUtils.isNotBlank;%0A
 import s
@@ -1273,16 +1273,41 @@
 Barbero%0A
+ * @author Mohamed Fawzy%0A
  * @sinc
@@ -1873,16 +1873,22 @@
 getRate(
+final 
 String k
@@ -1973,20 +1973,41 @@
 KVValue(
+buildValidConsulKey(
 key)
+)
 .getValu
@@ -2674,16 +2674,36 @@
 KVValue(
+buildValidConsulKey(
 rate.get
@@ -2707,16 +2707,17 @@
 getKey()
+)
 , value)
@@ -2735,10 +2735,232 @@
 %0A    %7D%0A%0A
+    // Slash will corrupt Consul Call , to be changed to _%0A    private String buildValidConsulKey(String key)%7B%0A        if(isNotBlank(key))%7B%0A            return key.replaceAll(%22/%22, %22_%22);%0A        %7D%0A        return key;%0A    %7D%0A%0A
 %7D%0A

Fix Consul key when using URL_PATTERN (#466)