Search in sources :

Example 11 with DistributionPackageImporter

use of org.apache.sling.distribution.packaging.DistributionPackageImporter in project sling by apache.

the class ImportingDistributionPackageProcessorTest method testGetPackagesCount.

@Test
public void testGetPackagesCount() throws Exception {
    DistributionPackageImporter importer = mock(DistributionPackageImporter.class);
    SlingRepository repository = mock(SlingRepository.class);
    String agentService = "agentService";
    ResourceResolverFactory resourceResolverFactory = mock(ResourceResolverFactory.class);
    String subServiceName = "ssn";
    SimpleDistributionAgentAuthenticationInfo authInfo = new SimpleDistributionAgentAuthenticationInfo(repository, agentService, resourceResolverFactory, subServiceName);
    String callingUser = "foo";
    String requestId = "123";
    DefaultDistributionLog log = mock(DefaultDistributionLog.class);
    ImportingDistributionPackageProcessor processor = new ImportingDistributionPackageProcessor(importer, authInfo, callingUser, requestId, log);
    assertEquals(0, processor.getPackagesCount());
}
Also used : SlingRepository(org.apache.sling.jcr.api.SlingRepository) ResourceResolverFactory(org.apache.sling.api.resource.ResourceResolverFactory) DistributionPackageImporter(org.apache.sling.distribution.packaging.DistributionPackageImporter) DefaultDistributionLog(org.apache.sling.distribution.log.impl.DefaultDistributionLog) Test(org.junit.Test)

Example 12 with DistributionPackageImporter

use of org.apache.sling.distribution.packaging.DistributionPackageImporter in project sling by apache.

the class ImportingDistributionPackageProcessorTest method testGetPackagesSize.

@Test
public void testGetPackagesSize() throws Exception {
    DistributionPackageImporter importer = mock(DistributionPackageImporter.class);
    SlingRepository repository = mock(SlingRepository.class);
    String agentService = "agentService";
    ResourceResolverFactory resourceResolverFactory = mock(ResourceResolverFactory.class);
    String subServiceName = "ssn";
    SimpleDistributionAgentAuthenticationInfo authInfo = new SimpleDistributionAgentAuthenticationInfo(repository, agentService, resourceResolverFactory, subServiceName);
    String callingUser = "foo";
    String requestId = "123";
    DefaultDistributionLog log = mock(DefaultDistributionLog.class);
    ImportingDistributionPackageProcessor processor = new ImportingDistributionPackageProcessor(importer, authInfo, callingUser, requestId, log);
    assertEquals(0, processor.getPackagesSize());
}
Also used : SlingRepository(org.apache.sling.jcr.api.SlingRepository) ResourceResolverFactory(org.apache.sling.api.resource.ResourceResolverFactory) DistributionPackageImporter(org.apache.sling.distribution.packaging.DistributionPackageImporter) DefaultDistributionLog(org.apache.sling.distribution.log.impl.DefaultDistributionLog) Test(org.junit.Test)

Example 13 with DistributionPackageImporter

use of org.apache.sling.distribution.packaging.DistributionPackageImporter in project sling by apache.

the class ReverseDistributionAgentFactory method createAgent.

@Override
protected SimpleDistributionAgent createAgent(String agentName, BundleContext context, Map<String, Object> config, DefaultDistributionLog distributionLog) {
    String serviceName = SettingsUtils.removeEmptyEntry(PropertiesUtil.toString(config.get(SERVICE_NAME), null));
    boolean queueProcessingEnabled = PropertiesUtil.toBoolean(config.get(QUEUE_PROCESSING_ENABLED), true);
    String[] exporterEndpoints = PropertiesUtil.toStringArray(config.get(EXPORTER_ENDPOINTS), new String[0]);
    exporterEndpoints = SettingsUtils.removeEmptyEntries(exporterEndpoints);
    int pullItems = PropertiesUtil.toInteger(config.get(PULL_ITEMS), Integer.MAX_VALUE);
    Integer timeout = PropertiesUtil.toInteger(HTTP, 10) * 1000;
    HttpConfiguration httpConfiguration = new HttpConfiguration(timeout);
    DistributionPackageExporter packageExporter = new RemoteDistributionPackageExporter(distributionLog, packageBuilder, transportSecretProvider, exporterEndpoints, pullItems, httpConfiguration);
    DistributionPackageImporter packageImporter = new LocalDistributionPackageImporter(agentName, distributionEventFactory, packageBuilder);
    DistributionQueueProvider queueProvider = new MonitoringDistributionQueueProvider(new JobHandlingDistributionQueueProvider(agentName, jobManager, context), context);
    DistributionQueueDispatchingStrategy exportQueueStrategy = new SingleQueueDispatchingStrategy();
    DistributionQueueDispatchingStrategy importQueueStrategy = null;
    DistributionRequestType[] allowedRequests = new DistributionRequestType[] { DistributionRequestType.PULL };
    Set<String> processingQueues = new HashSet<String>();
    processingQueues.addAll(exportQueueStrategy.getQueueNames());
    return new SimpleDistributionAgent(agentName, queueProcessingEnabled, processingQueues, serviceName, packageImporter, packageExporter, requestAuthorizationStrategy, queueProvider, exportQueueStrategy, importQueueStrategy, distributionEventFactory, resourceResolverFactory, slingRepository, distributionLog, allowedRequests, null, 0);
}
Also used : DistributionQueueDispatchingStrategy(org.apache.sling.distribution.queue.impl.DistributionQueueDispatchingStrategy) LocalDistributionPackageImporter(org.apache.sling.distribution.packaging.impl.importer.LocalDistributionPackageImporter) RemoteDistributionPackageExporter(org.apache.sling.distribution.packaging.impl.exporter.RemoteDistributionPackageExporter) JobHandlingDistributionQueueProvider(org.apache.sling.distribution.queue.impl.jobhandling.JobHandlingDistributionQueueProvider) HttpConfiguration(org.apache.sling.distribution.transport.impl.HttpConfiguration) SingleQueueDispatchingStrategy(org.apache.sling.distribution.queue.impl.SingleQueueDispatchingStrategy) RemoteDistributionPackageExporter(org.apache.sling.distribution.packaging.impl.exporter.RemoteDistributionPackageExporter) DistributionPackageExporter(org.apache.sling.distribution.packaging.DistributionPackageExporter) DistributionRequestType(org.apache.sling.distribution.DistributionRequestType) MonitoringDistributionQueueProvider(org.apache.sling.distribution.monitor.impl.MonitoringDistributionQueueProvider) JobHandlingDistributionQueueProvider(org.apache.sling.distribution.queue.impl.jobhandling.JobHandlingDistributionQueueProvider) DistributionQueueProvider(org.apache.sling.distribution.queue.DistributionQueueProvider) LocalDistributionPackageImporter(org.apache.sling.distribution.packaging.impl.importer.LocalDistributionPackageImporter) DistributionPackageImporter(org.apache.sling.distribution.packaging.DistributionPackageImporter) MonitoringDistributionQueueProvider(org.apache.sling.distribution.monitor.impl.MonitoringDistributionQueueProvider) HashSet(java.util.HashSet)

Example 14 with DistributionPackageImporter

use of org.apache.sling.distribution.packaging.DistributionPackageImporter in project sling by apache.

the class DistributionPackageImporterServlet method doPost.

@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
    DistributionPackageImporter distributionPackageImporter = request.getResource().adaptTo(DistributionPackageImporter.class);
    String digestAlgorithm = null;
    String digestMessage = null;
    String digestHeader = request.getHeader(DIGEST_HEADER);
    if (isNotEmpty(digestHeader)) {
        log.debug("Found Digest header {}, extracting algorithm and message...", digestHeader);
        Matcher matcher = digestHeaderRegex.matcher(digestHeader);
        if (matcher.matches()) {
            digestAlgorithm = matcher.group(1);
            digestMessage = matcher.group(2);
        } else {
            log.debug("Digest header {} not supported, it doesn't match with expected pattern {}", new Object[] { digestHeader, digestHeaderRegex.pattern() });
        }
    }
    final long start = System.currentTimeMillis();
    response.setContentType("application/json");
    InputStream stream;
    if (isNotEmpty(digestAlgorithm) && isNotEmpty(digestMessage)) {
        stream = openDigestInputStream(request.getInputStream(), digestAlgorithm);
    } else {
        stream = request.getInputStream();
    }
    ResourceResolver resourceResolver = request.getResourceResolver();
    try {
        if (request.getParameter("forceError") != null) {
            throw new Exception("manually forced error");
        }
        DistributionPackageInfo distributionPackageInfo = distributionPackageImporter.importStream(resourceResolver, stream);
        long end = System.currentTimeMillis();
        if (isNotEmpty(digestAlgorithm) && isNotEmpty(digestMessage)) {
            String receivedDigestMessage = readDigestMessage((DigestInputStream) stream);
            if (!digestMessage.equalsIgnoreCase(receivedDigestMessage)) {
                log.error("Error during distribution import: received distribution package is corrupted, expected [{}] but received [{}]", new Object[] { digestMessage, receivedDigestMessage });
                Map<String, String> kv = new HashMap<String, String>();
                kv.put("digestAlgorithm", digestAlgorithm);
                kv.put("expected", digestMessage);
                kv.put("received", receivedDigestMessage);
                ServletJsonUtils.writeJson(response, SC_BAD_REQUEST, "Received distribution package is corrupted", kv);
                return;
            }
        }
        log.info("Package {} imported successfully in {}ms", distributionPackageInfo, end - start);
        ServletJsonUtils.writeJson(response, SC_OK, "package imported successfully", null);
    } catch (final Throwable e) {
        ServletJsonUtils.writeJson(response, SC_INTERNAL_SERVER_ERROR, "an unexpected error has occurred during distribution import", null);
        log.error("Error during distribution import", e);
    } finally {
        long end = System.currentTimeMillis();
        log.debug("Processed package import request in {} ms", end - start);
    }
}
Also used : DistributionPackageInfo(org.apache.sling.distribution.packaging.DistributionPackageInfo) Matcher(java.util.regex.Matcher) HashMap(java.util.HashMap) DigestUtils.openDigestInputStream(org.apache.sling.distribution.util.impl.DigestUtils.openDigestInputStream) DigestInputStream(java.security.DigestInputStream) InputStream(java.io.InputStream) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) DistributionPackageImporter(org.apache.sling.distribution.packaging.DistributionPackageImporter) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 15 with DistributionPackageImporter

use of org.apache.sling.distribution.packaging.DistributionPackageImporter in project sling by apache.

the class SimpleDistributionAgentTest method testDistribution.

@Test
public void testDistribution() throws Exception {
    String name = "sample-agent";
    DistributionPackageImporter packageImporter = mock(DistributionPackageImporter.class);
    DistributionPackageExporter packageExporter = mock(DistributionPackageExporter.class);
    DistributionRequestAuthorizationStrategy requestAuthorizationStrategy = mock(DistributionRequestAuthorizationStrategy.class);
    DistributionQueueProvider queueProvider = mock(DistributionQueueProvider.class);
    DistributionQueueDispatchingStrategy dispatchingStrategy = mock(DistributionQueueDispatchingStrategy.class);
    DistributionEventFactory distributionEventFactory = mock(DistributionEventFactory.class);
    ResourceResolverFactory resolverFactory = mock(ResourceResolverFactory.class);
    when(dispatchingStrategy.add(any(DistributionPackage.class), any(DistributionQueueProvider.class))).thenReturn(new ArrayList<DistributionQueueItemStatus>());
    SimpleDistributionAgent agent = new SimpleDistributionAgent(name, false, null, "serviceName", packageImporter, packageExporter, requestAuthorizationStrategy, queueProvider, dispatchingStrategy, null, distributionEventFactory, resolverFactory, mock(SlingRepository.class), mock(DefaultDistributionLog.class), null, null, 0);
    DistributionRequest request = new SimpleDistributionRequest(DistributionRequestType.ADD, "/");
    final DistributionPackage distributionPackage = mock(DistributionPackage.class);
    DistributionPackageInfo packageInfo = new DistributionPackageInfo("type");
    when(distributionPackage.getInfo()).thenReturn(packageInfo);
    ResourceResolver resourceResolver = mock(ResourceResolver.class);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
            Object[] args = invocationOnMock.getArguments();
            ((DistributionPackageProcessor) args[2]).process(distributionPackage);
            return null;
        }
    }).when(packageExporter).exportPackages(any(ResourceResolver.class), any(DistributionRequest.class), any(DistributionPackageProcessor.class));
    when(queueProvider.getQueue(DistributionQueueDispatchingStrategy.DEFAULT_QUEUE_NAME)).thenReturn(new SimpleDistributionQueue(name, "name"));
    agent.execute(resourceResolver, request);
}
Also used : DistributionQueueDispatchingStrategy(org.apache.sling.distribution.queue.impl.DistributionQueueDispatchingStrategy) DistributionQueueItemStatus(org.apache.sling.distribution.queue.DistributionQueueItemStatus) DistributionPackageInfo(org.apache.sling.distribution.packaging.DistributionPackageInfo) SimpleDistributionQueue(org.apache.sling.distribution.queue.impl.simple.SimpleDistributionQueue) SlingRepository(org.apache.sling.jcr.api.SlingRepository) DistributionPackageProcessor(org.apache.sling.distribution.packaging.DistributionPackageProcessor) DistributionPackage(org.apache.sling.distribution.packaging.DistributionPackage) ResourceResolverFactory(org.apache.sling.api.resource.ResourceResolverFactory) DefaultDistributionLog(org.apache.sling.distribution.log.impl.DefaultDistributionLog) DistributionPackageExporter(org.apache.sling.distribution.packaging.DistributionPackageExporter) DistributionEventFactory(org.apache.sling.distribution.event.impl.DistributionEventFactory) SimpleDistributionRequest(org.apache.sling.distribution.SimpleDistributionRequest) DistributionRequest(org.apache.sling.distribution.DistributionRequest) SimpleDistributionRequest(org.apache.sling.distribution.SimpleDistributionRequest) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) DistributionQueueProvider(org.apache.sling.distribution.queue.DistributionQueueProvider) DistributionPackageImporter(org.apache.sling.distribution.packaging.DistributionPackageImporter) Test(org.junit.Test)

Aggregations

DistributionPackageImporter (org.apache.sling.distribution.packaging.DistributionPackageImporter)17 DefaultDistributionLog (org.apache.sling.distribution.log.impl.DefaultDistributionLog)13 Test (org.junit.Test)13 ResourceResolverFactory (org.apache.sling.api.resource.ResourceResolverFactory)12 DistributionPackageExporter (org.apache.sling.distribution.packaging.DistributionPackageExporter)12 DistributionQueueProvider (org.apache.sling.distribution.queue.DistributionQueueProvider)12 SlingRepository (org.apache.sling.jcr.api.SlingRepository)12 DistributionQueueDispatchingStrategy (org.apache.sling.distribution.queue.impl.DistributionQueueDispatchingStrategy)11 DistributionEventFactory (org.apache.sling.distribution.event.impl.DistributionEventFactory)9 DistributionPackage (org.apache.sling.distribution.packaging.DistributionPackage)7 DistributionPackageInfo (org.apache.sling.distribution.packaging.DistributionPackageInfo)7 DistributionQueueItemStatus (org.apache.sling.distribution.queue.DistributionQueueItemStatus)7 SimpleDistributionQueue (org.apache.sling.distribution.queue.impl.simple.SimpleDistributionQueue)7 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)6 DistributionRequest (org.apache.sling.distribution.DistributionRequest)5 SimpleDistributionRequest (org.apache.sling.distribution.SimpleDistributionRequest)5 DistributionPackageProcessor (org.apache.sling.distribution.packaging.DistributionPackageProcessor)5 InvocationOnMock (org.mockito.invocation.InvocationOnMock)5 DistributionResponse (org.apache.sling.distribution.DistributionResponse)4 HashMap (java.util.HashMap)3