/*
 * Copyright 2000-2015 JetBrains s.r.o.
 *
 * 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 jetbrains.buildServer.nuget.agent.parameters.impl;

import com.intellij.openapi.diagnostic.Logger;
import jetbrains.buildServer.agent.AgentBuildFeature;
import jetbrains.buildServer.agent.AgentRunningBuild;
import jetbrains.buildServer.nuget.agent.parameters.PackageSourceManager;
import jetbrains.buildServer.nuget.common.NuGetServerConstants;
import jetbrains.buildServer.nuget.common.PackagesConstants;
import jetbrains.buildServer.nuget.common.auth.PackageSource;
import jetbrains.buildServer.util.StringUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * Created 04.01.13 19:19
 *
 * @author Eugene Petrenko (eugene.petrenko@jetbrains.com)
 */
public class PackageSourceManagerImpl implements PackageSourceManager {

  private static final Logger LOG = Logger.getInstance(PackageSourceManagerImpl.class.getName());

  @NotNull
  public Set<PackageSource> getGlobalPackageSources(@NotNull AgentRunningBuild build) {
    final Map<String, PackageSource> result = new HashMap<String, PackageSource>();

    for (AgentBuildFeature feature : build.getBuildFeaturesOfType(PackagesConstants.ATHU_FEATURE_TYPE)) {
      final String feed = feature.getParameters().get(PackagesConstants.NUGET_AUTH_FEED);
      final String user = feature.getParameters().get(PackagesConstants.NUGET_AUTH_USERNAME);
      final String pass = feature.getParameters().get(PackagesConstants.NUGET_AUTH_PASSWORD);

      result.put(feed, source(feed, user, pass));
    }

    final String agentProvidedTcFeedUrl = build.getSharedConfigParameters().get(NuGetServerConstants.FEED_AUTH_REFERENCE_AGENT_PROVIDED);
    if (StringUtil.isEmptyOrSpaces(agentProvidedTcFeedUrl))
      LOG.debug("Failed to resolve TeamCity internal NuGet feed url via config parameter " + NuGetServerConstants.FEED_AUTH_REFERENCE_AGENT_PROVIDED);
    else
      result.put(agentProvidedTcFeedUrl, source(agentProvidedTcFeedUrl, build.getAccessUser(), build.getAccessCode()));

    final String serverProvidedTcFeedUrl = build.getSharedBuildParameters().getSystemProperties().get(NuGetServerConstants.FEED_AUTH_REFERENCE_SERVER_PROVIDED);
    if (StringUtil.isEmptyOrSpaces(serverProvidedTcFeedUrl))
      LOG.debug("Failed to resolve TeamCity internal NuGet feed url via system property " + NuGetServerConstants.FEED_AUTH_REFERENCE_SERVER_PROVIDED);
    else
      result.put(serverProvidedTcFeedUrl, source(serverProvidedTcFeedUrl, build.getAccessUser(), build.getAccessCode()));

    return new HashSet<PackageSource>(result.values());
  }

  @NotNull
  private PackageSource source(@NotNull final String feed,
                               @Nullable final String user,
                               @Nullable final String pass) {
    return new PackageSource() {
      @NotNull
      public String getSource() {
        return feed;
      }

      @Nullable
      public String getUsername() {
        return user;
      }

      @Nullable
      public String getPassword() {
        return pass;
      }

      @Override
      public int hashCode() {
        return feed.hashCode();
      }

      @Override
      public boolean equals(Object obj) {
        if ( this == obj ) return true;
        if (!(obj instanceof PackageSource)) return false;
        PackageSource that = (PackageSource)obj;
        return feed.equals(that.getSource());
      }
    };
  }
}

@@ -1203,16 +1203,37 @@
 lable;%0A%0A
+import java.net.URI;%0A
 import j
@@ -2175,20 +2175,34 @@
 ult.put(
+normalizeUrl(
 feed
+)
 , source
@@ -2604,16 +2604,29 @@
 ult.put(
+normalizeUrl(
 agentPro
@@ -2631,32 +2631,33 @@
 rovidedTcFeedUrl
+)
 , source(agentPr
@@ -3121,16 +3121,29 @@
 ult.put(
+normalizeUrl(
 serverPr
@@ -3157,16 +3157,17 @@
 cFeedUrl
+)
 , source
@@ -3302,16 +3302,136 @@
 );%0A  %7D%0A%0A
+  @Nullable%0A  private String normalizeUrl(@NotNull String url) %7B%0A    return URI.create(url).normalize().getPath();%0A  %7D%0A%0A
   @NotNu

fix TW-43113 - normalize target urls when passing credentials to nuget.exe (cherry picked from commit a5c5f02)