use of org.sonatype.nexus.repository.content.Asset in project nexus-public by sonatype.
the class MavenContentFacetImpl method hardLink.
@Override
public void hardLink(FluentAsset asset, Path contentPath) throws IOException {
String mimeType = mimeSupport.detectMimeType(Files.newInputStream(contentPath), contentPath.toString());
Map<String, String> headers = ImmutableMap.of(BLOB_NAME_HEADER, contentPath.toString(), CONTENT_TYPE_HEADER, mimeType);
byte[] bytes = Files.readAllBytes(contentPath);
Map<HashAlgorithm, HashCode> hashes = HashType.ALGORITHMS.stream().collect(Collectors.toMap(Function.identity(), a -> a.function().hashBytes(bytes)));
Blob blob = blobs().ingest(contentPath, headers, hashes.get(SHA1), Files.size(contentPath));
asset.attach(blob, hashes);
}
use of org.sonatype.nexus.repository.content.Asset in project nexus-public by sonatype.
the class AptProxyFacet method doIndicateVerified.
protected void doIndicateVerified(final Content content, final CacheInfo cacheInfo, final String assetPath) {
AptContentFacet contentFacet = facet(AptContentFacet.class);
Asset asset = content.getAttributes().get(Asset.class);
if (asset != null) {
contentFacet.assets().with(asset).markAsCached(cacheInfo);
return;
}
facet(AptContentFacet.class).getAsset(assetPath).ifPresent(a -> contentFacet.assets().with(a).markAsCached(cacheInfo));
}
use of org.sonatype.nexus.repository.content.Asset in project nexus-public by sonatype.
the class DataStoreMavenTestHelper method getLastDownloadedTime.
@Override
public DateTime getLastDownloadedTime(final Repository repository, final String assetPath) throws IOException {
MavenContentFacet mavenContentFacet = repository.facet(MavenContentFacet.class);
MavenPath mavenPath = mavenContentFacet.getMavenPathParser().parsePath(assetPath);
return mavenContentFacet.get(mavenPath).map(Content::getAttributes).map(attributes -> attributes.get(Asset.class)).map(Asset::lastDownloaded).filter(Optional::isPresent).map(Optional::get).map(DateHelper::toDateTime).orElse(null);
}
use of org.sonatype.nexus.repository.content.Asset in project nexus-public by sonatype.
the class InternalIdsTest method shouldFetchComponentIdFromComponentWhenComponentIdIsNullOnAsset.
@Test
public void shouldFetchComponentIdFromComponentWhenComponentIdIsNullOnAsset() {
ComponentData component = new ComponentData();
component.componentId = 2;
AssetData asset = new AssetData();
asset.assetId = 3;
Asset fluentAsset = new FluentAssetImpl(contentFacet, asset);
assertThat(internalComponentId(asset), is(empty()));
assertThat(internalComponentId(fluentAsset), is(empty()));
asset.setComponent(component);
asset.componentId = null;
assertThat(internalComponentId(asset).getAsInt(), is(component.componentId));
assertThat(internalComponentId(fluentAsset).getAsInt(), is(component.componentId));
}
use of org.sonatype.nexus.repository.content.Asset in project nexus-public by sonatype.
the class AssetDAOTest method testAttachingBlobs.
@Test
public void testAttachingBlobs() throws InterruptedException {
AssetBlobData assetBlob1 = randomAssetBlob();
AssetBlobData assetBlob2 = randomAssetBlob();
AssetData asset = randomAsset(repositoryId);
String path = asset.path();
Asset tempResult;
try (DataSession<?> session = sessionRule.openSession(DEFAULT_DATASTORE_NAME)) {
AssetBlobDAO dao = session.access(TestAssetBlobDAO.class);
dao.createAssetBlob(assetBlob1);
dao.createAssetBlob(assetBlob2);
session.access(TestAssetDAO.class).createAsset(asset);
session.getTransaction().commit();
assertThat(dao.browseUnusedAssetBlobs(10, null), contains(sameBlob(assetBlob1), sameBlob(assetBlob2)));
}
// ATTACH BLOB
// NOSONAR
Thread.sleep(2);
try (DataSession<?> session = sessionRule.openSession(DEFAULT_DATASTORE_NAME)) {
AssetDAO dao = session.access(TestAssetDAO.class);
tempResult = dao.readPath(repositoryId, path).get();
OffsetDateTime oldCreated = tempResult.created();
OffsetDateTime oldLastUpdated = tempResult.lastUpdated();
assertFalse(tempResult.blob().isPresent());
asset.setAssetBlob(assetBlob1);
dao.updateAssetBlobLink(asset);
tempResult = dao.readPath(repositoryId, path).get();
assertTrue(tempResult.blob().isPresent());
assertThat(tempResult.blob().get(), sameBlob(assetBlob1));
assertThat(tempResult, sameBlob(asset));
assertThat(tempResult.created(), is(oldCreated));
assertTrue(tempResult.lastUpdated().isAfter(oldLastUpdated));
session.getTransaction().commit();
assertThat(session.access(TestAssetBlobDAO.class).browseUnusedAssetBlobs(10, null), contains(sameBlob(assetBlob2)));
}
// REPLACE BLOB
// NOSONAR
Thread.sleep(2);
try (DataSession<?> session = sessionRule.openSession(DEFAULT_DATASTORE_NAME)) {
AssetDAO dao = session.access(TestAssetDAO.class);
tempResult = dao.readPath(repositoryId, path).get();
OffsetDateTime oldCreated = tempResult.created();
OffsetDateTime oldLastUpdated = tempResult.lastUpdated();
assertThat(tempResult.blob().get(), sameBlob(assetBlob1));
asset.setAssetBlob(assetBlob2);
dao.updateAssetBlobLink(asset);
tempResult = dao.readPath(repositoryId, path).get();
assertTrue(tempResult.blob().isPresent());
assertThat(tempResult.blob().get(), sameBlob(assetBlob2));
assertThat(tempResult, sameBlob(asset));
assertThat(tempResult.created(), is(oldCreated));
assertTrue(tempResult.lastUpdated().isAfter(oldLastUpdated));
session.getTransaction().commit();
assertThat(session.access(TestAssetBlobDAO.class).browseUnusedAssetBlobs(10, null), contains(sameBlob(assetBlob1)));
}
// REPLACING WITH SAME BLOB DOESN'T UPDATE
// NOSONAR
Thread.sleep(2);
try (DataSession<?> session = sessionRule.openSession(DEFAULT_DATASTORE_NAME)) {
AssetDAO dao = session.access(TestAssetDAO.class);
tempResult = dao.readPath(repositoryId, path).get();
OffsetDateTime oldCreated = tempResult.created();
OffsetDateTime oldLastUpdated = tempResult.lastUpdated();
assertThat(tempResult.blob().get(), sameBlob(assetBlob2));
asset.setAssetBlob(assetBlob2);
dao.updateAssetBlobLink(asset);
tempResult = dao.readPath(repositoryId, path).get();
assertTrue(tempResult.blob().isPresent());
assertThat(tempResult.blob().get(), sameBlob(assetBlob2));
assertThat(tempResult, sameBlob(asset));
assertThat(tempResult.created(), is(oldCreated));
assertThat(tempResult.lastUpdated(), is(oldLastUpdated));
session.getTransaction().commit();
assertThat(session.access(TestAssetBlobDAO.class).browseUnusedAssetBlobs(10, null), contains(sameBlob(assetBlob1)));
}
// DETACH BLOB
// NOSONAR
Thread.sleep(2);
try (DataSession<?> session = sessionRule.openSession(DEFAULT_DATASTORE_NAME)) {
AssetDAO dao = session.access(TestAssetDAO.class);
tempResult = dao.readPath(repositoryId, path).get();
OffsetDateTime oldCreated = tempResult.created();
OffsetDateTime oldLastUpdated = tempResult.lastUpdated();
assertThat(tempResult.blob().get(), sameBlob(assetBlob2));
asset.setAssetBlob(null);
dao.updateAssetBlobLink(asset);
tempResult = dao.readPath(repositoryId, path).get();
assertFalse(tempResult.blob().isPresent());
assertThat(tempResult.created(), is(oldCreated));
assertTrue(tempResult.lastUpdated().isAfter(oldLastUpdated));
session.getTransaction().commit();
assertThat(session.access(TestAssetBlobDAO.class).browseUnusedAssetBlobs(10, null), contains(sameBlob(assetBlob1), sameBlob(assetBlob2)));
}
// DETACHING BLOB AGAIN DOESN'T UPDATE
// NOSONAR
Thread.sleep(2);
try (DataSession<?> session = sessionRule.openSession(DEFAULT_DATASTORE_NAME)) {
AssetDAO dao = session.access(TestAssetDAO.class);
tempResult = dao.readPath(repositoryId, path).get();
OffsetDateTime oldCreated = tempResult.created();
OffsetDateTime oldLastUpdated = tempResult.lastUpdated();
assertFalse(tempResult.blob().isPresent());
asset.setAssetBlob(null);
dao.updateAssetBlobLink(asset);
tempResult = dao.readPath(repositoryId, path).get();
assertFalse(tempResult.blob().isPresent());
assertThat(tempResult.created(), is(oldCreated));
assertThat(tempResult.lastUpdated(), is(oldLastUpdated));
session.getTransaction().commit();
assertThat(session.access(TestAssetBlobDAO.class).browseUnusedAssetBlobs(10, null), contains(sameBlob(assetBlob1), sameBlob(assetBlob2)));
}
}
Aggregations