use of org.apache.archiva.indexer.merger.base.MergedRemoteIndexesTaskRequest in project archiva by apache.
the class ArchivaDavResourceFactory method buildMergedIndexDirectory.
protected StorageAsset buildMergedIndexDirectory(String activePrincipal, DavServletRequest request, RepositoryGroup repositoryGroup) throws DavException {
try {
final List<ManagedRepository> repositories = repositoryGroup.getRepositories();
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<>();
}
final String id = repositoryGroup.getId();
TemporaryGroupIndex tmp = temporaryGroupIndexMap.get(id);
if (tmp != null && tmp.getDirectory() != null && tmp.getDirectory().exists()) {
if (System.currentTimeMillis() - tmp.getCreationTime() > (repositoryGroup.getMergedIndexTTL() * 60 * 1000)) {
log.debug(MarkerFactory.getMarker("group.merged.index"), "tmp group index '{}' is too old so delete it", id);
indexMerger.cleanTemporaryGroupIndex(tmp);
} else {
log.debug(MarkerFactory.getMarker("group.merged.index"), "merged index for group '{}' found in cache", id);
return tmp.getDirectory();
}
}
Set<String> authzRepos = new HashSet<String>();
String permission = WebdavMethodUtil.getMethodPermission(request.getMethod());
for (ManagedRepository repository : repositories) {
try {
if (servletAuth.isAuthorized(activePrincipal, repository.getId(), permission)) {
authzRepos.add(repository.getId());
authzRepos.addAll(this.repositorySearch.getRemoteIndexingContextIds(repository.getId()));
}
} 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 '{}'", id, authzRepos);
IndexCreationFeature indexCreationFeature = repositoryGroup.getFeature(IndexCreationFeature.class);
Path indexPath = indexCreationFeature.getLocalIndexPath().getFilePath();
if (indexPath != null) {
Path tempRepoFile = Files.createTempDirectory("temp");
tempRepoFile.toFile().deleteOnExit();
FilesystemStorage storage = new FilesystemStorage(tempRepoFile, new DefaultFileLockManager());
StorageAsset tmpAsset = storage.getRoot();
IndexMergerRequest indexMergerRequest = new IndexMergerRequest(authzRepos, true, id, indexPath.toString(), repositoryGroup.getMergedIndexTTL()).mergedIndexDirectory(tmpAsset).temporary(true);
MergedRemoteIndexesTaskRequest taskRequest = new MergedRemoteIndexesTaskRequest(indexMergerRequest, indexMerger);
MergedRemoteIndexesTask job = new MergedRemoteIndexesTask(taskRequest);
ArchivaIndexingContext indexingContext = job.execute().getIndexingContext();
StorageAsset mergedRepoDir = indexingContext.getPath();
TemporaryGroupIndex temporaryGroupIndex = new TemporaryGroupIndex(mergedRepoDir, indexingContext.getId(), id, //
repositoryGroup.getMergedIndexTTL()).setCreationTime(new Date().getTime());
temporaryGroupIndexMap.put(id, temporaryGroupIndex);
session.setAttribute(TemporaryGroupIndexSessionCleaner.TEMPORARY_INDEX_SESSION_KEY, temporaryGroupIndexMap);
return mergedRepoDir;
} else {
log.error("Local index path for repository group {} does not exist.", repositoryGroup.getId());
throw new DavException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
} 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