Search in sources :

Example 21 with DistributionPackageInfo

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

the class ExtendedDistributionServiceResourceProvider method getItemProperties.

private Map<String, Object> getItemProperties(DistributionQueueEntry entry) {
    Map<String, Object> result = new HashMap<String, Object>();
    if (entry != null) {
        result.put(SLING_RESOURCE_TYPE, DistributionResourceTypes.AGENT_QUEUE_ITEM_RESOURCE_TYPE);
        DistributionQueueItem item = entry.getItem();
        DistributionPackageInfo packageInfo = DistributionPackageUtils.fromQueueItem(item);
        result.put("id", entry.getId());
        result.put("size", item.getSize());
        result.put("paths", packageInfo.getPaths());
        result.put("action", packageInfo.getRequestType());
        result.put("userid", packageInfo.get(DistributionPackageUtils.PACKAGE_INFO_PROPERTY_REQUEST_USER, String.class));
        DistributionQueueItemStatus status = entry.getStatus();
        result.put("attempts", status.getAttempts());
        result.put("time", status.getEntered().getTime());
        result.put("state", status.getItemState().name());
    }
    return result;
}
Also used : DistributionQueueItemStatus(org.apache.sling.distribution.queue.DistributionQueueItemStatus) DistributionPackageInfo(org.apache.sling.distribution.packaging.DistributionPackageInfo) HashMap(java.util.HashMap) DistributionQueueItem(org.apache.sling.distribution.queue.DistributionQueueItem)

Example 22 with DistributionPackageInfo

use of org.apache.sling.distribution.packaging.DistributionPackageInfo 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 23 with DistributionPackageInfo

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

the class SimpleHttpDistributionTransportTest method testDeliverPackage.

@Test
public void testDeliverPackage() throws Exception {
    DistributionTransportSecret secret = mock(DistributionTransportSecret.class);
    Map<String, String> credentialsMap = new HashMap<String, String>();
    credentialsMap.put("username", "foo");
    credentialsMap.put("password", "foo");
    when(secret.asCredentialsMap()).thenReturn(credentialsMap);
    DistributionTransportSecretProvider secretProvider = mock(DistributionTransportSecretProvider.class);
    when(secretProvider.getSecret(any(URI.class))).thenReturn(secret);
    Executor executor = mock(Executor.class);
    Response response = mock(Response.class);
    when(executor.execute(any(Request.class))).thenReturn(response);
    DistributionEndpoint endpoint = new DistributionEndpoint("http://127.0.0.1:8080/some/resource");
    DistributionPackageBuilder packageBuilder = mock(DistributionPackageBuilder.class);
    SimpleHttpDistributionTransport simpleHttpDistributionTransport = new SimpleHttpDistributionTransport(mock(DefaultDistributionLog.class), endpoint, packageBuilder, secretProvider, new HttpConfiguration(1000, 1000));
    ResourceResolver resourceResolver = mock(ResourceResolver.class);
    DistributionPackage distributionPackage = mock(DistributionPackage.class);
    when(distributionPackage.getInfo()).thenReturn(new DistributionPackageInfo("type"));
    InputStream stream = mock(InputStream.class);
    when(distributionPackage.createInputStream()).thenReturn(stream);
    DistributionTransportContext distributionContext = mock(DistributionTransportContext.class);
    when(distributionContext.get(any(String.class), same(Executor.class))).thenReturn(executor);
    when(distributionContext.containsKey(any(String.class))).thenReturn(true);
    simpleHttpDistributionTransport.deliverPackage(resourceResolver, distributionPackage, distributionContext);
}
Also used : DistributionPackageInfo(org.apache.sling.distribution.packaging.DistributionPackageInfo) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Request(org.apache.http.client.fluent.Request) SimpleDistributionRequest(org.apache.sling.distribution.SimpleDistributionRequest) DistributionRequest(org.apache.sling.distribution.DistributionRequest) DistributionTransportSecretProvider(org.apache.sling.distribution.transport.DistributionTransportSecretProvider) DefaultDistributionLog(org.apache.sling.distribution.log.impl.DefaultDistributionLog) URI(java.net.URI) HttpResponse(org.apache.http.HttpResponse) Response(org.apache.http.client.fluent.Response) DistributionPackage(org.apache.sling.distribution.packaging.DistributionPackage) DistributionTransportSecret(org.apache.sling.distribution.transport.DistributionTransportSecret) Executor(org.apache.http.client.fluent.Executor) DistributionPackageBuilder(org.apache.sling.distribution.packaging.DistributionPackageBuilder) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Test(org.junit.Test)

Example 24 with DistributionPackageInfo

use of org.apache.sling.distribution.packaging.DistributionPackageInfo 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)

Example 25 with DistributionPackageInfo

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

the class SimpleDistributionAgentTest method testDistributionWithFailingDistributionStrategy.

@Test
public void testDistributionWithFailingDistributionStrategy() throws Exception {
    String name = "sample-agent";
    DistributionPackageImporter packageImporter = mock(DistributionPackageImporter.class);
    DistributionPackageExporter packageExporter = mock(DistributionPackageExporter.class);
    DistributionRequestAuthorizationStrategy packageExporterStrategy = mock(DistributionRequestAuthorizationStrategy.class);
    DistributionQueueProvider queueProvider = mock(DistributionQueueProvider.class);
    DistributionQueueDispatchingStrategy distributionHandler = mock(DistributionQueueDispatchingStrategy.class);
    Iterable<DistributionQueueItemStatus> states = Collections.singletonList(new DistributionQueueItemStatus(DistributionQueueItemState.ERROR, DistributionQueueDispatchingStrategy.DEFAULT_QUEUE_NAME));
    when(distributionHandler.add(any(DistributionPackage.class), any(DistributionQueueProvider.class))).thenReturn(states);
    DistributionEventFactory distributionEventFactory = mock(DistributionEventFactory.class);
    ResourceResolverFactory resolverFactory = mock(ResourceResolverFactory.class);
    SimpleDistributionAgent agent = new SimpleDistributionAgent(name, false, null, "serviceName", packageImporter, packageExporter, packageExporterStrategy, queueProvider, distributionHandler, null, distributionEventFactory, resolverFactory, mock(SlingRepository.class), mock(DefaultDistributionLog.class), null, null, 0);
    DistributionRequest request = new SimpleDistributionRequest(DistributionRequestType.ADD, "/");
    final DistributionPackage distributionPackage = mock(DistributionPackage.class);
    ResourceResolver resourceResolver = mock(ResourceResolver.class);
    when(distributionPackage.getInfo()).thenReturn(new DistributionPackageInfo("type"));
    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"));
    DistributionResponse response = agent.execute(resourceResolver, request);
    assertNotNull(response);
    assertEquals("[ERROR]", response.getMessage());
    assertEquals(DistributionRequestState.DROPPED, response.getState());
}
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) DistributionResponse(org.apache.sling.distribution.DistributionResponse) Test(org.junit.Test)

Aggregations

DistributionPackageInfo (org.apache.sling.distribution.packaging.DistributionPackageInfo)35 DistributionPackage (org.apache.sling.distribution.packaging.DistributionPackage)25 Test (org.junit.Test)25 HashMap (java.util.HashMap)12 DefaultDistributionLog (org.apache.sling.distribution.log.impl.DefaultDistributionLog)10 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)9 DistributionQueueItemStatus (org.apache.sling.distribution.queue.DistributionQueueItemStatus)9 DistributionQueueProvider (org.apache.sling.distribution.queue.DistributionQueueProvider)9 DistributionRequest (org.apache.sling.distribution.DistributionRequest)8 SimpleDistributionRequest (org.apache.sling.distribution.SimpleDistributionRequest)7 DistributionPackageImporter (org.apache.sling.distribution.packaging.DistributionPackageImporter)7 DistributionQueue (org.apache.sling.distribution.queue.DistributionQueue)7 DistributionQueueItem (org.apache.sling.distribution.queue.DistributionQueueItem)7 InputStream (java.io.InputStream)6 ResourceResolverFactory (org.apache.sling.api.resource.ResourceResolverFactory)6 DistributionException (org.apache.sling.distribution.common.DistributionException)6 DistributionEventFactory (org.apache.sling.distribution.event.impl.DistributionEventFactory)6 DistributionQueueDispatchingStrategy (org.apache.sling.distribution.queue.impl.DistributionQueueDispatchingStrategy)6 SlingRepository (org.apache.sling.jcr.api.SlingRepository)6 DistributionPackageBuilder (org.apache.sling.distribution.packaging.DistributionPackageBuilder)5