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;
}
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 */
}
Aggregations