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());
}
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());
}
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);
}
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);
}
}
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);
}
Aggregations