@Override
 public RemoteRepository getRemoteRepository(String repositoryId) throws RepositoryAdminException {
   for (RemoteRepository remoteRepository : getRemoteRepositories()) {
     if (StringUtils.equals(repositoryId, remoteRepository.getId())) {
       return remoteRepository;
     }
   }
   return null;
 }
  @Override
  public Map<String, RemoteRepository> getRemoteRepositoriesAsMap()
      throws RepositoryAdminException {
    java.util.Map<String, RemoteRepository> map = new HashMap<>();

    for (RemoteRepository repo : getRemoteRepositories()) {
      map.put(repo.getId(), repo);
    }

    return map;
  }
  @Override
  public IndexingContext createIndexContext(RemoteRepository remoteRepository)
      throws RepositoryAdminException {
    try {
      String appServerBase = getRegistry().getString("appserver.base");

      String contextKey = "remote-" + remoteRepository.getId();
      IndexingContext indexingContext = indexer.getIndexingContexts().get(contextKey);
      if (indexingContext != null) {
        return indexingContext;
      }
      // create remote repository path
      File repoDir = new File(appServerBase, "data/remotes/" + remoteRepository.getId());
      if (!repoDir.exists()) {
        repoDir.mkdirs();
      }

      File indexDirectory = null;

      // is there configured indexDirectory ?
      String indexDirectoryPath = remoteRepository.getIndexDirectory();

      if (StringUtils.isNotBlank(indexDirectoryPath)) {
        if (new File(indexDirectoryPath).isAbsolute()) {
          indexDirectory = new File(indexDirectoryPath);
        } else {
          indexDirectory = new File(repoDir, indexDirectoryPath);
        }
      }
      // if not configured use a default value
      if (indexDirectory == null) {
        indexDirectory = new File(repoDir, ".index");
      }
      if (!indexDirectory.exists()) {
        indexDirectory.mkdirs();
      }
      return indexer.addIndexingContext(
          contextKey,
          remoteRepository.getId(),
          repoDir,
          indexDirectory,
          remoteRepository.getUrl(),
          calculateIndexRemoteUrl(remoteRepository),
          mavenIndexerUtils.getAllIndexCreators());
    } catch (MalformedURLException e) {
      throw new RepositoryAdminException(e.getMessage(), e);
    } catch (IOException e) {
      throw new RepositoryAdminException(e.getMessage(), e);
    } catch (UnsupportedExistingLuceneIndexException e) {
      throw new RepositoryAdminException(e.getMessage(), e);
    }
  }
 @PreDestroy
 private void shutdown() throws RepositoryAdminException {
   try {
     List<RemoteRepository> remoteRepositories = getRemoteRepositories();
     // close index on shutdown
     for (RemoteRepository remoteRepository : remoteRepositories) {
       IndexingContext context = indexer.getIndexingContexts().get(remoteRepository.getId());
       if (context != null) {
         indexer.removeIndexingContext(context, false);
       }
     }
   } catch (IOException e) {
     throw new RepositoryAdminException(e.getMessage(), e);
   }
 }
  @Override
  public Boolean updateRemoteRepository(
      RemoteRepository remoteRepository, AuditInformation auditInformation)
      throws RepositoryAdminException {

    String repositoryId = remoteRepository.getId();

    triggerAuditEvent(repositoryId, null, AuditEvent.MODIFY_REMOTE_REPO, auditInformation);

    // update means : remove and add

    Configuration configuration = getArchivaConfiguration().getConfiguration();

    RemoteRepositoryConfiguration remoteRepositoryConfiguration =
        configuration.getRemoteRepositoriesAsMap().get(repositoryId);
    if (remoteRepositoryConfiguration == null) {
      throw new RepositoryAdminException(
          "remoteRepository with id " + repositoryId + " not exist cannot remove it");
    }

    configuration.removeRemoteRepository(remoteRepositoryConfiguration);

    remoteRepositoryConfiguration = getRemoteRepositoryConfiguration(remoteRepository);
    configuration.addRemoteRepository(remoteRepositoryConfiguration);
    saveConfiguration(configuration);

    return Boolean.TRUE;
  }
  @Override
  public Boolean addRemoteRepository(
      RemoteRepository remoteRepository, AuditInformation auditInformation)
      throws RepositoryAdminException {
    triggerAuditEvent(remoteRepository.getId(), null, AuditEvent.ADD_REMOTE_REPO, auditInformation);
    getRepositoryCommonValidator().basicValidation(remoteRepository, false);

    // TODO we can validate it's a good uri/url
    if (StringUtils.isEmpty(remoteRepository.getUrl())) {
      throw new RepositoryAdminException("url cannot be null");
    }

    // MRM-752 - url needs trimming
    remoteRepository.setUrl(StringUtils.trim(remoteRepository.getUrl()));

    RemoteRepositoryConfiguration remoteRepositoryConfiguration =
        getRemoteRepositoryConfiguration(remoteRepository);

    Configuration configuration = getArchivaConfiguration().getConfiguration();
    configuration.addRemoteRepository(remoteRepositoryConfiguration);
    saveConfiguration(configuration);

    return Boolean.TRUE;
  }
  protected String calculateIndexRemoteUrl(RemoteRepository remoteRepository) {
    if (StringUtils.startsWith(remoteRepository.getRemoteIndexUrl(), "http")) {
      String baseUrl = remoteRepository.getRemoteIndexUrl();
      return baseUrl.endsWith("/") ? StringUtils.substringBeforeLast(baseUrl, "/") : baseUrl;
    }
    String baseUrl =
        StringUtils.endsWith(remoteRepository.getUrl(), "/")
            ? StringUtils.substringBeforeLast(remoteRepository.getUrl(), "/")
            : remoteRepository.getUrl();

    baseUrl =
        StringUtils.isEmpty(remoteRepository.getRemoteIndexUrl())
            ? baseUrl + "/.index"
            : baseUrl + "/" + remoteRepository.getRemoteIndexUrl();
    return baseUrl;
  }
 @Override
 public List<RemoteRepository> getRemoteRepositories() throws RepositoryAdminException {
   List<RemoteRepository> remoteRepositories =
       new ArrayList<>(
           getArchivaConfiguration().getConfiguration().getRemoteRepositories().size());
   for (RemoteRepositoryConfiguration repositoryConfiguration :
       getArchivaConfiguration().getConfiguration().getRemoteRepositories()) {
     RemoteRepository remoteRepository =
         new RemoteRepository(
             repositoryConfiguration.getId(),
             repositoryConfiguration.getName(),
             repositoryConfiguration.getUrl(),
             repositoryConfiguration.getLayout(),
             repositoryConfiguration.getUsername(),
             repositoryConfiguration.getPassword(),
             repositoryConfiguration.getTimeout());
     remoteRepository.setDownloadRemoteIndex(repositoryConfiguration.isDownloadRemoteIndex());
     remoteRepository.setRemoteIndexUrl(repositoryConfiguration.getRemoteIndexUrl());
     remoteRepository.setCronExpression(repositoryConfiguration.getRefreshCronExpression());
     remoteRepository.setIndexDirectory(repositoryConfiguration.getIndexDir());
     remoteRepository.setRemoteDownloadNetworkProxyId(
         repositoryConfiguration.getRemoteDownloadNetworkProxyId());
     remoteRepository.setRemoteDownloadTimeout(repositoryConfiguration.getRemoteDownloadTimeout());
     remoteRepository.setDownloadRemoteIndexOnStartup(
         repositoryConfiguration.isDownloadRemoteIndexOnStartup());
     remoteRepository.setDescription(repositoryConfiguration.getDescription());
     remoteRepository.setExtraHeaders(repositoryConfiguration.getExtraHeaders());
     remoteRepository.setExtraParameters(repositoryConfiguration.getExtraParameters());
     remoteRepositories.add(remoteRepository);
   }
   return remoteRepositories;
 }
 private RemoteRepositoryConfiguration getRemoteRepositoryConfiguration(
     RemoteRepository remoteRepository) {
   RemoteRepositoryConfiguration remoteRepositoryConfiguration =
       new RemoteRepositoryConfiguration();
   remoteRepositoryConfiguration.setId(remoteRepository.getId());
   remoteRepositoryConfiguration.setPassword(remoteRepository.getPassword());
   remoteRepositoryConfiguration.setTimeout(remoteRepository.getTimeout());
   remoteRepositoryConfiguration.setUrl(remoteRepository.getUrl());
   remoteRepositoryConfiguration.setUsername(remoteRepository.getUserName());
   remoteRepositoryConfiguration.setLayout(remoteRepository.getLayout());
   remoteRepositoryConfiguration.setName(remoteRepository.getName());
   remoteRepositoryConfiguration.setDownloadRemoteIndex(remoteRepository.isDownloadRemoteIndex());
   remoteRepositoryConfiguration.setRemoteIndexUrl(remoteRepository.getRemoteIndexUrl());
   remoteRepositoryConfiguration.setRefreshCronExpression(remoteRepository.getCronExpression());
   remoteRepositoryConfiguration.setIndexDir(remoteRepository.getIndexDirectory());
   remoteRepositoryConfiguration.setRemoteDownloadNetworkProxyId(
       remoteRepository.getRemoteDownloadNetworkProxyId());
   remoteRepositoryConfiguration.setRemoteDownloadTimeout(
       remoteRepository.getRemoteDownloadTimeout());
   remoteRepositoryConfiguration.setDownloadRemoteIndexOnStartup(
       remoteRepository.isDownloadRemoteIndexOnStartup());
   remoteRepositoryConfiguration.setDescription(remoteRepository.getDescription());
   remoteRepositoryConfiguration.setExtraHeaders(remoteRepository.getExtraHeaders());
   remoteRepositoryConfiguration.setExtraParameters(remoteRepository.getExtraParameters());
   return remoteRepositoryConfiguration;
 }