/*******************************************************************************
 * Copyright (c) 2014 Red Hat, Inc..
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 *
 * Contributors:
 *     Red Hat, Inc. - initial API and implementation
 ******************************************************************************/

package org.commonjava.maven.ext.manip.io;

import org.apache.maven.model.Repository;
import org.apache.maven.model.building.FileModelSource;
import org.apache.maven.model.building.ModelSource;
import org.apache.maven.model.resolution.InvalidRepositoryException;
import org.apache.maven.model.resolution.ModelResolver;
import org.apache.maven.model.resolution.UnresolvableModelException;
import org.commonjava.maven.atlas.ident.ref.ArtifactRef;
import org.commonjava.maven.atlas.ident.ref.ProjectVersionRef;
import org.commonjava.maven.ext.manip.resolver.GalleyAPIWrapper;
import org.commonjava.maven.galley.TransferException;
import org.commonjava.maven.galley.model.Transfer;

public class GalleyModelResolver
    implements ModelResolver
{

    private final GalleyAPIWrapper galleyWrapper;

    public GalleyModelResolver( final GalleyAPIWrapper galleyWrapper )
    {
        this.galleyWrapper = galleyWrapper;
    }

    @Override
    public ModelSource resolveModel( final String groupId, final String artifactId, final String version )
        throws UnresolvableModelException
    {
        Transfer transfer;
        ArtifactRef ar = new ProjectVersionRef( groupId, artifactId, version ).asPomArtifact();
        try
        {
            transfer =
                galleyWrapper.resolveArtifact( ar );
        }
        catch ( final TransferException e )
        {
            throw new UnresolvableModelException( "Failed to resolve POM: " + e.getMessage(), groupId, artifactId,
                                                  version, e );
        }
        if ( transfer == null )
        {
            throw new UnresolvableModelException( "Failed to resolve POM: " + ar, groupId, artifactId, version );
        }

        return new FileModelSource( transfer.getDetachedFile() );
    }

    @Override
    public void addRepository( final Repository repository )
        throws InvalidRepositoryException
    {
        // disregard.
        // FIXME: Is this dangerous, given the legacy of pom-declared repos??
    }

    @Override
    public ModelResolver newCopy()
    {
        // no state here, so we can keep this instance.
        return this;
    }

}

@@ -559,42 +559,96 @@
 ort 
-org.apache.maven.model.Repository;
+java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.net.MalformedURLException;%0A
 %0Aimp
@@ -678,32 +678,18 @@
 del.
-building.FileModelSource
+Repository
 ;%0Aim
@@ -1221,16 +1221,66 @@
 ransfer;
+%0Aimport org.commonjava.maven.galley.util.UrlUtils;
 %0A%0Apublic
@@ -1721,16 +1721,22 @@
         
+final 
 Artifact
@@ -2352,20 +2352,24 @@
 urn new 
-File
+Transfer
 ModelSou
@@ -2385,26 +2385,8 @@
 sfer
-.getDetachedFile()
  );%0A
@@ -2733,16 +2733,16 @@
 stance.%0A
-
         
@@ -2761,10 +2761,899 @@
 %0A    %7D%0A%0A
+    private static final class TransferModelSource%0A        implements ModelSource%0A    %7B%0A%0A        private final Transfer transfer;%0A%0A        public TransferModelSource( final Transfer transfer )%0A        %7B%0A            this.transfer = transfer;%0A        %7D%0A%0A        @Override%0A        public InputStream getInputStream()%0A            throws IOException%0A        %7B%0A            return transfer.openInputStream();%0A        %7D%0A%0A        @Override%0A        public String getLocation()%0A        %7B%0A            String location = null;%0A            try%0A            %7B%0A                location = UrlUtils.buildUrl( transfer.getLocation()%0A                                                      .getUri(), transfer.getPath() );%0A            %7D%0A            catch ( final MalformedURLException e )%0A            %7B%0A                location = transfer.toString();%0A            %7D%0A%0A            return location;%0A        %7D%0A%0A    %7D%0A%0A
 %7D%0A

Use a ModelSource that's specific to Transfers.