// Copyright 2015 Google Inc. All rights reserved.
//
// 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.google.devtools.build.workspace.maven;

import com.google.common.collect.Lists;
import org.apache.maven.model.Parent;
import org.apache.maven.model.Repository;
import org.apache.maven.model.building.ModelSource;
import org.apache.maven.model.building.UrlModelSource;
import org.apache.maven.model.resolution.InvalidRepositoryException;
import org.apache.maven.model.resolution.ModelResolver;
import org.apache.maven.model.resolution.UnresolvableModelException;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.List;

class DefaultModelResolver implements ModelResolver {

  private final List<Repository> repositories;

  public DefaultModelResolver() {
    repositories = Lists.newArrayList();
  }

  private DefaultModelResolver(List<Repository> repositories) {
    this.repositories = repositories;
  }

  @Override
  public ModelSource resolveModel(String groupId, String artifactId, String version)
      throws UnresolvableModelException {
    for (Repository repository : repositories) {
      try {
        UrlModelSource urlModelSource = new UrlModelSource(new URL(repository.getUrl()
            + groupId.replaceAll("\\.", "/") + "/" + artifactId + "/" + version + "/" + artifactId
            + "-" + version + ".pom"));
        if (urlModelSource.getInputStream().available() != 0) {
          return urlModelSource;
        }
      } catch (MalformedURLException e) {
        throw new UnresolvableModelException(e.getMessage(), groupId, artifactId, version, e);
      } catch (IOException e) {
        // The artifact could not be fetched from the current repo, just move on and check the next
        // one.
      }
    }
    throw new UnresolvableModelException("Could not find any repositories that knew how to "
        + "resolve the artifact (checked " + Arrays.toString(repositories.toArray()) + ")",
        groupId, artifactId, version);
  }

  @Override
  public ModelSource resolveModel(Parent parent) throws UnresolvableModelException {
    return resolveModel(parent.getGroupId(), parent.getArtifactId(), parent.getVersion());
  }

  @Override
  public void addRepository(Repository repository) throws InvalidRepositoryException {
    repositories.add(repository);
  }

  @Override
  public void addRepository(Repository repository, boolean replace)
      throws InvalidRepositoryException {
    addRepository(repository);
  }

  @Override
  public ModelResolver newCopy() {
    return new DefaultModelResolver(repositories);
  }
}
@@ -694,16 +694,86 @@
 .Lists;%0A
+import com.google.devtools.build.lib.bazel.repository.MavenConnector;%0A
 import o
@@ -1771,41 +1771,328 @@
     
-try %7B%0A        UrlModelSource urlM
+UrlModelSource modelSource = getModelSource(%0A          repository.getUrl(), groupId, artifactId, version);%0A      if (modelSource != null) %7B%0A        return modelSource;%0A      %7D%0A    %7D%0A    UrlModelSource modelSource = getModelSource(%0A      MavenConnector.getMavenCentral().getUrl(), groupId, artifactId, version);%0A    if (m
 odel
@@ -2103,58 +2103,514 @@
 ce =
+=
  n
-ew UrlModelSource(new URL(repository.getUrl()%0A  
+ull) %7B%0A      throw new UnresolvableModelException(%22Could not find any repositories that knew how to %22%0A          + %22resolve the artifact (checked %22 + Arrays.toString(repositories.toArray()) + %22)%22,%0A          groupId, artifactId, version);%0A    %7D%0A    return modelSource;%0A  %7D%0A%0A  private UrlModelSource getModelSource(%0A      String url, String groupId, String artifactId, String version)%0A      throws UnresolvableModelException %7B%0A    try %7B%0A      UrlModelSource urlModelSource = new UrlModelSource(new URL(url%0A
     
@@ -2712,18 +2712,16 @@
         
-  
 + %22-%22 + 
@@ -2740,18 +2740,16 @@
 pom%22));%0A
-  
       if
@@ -2806,26 +2806,24 @@
 ) %7B%0A        
- 
- 
 return urlMo
@@ -2839,22 +2839,18 @@
 ;%0A      
-  %7D%0A  
+%7D%0A
     %7D ca
@@ -2877,26 +2877,24 @@
 eption e) %7B%0A
-  
       throw 
@@ -2978,18 +2978,16 @@
 e);%0A    
- 
- 
 %7D catch 
@@ -3000,26 +3000,24 @@
 eption e) %7B%0A
-  
       // The
@@ -3112,18 +3112,16 @@
     
-  
 // one.%0A
     
@@ -3116,18 +3116,16 @@
 // one.%0A
-  
     %7D%0A  
@@ -3130,232 +3130,19 @@
     
-%7D%0A    throw new UnresolvableModelException(%22Could not find any repositories that knew how to %22%0A        + %22resolve the artifact (checked %22 + Arrays.toString(repositories.toArray()) + %22)%22,%0A        groupId, artifactId, version)
+return null
 ;%0A  

Add repository fallback on Maven Central