Search in sources :

Example 1 with DistributionExportFilter

use of org.apache.sling.distribution.serialization.DistributionExportFilter in project sling by apache.

the class FileVaultContentSerializerTest method testExportToStream.

@Test
public void testExportToStream() throws Exception {
    Packaging packaging = mock(Packaging.class);
    ImportMode importMode = ImportMode.REPLACE;
    AccessControlHandling aclHandling = AccessControlHandling.IGNORE;
    String[] packageRoots = new String[] { "/etc/packages" };
    String[] nodeFilters = new String[0];
    String[] propertyFilters = new String[0];
    boolean useReferences = false;
    int threshold = 1024;
    FileVaultContentSerializer fileVaultContentSerializer = new FileVaultContentSerializer("vlt", packaging, importMode, aclHandling, packageRoots, nodeFilters, propertyFilters, useReferences, threshold);
    ResourceResolver sessionResolver = mock(ResourceResolver.class);
    Session session = mock(Session.class);
    PackageManager pm = mock(PackageManager.class);
    when(packaging.getPackageManager()).thenReturn(pm);
    OutputStream outputStream = new ByteArrayOutputStream();
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
            return null;
        }
    }).when(pm).assemble(same(session), any(ExportOptions.class), same(outputStream));
    Workspace workspace = mock(Workspace.class);
    ObservationManager observationManager = mock(ObservationManager.class);
    when(workspace.getObservationManager()).thenReturn(observationManager);
    when(session.getWorkspace()).thenReturn(workspace);
    when(sessionResolver.adaptTo(Session.class)).thenReturn(session);
    DistributionExportFilter filter = mock(DistributionExportFilter.class);
    DistributionRequest request = mock(DistributionRequest.class);
    when(request.getPaths()).thenReturn(new String[] { "/libs" });
    when(request.getFilters("/libs")).thenReturn(new String[0]);
    DistributionExportOptions exportOptions = new DistributionExportOptions(request, filter);
    fileVaultContentSerializer.exportToStream(sessionResolver, exportOptions, outputStream);
}
Also used : AccessControlHandling(org.apache.jackrabbit.vault.fs.io.AccessControlHandling) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObservationManager(javax.jcr.observation.ObservationManager) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DistributionExportOptions(org.apache.sling.distribution.serialization.DistributionExportOptions) DistributionRequest(org.apache.sling.distribution.DistributionRequest) ImportMode(org.apache.jackrabbit.vault.fs.api.ImportMode) PackageManager(org.apache.jackrabbit.vault.packaging.PackageManager) InvocationOnMock(org.mockito.invocation.InvocationOnMock) DistributionExportOptions(org.apache.sling.distribution.serialization.DistributionExportOptions) ExportOptions(org.apache.jackrabbit.vault.packaging.ExportOptions) Packaging(org.apache.jackrabbit.vault.packaging.Packaging) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) DistributionExportFilter(org.apache.sling.distribution.serialization.DistributionExportFilter) Session(javax.jcr.Session) Workspace(javax.jcr.Workspace) Test(org.junit.Test)

Example 2 with DistributionExportFilter

use of org.apache.sling.distribution.serialization.DistributionExportFilter in project sling by apache.

the class KryoContentSerializer method exportToStream.

@Override
public void exportToStream(ResourceResolver resourceResolver, DistributionExportOptions options, OutputStream outputStream) throws DistributionException {
    DistributionExportFilter filter = options.getFilter();
    Kryo kryo = new Kryo();
    kryo.setInstantiatorStrategy(new Kryo.DefaultInstantiatorStrategy(new StdInstantiatorStrategy()));
    kryo.addDefaultSerializer(Resource.class, new ResourceSerializer(filter.getPropertyFilter()));
    kryo.addDefaultSerializer(InputStream.class, new InputStreamSerializer());
    Output output = new Output(outputStream);
    LinkedList<Resource> resources = new LinkedList<Resource>();
    for (DistributionExportFilter.TreeFilter nodeFilter : filter.getNodeFilters()) {
        Resource resource = resourceResolver.getResource(nodeFilter.getPath());
        if (resource != null) {
            addResource(nodeFilter, resources, resource);
        }
    }
    kryo.writeObject(output, resources);
    output.flush();
}
Also used : StdInstantiatorStrategy(org.objenesis.strategy.StdInstantiatorStrategy) Output(com.esotericsoftware.kryo.io.Output) Resource(org.apache.sling.api.resource.Resource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) DistributionExportFilter(org.apache.sling.distribution.serialization.DistributionExportFilter) Kryo(com.esotericsoftware.kryo.Kryo) LinkedList(java.util.LinkedList)

Example 3 with DistributionExportFilter

use of org.apache.sling.distribution.serialization.DistributionExportFilter in project sling by apache.

the class AvroContentSerializer method exportToStream.

@Override
public void exportToStream(ResourceResolver resourceResolver, DistributionExportOptions options, OutputStream outputStream) throws DistributionException {
    DatumWriter<AvroShallowResource> datumWriter = new SpecificDatumWriter<AvroShallowResource>(AvroShallowResource.class);
    DataFileWriter<AvroShallowResource> writer = new DataFileWriter<AvroShallowResource>(datumWriter);
    try {
        writer.create(schema, outputStream);
    } catch (IOException e) {
        throw new DistributionException(e);
    }
    try {
        DistributionExportFilter filter = options.getFilter();
        for (DistributionExportFilter.TreeFilter treeFilter : filter.getNodeFilters()) {
            String path = treeFilter.getPath();
            Resource resource = resourceResolver.getResource(path);
            AvroShallowResource avroShallowResource = getAvroShallowResource(treeFilter, filter.getPropertyFilter(), resource);
            writer.append(avroShallowResource);
        }
        outputStream.flush();
    } catch (Exception e) {
        throw new DistributionException(e);
    } finally {
        try {
            writer.close();
        } catch (IOException e) {
        // do nothing
        }
    }
}
Also used : DataFileWriter(org.apache.avro.file.DataFileWriter) Resource(org.apache.sling.api.resource.Resource) DistributionException(org.apache.sling.distribution.common.DistributionException) DistributionExportFilter(org.apache.sling.distribution.serialization.DistributionExportFilter) IOException(java.io.IOException) DistributionException(org.apache.sling.distribution.common.DistributionException) PersistenceException(org.apache.sling.api.resource.PersistenceException) IOException(java.io.IOException) SpecificDatumWriter(org.apache.avro.specific.SpecificDatumWriter)

Example 4 with DistributionExportFilter

use of org.apache.sling.distribution.serialization.DistributionExportFilter in project sling by apache.

the class AvroContentSerializerTest method testExtractDeep.

@Test
public void testExtractDeep() throws Exception {
    AvroContentSerializer avroContentSerializer = new AvroContentSerializer("avro");
    DistributionRequest request = new SimpleDistributionRequest(DistributionRequestType.ADD, true, "/libs");
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    NavigableMap<String, List<String>> nodeFilters = new TreeMap<String, List<String>>();
    NavigableMap<String, List<String>> propertyFilters = new TreeMap<String, List<String>>();
    try {
        DistributionExportFilter filter = DistributionExportFilter.createFilter(request, nodeFilters, propertyFilters);
        avroContentSerializer.exportToStream(resourceResolver, new DistributionExportOptions(request, filter), outputStream);
        byte[] bytes = outputStream.toByteArray();
        assertNotNull(bytes);
        assertTrue(bytes.length > 0);
    } finally {
        outputStream.close();
    }
}
Also used : DistributionExportOptions(org.apache.sling.distribution.serialization.DistributionExportOptions) SimpleDistributionRequest(org.apache.sling.distribution.SimpleDistributionRequest) DistributionRequest(org.apache.sling.distribution.DistributionRequest) SimpleDistributionRequest(org.apache.sling.distribution.SimpleDistributionRequest) List(java.util.List) DistributionExportFilter(org.apache.sling.distribution.serialization.DistributionExportFilter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TreeMap(java.util.TreeMap) Test(org.junit.Test)

Example 5 with DistributionExportFilter

use of org.apache.sling.distribution.serialization.DistributionExportFilter in project sling by apache.

the class AvroContentSerializerTest method testExtractShallow.

@Test
public void testExtractShallow() throws Exception {
    AvroContentSerializer avroContentSerializer = new AvroContentSerializer("avro");
    DistributionRequest request = new SimpleDistributionRequest(DistributionRequestType.ADD, "/libs");
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    NavigableMap<String, List<String>> nodeFilters = new TreeMap<String, List<String>>();
    NavigableMap<String, List<String>> propertyFilters = new TreeMap<String, List<String>>();
    try {
        DistributionExportFilter filter = DistributionExportFilter.createFilter(request, nodeFilters, propertyFilters);
        avroContentSerializer.exportToStream(resourceResolver, new DistributionExportOptions(request, filter), outputStream);
        byte[] bytes = outputStream.toByteArray();
        assertNotNull(bytes);
        assertTrue(bytes.length > 0);
    } finally {
        outputStream.close();
    }
}
Also used : DistributionExportOptions(org.apache.sling.distribution.serialization.DistributionExportOptions) SimpleDistributionRequest(org.apache.sling.distribution.SimpleDistributionRequest) DistributionRequest(org.apache.sling.distribution.DistributionRequest) SimpleDistributionRequest(org.apache.sling.distribution.SimpleDistributionRequest) List(java.util.List) DistributionExportFilter(org.apache.sling.distribution.serialization.DistributionExportFilter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TreeMap(java.util.TreeMap) Test(org.junit.Test)

Aggregations

DistributionExportFilter (org.apache.sling.distribution.serialization.DistributionExportFilter)8 DistributionExportOptions (org.apache.sling.distribution.serialization.DistributionExportOptions)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 DistributionRequest (org.apache.sling.distribution.DistributionRequest)4 Test (org.junit.Test)4 List (java.util.List)3 TreeMap (java.util.TreeMap)3 SimpleDistributionRequest (org.apache.sling.distribution.SimpleDistributionRequest)3 IOException (java.io.IOException)2 OutputStream (java.io.OutputStream)2 Resource (org.apache.sling.api.resource.Resource)2 DistributionException (org.apache.sling.distribution.common.DistributionException)2 Kryo (com.esotericsoftware.kryo.Kryo)1 Output (com.esotericsoftware.kryo.io.Output)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 DigestOutputStream (java.security.DigestOutputStream)1 LinkedList (java.util.LinkedList)1 Session (javax.jcr.Session)1 Workspace (javax.jcr.Workspace)1