use of org.apache.archiva.admin.model.beans.RemoteRepository in project archiva by apache.
the class ArchivaDavResourceFactory method createResource.
@Override
public DavResource createResource(final DavResourceLocator locator, final DavServletRequest request, final DavServletResponse response) throws DavException {
ArchivaDavResourceLocator archivaLocator = checkLocatorIsInstanceOfRepositoryLocator(locator);
RepositoryGroupConfiguration repoGroupConfig = archivaConfiguration.getConfiguration().getRepositoryGroupsAsMap().get(archivaLocator.getRepositoryId());
String activePrincipal = getActivePrincipal(request);
List<String> resourcesInAbsolutePath = new ArrayList<>();
boolean readMethod = WebdavMethodUtil.isReadMethod(request.getMethod());
DavResource resource;
if (repoGroupConfig != null) {
if (!readMethod) {
throw new DavException(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "Write method not allowed for repository groups.");
}
log.debug("Repository group '{}' accessed by '{}", repoGroupConfig.getId(), activePrincipal);
// handle browse requests for virtual repos
if (getLogicalResource(archivaLocator, null, true).endsWith("/")) {
DavResource davResource = getResourceFromGroup(request, repoGroupConfig.getRepositories(), archivaLocator, repoGroupConfig);
setHeaders(response, locator, davResource, true);
return davResource;
} else {
// make a copy to avoid potential concurrent modifications (eg. by configuration)
// TODO: ultimately, locking might be more efficient than copying in this fashion since updates are
// infrequent
List<String> repositories = new ArrayList<>(repoGroupConfig.getRepositories());
resource = processRepositoryGroup(request, archivaLocator, repositories, activePrincipal, resourcesInAbsolutePath, repoGroupConfig);
}
} else {
try {
RemoteRepository remoteRepository = remoteRepositoryAdmin.getRemoteRepository(archivaLocator.getRepositoryId());
if (remoteRepository != null) {
String logicalResource = getLogicalResource(archivaLocator, null, false);
IndexingContext indexingContext = remoteRepositoryAdmin.createIndexContext(remoteRepository);
Path resourceFile = StringUtils.equals(logicalResource, "/") ? Paths.get(indexingContext.getIndexDirectoryFile().getParent()) : Paths.get(indexingContext.getIndexDirectoryFile().getParent(), logicalResource);
resource = new //
ArchivaDavResource(//
resourceFile.toAbsolutePath().toString(), //
locator.getResourcePath(), //
null, //
request.getRemoteAddr(), //
activePrincipal, //
request.getDavSession(), //
archivaLocator, //
this, //
mimeTypes, //
auditListeners, //
scheduler, fileLockManager);
setHeaders(response, locator, resource, false);
return resource;
}
} catch (RepositoryAdminException e) {
log.debug("RepositoryException remote repository with d'{}' not found, msg: {}", archivaLocator.getRepositoryId(), e.getMessage());
}
ManagedRepository repo = repositoryRegistry.getManagedRepository(archivaLocator.getRepositoryId());
if (repo == null) {
throw new DavException(HttpServletResponse.SC_NOT_FOUND, "Invalid repository: " + archivaLocator.getRepositoryId());
}
ManagedRepositoryContent managedRepositoryContent = repo.getContent();
if (managedRepositoryContent == null) {
log.error("Inconsistency detected. Repository content not found for '{}'", archivaLocator.getRepositoryId());
throw new DavException(HttpServletResponse.SC_NOT_FOUND, "Invalid repository: " + archivaLocator.getRepositoryId());
}
log.debug("Managed repository '{}' accessed by '{}'", managedRepositoryContent.getId(), activePrincipal);
resource = processRepository(request, archivaLocator, activePrincipal, managedRepositoryContent, repo);
String logicalResource = getLogicalResource(archivaLocator, null, false);
resourcesInAbsolutePath.add(Paths.get(managedRepositoryContent.getRepoRoot(), logicalResource).toAbsolutePath().toString());
}
String requestedResource = request.getRequestURI();
// merge metadata only when requested via the repo group
if ((repositoryRequest.isMetadata(requestedResource) || repositoryRequest.isMetadataSupportFile(requestedResource)) && repoGroupConfig != null) {
// this should only be at the project level not version level!
if (isProjectReference(requestedResource)) {
ArchivaDavResource res = (ArchivaDavResource) resource;
String filePath = StringUtils.substringBeforeLast(res.getLocalResource().toAbsolutePath().toString().replace('\\', '/'), "/");
filePath = filePath + "/maven-metadata-" + repoGroupConfig.getId() + ".xml";
// for MRM-872 handle checksums of the merged metadata files
if (repositoryRequest.isSupportFile(requestedResource)) {
Path metadataChecksum = Paths.get(filePath + "." + StringUtils.substringAfterLast(requestedResource, "."));
if (Files.exists(metadataChecksum)) {
LogicalResource logicalResource = new LogicalResource(getLogicalResource(archivaLocator, null, false));
resource = new ArchivaDavResource(metadataChecksum.toAbsolutePath().toString(), logicalResource.getPath(), null, request.getRemoteAddr(), activePrincipal, request.getDavSession(), archivaLocator, this, mimeTypes, auditListeners, scheduler, fileLockManager);
}
} else {
if (resourcesInAbsolutePath != null && resourcesInAbsolutePath.size() > 1) {
// merge the metadata of all repos under group
ArchivaRepositoryMetadata mergedMetadata = new ArchivaRepositoryMetadata();
for (String resourceAbsPath : resourcesInAbsolutePath) {
try {
Path metadataFile = Paths.get(resourceAbsPath);
ArchivaRepositoryMetadata repoMetadata = MavenMetadataReader.read(metadataFile);
mergedMetadata = RepositoryMetadataMerge.merge(mergedMetadata, repoMetadata);
} catch (XMLException e) {
throw new DavException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Error occurred while reading metadata file.");
} catch (RepositoryMetadataException r) {
throw new DavException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Error occurred while merging metadata file.");
}
}
try {
Path resourceFile = writeMergedMetadataToFile(mergedMetadata, filePath);
LogicalResource logicalResource = new LogicalResource(getLogicalResource(archivaLocator, null, false));
resource = new ArchivaDavResource(resourceFile.toAbsolutePath().toString(), logicalResource.getPath(), null, request.getRemoteAddr(), activePrincipal, request.getDavSession(), archivaLocator, this, mimeTypes, auditListeners, scheduler, fileLockManager);
} catch (RepositoryMetadataException r) {
throw new DavException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Error occurred while writing metadata file.");
} catch (IOException ie) {
throw new DavException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Error occurred while generating checksum files.");
} catch (DigesterException de) {
throw new DavException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Error occurred while generating checksum files." + de.getMessage());
}
}
}
}
}
setHeaders(response, locator, resource, false);
// compatibility with MRM-440 to ensure browsing the repository works ok
if (resource.isCollection() && !request.getRequestURI().endsWith("/")) {
throw new BrowserRedirectException(resource.getHref());
}
resource.addLockManager(lockManager);
return resource;
}
use of org.apache.archiva.admin.model.beans.RemoteRepository in project archiva by apache.
the class DownloadArtifactsTest method downloadWithRemoteRedirect.
@Test
public void downloadWithRemoteRedirect() throws Exception {
RemoteRepository remoteRepository = getRemoteRepositoriesService().getRemoteRepository("central");
remoteRepository.setUrl("http://localhost:" + redirectPort);
getRemoteRepositoriesService().updateRemoteRepository(remoteRepository);
RoleManagementService roleManagementService = getRoleManagementService(authorizationHeader);
if (!roleManagementService.templatedRoleExists(ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, "internal")) {
roleManagementService.createTemplatedRole(ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, "internal");
}
getUserService(authorizationHeader).createGuestUser();
roleManagementService.assignRole(ArchivaRoleConstants.TEMPLATE_GUEST, "guest");
roleManagementService.assignTemplatedRole(ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, "internal", "guest");
getUserService(authorizationHeader).removeFromCache("guest");
Path file = Paths.get("target/junit-4.9.jar");
Files.deleteIfExists(file);
HttpWagon httpWagon = new HttpWagon();
httpWagon.connect(new Repository("foo", "http://localhost:" + port));
httpWagon.get("repository/internal/junit/junit/4.9/junit-4.9.jar", file.toFile());
ZipFile zipFile = new ZipFile(file.toFile());
List<String> entries = getZipEntriesNames(zipFile);
ZipEntry zipEntry = zipFile.getEntry("org/junit/runners/JUnit4.class");
assertNotNull("cannot find zipEntry org/junit/runners/JUnit4.class, entries: " + entries + ", content is: " + FileUtils.readFileToString(file.toFile(), Charset.forName("UTF-8")), zipEntry);
zipFile.close();
file.toFile().deleteOnExit();
}
use of org.apache.archiva.admin.model.beans.RemoteRepository in project archiva by apache.
the class DownloadMergedIndexTest method downloadMergedIndex.
@Test
public void downloadMergedIndex() throws Exception {
Path tmpIndexDir = Paths.get(System.getProperty("java.io.tmpdir"), "tmpIndex");
if (Files.exists(tmpIndexDir)) {
FileUtils.deleteDirectory(tmpIndexDir.toFile());
}
String id = Long.toString(System.currentTimeMillis());
ManagedRepository managedRepository = new ManagedRepository(Locale.getDefault());
managedRepository.setId(id);
managedRepository.setName("name of " + id);
managedRepository.setLocation(System.getProperty("basedir") + "/src/test/repositories/test-repo");
managedRepository.setIndexDirectory(System.getProperty("java.io.tmpdir") + "/tmpIndex/" + id);
ManagedRepositoriesService managedRepositoriesService = getManagedRepositoriesService();
if (managedRepositoriesService.getManagedRepository(id) != null) {
managedRepositoriesService.deleteManagedRepository(id, false);
}
getManagedRepositoriesService().addManagedRepository(managedRepository);
RepositoriesService repositoriesService = getRepositoriesService();
repositoriesService.scanRepositoryNow(id, true);
// wait a bit to ensure index is finished
int timeout = 20000;
while (timeout > 0 && repositoriesService.alreadyScanning(id)) {
Thread.sleep(500);
timeout -= 500;
}
RepositoryGroupService repositoryGroupService = getRepositoryGroupService();
String repoGroupId = "test-group";
if (repositoryGroupService.getRepositoryGroup(repoGroupId) != null) {
repositoryGroupService.deleteRepositoryGroup(repoGroupId);
}
RepositoryGroup repositoryGroup = new RepositoryGroup();
repositoryGroup.setId(repoGroupId);
repositoryGroup.setRepositories(Arrays.asList(id));
repositoryGroupService.addRepositoryGroup(repositoryGroup);
// create a repo with a remote on the one with index
id = Long.toString(System.currentTimeMillis());
managedRepository = new ManagedRepository(Locale.getDefault());
managedRepository.setId(id);
managedRepository.setName("name of " + id);
managedRepository.setLocation(System.getProperty("basedir") + "/src/test/repositories/test-repo");
managedRepository.setIndexDirectory(System.getProperty("java.io.tmpdir") + "/tmpIndex/" + id);
if (managedRepositoriesService.getManagedRepository(id) != null) {
managedRepositoriesService.deleteManagedRepository(id, false);
}
getManagedRepositoriesService().addManagedRepository(managedRepository);
RemoteRepository remoteRepository = new RemoteRepository(Locale.getDefault());
remoteRepository.setId("all-merged");
remoteRepository.setName("all-merged");
remoteRepository.setDownloadRemoteIndex(true);
remoteRepository.setUrl("http://localhost:" + port + "/repository/test-group");
remoteRepository.setRemoteIndexUrl("http://localhost:" + port + "/repository/test-group/.index");
remoteRepository.setUserName(RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME);
remoteRepository.setPassword(FakeCreateAdminService.ADMIN_TEST_PWD);
if (getRemoteRepositoriesService().getRemoteRepository(remoteRepository.getId()) != null) {
getRemoteRepositoriesService().deleteRemoteRepository(remoteRepository.getId());
}
getRemoteRepositoriesService().addRemoteRepository(remoteRepository);
ProxyConnectorService proxyConnectorService = getProxyConnectorService();
ProxyConnector proxyConnector = new ProxyConnector();
proxyConnector.setProxyId("foo-bar1");
proxyConnector.setSourceRepoId(id);
proxyConnector.setTargetRepoId("all-merged");
proxyConnectorService.addProxyConnector(proxyConnector);
repositoriesService.scheduleDownloadRemoteIndex("all-merged", true, true);
// wait a bit
timeout = 20000;
while (timeout > 0) {
Thread.sleep(500);
timeout -= 500;
}
SearchService searchService = getSearchService();
SearchRequest request = new SearchRequest();
request.setRepositories(Arrays.asList(id));
request.setGroupId("org.apache.felix");
List<Artifact> artifacts = searchService.searchArtifacts(request);
assertThat(artifacts).isNotNull().isNotEmpty().hasSize(1);
}
use of org.apache.archiva.admin.model.beans.RemoteRepository in project archiva by apache.
the class RemoteRepositoryConnectivityCheckTest method checkRemoteConnectivityFail.
@Test
public void checkRemoteConnectivityFail() throws Exception {
RemoteRepositoriesService service = getRemoteRepositoriesService();
WebClient.client(service).header("Authorization", authorizationHeader);
try {
RemoteRepository repo = getRemoteRepository();
repo.setUrl("http://localhost:8956");
service.addRemoteRepository(repo);
assertThat(service.checkRemoteConnectivity(repo.getId())).isFalse();
} finally {
service.deleteRemoteRepository("id-new");
}
}
use of org.apache.archiva.admin.model.beans.RemoteRepository in project archiva by apache.
the class RemoteRepositoryConnectivityCheckTest method checkRemoteConnectivityEmptyRemote.
@Test
public void checkRemoteConnectivityEmptyRemote() throws Exception {
Path tmpDir = Files.createTempDirectory("test");
Server repoServer = buildStaticServer(tmpDir);
ServerConnector serverConnector = new ServerConnector(repoServer, new HttpConnectionFactory());
repoServer.addConnector(serverConnector);
repoServer.start();
RemoteRepositoriesService service = getRemoteRepositoriesService();
WebClient.client(service).header("Authorization", authorizationHeader);
try {
int repoServerPort = serverConnector.getLocalPort();
RemoteRepository repo = getRemoteRepository();
repo.setUrl("http://localhost:" + repoServerPort);
service.addRemoteRepository(repo);
assertThat(service.checkRemoteConnectivity(repo.getId())).isTrue();
} finally {
service.deleteRemoteRepository("id-new");
org.apache.archiva.common.utils.FileUtils.deleteQuietly(tmpDir);
repoServer.stop();
}
}
Aggregations