public static String getRepositoryPolicy(Repository repository) { if (repository.getRepositoryKind().isFacetAvailable(MavenRepository.class)) { return repository.adaptToFacet(MavenRepository.class).getRepositoryPolicy().toString(); } else { return null; } }
@BenchmarkOptions(benchmarkRounds = 1, warmupRounds = 0) @Test public void validateRetieveItemWithLastAccessUpdateTimeDelay() throws Exception { // need to ensure we wait 100 ms int wait = 100; ((DefaultAttributesHandler) repository.getAttributesHandler()).setLastRequestedResolution(wait); Thread.sleep(wait); AttributeStorage attributeStorageSpy = Mockito.spy(repository.getAttributesHandler().getAttributeStorage()); repository.getAttributesHandler().setAttributeStorage(attributeStorageSpy); ResourceStoreRequest resourceRequest = new ResourceStoreRequest(testFilePath); resourceRequest.getRequestContext().put(AccessManager.REQUEST_REMOTE_ADDRESS, "127.0.0.1"); AbstractStorageItem storageItem = localRepositoryStorageUnderTest.retrieveItem(repository, resourceRequest); MatcherAssert.assertThat(storageItem, Matchers.notNullValue()); MatcherAssert.assertThat( storageItem.getLastRequested(), Matchers.greaterThan(originalLastAccessTime)); Mockito.verify(attributeStorageSpy, Mockito.times(1)) .putAttributes(Mockito.<RepositoryItemUid>any(), Mockito.<Attributes>any()); Mockito.verify(attributeStorageSpy, Mockito.times(1)) .getAttributes(Mockito.<RepositoryItemUid>any()); }
protected Repository getRepositoryForPathPrefixOrId( String pathPrefixOrId, Class<? extends Repository> kind) throws NoSuchRepositoryException { List<? extends Repository> repositories = repositoryRegistry.getRepositoriesWithFacet(kind); Repository idMatched = null; Repository pathPrefixMatched = null; for (Repository repository : repositories) { if (StringUtils.equals(repository.getId(), pathPrefixOrId)) { idMatched = repository; } if (StringUtils.equals(repository.getPathPrefix(), pathPrefixOrId)) { pathPrefixMatched = repository; } } if (idMatched != null) { // id wins return idMatched; } if (pathPrefixMatched != null) { // if no id found, prefix wins return pathPrefixMatched; } // nothing found throw new NoSuchRepositoryException("pathPrefixOrId: '" + pathPrefixOrId + "'"); }
// clean protected RepositoryResourceResponse getRepositoryResourceResponse(Request request, String repoId) throws ResourceException { RepositoryResourceResponse result = new RepositoryResourceResponse(); try { RepositoryBaseResource resource = null; Repository repository = getRepositoryRegistry().getRepository(repoId); if (repository.getRepositoryKind().isFacetAvailable(GroupRepository.class)) { // it is a group, not a repo throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "Repository Not Found"); } resource = getRepositoryRestModel(request, repository); result.setData(resource); } catch (NoSuchRepositoryAccessException e) { getLogger().warn("Repository access denied, id=" + repoId); throw new ResourceException(Status.CLIENT_ERROR_FORBIDDEN, "Access Denied to Repository"); } catch (NoSuchRepositoryException e) { getLogger().warn("Repository not found, id=" + repoId); throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "Repository Not Found"); } return result; }
@Override public ManagedRepositories getManagedRepositories() { final ManagedRepositories managedRepositories = new ManagedRepositories(); final Set<String> managedRepositoryIds = new HashSet<String>(this.groupManagementPluginConfiguration.getManagedRepositories()); for (final Repository repository : this.repositoryRegistry.getRepositories()) { final ManagedRepository managedRepository = createManagedRepository(repository); if (managedRepositoryIds.remove(repository.getId())) { managedRepositories.addManagedRepository(managedRepository); } else { managedRepositories.addUnmanagedRepository(managedRepository); } } if (!managedRepositoryIds.isEmpty()) { getLogger() .warn( "The following managed repository IDs no longer exist as repositories in Nexus: " + managedRepositoryIds); } return managedRepositories; }
@BenchmarkOptions(benchmarkRounds = 10, warmupRounds = 1) @Test public void validateRetieveItemWithOutLastAccessUpdateTimeDelay() throws LocalStorageException, ItemNotFoundException, IOException { // do the test, but make sure the _iterations_ will not get out of the "resolution" span! // Note: this test is just broken as is, what guarantees it will finish the cycles in given X // millis? // Meaning, the assertions + rounds is wrong. ((DefaultAttributesHandler) repository.getAttributesHandler()) .setLastRequestedResolution(30000L); AttributeStorage attributeStorageSpy = Mockito.spy(repository.getAttributesHandler().getAttributeStorage()); repository.getAttributesHandler().setAttributeStorage(attributeStorageSpy); ResourceStoreRequest resourceRequest = new ResourceStoreRequest(testFilePath); resourceRequest.getRequestContext().put(AccessManager.REQUEST_REMOTE_ADDRESS, "127.0.0.1"); AbstractStorageItem storageItem = localRepositoryStorageUnderTest.retrieveItem(repository, resourceRequest); MatcherAssert.assertThat(storageItem, Matchers.notNullValue()); MatcherAssert.assertThat( storageItem.getLastRequested(), Matchers.equalTo(originalLastAccessTime)); Mockito.verify(attributeStorageSpy, Mockito.times(0)) .putAttributes(Mockito.<RepositoryItemUid>any(), Mockito.<Attributes>any()); Mockito.verify(attributeStorageSpy, Mockito.times(1)) .getAttributes(Mockito.<RepositoryItemUid>any()); }
protected void dropRepositories() { for (Repository repository : repositoryRegistry.getRepositories()) { try { repositoryRegistry.removeRepositorySilently(repository.getId()); } catch (NoSuchRepositoryException e) { // will not happen } } }
@Override protected ResourceStore getResourceStore() throws NoSuchResourceStoreException, Exception { Repository repo1 = getRepositoryRegistry().getRepository("repo1"); repo1.setWritePolicy(RepositoryWritePolicy.ALLOW_WRITE); getApplicationConfiguration().saveConfiguration(); return repo1; }
private void inspectForTimeline(Event<?> evt, Repository repository) { // we do not want RSS entries about boot and repo additions during boot StringBuilder sb = new StringBuilder(); if (repository.getRepositoryKind().isFacetAvailable(GroupRepository.class)) { sb.append(" repository group "); } else { sb.append(" repository "); } sb.append(repository.getName()); sb.append(" (ID="); sb.append(repository.getId()); sb.append(") "); if (repository.getRepositoryKind().isFacetAvailable(ProxyRepository.class)) { sb.append(" as proxy repository for URL "); sb.append(repository.adaptToFacet(ProxyRepository.class).getRemoteUrl()); } else if (repository.getRepositoryKind().isFacetAvailable(HostedRepository.class)) { sb.append(" as hosted repository"); } else if (repository.getRepositoryKind().isFacetAvailable(ShadowRepository.class)) { sb.append(" as "); sb.append(repository.getClass().getName()); sb.append(" virtual repository for "); sb.append(repository.adaptToFacet(ShadowRepository.class).getMasterRepository().getName()); sb.append(" (ID="); sb.append(repository.adaptToFacet(ShadowRepository.class).getMasterRepository().getId()); sb.append(") "); } sb.append("."); if (evt instanceof RepositoryRegistryEventAdd) { sb.insert(0, "Registered"); } else if (evt instanceof RepositoryRegistryEventRemove) { sb.insert(0, "Unregistered"); } else if (evt instanceof RepositoryConfigurationUpdatedEvent) { sb.insert(0, "Updated"); } getFeedRecorder().addSystemEvent(FeedRecorder.SYSTEM_CONFIG_ACTION, sb.toString()); }
@Override public Collection<StorageItem> list() throws AccessDeniedException, NoSuchResourceStoreException, IllegalOperationException, ItemNotFoundException, StorageException { if (isVirtual()) { return getStore().list(getResourceStoreRequest()); } else { Repository repo = getRepositoryItemUid().getRepository(); Collection<StorageItem> result = repo.list(false, this); correctPaths(result); return result; } }
protected MavenRepository getMavenRepository(String id) throws ResourceException { try { Repository repository = getRepositoryRegistry().getRepository(id); if (!repository.getRepositoryKind().isFacetAvailable(MavenRepository.class)) { throw new ResourceException( Status.CLIENT_ERROR_BAD_REQUEST, "This is not a Maven repository!"); } return repository.adaptToFacet(MavenRepository.class); } catch (NoSuchRepositoryException e) { throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, e.getMessage()); } }
// clean public String getRestRepoType(Repository repository) { if (repository.getRepositoryKind().isFacetAvailable(ProxyRepository.class)) { return RepositoryBaseResourceConverter.REPO_TYPE_PROXIED; } else if (repository.getRepositoryKind().isFacetAvailable(HostedRepository.class)) { return RepositoryBaseResourceConverter.REPO_TYPE_HOSTED; } else if (repository.getRepositoryKind().isFacetAvailable(ShadowRepository.class)) { return RepositoryBaseResourceConverter.REPO_TYPE_VIRTUAL; } else if (repository.getRepositoryKind().isFacetAvailable(GroupRepository.class)) { return RepositoryBaseResourceConverter.REPO_TYPE_GROUP; } else { throw new IllegalArgumentException( "The passed model with class" + repository.getClass().getName() + " is not recognized!"); } }
@Override protected YumRepository doRun() throws Exception { String repositoryId = getRepositoryId(); if (!StringUtils.isEmpty(repositoryId)) { checkState( yumRegistry.isRegistered(repositoryId), "Metadata regeneration can only be run on repositories that have an enabled 'Yum: Generate Metadata' capability"); Yum yum = yumRegistry.get(repositoryId); checkState( yum.getNexusRepository().getRepositoryKind().isFacetAvailable(HostedRepository.class), "Metadata generation can only be run on hosted repositories"); } setDefaults(); LOG.debug("Generating Yum-Repository for '{}' ...", getRpmDir()); try { getRepoDir().mkdirs(); File rpmListFile = createRpmListFile(); commandLineExecutor.exec(buildCreateRepositoryCommand(rpmListFile)); if (isUseAbsoluteUrls() && StringUtils.isNotBlank(getRpmUrl())) { replaceUrlInRepomdXml(); } } catch (IOException e) { LOG.warn("Yum metadata generation failed", e); throw new IOException("Yum metadata generation failed", e); } // TODO dubious Thread.sleep(100); final Repository repository = findRepository(); if (repository != null) { final MavenRepository mavenRepository = repository.adaptToFacet(MavenRepository.class); if (mavenRepository != null) { try { routingManager.forceUpdatePrefixFile(mavenRepository); } catch (Exception e) { logger.warn("Could not update Whitelist for repository '{}'", mavenRepository, e); } } } regenerateMetadataForGroups(); return new YumRepositoryImpl(getRepoDir(), repositoryId, getVersion()); }
@Override public void doApplyConfiguration( Repository repository, ApplicationConfiguration configuration, CRepositoryCoreConfiguration coreConfiguration) throws ConfigurationException { super.doApplyConfiguration(repository, configuration, coreConfiguration); if (repository.getRepositoryKind().isFacetAvailable(ProxyRepository.class)) { ProxyRepository proxy = repository.adaptToFacet(ProxyRepository.class); proxy.getItemContentValidators().put("checksum", checksumValidator); proxy.getItemContentValidators().put("filetypevalidator", fileTypeItemContentValidator); } }
@Before public void setup() throws Exception { ApplicationConfiguration applicationConfiguration = this.lookup(ApplicationConfiguration.class); File repositoryStorageDir = applicationConfiguration.getWorkingDirectory("proxy/store/test-repo"); // adding one hosted repository = this.lookup(Repository.class, "maven2"); CRepository repoConf = new DefaultCRepository(); repoConf.setProviderRole(Repository.class.getName()); repoConf.setProviderHint("maven2"); repoConf.setId("test-repo"); repoConf.setLocalStorage(new CLocalStorage()); repoConf.getLocalStorage().setProvider("file"); repoConf.getLocalStorage().setUrl(repositoryStorageDir.toURI().toURL().toString()); Xpp3Dom exRepo = new Xpp3Dom("externalConfiguration"); repoConf.setExternalConfiguration(exRepo); M2RepositoryConfiguration exRepoConf = new M2RepositoryConfiguration(exRepo); exRepoConf.setRepositoryPolicy(RepositoryPolicy.RELEASE); exRepoConf.setChecksumPolicy(ChecksumPolicy.STRICT_IF_EXISTS); repository.configure(repoConf); applicationConfiguration.getConfigurationModel().addRepository(repoConf); this.lookup(RepositoryRegistry.class).addRepository(repository); localRepositoryStorageUnderTest = repository.getLocalStorage(); // write a test file File testFile = new File(repositoryStorageDir, testFilePath); testFile.getParentFile().mkdirs(); org.codehaus.plexus.util.FileUtils.fileWrite(testFile, "CONTENT"); // this test expects "old" behaviour: ((DefaultAttributesHandler) repository.getAttributesHandler()).setLastRequestedResolution(0); // prime the retrieve originalLastAccessTime = primeLastRequestedTimestamp(); // sleep so we are sure the clock is different when we validate the last update time. Thread.sleep(11); }
protected String extractRepositoryKind(Repository repository) { RepositoryKind kind = repository.getRepositoryKind(); if (kind.isFacetAvailable(HostedRepository.class)) { return "hosted"; } else if (kind.isFacetAvailable(ProxyRepository.class)) { return "proxy"; } else if (kind.isFacetAvailable(GroupRepository.class)) { return "group"; } else if (kind.isFacetAvailable(ShadowRepository.class)) { return "virtual"; } else { // huh? return repository.getRepositoryKind().getMainFacet().getName(); } }
@Override public void doApplyConfiguration( final Repository repository, final ApplicationConfiguration configuration, final CRepositoryCoreConfiguration coreConfig) throws ConfigurationException { repository.setIndexable(false); super.doApplyConfiguration(repository, configuration, coreConfig); final CRemoteStorage remoteStorage = coreConfig.getConfiguration(true).getRemoteStorage(); if (remoteStorage != null) { // // FIXME: on the fly upgrade, if needed // // it will trigger if detects that nexus.xml contains remoteUrl _with_ OBR XML file // String[] siteAndPath = ObrUtils.splitObrSiteAndPath( remoteStorage.getUrl(), false ); // // if ( siteAndPath[1] != null ) // { // // upgrade needed! // ( (ObrProxyRepository) repository ).setObrPath( siteAndPath[1] ); // // // write back the stripped URL // remoteStorage.setUrl( siteAndPath[0] ); // } // FIXME: this should happen in this super's class: AbstractProxyRepositoryConfigurator try { ((ObrProxyRepository) repository).setRemoteUrl(remoteStorage.getUrl()); } catch (final StorageException e) { throw new ConfigurationException( "Cannot configure OBR Proxy Repository! " + remoteStorage.getUrl(), e); } } }
private void regenerateMetadataForGroups() { if (StringUtils.isBlank(getVersion())) { try { final Repository repository = repositoryRegistry.getRepository(getRepositoryId()); for (GroupRepository groupRepository : repositoryRegistry.getGroupsOfRepository(repository)) { if (yumRegistry.isRegistered(repository.getId())) { MergeMetadataTask.createTaskFor(nexusScheduler, groupRepository); } } } catch (NoSuchRepositoryException e) { logger.warn( "Repository '{}' does not exist anymore. Backing out from triggering group merge for it.", getRepositoryId()); } } }
private List<Repository> filterAccessToRepositories( Collection<? extends Repository> repositories) { if (repositories == null) { return null; } List<Repository> filteredRepositories = new ArrayList<Repository>(); for (Repository repository : repositories) { if (this.itemAuthorizer.isViewable( NexusItemAuthorizer.VIEW_REPOSITORY_KEY, repository.getId())) { filteredRepositories.add(repository); } } return filteredRepositories; }
private void doIt(final Map<String, String> properties) { final Repository repo = getRepository(TouchTestCapabilityDescriptor.FIELD_REPO_OR_GROUP_ID, properties); try { repo.storeItem( new ResourceStoreRequest("/capability/test.txt"), new ByteArrayInputStream( ("capabilities test!\n" + properties.get(TouchTestCapabilityDescriptor.FIELD_MSG_ID) + "\n" + properties.get(TouchTestCapabilityDescriptor.FIELD_REPO_OR_GROUP_ID)) .getBytes()), null); } catch (Exception e) { throw new RuntimeException(e.getMessage(), e); } }
@Override public boolean isCompatible(Repository repository) { if (super.isCompatible(repository) && MavenRepository.class.isAssignableFrom(repository.getClass()) && getRepositoryPolicy().equals(((MavenRepository) repository).getRepositoryPolicy())) { return true; } return false; }
@BenchmarkOptions(benchmarkRounds = 10, warmupRounds = 1) @Test public void validateRetieveItemWithoutLastAccessUpdate() throws LocalStorageException, ItemNotFoundException, IOException { AttributeStorage attributeStorageSpy = Mockito.spy(repository.getAttributesHandler().getAttributeStorage()); repository.getAttributesHandler().setAttributeStorage(attributeStorageSpy); ResourceStoreRequest resourceRequest = new ResourceStoreRequest(testFilePath); AbstractStorageItem storageItem = localRepositoryStorageUnderTest.retrieveItem(repository, resourceRequest); MatcherAssert.assertThat(storageItem, Matchers.notNullValue()); MatcherAssert.assertThat( storageItem.getLastRequested(), Matchers.equalTo(originalLastAccessTime)); Mockito.verify(attributeStorageSpy, Mockito.times(1)) .getAttributes(Mockito.<RepositoryItemUid>any()); }
protected void addRepositoryDetails( Request request, SearchNGResponse response, Repository repository) { boolean add = true; for (NexusNGRepositoryDetail repoDetail : response.getRepoDetails()) { if (repoDetail.getRepositoryId().equals(repository.getId())) { add = false; break; } } if (add) { NexusNGRepositoryDetail repoDetail = new NexusNGRepositoryDetail(); repoDetail.setRepositoryId(repository.getId()); repoDetail.setRepositoryName(repository.getName()); repoDetail.setRepositoryURL( createRepositoryReference(request, repository.getId()).getTargetRef().toString()); repoDetail.setRepositoryContentClass(repository.getRepositoryContentClass().getId()); repoDetail.setRepositoryKind(extractRepositoryKind(repository)); MavenRepository mavenRepo = repository.adaptToFacet(MavenRepository.class); if (mavenRepo != null) { repoDetail.setRepositoryPolicy(mavenRepo.getRepositoryPolicy().name()); } response.addRepoDetail(repoDetail); } }
protected void setDefaults() throws MalformedURLException, URISyntaxException { final Repository repository = findRepository(); if (isBlank(getRpmDir()) && repository != null) { setRpmDir(RepositoryUtils.getBaseDir(repository).getAbsolutePath()); } if (isBlank(getRpmUrl()) && repository != null) { final String rpmUrl = repositoryURLBuilder.getExposedRepositoryContentUrl(repository, true); if (StringUtils.isBlank(rpmUrl)) { throw new IllegalStateException( SimpleFormat.format( "Not able to build content URL of the repository \"%s\" [id=%s], baseUrl not set!", repository.getName(), repository.getId())); } setRpmUrl(rpmUrl); } if (isBlank(getParameter(PARAM_REPO_DIR)) && isNotBlank(getRpmDir())) { setRepoDir(new File(getRpmDir())); } if (isBlank(getRepoUrl()) && isNotBlank(getRpmUrl())) { setRepoUrl(getRpmUrl()); } }
protected void inspect(Event<?> evt) { if (!isNexusStarted()) { return; } Repository repository = null; if (evt instanceof RepositoryRegistryRepositoryEvent) { repository = ((RepositoryRegistryRepositoryEvent) evt).getRepository(); } else { repository = ((RepositoryConfigurationUpdatedEvent) evt).getRepository(); } try { // check registry for existence, wont be able to do much // if doesn't exist yet repoRegistry.getRepository(repository.getId()); inspectForTimeline(evt, repository); } catch (NoSuchRepositoryException e) { log.debug("Attempted to handle repository that isn't yet in registry"); } }
public SnapshotRemovalResult removeSnapshots(SnapshotRemovalRequest request) throws NoSuchRepositoryException, IllegalArgumentException { SnapshotRemovalResult result = new SnapshotRemovalResult(); logDetails(request); if (request.getRepositoryId() != null) { Repository repository = getRepositoryRegistry().getRepository(request.getRepositoryId()); if (!process(request, result, repository)) { throw new IllegalArgumentException( "The repository with ID=" + repository.getId() + " is not valid for Snapshot Removal Task!"); } } else { for (Repository repository : getRepositoryRegistry().getRepositories()) { process(request, result, repository); } } return result; }
@Override public ContentLocator generateContent(Repository repository, String path, StorageFileItem item) throws IllegalOperationException, ItemNotFoundException, LocalStorageException { // make length unknown (since it will be known only in the moment of actual content pull) item.setLength(-1); return new ArchetypeContentLocator( repository.getId(), ((DefaultIndexerManager) indexerManager).getRepositoryIndexContext(repository), macPlugin, new ArtifactInfoFilter() { public boolean accepts(IndexingContext ctx, ArtifactInfo ai) { return indexArtifactFilter.filterArtifactInfo(ai); } }); }
private String generateCatalogPayload(IndexingContext context) throws IOException { final MacRequest req = new MacRequest(repository.getId(), repositoryContentUrl, artifactInfoFilter); // NEXUS-5216: Warn if indexing context is null (indexable=false) for given repository but // continue // to return the correct empty catalog if (context == null) { logger.info( "Archetype Catalog for repository {} is not buildable as it lacks IndexingContext (indexable=false?).", RepositoryStringUtils.getHumanizedNameString(repository)); } // get the catalog final ArchetypeCatalog catalog = macPlugin.listArcherypesAsCatalog(req, context); // serialize it to XML final StringWriter sw = new StringWriter(); final ArchetypeCatalogXpp3Writer writer = new ArchetypeCatalogXpp3Writer(); writer.write(sw, catalog); return sw.toString(); }
private boolean process( SnapshotRemovalRequest request, SnapshotRemovalResult result, Repository repository) { // only from maven repositories, stay silent for others and simply skip if (!repository.getRepositoryContentClass().isCompatible(contentClass)) { getLogger().debug("Skipping '" + repository.getId() + "' is not a maven 2 repository"); return false; } if (!repository.getLocalStatus().shouldServiceRequest()) { getLogger().debug("Skipping '" + repository.getId() + "' the repository is out of service"); return false; } if (repository.getRepositoryKind().isFacetAvailable(GroupRepository.class)) { process(request, result, repository.adaptToFacet(GroupRepository.class)); } else if (repository.getRepositoryKind().isFacetAvailable(MavenRepository.class)) { result.addResult( removeSnapshotsFromMavenRepository( repository.adaptToFacet(MavenRepository.class), request)); } return true; }
protected boolean isRepositoryHandled(final Repository repository) { return applicationStatusSource.getSystemStatus().isNexusStarted() && repository != null && repository.getRepositoryKind().isFacetAvailable(MavenProxyRepository.class); }