Search in sources :

Example 11 with Content

use of org.sonatype.nexus.repository.view.Content 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 12 with Content

use of org.sonatype.nexus.repository.view.Content 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)

Example 13 with Content

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

the class RHostedFacetImplTest method getStoredContent.

@Test
public void getStoredContent() throws Exception {
    Content archive = underTest.getStoredContent(PACKAGE_PATH);
    assertThat(archive, is(notNullValue()));
}
Also used : Content(org.sonatype.nexus.repository.view.Content) Test(org.junit.Test)

Example 14 with Content

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

the class RHostedFacetImplTest method nullWhenAssetNullOnGetContent.

@Test
public void nullWhenAssetNullOnGetContent() throws Exception {
    when(storageTx.findAssetWithProperty(anyString(), anyString(), any(Bucket.class))).thenReturn(null);
    Content archive = underTest.getStoredContent(PACKAGES_GZ_PATH);
    assertThat(archive, is(nullValue()));
}
Also used : Bucket(org.sonatype.nexus.repository.storage.Bucket) Content(org.sonatype.nexus.repository.view.Content) Test(org.junit.Test)

Example 15 with Content

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

the class RFacetUtilsTest method content.

@Test
public void content() throws Exception {
    Content content = toContent(asset, blob);
    assertThat(content.getAttributes().get("last_modified"), is(notNullValue()));
}
Also used : RFacetUtils.toContent(org.sonatype.nexus.repository.r.internal.util.RFacetUtils.toContent) Content(org.sonatype.nexus.repository.view.Content) Test(org.junit.Test)

Aggregations

Content (org.sonatype.nexus.repository.view.Content)17 Test (org.junit.Test)9 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 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 Map (java.util.Map)2 AttributesMap (org.sonatype.nexus.common.collect.AttributesMap)2 RFacetUtils.toContent (org.sonatype.nexus.repository.r.internal.RFacetUtils.toContent)2 RFacetUtils.toContent (org.sonatype.nexus.repository.r.internal.util.RFacetUtils.toContent)2 Asset (org.sonatype.nexus.repository.storage.Asset)2 Bucket (org.sonatype.nexus.repository.storage.Bucket)2 StorageTx (org.sonatype.nexus.repository.storage.StorageTx)2 TransactionalTouchMetadata (org.sonatype.nexus.repository.transaction.TransactionalTouchMetadata)2 BlobPayload (org.sonatype.nexus.repository.view.payloads.BlobPayload)2 BytesPayload (org.sonatype.nexus.repository.view.payloads.BytesPayload)2