示例#1
0
  /**
   * Should serve by policies.
   *
   * @param uid the uid
   * @return true, if successful
   */
  public boolean shouldServeByPolicies(ResourceStoreRequest request) {
    if (M1ArtifactRecognizer.isMetadata(request.getRequestPath())) {
      if (M1ArtifactRecognizer.isSnapshot(request.getRequestPath())) {
        return RepositoryPolicy.SNAPSHOT.equals(getRepositoryPolicy());
      } else {
        // metadatas goes always
        return true;
      }
    }

    // we are using Gav to test the path
    Gav gav = null;

    try {
      gav = getGavCalculator().pathToGav(request.getRequestPath());
    } catch (IllegalArtifactCoordinateException e) {
      getLogger()
          .info("Illegal artifact path: '" + request.getRequestPath() + "'" + e.getMessage());

      return false;
    }

    if (gav == null) {
      return true;
    } else {
      if (gav.isSnapshot()) {
        // snapshots goes if enabled
        return RepositoryPolicy.SNAPSHOT.equals(getRepositoryPolicy());
      } else {
        return RepositoryPolicy.RELEASE.equals(getRepositoryPolicy());
      }
    }
  }
  protected boolean versionMatchesPolicy(String version, RepositoryPolicy policy) {
    boolean result = false;

    if ((RepositoryPolicy.SNAPSHOT.equals(policy) && VersionUtils.isSnapshot(version))
        || (RepositoryPolicy.RELEASE.equals(policy) && !VersionUtils.isSnapshot(version))) {
      result = true;
    }

    return result;
  }
    public boolean releaseExistsForSnapshot(Gav snapshotGav, Map<String, Object> context) {
      for (Repository repository : repositoryRegistry.getRepositories()) {
        // we need to filter for:
        // repository that is MavenRepository and is hosted or proxy
        // repository that has release policy
        if (repository.getRepositoryKind().isFacetAvailable(MavenHostedRepository.class)
            || repository.getRepositoryKind().isFacetAvailable(MavenProxyRepository.class)) {
          // actually, we don't care is it proxy or hosted, we only need to filter out groups and
          // other
          // "composite" reposes like shadows
          MavenRepository mrepository = repository.adaptToFacet(MavenRepository.class);

          // look in release reposes only
          if (mrepository.isUserManaged()
              && RepositoryPolicy.RELEASE.equals(mrepository.getRepositoryPolicy())) {
            try {
              String releaseVersion = null;

              // NEXUS-3148
              if (snapshotGav.getBaseVersion().endsWith("-SNAPSHOT")) {
                // "-SNAPSHOT" :== 9 chars
                releaseVersion =
                    snapshotGav
                        .getBaseVersion()
                        .substring(0, snapshotGav.getBaseVersion().length() - 9);
              } else {
                // "SNAPSHOT" :== 8 chars
                releaseVersion =
                    snapshotGav
                        .getBaseVersion()
                        .substring(0, snapshotGav.getBaseVersion().length() - 8);
              }

              Gav releaseGav =
                  new Gav(
                      snapshotGav.getGroupId(),
                      snapshotGav.getArtifactId(),
                      releaseVersion,
                      snapshotGav.getClassifier(),
                      snapshotGav.getExtension(),
                      null,
                      null,
                      null,
                      false,
                      null,
                      false,
                      null);

              String path = mrepository.getGavCalculator().gavToPath(releaseGav);

              ResourceStoreRequest req = new ResourceStoreRequest(path, true);

              req.getRequestContext().putAll(context);

              mrepository.retrieveItem(false, req);

              return true;
            } catch (ItemNotFoundException e) {
              // nothing
            } catch (Exception e) {
              // nothing
            }
          }
        }
      }

      return false;
    }