Java Code Examples for org.sonatype.nexus.repository.Repository#facet()

The following examples show how to use org.sonatype.nexus.repository.Repository#facet() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RawUploadHandler.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected List<Content> getResponseContents(final Repository repository, final Map<String, PartPayload> pathToPayload)
    throws IOException
{
  RawContentFacet facet = repository.facet(RawContentFacet.class);

  List<Content> responseContents = Lists.newArrayList();
  UnitOfWork.begin(repository.facet(StorageFacet.class).txSupplier());
  try {
    for (Entry<String,PartPayload> entry : pathToPayload.entrySet()) {
      String path = entry.getKey();

      Content content = facet.put(path, entry.getValue());

      responseContents.add(content);
    }
  }
  finally {
    UnitOfWork.end();
  }
  return responseContents;
}
 
Example 2
Source File: OrientMetadataUtils.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Writes passed in metadata as XML.
 */
public static void write(final Repository repository, final MavenPath mavenPath, final Metadata metadata)
    throws IOException
{
  MavenFacet mavenFacet = repository.facet(MavenFacet.class);
  final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  MavenModels.writeMetadata(buffer, metadata);
  mavenFacet.put(mavenPath, new BytesPayload(buffer.toByteArray(),
      MavenMimeRulesSource.METADATA_TYPE));
  final Map<HashAlgorithm, HashCode> hashCodes = mavenFacet.get(mavenPath).getAttributes()
      .require(Content.CONTENT_HASH_CODES_MAP, Content.T_CONTENT_HASH_CODES_MAP);
  checkState(hashCodes != null, "hashCodes");
  for (HashType hashType : HashType.values()) {
    MavenPath checksumPath = mavenPath.hash(hashType);
    HashCode hashCode = hashCodes.get(hashType.getHashAlgorithm());
    checkState(hashCode != null, "hashCode: type=%s", hashType);
    mavenFacet.put(checksumPath, new StringPayload(hashCode.toString(), Constants.CHECKSUM_CONTENT_TYPE));
  }
}
 
Example 3
Source File: GroupHandler.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns the first OK response from member repositories or {@link HttpResponses#notFound()} if none of the members
 * responded with OK.
 */
protected Response getFirst(@Nonnull final Context context,
                            @Nonnull final List<Repository> members,
                            @Nonnull final DispatchedRepositories dispatched)
    throws Exception
{
  final Request request = context.getRequest();
  for (Repository member : members) {
    log.trace("Trying member: {}", member);
    // track repositories we have dispatched to, prevent circular dispatch for nested groups
    if (dispatched.contains(member)) {
      log.trace("Skipping already dispatched member: {}", member);
      continue;
    }
    dispatched.add(member);

    final ViewFacet view = member.facet(ViewFacet.class);
    final Response response = view.dispatch(request, context);
    log.trace("Member {} response {}", member, response.getStatus());
    if (isValidResponse(response)) {
      return response;
    }
  }
  return notFoundResponse(context);
}
 
Example 4
Source File: AptSnapshotHandler.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private Response handleSnapshotAdminRequest(final Context context, final String id) throws Exception {
  String method = context.getRequest().getAction();
  Repository repository = context.getRepository();
  AptSnapshotFacet snapshotFacet = repository.facet(AptSnapshotFacet.class);

  switch (method) {
    case MKCOL:
      return doMkcol(id, snapshotFacet);
    case PUT:
      return doPut(context, id, snapshotFacet);
    case DELETE:
      return doDelete(id, snapshotFacet);
    default:
      return HttpResponses.methodNotAllowed(method, DELETE, MKCOL, PUT);
  }
}
 
Example 5
Source File: RawUploadHandler.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected List<Content> getResponseContents(final Repository repository, final Map<String, PartPayload> pathToPayload)
    throws IOException
{
  RawContentFacet facet = repository.facet(RawContentFacet.class);

  List<Content> responseContents = Lists.newArrayList();
  for (Entry<String,PartPayload> entry : pathToPayload.entrySet()) {
    String path = entry.getKey();

    Content content = new Content(facet.put(path, entry.getValue()));

    responseContents.add(content);
  }
  return responseContents;
}
 
Example 6
Source File: RawITSupport.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
protected Content read(final Repository repository, final String path) throws IOException {
  RawContentFacet rawFacet = repository.facet(RawContentFacet.class);
  UnitOfWork.begin(repository.facet(StorageFacet.class).txSupplier());
  try {
    return rawFacet.get(path);
  }
  finally {
    UnitOfWork.end();
  }
}
 
Example 7
Source File: DatastoreRebuildBrowseNodeService.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void rebuild(final Repository repo, final BooleanSupplier isCancelled)
    throws RebuildBrowseNodeFailedException
{
  browseNodeManager.deleteByRepository(repo);

  log.info("Rebuilding browse nodes for repository {}", repo.getName());

  ProgressLogIntervalHelper progressLogger = new ProgressLogIntervalHelper(log, 60);

  try {
    Stopwatch sw = Stopwatch.createStarted();
    long processed = 0;

    ContentFacet contentFacet = repo.facet(ContentFacet.class);
    Continuation<FluentAsset> assetContinuation = contentFacet.assets().browse(browseLimit, null);

    while (!assetContinuation.isEmpty()) {
      checkContinuation(isCancelled, repo);

      browseNodeManager.createFromAssets(repo, assetContinuation);

      processed += assetContinuation.size();
      long elapsed = sw.elapsed(TimeUnit.MILLISECONDS);
      progressLogger.info("Rebuilt browse nodes for {} assets in {} ms", processed, elapsed);

      assetContinuation = contentFacet.assets().browse(browseLimit, assetContinuation.nextContinuationToken());
    }
  }
  catch (Exception ex) {
    throw new RebuildBrowseNodeFailedException("Could not re-create browse nodes", ex);
  }
  finally {
    progressLogger.flush(); // ensure final rebuild message is flushed
    log.info("Browse Node Rebuild ended for {}", repo.getName());
  }
}
 
Example 8
Source File: PublishMavenIndexTask.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected void execute(final Repository repository) {
  MavenIndexFacet mavenIndexFacet = repository.facet(MavenIndexFacet.class);
  try {
    mavenIndexFacet.publishIndex();
  }
  catch (IOException e) {
    throw new RuntimeException(e);
  }
}
 
Example 9
Source File: AptSnapshotHandler.java    From nexus-repository-apt with Eclipse Public License 1.0 5 votes vote down vote up
private Response handleSnapshotAdminRequest(Context context, String id) throws Exception {
  String method = context.getRequest().getAction();
  Repository repository = context.getRepository();
  AptSnapshotFacet snapshotFacet = repository.facet(AptSnapshotFacet.class);

  switch (method) {
    case MKCOL: {
      snapshotFacet.createSnapshot(id, new AllSnapshotComponentSelector());
      return HttpResponses.created();
    }

    case PUT: {
      try (InputStream is = context.getRequest().getPayload().openInputStream()) {
        ControlFile settings = new ControlFileParser().parseControlFile(is);
        snapshotFacet.createSnapshot(id, new FilteredSnapshotComponentSelector(settings));
      }
      return HttpResponses.created();
    }

    case DELETE: {
      snapshotFacet.deleteSnapshot(id);
      return HttpResponses.noContent();
    }

    default:
      return HttpResponses.methodNotAllowed(method, DELETE, MKCOL, PUT);
  }
}
 
Example 10
Source File: AptUploadHandler.java    From nexus-repository-apt with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public UploadResponse handle(Repository repository, ComponentUpload upload) throws IOException {
  AptHostedFacet hostedFacet = repository.facet(AptHostedFacet.class);
  StorageFacet storageFacet = repository.facet(StorageFacet.class);

  try (TempBlob tempBlob = storageFacet.createTempBlob(upload.getAssetUploads().get(0).getPayload(), FacetHelper.hashAlgorithms)) {
    ControlFile controlFile = AptPackageParser.parsePackage(tempBlob);
    if (controlFile == null) {
      throw new IOException("Invalid debian package:  no control file");
    }
    String name = controlFile.getField("Package").map(f -> f.value).get();
    String version = controlFile.getField("Version").map(f -> f.value).get();
    String architecture = controlFile.getField("Architecture").map(f -> f.value).get();
    String assetPath = FacetHelper.buildAssetPath(name, version, architecture);

    doValidation(repository, assetPath);

    UnitOfWork.begin(storageFacet.txSupplier());
    try {
      Asset asset = hostedFacet.ingestAsset(upload.getAssetUploads().get(0).getPayload());
      return new UploadResponse(asset);
    }
    catch (PGPException e) {
      throw new IOException(e);
    }
    finally {
      UnitOfWork.end();
    }
  }
}
 
Example 11
Source File: OrientMavenTestHelper.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public Payload read(final Repository repository, final String path) throws IOException {
  final MavenFacet mavenFacet = repository.facet(MavenFacet.class);
  final MavenPath mavenPath = mavenFacet.getMavenPathParser().parsePath(path);
  UnitOfWork.begin(repository.facet(StorageFacet.class).txSupplier());
  try {
    return mavenFacet.get(mavenPath);
  }
  finally {
    UnitOfWork.end();
  }
}
 
Example 12
Source File: AptSnapshotHandler.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private Response handleSnapshotFetchRequest(final Context context, final String id, final String path) throws Exception {
  Repository repository = context.getRepository();
  AptSnapshotFacet snapshotFacet = repository.facet(AptSnapshotFacet.class);
  if (snapshotFacet.isSnapshotableFile(path)) {
    Content content = snapshotFacet.getSnapshotFile(id, path);
    return content == null ? HttpResponses.notFound() : HttpResponses.ok(content);
  }
  context.getAttributes().set(AptSnapshotHandler.State.class, new AptSnapshotHandler.State(path));
  return context.proceed();
}
 
Example 13
Source File: OrientMavenTestHelper.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void write(final Repository repository, final String path, final Payload payload) throws IOException {
  final MavenFacet mavenFacet = repository.facet(MavenFacet.class);
  final MavenPath mavenPath = mavenFacet.getMavenPathParser().parsePath(path);
  UnitOfWork.begin(repository.facet(StorageFacet.class).txSupplier());
  try {
    mavenFacet.put(mavenPath, payload);
  }
  finally {
    UnitOfWork.end();
  }
}
 
Example 14
Source File: IndexYamlAbsoluteUrlRewriter.java    From nexus-repository-helm with Eclipse Public License 1.0 5 votes vote down vote up
public TempBlob removeUrlsFromIndexYamlAndWriteToTempBlob(final TempBlob index,
                                                          final Repository repository)
{
  storageFacet = repository.facet(StorageFacet.class);

  return new StreamCopier<>(outputStream -> updateUrls(index.get(), outputStream),
      this::createTempBlob).read();
}
 
Example 15
Source File: CreateIndexFacetImpl.java    From nexus-repository-helm with Eclipse Public License 1.0 5 votes vote down vote up
private void createIndexYaml(final TempBlob indexYaml) {
  Repository repository = getRepository();
  HelmFacet helmFacet = repository.facet(HelmFacet.class);
  StorageTx tx = UnitOfWork.currentTx();
  HelmAttributes attributes = new HelmAttributes(Collections.emptyMap());
  Asset asset = helmFacet.findOrCreateAsset(tx, INDEX_YAML, HELM_INDEX, attributes);
  try {
    helmFacet.saveAsset(tx, asset, indexYaml, TGZ_CONTENT_TYPE, null);
  }
  catch (IOException ex) {
    log.warn("Could not set blob {}", ex.getMessage(), ex);
  }
}
 
Example 16
Source File: RebuildMaven2MetadataTask.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected void execute(final Repository repository) {
  MavenHostedFacet mavenHostedFacet = repository.facet(MavenHostedFacet.class);
  mavenHostedFacet.rebuildMetadata(
      getConfiguration().getString(GROUPID_FIELD_ID),
      getConfiguration().getString(ARTIFACTID_FIELD_ID),
      getConfiguration().getString(BASEVERSION_FIELD_ID),
      getConfiguration().getBoolean(REBUILD_CHECKSUMS, false),
      false
  );
}
 
Example 17
Source File: ArtifactsXmlAbsoluteUrlRemover.java    From nexus-repository-p2 with Eclipse Public License 1.0 4 votes vote down vote up
private TempBlob convertFileToTempBlob(final Path tempFile, final Repository repository) throws IOException {
  StorageFacet storageFacet = repository.facet(StorageFacet.class);
  try (InputStream tempFileInputStream = newInputStream(tempFile)) {
    return storageFacet.createTempBlob(tempFileInputStream, HASH_ALGORITHMS);
  }
}
 
Example 18
Source File: P2RestoreBlobStrategy.java    From nexus-repository-p2 with Eclipse Public License 1.0 4 votes vote down vote up
private P2RestoreFacet getRestoreFacet(@Nonnull final P2RestoreBlobData p2RestoreBlobData) {
  final Repository repository = getRepository(p2RestoreBlobData);
  return repository.facet(P2RestoreFacet.class);
}
 
Example 19
Source File: MavenIndexPublisher.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
Maven2WritableResourceHandler(final Repository repository) {
  this.mavenFacet = repository.facet(MavenFacet.class);
}
 
Example 20
Source File: MavenUploadHandler.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
private UploadResponse doUpload(final Repository repository, final ComponentUpload componentUpload) throws IOException {
  MavenFacet facet = repository.facet(MavenFacet.class);
  StorageFacet storageFacet = repository.facet(StorageFacet.class);

  if (VersionPolicy.SNAPSHOT.equals(facet.getVersionPolicy())) {
    throw new ValidationErrorsException("Upload to snapshot repositories not supported, use the maven client.");
  }

  ContentAndAssetPathResponseData responseData;

  AssetUpload pomAsset = findPomAsset(componentUpload);

  //purposefully not using a try with resources, as this will only be used in case of an included pom file, which
  //isn't required
  TempBlob pom = null;

  try {
    if (pomAsset != null) {
      PartPayload payload = pomAsset.getPayload();
      pom = storageFacet.createTempBlob(payload, HashType.ALGORITHMS);
      pomAsset.setPayload(new TempBlobPartPayload(payload, pom));
    }

    String basePath = getBasePath(componentUpload, pom);

    doValidation(repository, basePath, componentUpload.getAssetUploads());

    UnitOfWork.begin(storageFacet.txSupplier());
    try {
      responseData = createAssets(repository, basePath, componentUpload.getAssetUploads());

      if (isGeneratePom(componentUpload.getField(GENERATE_POM))) {
        String pomPath = generatePom(repository, basePath, componentUpload.getFields().get(GROUP_ID),
            componentUpload.getFields().get(ARTIFACT_ID), componentUpload.getFields().get(VERSION),
            componentUpload.getFields().get(PACKAGING));

        responseData.addAssetPath(pomPath);
      }

      updateMetadata(repository, responseData.coordinates);
    }
    finally {
      UnitOfWork.end();
    }

    return new UploadResponse(responseData.getContent(), responseData.getAssetPaths());
  }
  finally {
    if (pom != null) {
      pom.close();
    }
  }
}