package org.carlspring.strongbox.providers.repository;


import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.NoSuchAlgorithmException;
import java.util.Iterator;
import java.util.Map;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.ws.rs.core.Response;

import org.carlspring.commons.io.MultipleDigestInputStream;
import org.carlspring.strongbox.artifact.coordinates.ArtifactCoordinates;
import org.carlspring.strongbox.client.ArtifactResolver;
import org.carlspring.strongbox.client.ArtifactTransportException;

import org.carlspring.strongbox.domain.RemoteArtifactEntry;
import org.carlspring.strongbox.io.ArtifactInputStream;
import org.carlspring.strongbox.io.ArtifactOutputStream;
import org.carlspring.strongbox.providers.ProviderImplementationException;
import org.carlspring.strongbox.providers.io.RepositoryPath;
import org.carlspring.strongbox.providers.layout.LayoutProvider;
import org.carlspring.strongbox.providers.repository.proxied.LocalStorageProxyRepositoryArtifactResolver;
import org.carlspring.strongbox.providers.repository.proxied.ProxyRepositoryArtifactResolver;
import org.carlspring.strongbox.storage.Storage;
import org.carlspring.strongbox.storage.repository.Repository;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import java.io.IOException;
import java.nio.file.Files;
import java.security.NoSuchAlgorithmException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * @author carlspring
 */
@Component
public class ProxyRepositoryProvider
        extends AbstractRepositoryProvider
{

    private static final Logger logger = LoggerFactory.getLogger(ProxyRepositoryProvider.class);

    private static final String ALIAS = "proxy";

    @Inject
    @LocalStorageProxyRepositoryArtifactResolver.LocalStorageProxyRepositoryArtifactResolverQualifier
    private ProxyRepositoryArtifactResolver proxyRepositoryArtifactResolver;

    @PostConstruct
    @Override
    public void register()
    {
        repositoryProviderRegistry.addProvider(ALIAS, this);

        logger.info("Registered repository provider '" + getClass().getCanonicalName() +
                    "' with alias '" + ALIAS + "'.");
    }

    @Override
    public String getAlias()
    {
        return ALIAS;
    }

    @Override
    public ArtifactInputStream getInputStream(String storageId,
                                              String repositoryId,
                                              String path)
            throws IOException,
                   NoSuchAlgorithmException,
                   ArtifactTransportException,
                   ProviderImplementationException
    {
        return (ArtifactInputStream) proxyRepositoryArtifactResolver.getInputStream(storageId, repositoryId, path);
    }

    @Override
    public ArtifactOutputStream getOutputStream(String storageId,
                                                String repositoryId,
                                                String artifactPath)
            throws IOException,
                   NoSuchAlgorithmException
    {
        Storage storage = getConfiguration().getStorage(storageId);
        Repository repository = storage.getRepository(repositoryId);

        LayoutProvider layoutProvider = layoutProviderRegistry.getProvider(repository.getLayout());
        RepositoryPath repositoryPath = layoutProvider.resolve(repository).resolve(artifactPath);

        return (ArtifactOutputStream) Files.newOutputStream(repositoryPath);
    }

    @Override
    public Iterator<Path> search(Map<String, String> coordinates,
                                 int skip,
                                 int top,
                                 String orderBy)
    {
        return null;
    }
    
    @Override
    public List<Path> search(String storageId,
                             String repositoryId,
                             Map<String, String> coordinates,
                             int skip,
                             int limit,
                             String orderBy)
    {
        return null;
    }
    
}

@@ -69,73 +69,19 @@
 .io.
-Closeable;%0Aimport java.io.IOException;%0Aimport java.io.InputStream
+IOException
 ;%0Aim
@@ -207,38 +207,16 @@
 erator;%0A
-import java.util.Map;%0A
 import j
@@ -324,233 +324,8 @@
 ct;%0A
-import javax.ws.rs.core.Response;%0A%0Aimport org.carlspring.commons.io.MultipleDigestInputStream;%0Aimport org.carlspring.strongbox.artifact.coordinates.ArtifactCoordinates;%0Aimport org.carlspring.strongbox.client.ArtifactResolver;
 %0Aimp
@@ -391,69 +391,8 @@
 ion;
-%0A%0Aimport org.carlspring.strongbox.domain.RemoteArtifactEntry;
 %0Aimp
@@ -1018,180 +1018,8 @@
 ry;%0A
-%0Aimport javax.annotation.PostConstruct;%0Aimport javax.inject.Inject;%0Aimport java.io.IOException;%0Aimport java.nio.file.Files;%0Aimport java.security.NoSuchAlgorithmException;%0A%0A
 impo
@@ -3172,256 +3172,8 @@
    %7D
-%0A%0A    @Override%0A    public Iterator%3CPath%3E search(Map%3CString, String%3E coordinates,%0A                                 int skip,%0A                                 int top,%0A                                 String orderBy)%0A    %7B%0A        return null;%0A    %7D
 %0A   

rebase with master