use of org.apache.archiva.indexer.merger.MergedRemoteIndexesTaskRequest in project archiva by apache.
the class ArchivaDavResourceFactory method buildMergedIndexDirectory.
protected Path buildMergedIndexDirectory(List<String> repositories, String activePrincipal, DavServletRequest request, RepositoryGroupConfiguration repositoryGroupConfiguration) throws DavException {
try {
HttpSession session = request.getSession();
@SuppressWarnings("unchecked") Map<String, TemporaryGroupIndex> temporaryGroupIndexMap = (Map<String, TemporaryGroupIndex>) session.getAttribute(TemporaryGroupIndexSessionCleaner.TEMPORARY_INDEX_SESSION_KEY);
if (temporaryGroupIndexMap == null) {
temporaryGroupIndexMap = new HashMap<>();
}
TemporaryGroupIndex tmp = temporaryGroupIndexMap.get(repositoryGroupConfiguration.getId());
if (tmp != null && tmp.getDirectory() != null && Files.exists(tmp.getDirectory())) {
if (System.currentTimeMillis() - tmp.getCreationTime() > (repositoryGroupConfiguration.getMergedIndexTtl() * 60 * 1000)) {
log.debug(MarkerFactory.getMarker("group.merged.index"), "tmp group index '{}' is too old so delete it", repositoryGroupConfiguration.getId());
indexMerger.cleanTemporaryGroupIndex(tmp);
} else {
log.debug(MarkerFactory.getMarker("group.merged.index"), "merged index for group '{}' found in cache", repositoryGroupConfiguration.getId());
return tmp.getDirectory();
}
}
Set<String> authzRepos = new HashSet<String>();
String permission = WebdavMethodUtil.getMethodPermission(request.getMethod());
for (String repository : repositories) {
try {
if (servletAuth.isAuthorized(activePrincipal, repository, permission)) {
authzRepos.add(repository);
authzRepos.addAll(this.repositorySearch.getRemoteIndexingContextIds(repository));
}
} catch (UnauthorizedException e) {
// TODO: review exception handling
log.debug("Skipping repository '{}' for user '{}': {}", repository, activePrincipal, e.getMessage());
}
}
log.info("generate temporary merged index for repository group '{}' for repositories '{}'", repositoryGroupConfiguration.getId(), authzRepos);
Path tempRepoFile = Files.createTempDirectory("temp");
tempRepoFile.toFile().deleteOnExit();
IndexMergerRequest indexMergerRequest = new IndexMergerRequest(authzRepos, true, repositoryGroupConfiguration.getId(), repositoryGroupConfiguration.getMergedIndexPath(), repositoryGroupConfiguration.getMergedIndexTtl()).mergedIndexDirectory(tempRepoFile).temporary(true);
MergedRemoteIndexesTaskRequest taskRequest = new MergedRemoteIndexesTaskRequest(indexMergerRequest, indexMerger);
MergedRemoteIndexesTask job = new MergedRemoteIndexesTask(taskRequest);
IndexingContext indexingContext = job.execute().getIndexingContext();
Path mergedRepoDir = indexingContext.getIndexDirectoryFile().toPath();
TemporaryGroupIndex temporaryGroupIndex = new TemporaryGroupIndex(mergedRepoDir, indexingContext.getId(), repositoryGroupConfiguration.getId(), //
repositoryGroupConfiguration.getMergedIndexTtl()).setCreationTime(new Date().getTime());
temporaryGroupIndexMap.put(repositoryGroupConfiguration.getId(), temporaryGroupIndex);
session.setAttribute(TemporaryGroupIndexSessionCleaner.TEMPORARY_INDEX_SESSION_KEY, temporaryGroupIndexMap);
return mergedRepoDir;
} catch (RepositorySearchException e) {
throw new DavException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);
} catch (IndexMergerException e) {
throw new DavException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);
} catch (IOException e) {
throw new DavException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);
}
}
Aggregations