use of org.apache.sling.distribution.packaging.DistributionPackageProcessor in project sling by apache.
the class SimpleDistributionAgent method exportPackages.
private CompositeDistributionResponse exportPackages(ResourceResolver agentResourceResolver, DistributionRequest distributionRequest, String callingUser, String requestId) throws DistributionException {
final long startTime = System.currentTimeMillis();
// callback function
DistributionPackageProcessor packageProcessor;
if (DistributionRequestType.TEST.equals(distributionRequest.getRequestType())) {
// test packages do not get passed to the queues and get imported immediately
packageProcessor = new ImportingDistributionPackageProcessor(distributionPackageImporter, agentAuthenticationInfo, callingUser, requestId, log);
} else {
packageProcessor = new QueueingDistributionPackageProcessor(callingUser, requestId, startTime, distributionEventFactory, scheduleQueueStrategy, queueProvider, log, name);
}
// export packages
distributionPackageExporter.exportPackages(agentResourceResolver, distributionRequest, packageProcessor);
final long endTime = System.currentTimeMillis();
generatePackageEvent(DistributionEventTopics.AGENT_PACKAGE_CREATED);
// create response
List<DistributionResponse> distributionResponses = packageProcessor.getAllResponses();
int packagesCount = packageProcessor.getPackagesCount();
long packagesSize = packageProcessor.getPackagesSize();
return new CompositeDistributionResponse(distributionResponses, packagesCount, packagesSize, endTime - startTime);
}
use of org.apache.sling.distribution.packaging.DistributionPackageProcessor in project sling by apache.
the class DistributionPackageExporterServlet method exportOnePackage.
private void exportOnePackage(final SlingHttpServletRequest request, final SlingHttpServletResponse response, final boolean delete) throws ServletException, IOException {
DistributionPackageExporter distributionPackageExporter = request.getResource().adaptTo(DistributionPackageExporter.class);
final long start = System.currentTimeMillis();
response.setContentType(ContentType.APPLICATION_OCTET_STREAM.toString());
DistributionRequest distributionRequest = RequestUtils.fromServletRequest(request);
ResourceResolver resourceResolver = request.getResourceResolver();
final AtomicInteger fetched = new AtomicInteger(0);
try {
// get all items
distributionPackageExporter.exportPackages(resourceResolver, distributionRequest, new DistributionPackageProcessor() {
@Override
public void process(DistributionPackage distributionPackage) {
fetched.incrementAndGet();
InputStream inputStream = null;
int bytesCopied = -1;
try {
inputStream = DistributionPackageUtils.createStreamWithHeader(distributionPackage);
bytesCopied = IOUtils.copy(inputStream, response.getOutputStream());
} catch (IOException e) {
log.error("cannot process package", e);
} finally {
IOUtils.closeQuietly(inputStream);
}
String packageId = distributionPackage.getId();
if (delete) {
// delete the package permanently
distributionPackage.delete();
}
// everything ok
response.setStatus(200);
log.debug("exported package {} was sent (and deleted={}), bytes written {}", new Object[] { packageId, delete, bytesCopied });
}
@Override
public List<DistributionResponse> getAllResponses() {
return null;
}
@Override
public int getPackagesCount() {
return 0;
}
@Override
public long getPackagesSize() {
return 0;
}
});
if (fetched.get() > 0) {
long end = System.currentTimeMillis();
log.info("Processed distribution export request in {} ms: : fetched {}", new Object[] { end - start, fetched });
} else {
response.setStatus(204);
log.debug("nothing to fetch");
}
} catch (Throwable e) {
response.setStatus(503);
log.error("error while exporting package", e);
}
}
use of org.apache.sling.distribution.packaging.DistributionPackageProcessor in project sling by apache.
the class SimpleDistributionAgentTest method testDistributionWithDisallowedRoot.
@Test
public void testDistributionWithDisallowedRoot() 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 queueDistributionStrategy = mock(DistributionQueueDispatchingStrategy.class);
DistributionEventFactory distributionEventFactory = mock(DistributionEventFactory.class);
ResourceResolverFactory resolverFactory = mock(ResourceResolverFactory.class);
when(queueDistributionStrategy.add(any(DistributionPackage.class), any(DistributionQueueProvider.class))).thenReturn(Collections.singletonList(new DistributionQueueItemStatus(DistributionQueueItemState.QUEUED, "default")));
SimpleDistributionAgent agent = new SimpleDistributionAgent(name, false, null, "serviceName", packageImporter, packageExporter, packageExporterStrategy, queueProvider, queueDistributionStrategy, null, distributionEventFactory, resolverFactory, mock(SlingRepository.class), mock(DefaultDistributionLog.class), null, new String[] { "/content" }, 0);
DistributionRequest request = new SimpleDistributionRequest(DistributionRequestType.ADD, "/home");
final DistributionPackage distributionPackage = mock(DistributionPackage.class);
DistributionPackageInfo packageInfo = new DistributionPackageInfo("type");
when(distributionPackage.getInfo()).thenReturn(packageInfo);
ResourceResolver resourceResolver = mock(ResourceResolver.class);
queueDistributionStrategy.add(distributionPackage, queueProvider);
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);
assertFalse(response.isSuccessful());
}
use of org.apache.sling.distribution.packaging.DistributionPackageProcessor in project sling by apache.
the class SimpleDistributionAgentTest method testDistributionWithAllowedRoot.
@Test
public void testDistributionWithAllowedRoot() 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 queueDistributionStrategy = mock(DistributionQueueDispatchingStrategy.class);
DistributionEventFactory distributionEventFactory = mock(DistributionEventFactory.class);
ResourceResolverFactory resolverFactory = mock(ResourceResolverFactory.class);
when(queueDistributionStrategy.add(any(DistributionPackage.class), any(DistributionQueueProvider.class))).thenReturn(Collections.singletonList(new DistributionQueueItemStatus(DistributionQueueItemState.QUEUED, "default")));
SimpleDistributionAgent agent = new SimpleDistributionAgent(name, false, null, "serviceName", packageImporter, packageExporter, packageExporterStrategy, queueProvider, queueDistributionStrategy, null, distributionEventFactory, resolverFactory, mock(SlingRepository.class), mock(DefaultDistributionLog.class), null, new String[] { "/content" }, 0);
DistributionRequest request = new SimpleDistributionRequest(DistributionRequestType.ADD, "/content");
final DistributionPackage distributionPackage = mock(DistributionPackage.class);
DistributionPackageInfo packageInfo = new DistributionPackageInfo("type");
when(distributionPackage.getInfo()).thenReturn(packageInfo);
ResourceResolver resourceResolver = mock(ResourceResolver.class);
queueDistributionStrategy.add(distributionPackage, queueProvider);
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);
assertTrue(response.isSuccessful());
}
use of org.apache.sling.distribution.packaging.DistributionPackageProcessor in project sling by apache.
the class AgentDistributionPackageExporterTest method testTestExport.
@Test
public void testTestExport() throws Exception {
AgentDistributionPackageExporter distributionPackageExporter = new AgentDistributionPackageExporter(null, mock(DistributionAgent.class), mock(DistributionPackageBuilderProvider.class), null);
ResourceResolver resourceResolver = mock(ResourceResolver.class);
// vargarg doesn't match and causes compiler warning
String[] args = new String[0];
DistributionRequest distributionRequest = new SimpleDistributionRequest(DistributionRequestType.TEST, args);
final List<DistributionPackage> distributionPackages = new ArrayList<DistributionPackage>();
distributionPackageExporter.exportPackages(resourceResolver, distributionRequest, new DistributionPackageProcessor() {
@Override
public void process(DistributionPackage distributionPackage) {
distributionPackages.add(distributionPackage);
}
@Override
public List<DistributionResponse> getAllResponses() {
return null;
}
@Override
public int getPackagesCount() {
return 0;
}
@Override
public long getPackagesSize() {
return 0;
}
});
assertNotNull(distributionPackages);
assertEquals(1, distributionPackages.size());
}
Aggregations