Search in sources :

Example 1 with UrlInfo

use of org.commonjava.indy.util.UrlInfo in project indy by Commonjava.

the class ProxyResponseWriter method getRepository.

private RemoteRepository getRepository(final URL url) throws IndyDataException {
    int port = url.getPort();
    if (port < 1) {
        port = url.getDefaultPort();
    }
    String name = PROXY_REPO_PREFIX + url.getHost().replace('.', '-') + '_' + port;
    final String baseUrl = String.format("%s://%s:%s/", url.getProtocol(), url.getHost(), port);
    ArtifactStoreQuery<RemoteRepository> query = storeManager.query().packageType(GENERIC_PKG_KEY).storeType(RemoteRepository.class);
    RemoteRepository remote = query.getRemoteRepositoryByUrl(baseUrl);
    if (remote == null) {
        logger.debug("Looking for remote repo with name: {}", name);
        remote = query.getByName(name);
        // if repo with this name already exists, it has a different url, so we need to use a different name
        int i = 1;
        while (remote != null) {
            name = PROXY_REPO_PREFIX + url.getHost().replace('.', '-') + "_" + i++;
            logger.debug("Looking for remote repo with name: {}", name);
            remote = query.getByName(name);
        }
    }
    if (remote == null) {
        logger.debug("Creating remote repository for HTTProx request: {}", url);
        final UrlInfo info = new UrlInfo(url.toExternalForm());
        if (repoCreator == null) {
            throw new IndyDataException("No valid instance of ProxyRepositoryCreator. Cannot auto-create remote proxy to: '{}'", baseUrl);
        }
        final UserPass up = UserPass.parse(ApplicationHeader.authorization, httpRequest, url.getAuthority());
        remote = repoCreator.create(name, baseUrl, info, up, LoggerFactory.getLogger(repoCreator.getClass()));
        remote.setMetadata(ArtifactStore.METADATA_ORIGIN, ProxyAcceptHandler.HTTPROX_ORIGIN);
        final ChangeSummary changeSummary = new ChangeSummary(ChangeSummary.SYSTEM_USER, "Creating HTTProx proxy for: " + info.getUrl());
        storeManager.storeArtifactStore(remote, changeSummary, false, true, new EventMetadata());
    }
    return remote;
}
Also used : IndyDataException(org.commonjava.indy.data.IndyDataException) UrlInfo(org.commonjava.indy.util.UrlInfo) RemoteRepository(org.commonjava.indy.model.core.RemoteRepository) UserPass(org.commonjava.indy.subsys.http.util.UserPass) ChangeSummary(org.commonjava.indy.audit.ChangeSummary) EventMetadata(org.commonjava.maven.galley.event.EventMetadata)

Example 2 with UrlInfo

use of org.commonjava.indy.util.UrlInfo in project indy by Commonjava.

the class MemoryArtifactStoreQuery method getRemoteRepositoryByUrl.

@Override
public RemoteRepository getRemoteRepositoryByUrl(String url) throws IndyDataException {
    /*
           This filter does these things:
             * First compare ip, if ip same, and the path(without last slash) same too, the repo is found
             * If ip not same, then compare the url without scheme and last slash (if has) to find the repo
         */
    UrlInfo temp = null;
    try {
        temp = new UrlInfo(url);
    } catch (IllegalArgumentException error) {
        logger.error("Failed to find repository for: '{}'. Reason: {}", error, url, error.getMessage());
    }
    final UrlInfo urlInfo = temp;
    /* @formatter:off */
    return new MemoryArtifactStoreQuery<>(dataManager, packageType, enabled, RemoteRepository.class).stream(store -> {
        if ((remote == store.getType()) && urlInfo != null) {
            final String targetUrl = ((RemoteRepository) store).getUrl();
            UrlInfo targetUrlInfo = null;
            try {
                targetUrlInfo = new UrlInfo(targetUrl);
            } catch (IllegalArgumentException error) {
                logger.error("Failed to find repository for: '{}'. Reason: {}", error, targetUrl, error.getMessage());
            }
            if (targetUrlInfo != null) {
                String ipForUrl = null;
                String ipForTargetUrl = null;
                try {
                    ipForUrl = urlInfo.getIpForUrl();
                    ipForTargetUrl = targetUrlInfo.getIpForUrl();
                    if (ipForUrl != null && ipForUrl.equals(ipForTargetUrl)) {
                        if (urlInfo.getFileWithNoLastSlash().equals(targetUrlInfo.getFileWithNoLastSlash())) {
                            logger.debug("Repository found because of same ip, url is {}, store key is {}", url, store.getKey());
                            return true;
                        } else {
                            return false;
                        }
                    }
                } catch (UnknownHostException ue) {
                    logger.warn("Failed to filter remote: ip fetch error.", ue);
                }
                logger.debug("ip not same: ip for url:{}-{}; ip for searching repo: {}-{}", url, ipForUrl, store.getKey(), ipForTargetUrl);
                if (urlInfo.getUrlWithNoSchemeAndLastSlash().equals(targetUrlInfo.getUrlWithNoSchemeAndLastSlash())) {
                    logger.debug("Repository found because of same host, url is {}, store key is {}", url, store.getKey());
                    return true;
                }
            }
        }
        return false;
    }).findFirst().orElse(null);
/* @formatter:on */
}
Also used : UrlInfo(org.commonjava.indy.util.UrlInfo) UnknownHostException(java.net.UnknownHostException) RemoteRepository(org.commonjava.indy.model.core.RemoteRepository)

Aggregations

RemoteRepository (org.commonjava.indy.model.core.RemoteRepository)2 UrlInfo (org.commonjava.indy.util.UrlInfo)2 UnknownHostException (java.net.UnknownHostException)1 ChangeSummary (org.commonjava.indy.audit.ChangeSummary)1 IndyDataException (org.commonjava.indy.data.IndyDataException)1 UserPass (org.commonjava.indy.subsys.http.util.UserPass)1 EventMetadata (org.commonjava.maven.galley.event.EventMetadata)1