use of org.sonatype.nexus.repository.apt.internal.hosted.CompressingTempFileStore in project nexus-public by sonatype.
the class AptHostedFacet method buildPackageIndexes.
private CompressingTempFileStore buildPackageIndexes(final List<AssetChange> changes) throws IOException {
CompressingTempFileStore result = new CompressingTempFileStore();
Map<String, Writer> streams = new HashMap<>();
boolean ok = false;
try {
Set<String> architectures = changes.stream().map(change -> getArchitecture(change.getAsset())).collect(Collectors.toSet());
final Continuation<FluentAsset> allAssets = getRepository().facet(ContentFacet.class).assets().byKind(DEB).browse(Integer.MAX_VALUE, null);
allAssets.stream().map(this::getArchitecture).collect(Collectors.toCollection(() -> architectures));
Map<String, List<FluentAsset>> assetsPerArch = new HashMap<>();
for (String architecture : architectures) {
List<FluentAsset> assets = allAssets.stream().filter(asset -> getArchitecture(asset).equals(architecture)).collect(Collectors.toList());
assetsPerArch.put(architecture, assets);
}
for (List<FluentAsset> assets : assetsPerArch.values()) {
Optional<AssetChange> removeAssetChange = changes.stream().filter(change -> change.getAsset().kind().equals(DEB)).filter(change -> change.getAction() == AssetAction.REMOVED).findAny();
if (assets.isEmpty() && removeAssetChange.isPresent()) {
createEmptyMetadataFile(result, streams, removeAssetChange.get());
} else {
createMetadataFileWithData(changes, result, streams, assets);
}
}
ok = true;
} finally {
for (Writer writer : streams.values()) {
IOUtils.closeQuietly(writer);
}
if (!ok) {
result.close();
}
}
return result;
}
use of org.sonatype.nexus.repository.apt.internal.hosted.CompressingTempFileStore in project nexus-public by sonatype.
the class OrientAptHostedFacet method buildPackageIndexes.
private CompressingTempFileStore buildPackageIndexes(final StorageTx tx, final Bucket bucket, final List<AssetChange> changes) throws IOException {
CompressingTempFileStore result = new CompressingTempFileStore();
Map<String, Writer> streams = new HashMap<>();
boolean ok = false;
try {
Map<String, Object> sqlParams = new HashMap<>();
sqlParams.put(P_BUCKET, AttachedEntityHelper.id(bucket));
sqlParams.put(P_ASSET_KIND, "DEB");
// NOTE: We exclude added assets as well to account for the case where we are replacing an asset
Set<String> excludeNames = changes.stream().map(change -> change.asset.name()).collect(Collectors.toSet());
Iterable<ODocument> browse = tx.browse(SELECT_HOSTED_ASSETS, sqlParams);
for (ODocument document : browse) {
String name = document.field(P_NAME, String.class);
String arch = document.field(P_ARCHITECTURE, String.class);
Writer outWriter = streams.computeIfAbsent(arch, result::openOutput);
if (!excludeNames.contains(name)) {
String indexSection = document.field(P_INDEX_SECTION, String.class);
outWriter.write(indexSection);
outWriter.write("\n\n");
}
}
List<Asset> addAssets = changes.stream().filter(change -> change.action == AssetAction.ADDED).map(change -> change.asset).collect(Collectors.toList());
// HACK: tx.browse won't see changes in the current transaction, so we have to manually add these in here
for (Asset asset : addAssets) {
String arch = asset.formatAttributes().get(P_ARCHITECTURE, String.class);
String indexSection = asset.formatAttributes().get(P_INDEX_SECTION, String.class);
Writer outWriter = streams.computeIfAbsent(arch, result::openOutput);
outWriter.write(indexSection);
outWriter.write("\n\n");
}
ok = true;
} finally {
for (Writer writer : streams.values()) {
IOUtils.closeQuietly(writer);
}
if (!ok) {
result.close();
}
}
return result;
}
use of org.sonatype.nexus.repository.apt.internal.hosted.CompressingTempFileStore in project nexus-public by sonatype.
the class OrientAptHostedFacet method rebuildIndexes.
@TransactionalStoreMetadata
public void rebuildIndexes(final List<AssetChange> changes) throws IOException {
StorageTx tx = UnitOfWork.currentTx();
OrientAptFacet aptFacet = getRepository().facet(OrientAptFacet.class);
AptSigningFacet signingFacet = getRepository().facet(AptSigningFacet.class);
Bucket bucket = tx.findBucket(getRepository());
StringBuilder sha256Builder = new StringBuilder();
StringBuilder md5Builder = new StringBuilder();
String releaseFile;
try (CompressingTempFileStore store = buildPackageIndexes(tx, bucket, changes)) {
for (Map.Entry<String, CompressingTempFileStore.FileMetadata> entry : store.getFiles().entrySet()) {
Content plainContent = aptFacet.put(packageIndexName(entry.getKey(), ""), new StreamPayload(entry.getValue().plainSupplier(), entry.getValue().plainSize(), AptMimeTypes.TEXT));
addSignatureItem(md5Builder, MD5, plainContent, packageRelativeIndexName(entry.getKey(), ""));
addSignatureItem(sha256Builder, SHA256, plainContent, packageRelativeIndexName(entry.getKey(), ""));
Content gzContent = aptFacet.put(packageIndexName(entry.getKey(), ".gz"), new StreamPayload(entry.getValue().gzSupplier(), entry.getValue().bzSize(), AptMimeTypes.GZIP));
addSignatureItem(md5Builder, MD5, gzContent, packageRelativeIndexName(entry.getKey(), ".gz"));
addSignatureItem(sha256Builder, SHA256, gzContent, packageRelativeIndexName(entry.getKey(), ".gz"));
Content bzContent = aptFacet.put(packageIndexName(entry.getKey(), ".bz2"), new StreamPayload(entry.getValue().bzSupplier(), entry.getValue().bzSize(), AptMimeTypes.BZIP));
addSignatureItem(md5Builder, MD5, bzContent, packageRelativeIndexName(entry.getKey(), ".bz2"));
addSignatureItem(sha256Builder, SHA256, bzContent, packageRelativeIndexName(entry.getKey(), ".bz2"));
}
releaseFile = buildReleaseFile(aptFacet.getDistribution(), store.getFiles().keySet(), md5Builder.toString(), sha256Builder.toString());
}
aptFacet.put(releaseIndexName(RELEASE), new BytesPayload(releaseFile.getBytes(Charsets.UTF_8), AptMimeTypes.TEXT));
byte[] inRelease = signingFacet.signInline(releaseFile);
aptFacet.put(releaseIndexName(INRELEASE), new BytesPayload(inRelease, AptMimeTypes.TEXT));
byte[] releaseGpg = signingFacet.signExternal(releaseFile);
aptFacet.put(releaseIndexName(RELEASE_GPG), new BytesPayload(releaseGpg, AptMimeTypes.SIGNATURE));
}
use of org.sonatype.nexus.repository.apt.internal.hosted.CompressingTempFileStore in project nexus-public by sonatype.
the class AptHostedFacet method rebuildMetadata.
/**
* Method for triggering Apt metadata recalculation with possibility to specify what actually asset was changed
*/
public void rebuildMetadata(List<AssetChange> changeList) throws IOException {
AptContentFacet aptFacet = getRepository().facet(AptContentFacet.class);
AptSigningFacet signingFacet = getRepository().facet(AptSigningFacet.class);
StringBuilder sha256Builder = new StringBuilder();
StringBuilder md5Builder = new StringBuilder();
String releaseFile;
try (CompressingTempFileStore store = buildPackageIndexes(changeList)) {
for (Map.Entry<String, CompressingTempFileStore.FileMetadata> entry : store.getFiles().entrySet()) {
FluentAsset metadataAsset = aptFacet.put(packageIndexName(entry.getKey(), StringUtils.EMPTY), new StreamPayload(entry.getValue().plainSupplier(), entry.getValue().plainSize(), AptMimeTypes.TEXT));
addSignatureItem(md5Builder, MD5, metadataAsset, packageRelativeIndexName(entry.getKey(), StringUtils.EMPTY));
addSignatureItem(sha256Builder, SHA256, metadataAsset, packageRelativeIndexName(entry.getKey(), StringUtils.EMPTY));
FluentAsset gzMetadataAsset = aptFacet.put(packageIndexName(entry.getKey(), GZ), new StreamPayload(entry.getValue().gzSupplier(), entry.getValue().bzSize(), AptMimeTypes.GZIP));
addSignatureItem(md5Builder, MD5, gzMetadataAsset, packageRelativeIndexName(entry.getKey(), GZ));
addSignatureItem(sha256Builder, SHA256, gzMetadataAsset, packageRelativeIndexName(entry.getKey(), GZ));
FluentAsset bzMetadataAsset = aptFacet.put(packageIndexName(entry.getKey(), BZ2), new StreamPayload(entry.getValue().bzSupplier(), entry.getValue().bzSize(), AptMimeTypes.BZIP));
addSignatureItem(md5Builder, MD5, bzMetadataAsset, packageRelativeIndexName(entry.getKey(), BZ2));
addSignatureItem(sha256Builder, SHA256, bzMetadataAsset, packageRelativeIndexName(entry.getKey(), BZ2));
}
releaseFile = buildReleaseFile(aptFacet.getDistribution(), store.getFiles().keySet(), md5Builder.toString(), sha256Builder.toString());
}
aptFacet.put(releaseIndexName(RELEASE), new BytesPayload(releaseFile.getBytes(Charsets.UTF_8), AptMimeTypes.TEXT));
byte[] inRelease = signingFacet.signInline(releaseFile);
aptFacet.put(releaseIndexName(INRELEASE), new BytesPayload(inRelease, AptMimeTypes.TEXT));
byte[] releaseGpg = signingFacet.signExternal(releaseFile);
aptFacet.put(releaseIndexName(RELEASE_GPG), new BytesPayload(releaseGpg, AptMimeTypes.SIGNATURE));
}
use of org.sonatype.nexus.repository.apt.internal.hosted.CompressingTempFileStore in project nexus-public by sonatype.
the class AptHostedFacet method createMetadataFileWithData.
private void createMetadataFileWithData(final List<AssetChange> changes, final CompressingTempFileStore result, final Map<String, Writer> streams, final List<FluentAsset> assets) throws IOException {
// NOTE: We exclude added assets as well to account for the case where we are replacing an asset
Set<String> excludeNames = changes.stream().map(c -> c.getAsset().path()).collect(Collectors.toSet());
for (FluentAsset asset : assets) {
final String name = asset.path();
final String arch = (String) FormatAttributesUtils.getFormatAttributes(asset).get(P_ARCHITECTURE);
Writer outWriter = streams.computeIfAbsent(arch, result::openOutput);
if (!excludeNames.contains(name)) {
final String indexSection = (String) FormatAttributesUtils.getFormatAttributes(asset).get(P_INDEX_SECTION);
outWriter.write(indexSection);
outWriter.write("\n\n");
}
}
List<FluentAsset> addAssets = changes.stream().filter(c -> c.getAction() == AssetAction.ADDED).map(AssetChange::getAsset).collect(Collectors.toList());
// HACK: tx.browse won't see changes in the current transaction, so we have to manually add these in here
for (FluentAsset asset : addAssets) {
String arch = (String) FormatAttributesUtils.getFormatAttributes(asset).get(P_ARCHITECTURE);
String indexSection = (String) FormatAttributesUtils.getFormatAttributes(asset).get(P_INDEX_SECTION);
Writer outWriter = streams.computeIfAbsent(arch, result::openOutput);
outWriter.write(indexSection);
outWriter.write("\n\n");
}
}
Aggregations