private P2Repo resolveLocalP2RepoFromUrl(String url) {
   String rpp = StringUtils.removeStart(url, "local://");
   // rpp = rpp.substring(0, rpp.indexOf('/'));
   RepoPath repoPath = RepoPathFactory.create(rpp);
   LocalRepoDescriptor localRepoDescriptor =
       centralConfig.getMutableDescriptor().getLocalRepositoriesMap().get(repoPath.getRepoKey());
   if (localRepoDescriptor != null) {
     return new P2Repo(null, repoPath.getRepoKey(), url);
   }
   return null;
 }
  private RepoPath getMetadataRepoPath(RepoPath parentRepoPath, String metadataName) {
    String path = parentRepoPath.getPath();
    boolean alreadyMetadataPath = NamingUtils.isMetadata(path);
    if (alreadyMetadataPath) {
      log.warn("Path {} is already a metadata path.", path);
    }
    // TODO: [by yl] Evaluate the impact of normalizing the path to use the standard metadata format
    // (a.jar#mdname)

    return InternalRepoPathFactory.create(
        parentRepoPath.getRepoKey(),
        alreadyMetadataPath ? path : NamingUtils.getMetadataPath(path, metadataName));
  }
 /**
  * @return The last build number for snapshot version. 0 if maven-metadata not found for the path.
  */
 private int getLastBuildNumber(RepoPath repoPath) {
   int buildNumber = 0;
   try {
     // get the parent path which should contains the maven-metadata
     RepoPath parentRepoPath = repoPath.getParent();
     RepositoryService repoService = ContextHelper.get().getRepositoryService();
     RepoPathImpl mavenMetadataPath =
         new RepoPathImpl(parentRepoPath, MavenNaming.MAVEN_METADATA_NAME);
     if (repoService.exists(mavenMetadataPath)) {
       String mavenMetadataStr = repoService.getStringContent(mavenMetadataPath);
       Metadata metadata = MavenModelUtils.toMavenMetadata(mavenMetadataStr);
       Versioning versioning = metadata.getVersioning();
       if (versioning != null) {
         Snapshot snapshot = versioning.getSnapshot();
         if (snapshot != null) {
           buildNumber = snapshot.getBuildNumber();
         }
       }
     } else {
       // ok probably not found. just log
       log.debug("No maven metadata found for {}.", repoPath);
     }
   } catch (Exception e) {
     log.error("Cannot obtain build number from metadata.", e);
   }
   return buildNumber;
 }
  private String adjustUsingClientTimestamp(MavenSnapshotVersionAdapterContext context) {
    // artifact was uploaded with a timestamp, we use it for the unique snapshot timestamp and to
    // locate build number
    long timestamp = context.getTimestamp();
    String uniqueTimestamp = MavenModelUtils.dateToUniqueSnapshotTimestamp(new Date(timestamp));
    RepoPath repoPath = context.getRepoPath();
    String existingArtifact = findSnapshotFileByTimestamp(repoPath.getParent(), uniqueTimestamp);
    int buildNumber;
    if (existingArtifact != null) {
      buildNumber = MavenNaming.getUniqueSnapshotVersionBuildNumber(existingArtifact);
    } else {
      buildNumber = getLastBuildNumber(repoPath) + 1;
    }

    return buildUniqueSnapshotFileName(buildNumber, uniqueTimestamp, context.getModuleInfo());
  }
  private String adjustUsingServerData(MavenSnapshotVersionAdapterContext context) {
    // Get the latest build number from the metadata
    RepoPath repoPath = context.getRepoPath();
    String filePath = repoPath.getPath();
    int metadataBuildNumber = getLastBuildNumber(repoPath);
    int nextBuildNumber = metadataBuildNumber + 1;

    RepoPath parentPath = repoPath.getParent();

    // determine if the next build number should be the one read from the metadata
    ModuleInfo moduleInfo = context.getModuleInfo();
    String classifier = moduleInfo.getClassifier();
    boolean isPomChecksum =
        MavenNaming.isChecksum(filePath) && MavenNaming.isPom(PathUtils.stripExtension(filePath));
    if (metadataBuildNumber > 0 && (StringUtils.isNotBlank(classifier) || isPomChecksum)) {
      // pom checksums and artifacts with classifier are always deployed after the pom (which
      // triggers the
      // maven-metadata.xml calculation) so use the metadata build number
      nextBuildNumber = metadataBuildNumber;
    }
    if (metadataBuildNumber > 0 && MavenNaming.isPom(filePath)) {
      // the metadata might already represent an existing main artifact (in case the
      // maven-metadata.xml was deployed after the main artifact and before the pom/classifier)
      // so first check if there's already a file with the next build number
      if (findSnapshotFileByBuildNumber(parentPath, metadataBuildNumber + 1) == null) {
        // no files deployed with the next build number so either this is a pom deployment (parent
        // pom)
        // or this is a pom of a main artifact for which the maven-metadata.xml was deployed before
        // this pom
        if (findSnapshotPomFile(parentPath, metadataBuildNumber) == null) {
          // this is a pom attached to a main artifact deployed after maven-metadata.xml
          nextBuildNumber = metadataBuildNumber;
        }
      }
    }

    String timestamp = getSnapshotTimestamp(parentPath, nextBuildNumber);
    if (timestamp == null) {
      // probably the first deployed file for this build, use now for the timestamp
      timestamp = MavenModelUtils.dateToUniqueSnapshotTimestamp(new Date());
    }
    return buildUniqueSnapshotFileName(nextBuildNumber, timestamp, moduleInfo);
  }
  private String adjustChecksum(MavenSnapshotVersionAdapterContext context) {
    // find latest unique file matching the checksum coordinates
    RepositoryService repoService = ContextHelper.get().getRepositoryService();
    RepoPath repoPath = context.getRepoPath();
    RepoPath parentRepoPath = repoPath.getParent();
    RepoDescriptor repoDescriptor = repoService.repoDescriptorByKey(parentRepoPath.getRepoKey());
    RepoLayout repoLayout = repoDescriptor.getRepoLayout();

    String latestMatching = null;

    String originalChecksumRequestPath = repoPath.getPath();
    String originalRequestPathWithNoChecksum =
        PathUtils.stripExtension(originalChecksumRequestPath);

    if (repoService.exists(parentRepoPath)) {
      List<String> children = repoService.getChildrenNames(parentRepoPath);
      for (String child : children) {
        if (MavenNaming.isUniqueSnapshotFileName(child)) {

          ModuleInfo childModule =
              repoService.getItemModuleInfo(InternalRepoPathFactory.create(parentRepoPath, child));
          String fileRevisionIntegration = childModule.getFileIntegrationRevision();

          // Try to construct a new non-unique path as a descriptor
          String nonUniquePath =
              replaceIntegration(
                  ModuleInfoUtils.constructDescriptorPath(childModule, repoLayout, true),
                  fileRevisionIntegration);

          // If the path as a descriptor doesn't match, perhaps it's an artifact path
          if (!nonUniquePath.equals(originalRequestPathWithNoChecksum)) {
            // Try to construct a new non-unique path as an artifact
            nonUniquePath =
                replaceIntegration(
                    ModuleInfoUtils.constructArtifactPath(childModule, repoLayout),
                    fileRevisionIntegration);
          }

          if (nonUniquePath.equals(originalRequestPathWithNoChecksum)) {
            if (latestMatching == null
                || MavenNaming.getUniqueSnapshotVersionBuildNumber(latestMatching)
                    < MavenNaming.getUniqueSnapshotVersionBuildNumber(child)) {
              latestMatching = child;
            }
          }
        }
      }
    }

    // if latest not found, return invalid path which will fail and return a message to the client
    String timestamp =
        latestMatching != null
            ? MavenNaming.getUniqueSnapshotVersionTimestamp(latestMatching)
            : System.currentTimeMillis() + "";
    int buildNumber =
        latestMatching != null
            ? MavenNaming.getUniqueSnapshotVersionBuildNumber(latestMatching)
            : 0;

    // use the timestamp and build number from it. if not found return something that will fail?
    return buildUniqueSnapshotFileName(buildNumber, timestamp, context.getModuleInfo());
  }
 public MetadataInfoImpl(RepoPath repoPath) {
   this.repoPath = repoPath;
   this.name = NamingUtils.getMetadataName(repoPath.getPath());
   this.checksumsInfo = new ChecksumsInfo();
 }