/*
 * Copyright 2014 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.plugin.use.resolve.service.internal;

import com.google.gson.Gson;
import com.google.gson.JsonIOException;
import com.google.gson.JsonSyntaxException;
import org.apache.commons.io.IOUtils;
import org.gradle.api.*;
import org.gradle.internal.resource.transport.http.HttpResourceAccessor;
import org.gradle.internal.resource.transport.http.HttpResponseResource;
import org.gradle.plugin.use.internal.PluginRequest;
import org.gradle.util.GradleVersion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;

public class PluginResolutionServiceClient {
    private static final Logger LOGGER = LoggerFactory.getLogger(PluginResolutionServiceClient.class);
    private static final String REQUEST_URL = "/api/gradle/%s/plugin/use/%s/%s";

    private final HttpResourceAccessor resourceAccessor;

    public PluginResolutionServiceClient(HttpResourceAccessor resourceAccessor) {
        this.resourceAccessor = resourceAccessor;
    }

    @Nullable
    PluginUseMetaData queryPluginMetadata(final PluginRequest pluginRequest, String portalUrl) {
        String requestUrl = String.format(portalUrl + REQUEST_URL, GradleVersion.current().getVersion(), pluginRequest.getId(), pluginRequest.getVersion());
        final URI requestUri = toUri(requestUrl, "plugin request");

        HttpResponseResource response = null;
        try {
            response = resourceAccessor.getRawResource(requestUri);
            final int statusCode = response.getStatusCode();
            if (!response.getContentType().equalsIgnoreCase("application/json")) {
                final String message = String.format("Response from '%s' was not a valid plugin resolution service response (returned content type '%s', not 'application/json')", requestUri, response.getContentType());
                if (LOGGER.isInfoEnabled()) {
                    response.withContent(new Action<InputStream>() {
                        public void execute(InputStream inputStream) {
                            try {
                                String content = IOUtils.toString(inputStream, "utf8"); // might not be UTF8, but good enough
                                LOGGER.info("{}, content:\n{}", message, content);
                            } catch (IOException e) {
                                LOGGER.info(String.format("exception raised while trying to log response from %s", requestUri), e);
                            }
                        }
                    });
                }
                throw new GradleException(message);
            }

            return response.withContent(new Transformer<PluginUseMetaData, InputStream>() {
                public PluginUseMetaData transform(InputStream inputStream) {
                    Reader reader;
                    try {
                        reader = new InputStreamReader(inputStream, "utf-8");
                    } catch (UnsupportedEncodingException e) {
                        throw new AssertionError(e);
                    }
                    try {
                        if (statusCode != 200) {
                            ErrorResponse errorResponse = new Gson().fromJson(reader, ErrorResponse.class);
                            if (statusCode == 404) {
                                return null;
                            } else {
                                throw new GradleException(String.format("Plugin resolution service returned HTTP %d with message '%s'.", statusCode, errorResponse.message));
                            }
                        }

                        PluginUseMetaData metadata = new Gson().fromJson(reader, PluginUseMetaData.class);
                        metadata.verify();
                        return metadata;
                    } catch (JsonSyntaxException e) {
                        throw new GradleException("Failed to parse plugin resolution service JSON response.", e);
                    } catch (JsonIOException e) {
                        throw new GradleException("Failed to read plugin resolution service JSON response.", e);
                    }
                }
            });
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                LOGGER.warn("Error closing HTTP resource", e);
            }
        }
    }

    private URI toUri(String url, String kind) {
        try {
            return new URI(url);
        } catch (URISyntaxException e) {
            throw new GradleException(String.format("Invalid %s URL: %s", kind, url, e));
        }
    }

}

@@ -5394,13 +5394,13 @@
  url
+)
 , e)
-)
 ;%0A  

Fix varargs problem.