package com.chbi.rest;

import com.chbi.ApplicationConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * Handles the jenkins urls, appends the json api suffix and replaces url parts if necessary.
 */
@Service
public class UrlRewriter {

    private static final String API_JSON_URL_ENDING = "api/json";
    private static final String HTML_WHITESPACE = "%20";
    private static final String WHITESPACE = " ";

    @Autowired
    private ApplicationConfiguration configuration;

    String prepareUrl(String baseUrl) {

        String preparedUrl = StringUtils.replace(baseUrl, configuration.getUrlSearchPattern(),
                configuration.getUrlReplacement());
        preparedUrl = StringUtils.replace(preparedUrl, HTML_WHITESPACE, WHITESPACE);
        preparedUrl += API_JSON_URL_ENDING;

        return preparedUrl;
    }

    String getPreparedBaseUrl() {
        return prepareUrl(configuration.getBaseUrl());
    }
}

@@ -215,16 +215,90 @@
 Utils;%0A%0A
+import java.io.UnsupportedEncodingException;%0Aimport java.net.URLDecoder;%0A%0A
 /**%0A * H
@@ -497,115 +497,8 @@
 on%22;
-%0A    private static final String HTML_WHITESPACE = %22%2520%22;%0A    private static final String WHITESPACE = %22 %22;
 %0A%0A  
@@ -603,17 +603,16 @@
 eUrl) %7B%0A
-%0A
         
@@ -746,24 +746,42 @@
 acement());%0A
+        try %7B%0A    
         prep
@@ -794,115 +794,191 @@
 l = 
-StringUtils.replace(preparedUrl, HTML_WHITESPACE, WHITESPACE);%0A        preparedUrl += API_JSON_URL_ENDING;%0A
+URLDecoder.decode(preparedUrl, %22UTF-8%22);%0A%0A            preparedUrl += API_JSON_URL_ENDING;%0A        %7D catch (UnsupportedEncodingException e) %7B%0A            e.printStackTrace();%0A        %7D
 %0A   

Fix url encoding