use of org.apache.sling.distribution.packaging.DistributionPackage 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.DistributionPackage in project sling by apache.
the class DistributionPackageExporterServlet method deletePackage.
private void deletePackage(final SlingHttpServletRequest request, final SlingHttpServletResponse response) throws DistributionException {
DistributionPackageExporter distributionPackageExporter = request.getResource().adaptTo(DistributionPackageExporter.class);
ResourceResolver resourceResolver = request.getResourceResolver();
String id = request.getParameter("id");
DistributionPackage distributionPackage = distributionPackageExporter.getPackage(resourceResolver, id);
if (distributionPackage != null) {
distributionPackage.delete();
log.debug("exported package {} was deleted", distributionPackage.getId());
response.setStatus(200);
} else {
response.setStatus(204);
log.debug("nothing to delete {}", id);
}
}
use of org.apache.sling.distribution.packaging.DistributionPackage in project sling by apache.
the class DistributionAgentQueueServlet method addItems.
private void addItems(ResourceResolver resourceResolver, DistributionQueue targetQueue, DistributionQueue sourceQueue, String[] ids) {
for (String id : ids) {
DistributionQueueEntry entry = sourceQueue.getItem(id);
if (entry != null) {
targetQueue.add(entry.getItem());
DistributionPackage distributionPackage = getPackage(resourceResolver, entry.getItem());
DistributionPackageUtils.acquire(distributionPackage, targetQueue.getName());
}
}
}
use of org.apache.sling.distribution.packaging.DistributionPackage in project sling by apache.
the class AvroContentSerializerTest method testBuildAndInstallOnSingleDeepPath.
@Test
public void testBuildAndInstallOnSingleDeepPath() throws Exception {
String type = "avro";
DistributionContentSerializer contentSerializer = new AvroContentSerializer(type);
String tempFilesFolder = "target";
String[] nodeFilters = new String[0];
String[] propertyFilters = new String[0];
DistributionPackageBuilder packageBuilder = new FileDistributionPackageBuilder(type, contentSerializer, tempFilesFolder, null, nodeFilters, propertyFilters);
DistributionRequest request = new SimpleDistributionRequest(DistributionRequestType.ADD, true, "/libs");
DistributionPackage distributionPackage = packageBuilder.createPackage(resourceResolver, request);
Resource resource = resourceResolver.getResource("/libs/sub");
resourceResolver.delete(resource);
resourceResolver.commit();
assertTrue(packageBuilder.installPackage(resourceResolver, distributionPackage));
assertNotNull(resourceResolver.getResource("/libs"));
assertNotNull(resourceResolver.getResource("/libs/sub"));
assertNotNull(resourceResolver.getResource("/libs/sameLevel"));
}
use of org.apache.sling.distribution.packaging.DistributionPackage in project sling by apache.
the class QueueingDistributionPackageProcessorTest method testProcess.
@Test
public void testProcess() throws Exception {
String callingUser = "mr-who-cares";
String requestId = "id231";
long startTime = System.currentTimeMillis();
DistributionEventFactory eventFactory = mock(DistributionEventFactory.class);
DistributionQueueProvider queueProvider = mock(DistributionQueueProvider.class);
DistributionPackage distributionPackage = mock(DistributionPackage.class);
DistributionQueueDispatchingStrategy scheduleQueueStrategy = mock(DistributionQueueDispatchingStrategy.class);
// assume scheduling works
List<DistributionQueueItemStatus> statuses = new LinkedList<DistributionQueueItemStatus>();
DistributionQueueItemStatus qis = new DistributionQueueItemStatus(DistributionQueueItemState.QUEUED, "queue-1");
statuses.add(qis);
when(scheduleQueueStrategy.add(distributionPackage, queueProvider)).thenReturn(statuses);
DefaultDistributionLog log = mock(DefaultDistributionLog.class);
String agentName = "dummy";
QueueingDistributionPackageProcessor exporterProcessor = new QueueingDistributionPackageProcessor(callingUser, requestId, startTime, eventFactory, scheduleQueueStrategy, queueProvider, log, agentName);
DistributionPackageInfo info = new DistributionPackageInfo("type-a", new HashMap<String, Object>());
when(distributionPackage.getInfo()).thenReturn(info);
exporterProcessor.process(distributionPackage);
}
Aggregations