Search in sources :

Example 1 with BytesPayload

use of org.sonatype.nexus.repository.view.payloads.BytesPayload in project nexus-repository-r by sonatype-nexus-community.

the class RPackagesUtils method buildPackages.

public static Content buildPackages(final Collection<Map<String, String>> entries) throws IOException {
    CompressorStreamFactory compressorStreamFactory = new CompressorStreamFactory();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    try (CompressorOutputStream cos = compressorStreamFactory.createCompressorOutputStream(GZIP, os)) {
        try (OutputStreamWriter writer = new OutputStreamWriter(cos, UTF_8)) {
            for (Map<String, String> entry : entries) {
                InternetHeaders headers = new InternetHeaders();
                headers.addHeader(P_PACKAGE, entry.get(P_PACKAGE));
                headers.addHeader(P_VERSION, entry.get(P_VERSION));
                headers.addHeader(P_DEPENDS, entry.get(P_DEPENDS));
                headers.addHeader(P_IMPORTS, entry.get(P_IMPORTS));
                headers.addHeader(P_SUGGESTS, entry.get(P_SUGGESTS));
                headers.addHeader(P_LINKINGTO, entry.get(P_LINKINGTO));
                headers.addHeader(P_LICENSE, entry.get(P_LICENSE));
                headers.addHeader(P_NEEDS_COMPILATION, entry.get(P_NEEDS_COMPILATION));
                Enumeration<String> headerLines = headers.getAllHeaderLines();
                while (headerLines.hasMoreElements()) {
                    String line = headerLines.nextElement();
                    writer.write(line, 0, line.length());
                    writer.write('\n');
                }
                writer.write('\n');
            }
        }
    } catch (CompressorException e) {
        throw new RException(null, e);
    }
    return new Content(new BytesPayload(os.toByteArray(), "application/x-gzip"));
}
Also used : InternetHeaders(javax.mail.internet.InternetHeaders) BytesPayload(org.sonatype.nexus.repository.view.payloads.BytesPayload) RException(org.sonatype.nexus.repository.r.internal.RException) CompressorException(org.apache.commons.compress.compressors.CompressorException) Content(org.sonatype.nexus.repository.view.Content) CompressorOutputStream(org.apache.commons.compress.compressors.CompressorOutputStream) CompressorStreamFactory(org.apache.commons.compress.compressors.CompressorStreamFactory) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 2 with BytesPayload

use of org.sonatype.nexus.repository.view.payloads.BytesPayload in project nexus-repository-r by sonatype-nexus-community.

the class RHostedFacetImpl method getPackages.

@Override
@TransactionalTouchMetadata
public Content getPackages(final String packagesPath) {
    checkNotNull(packagesPath);
    try {
        // TODO: Do NOT do this on each request as there is at least some overhead, and memory usage is proportional to
        // the number of packages contained in a particular path. We should be able to generate this when something has
        // changed or via a scheduled task using an approach similar to the Yum implementation rather than this method.
        StorageTx tx = UnitOfWork.currentTx();
        RPackagesBuilder packagesBuilder = new RPackagesBuilder(packagesPath);
        for (Asset asset : tx.browseAssets(tx.findBucket(getRepository()))) {
            packagesBuilder.append(asset);
        }
        CompressorStreamFactory compressorStreamFactory = new CompressorStreamFactory();
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        try (CompressorOutputStream cos = compressorStreamFactory.createCompressorOutputStream(GZIP, os)) {
            try (OutputStreamWriter writer = new OutputStreamWriter(cos, UTF_8)) {
                for (Entry<String, Map<String, String>> eachPackage : packagesBuilder.getPackageInformation().entrySet()) {
                    Map<String, String> packageInfo = eachPackage.getValue();
                    InternetHeaders headers = new InternetHeaders();
                    headers.addHeader(P_PACKAGE, packageInfo.get(P_PACKAGE));
                    headers.addHeader(P_VERSION, packageInfo.get(P_VERSION));
                    headers.addHeader(P_DEPENDS, packageInfo.get(P_DEPENDS));
                    headers.addHeader(P_IMPORTS, packageInfo.get(P_IMPORTS));
                    headers.addHeader(P_SUGGESTS, packageInfo.get(P_SUGGESTS));
                    headers.addHeader(P_LICENSE, packageInfo.get(P_LICENSE));
                    headers.addHeader(P_NEEDS_COMPILATION, packageInfo.get(P_NEEDS_COMPILATION));
                    Enumeration<String> headerLines = headers.getAllHeaderLines();
                    while (headerLines.hasMoreElements()) {
                        String line = headerLines.nextElement();
                        writer.write(line, 0, line.length());
                        writer.write('\n');
                    }
                    writer.write('\n');
                }
            }
        }
        return new Content(new BytesPayload(os.toByteArray(), "application/x-gzip"));
    } catch (CompressorException | IOException e) {
        throw new RException(packagesPath, e);
    }
}
Also used : StorageTx(org.sonatype.nexus.repository.storage.StorageTx) InternetHeaders(javax.mail.internet.InternetHeaders) BytesPayload(org.sonatype.nexus.repository.view.payloads.BytesPayload) CompressorOutputStream(org.apache.commons.compress.compressors.CompressorOutputStream) CompressorStreamFactory(org.apache.commons.compress.compressors.CompressorStreamFactory) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) RFacetUtils.toContent(org.sonatype.nexus.repository.r.internal.RFacetUtils.toContent) Content(org.sonatype.nexus.repository.view.Content) CompressorException(org.apache.commons.compress.compressors.CompressorException) RFacetUtils.findAsset(org.sonatype.nexus.repository.r.internal.RFacetUtils.findAsset) RFacetUtils.saveAsset(org.sonatype.nexus.repository.r.internal.RFacetUtils.saveAsset) Asset(org.sonatype.nexus.repository.storage.Asset) OutputStreamWriter(java.io.OutputStreamWriter) Map(java.util.Map) TransactionalTouchMetadata(org.sonatype.nexus.repository.transaction.TransactionalTouchMetadata)

Example 3 with BytesPayload

use of org.sonatype.nexus.repository.view.payloads.BytesPayload in project nexus-repository-r by sonatype-nexus-community.

the class RPackagesUtils method buildPackages.

static Content buildPackages(final Collection<Map<String, String>> entries) throws IOException {
    CompressorStreamFactory compressorStreamFactory = new CompressorStreamFactory();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    try (CompressorOutputStream cos = compressorStreamFactory.createCompressorOutputStream(GZIP, os)) {
        try (OutputStreamWriter writer = new OutputStreamWriter(cos, UTF_8)) {
            for (Map<String, String> entry : entries) {
                InternetHeaders headers = new InternetHeaders();
                headers.addHeader(P_PACKAGE, entry.get(P_PACKAGE));
                headers.addHeader(P_VERSION, entry.get(P_VERSION));
                headers.addHeader(P_DEPENDS, entry.get(P_DEPENDS));
                headers.addHeader(P_IMPORTS, entry.get(P_IMPORTS));
                headers.addHeader(P_SUGGESTS, entry.get(P_SUGGESTS));
                headers.addHeader(P_LICENSE, entry.get(P_LICENSE));
                headers.addHeader(P_NEEDS_COMPILATION, entry.get(P_NEEDS_COMPILATION));
                Enumeration<String> headerLines = headers.getAllHeaderLines();
                while (headerLines.hasMoreElements()) {
                    String line = headerLines.nextElement();
                    writer.write(line, 0, line.length());
                    writer.write('\n');
                }
                writer.write('\n');
            }
        }
    } catch (CompressorException e) {
        throw new RException(null, e);
    }
    return new Content(new BytesPayload(os.toByteArray(), "application/x-gzip"));
}
Also used : InternetHeaders(javax.mail.internet.InternetHeaders) BytesPayload(org.sonatype.nexus.repository.view.payloads.BytesPayload) CompressorException(org.apache.commons.compress.compressors.CompressorException) Content(org.sonatype.nexus.repository.view.Content) CompressorOutputStream(org.apache.commons.compress.compressors.CompressorOutputStream) CompressorStreamFactory(org.apache.commons.compress.compressors.CompressorStreamFactory) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 OutputStreamWriter (java.io.OutputStreamWriter)3 InternetHeaders (javax.mail.internet.InternetHeaders)3 CompressorException (org.apache.commons.compress.compressors.CompressorException)3 CompressorOutputStream (org.apache.commons.compress.compressors.CompressorOutputStream)3 CompressorStreamFactory (org.apache.commons.compress.compressors.CompressorStreamFactory)3 Content (org.sonatype.nexus.repository.view.Content)3 BytesPayload (org.sonatype.nexus.repository.view.payloads.BytesPayload)3 IOException (java.io.IOException)1 Map (java.util.Map)1 RException (org.sonatype.nexus.repository.r.internal.RException)1 RFacetUtils.findAsset (org.sonatype.nexus.repository.r.internal.RFacetUtils.findAsset)1 RFacetUtils.saveAsset (org.sonatype.nexus.repository.r.internal.RFacetUtils.saveAsset)1 RFacetUtils.toContent (org.sonatype.nexus.repository.r.internal.RFacetUtils.toContent)1 Asset (org.sonatype.nexus.repository.storage.Asset)1 StorageTx (org.sonatype.nexus.repository.storage.StorageTx)1 TransactionalTouchMetadata (org.sonatype.nexus.repository.transaction.TransactionalTouchMetadata)1