org.sonatype.nexus.repository.view.Content Java Examples

The following examples show how to use org.sonatype.nexus.repository.view.Content. 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: AptRestoreFacetImpl.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
@TransactionalStoreBlob
public Content restore(final AssetBlob assetBlob, final String path) throws IOException {
  StorageTx tx = UnitOfWork.currentTx();
  Asset asset;
  AptFacet aptFacet = facet(AptFacet.class);
  if (isDebPackageContentType(path)) {
    ControlFile controlFile = AptPackageParser.getDebControlFile(assetBlob.getBlob());
    asset = aptFacet.findOrCreateDebAsset(tx, path, new PackageInfo(controlFile));
  }
  else {
    asset = aptFacet.findOrCreateMetadataAsset(tx, path);
  }

  tx.attachBlob(asset, assetBlob);
  Content.applyToAsset(asset, Content.maintainLastModified(asset, new AttributesMap()));
  tx.saveAsset(asset);
  return FacetHelper.toContent(asset, assetBlob.getBlob());
}
 
Example #2
Source File: ComposerContentFacetImpl.java    From nexus-repository-composer with Eclipse Public License 1.0 6 votes vote down vote up
@Nullable
@Override
@TransactionalTouchBlob
public Content get(final String path) throws IOException {
  StorageTx tx = UnitOfWork.currentTx();

  final Asset asset = findAsset(tx, path);
  if (asset == null) {
    return null;
  }
  if (asset.markAsDownloaded()) {
    tx.saveAsset(asset);
  }

  final Blob blob = tx.requireBlob(asset.requireBlobRef());
  return toContent(asset, blob);
}
 
Example #3
Source File: NpmFacetUtilsTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void mergeDistTagResponse_multipleEntries() throws Exception {
  when(response1.getPayload()).thenReturn(new Content(
      new StringPayload("{\"latest\":\"1.0.2\",\"hello\":\"1.2.3\",\"world\":\"4.5.6\"}", APPLICATION_JSON)));
  when(response2.getPayload())
      .thenReturn(new Content(new StringPayload("{\"latest\":\"1.0.1\",\"world\":\"5.5.5\"}", APPLICATION_JSON)));
  when(response3.getPayload())
      .thenReturn(new Content(new StringPayload("{\"latest\":\"1.0.0\",\"hello\":\"2.2.2\"}", APPLICATION_JSON)));

  final Response mergedResponse = NpmFacetUtils
      .mergeDistTagResponse(ImmutableMap.of(repository1, response1, repository2, response2, repository3, response3));

  final byte[] content = IOUtils.toByteArray(mergedResponse.getPayload().openInputStream());
  final Map<String, String> actual = objectMapper.readValue(content, new TypeReference<Map<String, String>>() { });
  assertThat(actual.get("latest"), containsString("1.0.2"));
  assertThat(actual.get("hello"), containsString("2.2.2"));
  assertThat(actual.get("world"), containsString("5.5.5"));
}
 
Example #4
Source File: MavenHostedFacetImpl.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@TransactionalStoreBlob
protected int doRebuildArchetypeCatalog() throws IOException {
  final Path path = Files.createTempFile("hosted-archetype-catalog", "xml");
  int count = 0;
  try {
    StorageTx tx = UnitOfWork.currentTx();
    Iterable<Archetype> archetypes = getArchetypes(tx);
    ArchetypeCatalog hostedCatalog = new ArchetypeCatalog();
    Iterables.addAll(hostedCatalog.getArchetypes(), archetypes);
    count = hostedCatalog.getArchetypes().size();

    try (Content content = MavenFacetUtils.createTempContent(
        path,
        ContentTypes.APPLICATION_XML,
        (OutputStream outputStream) -> MavenModels.writeArchetypeCatalog(outputStream, hostedCatalog))) {
      MavenFacetUtils.putWithHashes(mavenFacet, archetypeCatalogMavenPath, content);
      log.trace("Rebuilt hosted archetype catalog for {} with {} archetype", getRepository().getName(), count);
    }
  }
  finally {
    Files.delete(path);
    
  }
  return count;
}
 
Example #5
Source File: ConanProxyFacet.java    From nexus-repository-conan with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Save an asset and create a blob
 *
 * @return blob content
 */
private Content saveAsset(final StorageTx tx,
                          final Asset asset,
                          final Supplier<InputStream> contentSupplier,
                          final String contentType,
                          final AttributesMap contentAttributes,
                          final HashCode hash) throws IOException
{
  Content.applyToAsset(asset, maintainLastModified(asset, contentAttributes));
  AssetBlob assetBlob = tx.setBlob(
      asset, asset.name(), contentSupplier, HASH_ALGORITHMS, null, contentType, false
  );

  if (!hashVerifier.verify(hash, assetBlob.getHashes().get(MD5))) {
    return null;
  }
  asset.markAsDownloaded();
  tx.saveAsset(asset);
  return toContent(asset, assetBlob.getBlob());
}
 
Example #6
Source File: P2ProxyFacetImpl.java    From nexus-repository-p2 with Eclipse Public License 1.0 6 votes vote down vote up
@TransactionalStoreBlob
protected Content saveMetadataAsAsset(final String assetPath,
                                      final TempBlob metadataContent,
                                      final Payload payload,
                                      final AssetKind assetKind) throws IOException
{
  StorageTx tx = UnitOfWork.currentTx();
  Bucket bucket = tx.findBucket(getRepository());

  Asset asset = facet(P2Facet.class).findAsset(tx, bucket, assetPath);
  if (asset == null) {
    asset = tx.createAsset(bucket, getRepository().getFormat());
    asset.name(assetPath);
    asset.formatAttributes().set(P_ASSET_KIND, assetKind.name());
  }

  return facet(P2Facet.class).saveAsset(tx, asset, metadataContent, payload);
}
 
Example #7
Source File: HandlerContributorTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void addContributedHandlers() throws Exception {
  final AttributesMap attributes = new AttributesMap();

  final Request request = mock(Request.class);
  final Context context = mock(Context.class);

  when(context.getRequest()).thenReturn(request);
  when(context.getAttributes()).thenReturn(attributes);
  when(context.proceed()).thenReturn(HttpResponses.ok(new Content(new StringPayload("test", "text/plain"))));

  final ContributedHandler handlerA = mock(ContributedHandler.class);
  final ContributedHandler handlerB = mock(ContributedHandler.class);
  final HandlerContributor underTest = new HandlerContributor(asList(handlerA, handlerB));
  final Response response = underTest.handle(context);

  assertThat(response.getStatus().isSuccessful(), is(true));
  assertThat(attributes.get(HandlerContributor.EXTENDED_MARKER), is(Boolean.TRUE));

  // Handle a second time to ensure the contributed handlers aren't injected twice
  underTest.handle(context);

  ArgumentCaptor<ContributedHandler> handlerCaptor = ArgumentCaptor.forClass(ContributedHandler.class);
  verify(context, times(2)).insertHandler(handlerCaptor.capture());
  assertThat(handlerCaptor.getAllValues(), is(asList(handlerB, handlerA))); // Intentionally added in "reverse" order
}
 
Example #8
Source File: OrientPyPiProxyFacetImpl.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@TransactionalStoreBlob
protected Content doPutIndex(final String name,
                             final TempBlob tempBlob,
                             final Payload payload,
                             final Map<String, Object> attributes) throws IOException
{
  StorageTx tx = UnitOfWork.currentTx();
  Bucket bucket = tx.findBucket(getRepository());

  Asset asset = findAsset(tx, bucket, name);
  if (asset == null) {
    asset = tx.createAsset(bucket, getRepository().getFormat());
    asset.name(name);
  }
  if (attributes != null) {
    for (Entry<String, Object> entry : attributes.entrySet()) {
      asset.formatAttributes().set(entry.getKey(), entry.getValue());
    }
  }
  return saveAsset(tx, asset, tempBlob, payload);
}
 
Example #9
Source File: MetadataUpdaterTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void replaceWithUnchangedExisting() throws IOException {
  when(mavenFacet.get(mavenPath)).thenReturn(
      new Content(
          new StringPayload("<?xml version=\"1.0\" encoding=\"UTF-8\"?><metadata></metadata>",
              "text/xml")), content);
  UnitOfWork.beginBatch(tx);
  try {
    testSubject.replace(mavenPath, Maven2Metadata.newGroupLevel(DateTime.now(), new ArrayList<Plugin>()));
  }
  finally {
    UnitOfWork.end();
  }
  verify(tx, times(1)).commit();
  verify(mavenFacet, times(1)).get(eq(mavenPath));
  verify(mavenFacet, times(0)).put(eq(mavenPath), any(Payload.class));
}
 
Example #10
Source File: MavenFacetImplTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testGet_expiredMetadata() throws Exception {
  String path = "/org/sonatype/nexus/nexus-base/3.19.0-SNAPSHOT/maven-metadata.xml";
  Asset asset = createMetadataAsset(path, INVALIDATED);
  Blob blob = mock(Blob.class);
  when(blob.getInputStream())
      .thenReturn(getClass().getResourceAsStream("/org/sonatype/nexus/repository/maven/gavMetadata.xml"));
  when(storageTx.findAssetWithProperty(any(), any(), any(Bucket.class))).thenReturn(asset);
  when(storageTx.requireBlob(any())).thenReturn(blob);
  MavenPath mavenPath = maven2MavenPathParser.parsePath(path);
  Content content = underTest.get(mavenPath);

  assertThat(content, not(nullValue()));
  assertThat(content.getContentType(), is(TEXT_XML));
  verify(metadataRebuilder)
      .rebuildInTransaction(any(), eq(false), eq(false), eq("org.sonatype.nexus"), eq("nexus-base"),
          eq("3.19.0-SNAPSHOT"));
}
 
Example #11
Source File: OrientNpmGroupFacet.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
protected InputStream buildMergedPackageRootOnMissingBlob(final Map<Repository, Response> responses,
                                                          final Context context,
                                                          final MissingAssetBlobException e) throws IOException
{
  if (log.isTraceEnabled()) {
    log.info("Missing blob {} containing cached metadata {}, deleting asset and triggering rebuild.",
        e.getBlobRef(), e.getAsset(), e);
  }
  else {
    log.info("Missing blob {} containing cached metadata {}, deleting asset and triggering rebuild.",
        e.getBlobRef(), e.getAsset().name());
  }

  cleanupPackageRootAssetOnlyFromCache(e.getAsset());

  Content content = buildMergedPackageRoot(responses, context);
  // it should never be null, but we are being kind and return an error stream.
  return nonNull(content) ? content.openInputStream() :
      errorInputStream("Unable to retrieve merged package root on recovery for missing blob");
}
 
Example #12
Source File: OrientNpmProxyFacet.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@TransactionalTouchMetadata
protected Content getDistTags(final NpmPackageId packageId) {
  checkNotNull(packageId);
  StorageTx tx = UnitOfWork.currentTx();
  Asset packageRootAsset = NpmFacetUtils.findPackageRootAsset(tx, tx.findBucket(getRepository()), packageId);
  if (packageRootAsset != null) {
    try {
      final NestedAttributesMap attributesMap = NpmFacetUtils.loadPackageRoot(tx, packageRootAsset);
      final NestedAttributesMap distTags = attributesMap.child(DIST_TAGS);
      return NpmFacetUtils.distTagsToContent(distTags);
    }
    catch (IOException e) {
      log.error("Unable to read packageRoot {}", packageId.id(), e);
    }
  }
  return null;
}
 
Example #13
Source File: OrientArchetypeCatalogHandler.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Tries to get the catalog from hosted repository, and generate it if not present.
 */
private Response doGet(final MavenPath path, final Repository repository) throws IOException {
  MavenFacet mavenFacet = repository.facet(MavenFacet.class);
  Content content = mavenFacet.get(path);
  if (content == null) {
    // try to generate it
    repository.facet(MavenHostedFacet.class).rebuildArchetypeCatalog();
    content = mavenFacet.get(path);
    if (content == null) {
      return HttpResponses.notFound(path.getPath());
    }
  }
  AttributesMap attributesMap = content.getAttributes();
  MavenFacetUtils.mayAddETag(attributesMap, getHashAlgorithmFromContent(attributesMap));
  return HttpResponses.ok(content);
}
 
Example #14
Source File: OrientPyPiProxyFacetImpl.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Nullable
@Override
protected Content getCachedContent(final Context context) throws IOException {
  AssetKind assetKind = context.getAttributes().require(AssetKind.class);
  if (assetKind.equals(AssetKind.SEARCH)) {
    return null;  // we do not store search results
  }
  TokenMatcher.State state = matcherState(context);
  switch (assetKind) {
    case ROOT_INDEX:
      return getAsset(INDEX_PATH_PREFIX);
    case INDEX:
      return rewriteIndex(name(state));
    case PACKAGE:
      return getAsset(path(state));
    default:
      throw new IllegalStateException();
  }
}
 
Example #15
Source File: ProxyFacetSupport.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public Content get(final Context context) throws IOException {
  checkNotNull(context);

  Content content = maybeGetCachedContent(context);
  if (!isStale(context, content)) {
    return content;
  }
  if (proxyCooperation == null) {
    return doGet(context, content);
  }
  return proxyCooperation.cooperate(getRequestKey(context), failover -> {
    Content latestContent = content;
    if (failover) {
      // re-check cache when failing over to new thread
      latestContent = proxyCooperation.join(() -> maybeGetCachedContent(context));
      if (!isStale(context, latestContent)) {
        return latestContent;
      }
    }
    return doGet(context, latestContent);
  });
}
 
Example #16
Source File: ProxyFacetSupportTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testGet_RemoteBlockedException_contentReturnedIfCached() throws IOException {
  when(cacheController.isStale(cacheInfo)).thenReturn(true);
  doReturn(content).when(underTest).getCachedContent(cachedContext);

  doThrow(new RemoteBlockedIOException("blocked")).when(underTest).fetch(cachedContext, content);

  Content foundContent = underTest.get(cachedContext);

  assertThat(foundContent, is(content));
}
 
Example #17
Source File: NpmFacetUtils.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Save repository root asset & create blob from a temporary blob.
 *
 * @return blob content
 */
@Nonnull
static Content saveRepositoryRoot(final StorageTx tx,
                                  final Asset asset,
                                  final TempBlob tempBlob,
                                  final Content content) throws IOException
{
  Content.applyToAsset(asset, Content.maintainLastModified(asset, content.getAttributes()));
  AssetBlob assetBlob = storeContent(tx, asset, tempBlob, AssetKind.REPOSITORY_ROOT);
  tx.saveAsset(asset);
  return toContent(asset, assetBlob.getBlob());
}
 
Example #18
Source File: ContentHeadersHandlerTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void okResponseNoExtraData() throws Exception {
  when(context.proceed()).thenReturn(
      HttpResponses.ok(new Content(new StringPayload(payloadString, "text/plain"))));
  final Response r = subject.handle(context);
  assertThat(r.getStatus().isSuccessful(), is(true));
  assertThat(r.getHeaders().get(HttpHeaders.LAST_MODIFIED), nullValue());
  assertThat(r.getHeaders().get(HttpHeaders.ETAG), nullValue());
}
 
Example #19
Source File: RProxyFacetImpl.java    From nexus-repository-r with Eclipse Public License 1.0 5 votes vote down vote up
private Content putArchive(final String path, final Content content) throws IOException {
  checkNotNull(path);
  checkNotNull(content);
  StorageFacet storageFacet = facet(StorageFacet.class);
  try (TempBlob tempBlob = storageFacet.createTempBlob(content.openInputStream(), RFacetUtils.HASH_ALGORITHMS)) {
    return doPutArchive(path, tempBlob, content);
  }
}
 
Example #20
Source File: CocoapodsFacetImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
@TransactionalStoreBlob
public Content getOrCreateAsset(final String path, final Content content, boolean toAttachComponent)
    throws IOException
{
  StorageFacet storageFacet = facet(StorageFacet.class);
  try (final TempBlob tempBlob = storageFacet.createTempBlob(content, HASH_ALGORITHMS)) {
    StorageTx tx = UnitOfWork.currentTx();
    Bucket bucket = tx.findBucket(getRepository());

    Asset asset = tx.findAssetWithProperty(P_NAME, path, bucket);
    if (asset == null) {
      if (toAttachComponent) {
        Component component = findOrCreateComponent(tx, bucket, path);
        asset = tx.createAsset(bucket, component).name(path);
      }
      else {
        asset = tx.createAsset(bucket, getRepository().getFormat()).name(path);
      }
    }
    Content.applyToAsset(asset, content.getAttributes());
    AssetBlob blob = tx.setBlob(asset, path, tempBlob, HASH_ALGORITHMS, null, null, false);
    tx.saveAsset(asset);

    final Content updatedContent = new Content(new BlobPayload(blob.getBlob(), asset.requireContentType()));
    Content.extractFromAsset(asset, HASH_ALGORITHMS, updatedContent.getAttributes());
    return updatedContent;
  }
}
 
Example #21
Source File: OrientMavenGroupFacet.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private <T> T merge(final MetadataMerger merger,
                    final MavenPath mavenPath,
                    final LinkedHashMap<Repository, Content> contents,
                    final Function<InputStream, T> streamFunction)
{
  return new StreamCopier<>(
      outputStream -> merger.merge(outputStream, mavenPath, contents),
      streamFunction).read();
}
 
Example #22
Source File: OrientPyPiDataUtils.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Save an asset and create blob.
 *
 * @return blob content
 */
static Content saveAsset(
    final StorageTx tx,
    final Asset asset,
    final TempBlob tempBlob,
    final Payload payload) throws IOException
{
  AttributesMap contentAttributes = null;
  String contentType = null;
  if (payload instanceof Content) {
    contentAttributes = ((Content) payload).getAttributes();
    contentType = payload.getContentType();
  }
  return saveAsset(tx, asset, tempBlob, contentType, contentAttributes);
}
 
Example #23
Source File: RawContentFacetImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public Content put(final String path, final Payload content) throws IOException {
  try (TempBlob blob = blobs().ingest(content, HASHING)){
    return assets()
        .path(path)
        .component(components()
            .name(path)
            .namespace(RawCoordinatesHelper.getGroup(path))
            .getOrCreate())
        .getOrCreate()
        .attach(blob)
        .markAsCached(content)
        .download();
  }
}
 
Example #24
Source File: ConanHostedFacet.java    From nexus-repository-conan with Eclipse Public License 1.0 5 votes vote down vote up
public Response get(final Context context) {
  log.debug("Request {}", context.getRequest().getPath());

  TokenMatcher.State state = context.getAttributes().require(TokenMatcher.State.class);
  ConanCoords coord = ConanHostedHelper.convertFromState(state);
  AssetKind assetKind = context.getAttributes().require(AssetKind.class);
  String assetPath = ConanHostedHelper.getHostedAssetPath(coord, assetKind);

  Content content = doGet(assetPath);
  if (content == null) {
    return HttpResponses.notFound();
  }

  return new Response.Builder()
      .status(success(OK))
      .payload(new StreamPayload(
          new InputStreamSupplier()
          {
            @Nonnull
            @Override
            public InputStream get() throws IOException {
              return content.openInputStream();
            }
          },
          content.getSize(),
          content.getContentType()))
      .build();
}
 
Example #25
Source File: AptFacetImpl.java    From nexus-repository-apt with Eclipse Public License 1.0 5 votes vote down vote up
@Override
@TransactionalStoreBlob
public Content put(String path, Payload content) throws IOException {
  StorageFacet storageFacet = facet(StorageFacet.class);
  try (final TempBlob tembBlob = storageFacet.createTempBlob(content, FacetHelper.hashAlgorithms)) {
    StorageTx tx = UnitOfWork.currentTx();
    Bucket bucket = tx.findBucket(getRepository());
    Asset asset = tx.findAssetWithProperty(P_NAME, path, bucket);
    if (asset == null) {
      asset = tx.createAsset(bucket, getRepository().getFormat()).name(path);
    }

    AttributesMap contentAttributes = null;
    if (content instanceof Content) {
      contentAttributes = ((Content) content).getAttributes();
    }
    Content.applyToAsset(asset, Content.maintainLastModified(asset, contentAttributes));
    AssetBlob blob = tx.setBlob(
        asset,
        path,
        tembBlob,
        FacetHelper.hashAlgorithms,
        null,
        content.getContentType(),
        false);
    tx.saveAsset(asset);
    return FacetHelper.toContent(asset, blob.getBlob());
  }
}
 
Example #26
Source File: OrientPyPiHostedFacetImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
@TransactionalTouchBlob
public Content getPackage(final String packagePath) {
  checkNotNull(packagePath);
  StorageTx tx = UnitOfWork.currentTx();

  Asset asset = findAsset(tx, tx.findBucket(getRepository()), packagePath);
  if (asset == null) {
    return null;
  }
  Content content = toContent(asset, tx.requireBlob(asset.requireBlobRef()));
  mayAddEtag(content.getAttributes(), asset.getChecksum(HashAlgorithm.SHA1));
  return content;
}
 
Example #27
Source File: NpmFacetUtilsTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void mergeDistTagResponse_singleEntry() throws Exception {
  when(response1.getPayload()).thenReturn(new Content(new StringPayload("{\"latest\":\"1.0.1\"}", APPLICATION_JSON)));

  final Response mergedResponse = NpmFacetUtils
      .mergeDistTagResponse(ImmutableMap.of(repository1, response1));

  final byte[] content = IOUtils.toByteArray(mergedResponse.getPayload().openInputStream());
  final Map<String, String> actual = objectMapper.readValue(content, new TypeReference<Map<String, String>>() { });
  assertThat(actual.get("latest"), containsString("1.0.1"));
}
 
Example #28
Source File: GolangProxyFacetImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@TransactionalTouchMetadata
public void setCacheInfo(final Content content, final CacheInfo cacheInfo) throws IOException {
  StorageTx tx = UnitOfWork.currentTx();
  Asset asset = Content.findAsset(tx, tx.findBucket(getRepository()), content);
  if (asset == null) {
    log.debug(
        "Attempting to set cache info for non-existent go asset {}", content.getAttributes().require(Asset.class)
    );
    return;
  }
  log.debug("Updating cacheInfo of {} to {}", asset, cacheInfo);
  CacheInfo.applyToAsset(asset, cacheInfo);
  tx.saveAsset(asset);
}
 
Example #29
Source File: MavenFacetImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public Content put(final MavenPath path,
                   final Path sourceFile,
                   final String contentType,
                   final AttributesMap contentAttributes,
                   final Map<HashAlgorithm, HashCode> hashes,
                   final long size)
    throws IOException
{
  log.debug("PUT {} : {}", getRepository().getName(), path.getPath());

  return doPut(path, sourceFile, contentType, contentAttributes, hashes, size);
}
 
Example #30
Source File: MavenFacetUtils.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Creates a temporary {@link Content} equipped will all the whistles and bells, like hashes and so.
 */
public static Content createTempContent(final Path path, final String contentType, final Writer writer) throws IOException {
  HashedPayload hashedPayload = MavenIOUtils.createStreamPayload(path, contentType, writer);

  Content content = new Content(hashedPayload.getPayload());
  content.getAttributes().set(Content.CONTENT_LAST_MODIFIED, DateTime.now());
  content.getAttributes().set(Content.CONTENT_HASH_CODES_MAP, hashedPayload.getHashCodes());
  mayAddETag(content.getAttributes(), hashedPayload.getHashCodes());
  return content;
}