/** * Returnes opened stream from configured supplier. */ @Override public InputStream openInputStream() throws IOException { return stream.get(); }
@Override public String toString() { return getClass().getSimpleName() + "{" + "size=" + getSize() + ", contentType='" + contentType + '\'' + '}'; } }
/** * Create {@link Content} out of HTTP response. */ protected Content createContent(final Context context, final HttpResponse response) { return new Content(new HttpEntityPayload(response, response.getEntity())); }
/** * Convert an asset blob to {@link Content}. * * @return content of asset blob */ public Content toContent(final Asset asset, final Blob blob) { Content content = new Content(new BlobPayload(blob, asset.requireContentType())); Content.extractFromAsset(asset, HASH_ALGORITHMS, content.getAttributes()); return content; } }
private static Payload cachePayload(final Content remote) throws IOException { try (InputStream in = remote.openInputStream()) { return new BytesPayload(toByteArray(in), remote.getContentType()); } } }
/** * 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)); } }
@Nonnull @Override public Response handle(@Nonnull final Context context) throws Exception { TemplateParameters params = templateHelper.parameters(); params.set("repository", context.getRepository()); String html = templateHelper.render(template, params); return HttpResponses.ok(new StringPayload(html, "text/html")); }
@Override public void close() throws IOException { if (path != null) { mavenFacet.put( mavenPath, new StreamPayload( new InputStreamSupplier() { @Nonnull @Override public InputStream get() throws IOException { return new BufferedInputStream(Files.newInputStream(path)); } }, Files.size(path), contentType ) ); Files.delete(path); path = null; } } }
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; } }
/** * Merges the metadata but doesn't cache it. Returns {@code null} if no usable response was in passed in map. * * @since 3.13 */ @Nullable public Content mergeWithoutCaching(final MavenPath mavenPath, final Map<Repository, Response> responses) throws IOException { return merge(mavenPath, responses, Function.identity(), (in, contentType) -> { // load bytes in memory to make content re-usable; metadata shouldn't be too large // (don't include cache-related attributes since this content has not been cached) return new Content(new BytesPayload(toByteArray(in), contentType)); }); }
private String generatePom(final Repository repository, final String basePath, final String groupId, final String artifactId, final String version, @Nullable final String packaging) throws IOException { log.debug("Generating pom for {} {} {} with packaging {}", groupId, artifactId, version, packaging); String pom = mavenPomGenerator.generatePom(groupId, artifactId, version, packaging); MavenPath mavenPath = parser.parsePath(basePath + ".pom"); storeAssetContent(repository, mavenPath, new StringPayload(pom, "text/xml")); return mavenPath.getPath(); }
private Content toContent(final Asset asset, final Blob blob) { final String contentType = asset.contentType(); final Content content = new Content(new BlobPayload(blob, contentType)); Content.extractFromAsset(asset, HashType.ALGORITHMS, content.getAttributes()); return content; }
private static Payload statusPayload(final boolean success, @Nullable final String error) { NestedAttributesMap errorObject = new NestedAttributesMap("error", Maps.<String, Object>newHashMap()); errorObject.set("success", Boolean.valueOf(success)); if (error != null) { errorObject.set("error", error); } return new BytesPayload(NpmJsonUtils.bytes(errorObject), ContentTypes.APPLICATION_JSON); }
public static 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; }
/** * Creates a {@link Content} out of passed in package metadata. */ @Nonnull static Content toContent(final Asset packageRootAsset, final NestedAttributesMap packageRoot) { Content content = new Content(new BytesPayload(NpmJsonUtils.bytes(packageRoot), ContentTypes.APPLICATION_JSON)); Content.extractFromAsset(packageRootAsset, HASH_ALGORITHMS, content.getAttributes()); content.getAttributes().set(NestedAttributesMap.class, packageRoot); return content; }
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; } }
public Content getPublicKey() throws IOException, PGPException { PGPSecretKey signKey = readSecretKey(); PGPPublicKey publicKey = signKey.getPublicKey(); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); try (BCPGOutputStream os = new BCPGOutputStream(new ArmoredOutputStream(buffer))) { publicKey.encode(os); } return new Content(new BytesPayload(buffer.toByteArray(), AptMimeTypes.PUBLICKEY)); }
/** * Convert an asset blob to {@link Content}. * * @return content of asset blob */ @Nonnull public static Content toContent(final Asset asset, final Blob blob) { Content content = new Content(new BlobPayload(blob, asset.requireContentType())); Content.extractFromAsset(asset, HASH_ALGORITHMS, content.getAttributes()); return content; }
@Override public Response logout(final Context context) { if (npmTokenManager.logout()) { NestedAttributesMap response = new NestedAttributesMap("response", Maps.newHashMap()); response.set("ok", Boolean.TRUE.toString()); return NpmResponses.ok(new BytesPayload(NpmJsonUtils.bytes(response), ContentTypes.APPLICATION_JSON)); } else { return NpmResponses.notFound("Token not found"); } } }
/** * Returns the tarball content. */ @Nullable static Content getTarballContent(final StorageTx tx, final Bucket bucket, final NpmPackageId packageId, final String tarballName) { Asset asset = findTarballAsset(tx, bucket, packageId, tarballName); if (asset == null) { return null; } Blob blob = tx.requireBlob(asset.requireBlobRef()); Content content = new Content(new BlobPayload(blob, asset.requireContentType())); Content.extractFromAsset(asset, HASH_ALGORITHMS, content.getAttributes()); return content; }