/*
 * Copyright 2016 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 org.gradle.caching.http.internal;

import org.gradle.api.GradleException;
import org.gradle.caching.BuildCacheService;
import org.gradle.caching.BuildCacheServiceFactory;
import org.gradle.caching.http.HttpBuildCache;

import java.net.URI;
import java.net.URISyntaxException;

/**
 * Build cache factory for HTTP backend.
 */
public class DefaultHttpBuildCacheServiceFactory implements BuildCacheServiceFactory<HttpBuildCache> {
    @Override
    public Class<HttpBuildCache> getConfigurationType() {
        return HttpBuildCache.class;
    }

    @Override
    public BuildCacheService build(HttpBuildCache configuration) {
        URI url = configuration.getUrl();
        if (url == null) {
            throw new IllegalStateException("HTTP build cache has no URL configured");
        }
        URI root = URI.create(String.valueOf(url));
        if (configuration.getCredentials().getUsername() != null && configuration.getCredentials().getPassword() != null) {
            String userInfo = configuration.getCredentials().getUsername() + ":" + configuration.getCredentials().getPassword();
            try {
                root = new URI(root.getScheme(), userInfo, root.getHost(), root.getPort(), root.getPath(), root.getQuery(), root.getFragment());
            } catch (URISyntaxException e) {
                throw new GradleException("Invalid credentials", e);
            }
        }
        return new HttpBuildCacheService(root);
    }
}

@@ -1414,60 +1414,8 @@
   %7D%0A
-        URI root = URI.create(String.valueOf(url));%0A
     
@@ -1526,32 +1526,32 @@
 rd() != null) %7B%0A
+
             Stri
@@ -1697,20 +1697,19 @@
         
-root
+url
  = new U
@@ -1711,20 +1711,19 @@
 new URI(
-root
+url
 .getSche
@@ -1738,20 +1738,19 @@
 erInfo, 
-root
+url
 .getHost
@@ -1749,28 +1749,27 @@
 .getHost(), 
-root
+url
 .getPort(), 
@@ -1764,28 +1764,27 @@
 .getPort(), 
-root
+url
 .getPath(), 
@@ -1779,28 +1779,27 @@
 .getPath(), 
-root
+url
 .getQuery(),
@@ -1799,20 +1799,19 @@
 uery(), 
-root
+url
 .getFrag
@@ -2002,12 +2002,11 @@
 ice(
-root
+url
 );%0A 

Clean up build cache service factory for the HTTP build cache