private Content toContent(final Asset asset, final Blob blob) { final Content content = new Content(new BlobPayload(blob, asset.requireContentType())); Content.extractFromAsset(asset, hashAlgorithms, content.getAttributes()); return content; } }
/** * 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()); }
@Override public InputStream read() throws IOException { Content content = mavenFacet.get(mavenPath); if (content != null) { return content.openInputStream(); } return null; }
private void putAssetPayload(final StorageTx tx, final Asset asset, final AssetBlob assetBlob, @Nullable final AttributesMap contentAttributes) throws IOException { tx.attachBlob(asset, assetBlob); Content.applyToAsset(asset, Content.maintainLastModified(asset, contentAttributes)); }
private static Payload cachePayload(final Content remote) throws IOException { try (InputStream in = remote.openInputStream()) { return new BytesPayload(toByteArray(in), remote.getContentType()); } } }
private void addSignatureItem(StringBuilder builder, HashAlgorithm algo, Content content, String filename) { Map<HashAlgorithm, HashCode> hashMap = content.getAttributes().get(Content.CONTENT_HASH_CODES_MAP, Content.T_CONTENT_HASH_CODES_MAP); builder.append("\n "); builder.append(hashMap.get(algo).toString()); builder.append(" "); builder.append(content.getSize()); builder.append(" "); builder.append(filename); }
private Asset getAssetFromPayload(final Payload payload) { if (payload instanceof Content) { return ((Content) payload).getAttributes().get(Asset.class); } return null; } }
public static void addHashes(final MavenFacet mavenFacet, final MavenPath mavenPath, final Map<HashAlgorithm, HashCode> hashCodes, final DateTime now) throws IOException { for (HashType hashType : HashType.values()) { final HashCode hashCode = hashCodes.get(hashType.getHashAlgorithm()); if (hashCode != null) { final Content hashContent = new Content( new StringPayload(hashCode.toString(), Constants.CHECKSUM_CONTENT_TYPE)); hashContent.getAttributes().set(Content.CONTENT_LAST_MODIFIED, now); mavenFacet.put(mavenPath.hash(hashType), hashContent); } } }
/** * Builds a packages.json file as a {@code Content} instance containing the actual JSON for the given providers. */ private Content buildPackagesJson(final Repository repository, final Set<String> names) throws IOException { Map<String, Object> packagesJson = new LinkedHashMap<>(); packagesJson.put(PROVIDERS_URL_KEY, repository.getUrl() + PACKAGE_JSON_PATH); packagesJson.put(PROVIDERS_KEY, names.stream() .collect(Collectors.toMap((each) -> each, (each) -> singletonMap(SHA256_KEY, null)))); return new Content(new StringPayload(mapper.writeValueAsString(packagesJson), ContentTypes.APPLICATION_JSON)); }
@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 npm asset {}", content.getAttributes().require(Asset.class) ); return; } log.debug("Updating cacheInfo of {} to {}", asset, cacheInfo); CacheInfo.applyToAsset(asset, cacheInfo); tx.saveAsset(asset); }
final Closer closer = Closer.create(); try { final JsonParser jsonParser = mapper.getFactory().createParser(closer.register(fullIndex.openInputStream())); if (jsonParser.nextToken() == JsonToken.START_OBJECT && NpmMetadataUtils.META_UPDATED.equals(jsonParser.nextFieldName())) { return new Content(new StreamPayload( new InputStreamSupplier()
@Override @TransactionalTouchMetadata public void setCacheInfo(final String path, final Content content, final CacheInfo cacheInfo) throws IOException { StorageTx tx = UnitOfWork.currentTx(); Bucket bucket = tx.findBucket(getRepository()); Asset asset = Content.findAsset(tx, bucket, content); if (asset == null) { log.debug("Attempting to set cache info for non-existent Composer asset {}", path); return; } CacheInfo.applyToAsset(asset, cacheInfo); tx.saveAsset(asset); }
@Transactional(retryOn = { ONeedRetryException.class }) @Override public void createSnapshot(String id, SnapshotComponentSelector selector) throws IOException { StorageTx tx = UnitOfWork.currentTx(); StorageFacet storageFacet = facet(StorageFacet.class); Bucket bucket = tx.findBucket(getRepository()); Component component = tx.createComponent(bucket, getRepository().getFormat()).name(id); tx.saveComponent(component); for (SnapshotItem item : collectSnapshotItems(selector)) { String assetName = createAssetPath(id, item.specifier.path); Asset asset = tx.createAsset(bucket, component).name(assetName); try (final TempBlob streamSupplier = storageFacet.createTempBlob(item.content.openInputStream(), FacetHelper.hashAlgorithms)) { AssetBlob blob = tx.createBlob(item.specifier.path, streamSupplier, FacetHelper.hashAlgorithms, null, FacetHelper.determineContentType(item), true); tx.attachBlob(asset, blob); } finally { item.content.close(); } tx.saveAsset(asset); } }
public static String determineContentType(SnapshotItem item) { String ct = item.content.getContentType(); if (ct == null) { switch (item.specifier.role) { case RELEASE_INDEX: case RELEASE_INLINE_INDEX: case PACKAGE_INDEX_RAW: return AptMimeTypes.TEXT; case RELEASE_SIG: return AptMimeTypes.SIGNATURE; case PACKAGE_INDEX_BZ2: return AptMimeTypes.BZIP; case PACKAGE_INDEX_GZ: return AptMimeTypes.GZIP; } } return ct; }
@Nullable private static EntityId extractComponentId(final Content content) { Optional<Asset> asset = Optional.ofNullable(content.getAttributes().get(Asset.class)); return asset.map(Asset::componentId).orElse(null); } }
private void saveAsset(final StorageTx tx, final Asset asset, final AssetBlob assetBlob, final AssetKind kind, @Nullable final AttributesMap contentAttributes) { asset.formatAttributes().set(P_ASSET_KIND, kind.name()); tx.attachBlob(asset, assetBlob); Content.applyToAsset(asset, Content.maintainLastModified(asset, contentAttributes)); tx.saveAsset(asset); }
hashCodes.put(entry.getKey(), entry.getValue().hash()); Content content = new Content(new StreamPayload( new InputStreamSupplier() contentType) ); content.getAttributes().set(Content.CONTENT_LAST_MODIFIED, DateTime.now()); content.getAttributes().set(Content.CONTENT_HASH_CODES_MAP, hashCodes); AttributesMap attributesMap = content.getAttributes(); mayAddETag(attributesMap, getHashAlgorithmFromContent(attributesMap)); return content;
/** * Create {@link Content} out of HTTP response. */ protected Content createContent(final Context context, final HttpResponse response) { return new Content(new HttpEntityPayload(response, response.getEntity())); }
@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 Helm asset {}", content.getAttributes().require(Asset.class) ); return; } log.debug("Updating cacheInfo of {} to {}", asset, cacheInfo); CacheInfo.applyToAsset(asset, cacheInfo); tx.saveAsset(asset); }
@Override @TransactionalTouchMetadata public void setCacheInfo(final String path, final Content content, final CacheInfo cacheInfo) throws IOException { StorageTx tx = UnitOfWork.currentTx(); Bucket bucket = tx.findBucket(getRepository()); // by EntityId Asset asset = Content.findAsset(tx, bucket, content); if (asset == null) { // by format coordinates Component component = tx.findComponentWithProperty(P_NAME, path, bucket); if (component != null) { asset = tx.firstAsset(component); } } if (asset == null) { log.debug("Attempting to set cache info for non-existent raw component {}", path); return; } log.debug("Updating cacheInfo of {} to {}", path, cacheInfo); CacheInfo.applyToAsset(asset, cacheInfo); tx.saveAsset(asset); }