use of org.apache.sling.distribution.packaging.DistributionPackage in project sling by apache.
the class SimpleHttpDistributionTransportTest method testRetrievePackagesRemotelyWorking.
@Test
public void testRetrievePackagesRemotelyWorking() 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);
HttpResponse httpResponse = mock(HttpResponse.class);
StatusLine statusLine = mock(StatusLine.class);
when(statusLine.getStatusCode()).thenReturn(200);
when(httpResponse.getStatusLine()).thenReturn(statusLine);
HttpEntity entity = mock(HttpEntity.class);
InputStream stream = new ByteArrayInputStream("package binary stuff".getBytes("UTF-8"));
when(entity.getContent()).thenReturn(stream);
when(httpResponse.getEntity()).thenReturn(entity);
when(response.returnResponse()).thenReturn(httpResponse);
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);
DistributionPackage distributionPackage = mock(DistributionPackage.class);
when(distributionPackage.getInfo()).thenReturn(new DistributionPackageInfo("type"));
when(packageBuilder.readPackage(any(ResourceResolver.class), any(InputStream.class))).thenReturn(distributionPackage);
SimpleHttpDistributionTransport simpleHttpDistributionTransport = new SimpleHttpDistributionTransport(mock(DefaultDistributionLog.class), endpoint, packageBuilder, secretProvider, new HttpConfiguration(1000, 1000));
ResourceResolver resourceResolver = mock(ResourceResolver.class);
DistributionRequest distributionRequest = new SimpleDistributionRequest(DistributionRequestType.ADD, "/");
DistributionTransportContext distributionContext = mock(DistributionTransportContext.class);
when(distributionContext.get(any(String.class), same(Executor.class))).thenReturn(executor);
when(distributionContext.containsKey(any(String.class))).thenReturn(true);
RemoteDistributionPackage retrievedPackage = simpleHttpDistributionTransport.retrievePackage(resourceResolver, distributionRequest, distributionContext);
assertNotNull(retrievedPackage);
}
use of org.apache.sling.distribution.packaging.DistributionPackage in project sling by apache.
the class KryoContentSerializerTest method testBuildAndInstallOnSingleDeepPath.
@Test
public void testBuildAndInstallOnSingleDeepPath() throws Exception {
String type = "kryo";
DistributionContentSerializer contentSerializer = new KryoContentSerializer(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 SimpleDistributionAgentQueueProcessor method processQueueItem.
private boolean processQueueItem(String queueName, DistributionQueueEntry queueEntry) throws DistributionException {
boolean removeItemFromQueue = false;
ResourceResolver agentResourceResolver = null;
DistributionPackage distributionPackage = null;
DistributionQueueItem queueItem = queueEntry.getItem();
DistributionQueueItemStatus queueItemStatus = queueEntry.getStatus();
try {
String callingUser = queueItem.get(DistributionPackageUtils.PACKAGE_INFO_PROPERTY_REQUEST_USER, String.class);
String requestId = queueItem.get(DistributionPackageUtils.PACKAGE_INFO_PROPERTY_REQUEST_ID, String.class);
Long globalStartTime = queueItem.get(DistributionPackageUtils.PACKAGE_INFO_PROPERTY_REQUEST_START_TIME, Long.class);
agentResourceResolver = DistributionUtils.getResourceResolver(callingUser, authenticationInfo.getAgentService(), authenticationInfo.getSlingRepository(), authenticationInfo.getSubServiceName(), authenticationInfo.getResourceResolverFactory());
final long startTime = System.currentTimeMillis();
distributionPackage = distributionPackageExporter.getPackage(agentResourceResolver, queueItem.getPackageId());
if (distributionPackage != null) {
final long packageSize = distributionPackage.getSize();
DistributionPackageUtils.mergeQueueEntry(distributionPackage.getInfo(), queueEntry);
final DistributionRequestType requestType = distributionPackage.getInfo().getRequestType();
final String[] paths = distributionPackage.getInfo().getPaths();
try {
// import package
distributionPackageImporter.importPackage(agentResourceResolver, distributionPackage);
// generated event
distributionEventFactory.generatePackageEvent(DistributionEventTopics.AGENT_PACKAGE_DISTRIBUTED, DistributionComponentKind.AGENT, agentName, distributionPackage.getInfo());
removeItemFromQueue = true;
final long endTime = System.currentTimeMillis();
distributionLog.info("[{}] PACKAGE-DELIVERED {}: {} paths={}, importTime={}ms, execTime={}ms, size={}B", queueName, requestId, requestType, paths, endTime - startTime, endTime - globalStartTime, packageSize);
} catch (RecoverableDistributionException e) {
distributionLog.error("[{}] PACKAGE-FAIL {}: could not deliver {}, {}", queueName, requestId, distributionPackage.getId(), e.getMessage());
distributionLog.debug("could not deliver package {}", distributionPackage.getId(), e);
log.error("could not deliver package {}", distributionPackage.getId(), e);
} catch (Throwable e) {
distributionLog.error("[{}] PACKAGE-FAIL {}: could not deliver package {} {}", queueName, requestId, distributionPackage.getId(), e.getMessage(), e);
log.error("could not deliver package {} from queue {}", new Object[] { distributionPackage.getId(), queueName }, e);
if (errorQueueStrategy != null && queueItemStatus.getAttempts() > retryAttempts) {
removeItemFromQueue = reEnqueuePackage(distributionPackage);
distributionLog.info("[{}] PACKAGE-QUEUED {}: distribution package {} was enqueued to an error queue", queueName, requestId, distributionPackage.getId());
}
}
} else {
// return success if package does not exist in order to clear the queue.
removeItemFromQueue = true;
distributionLog.error("distribution package with id {} does not exist. the package will be skipped.", queueItem.getPackageId());
}
} finally {
if (removeItemFromQueue) {
DistributionPackageUtils.releaseOrDelete(distributionPackage, queueName);
} else {
DistributionPackageUtils.closeSafely(distributionPackage);
}
DistributionUtils.ungetResourceResolver(agentResourceResolver);
}
// return true if item should be removed from queue
return removeItemFromQueue;
}
use of org.apache.sling.distribution.packaging.DistributionPackage in project sling by apache.
the class FileDistributionPackageBuilder method readPackageInternal.
@Override
protected DistributionPackage readPackageInternal(@Nonnull ResourceResolver resourceResolver, @Nonnull InputStream stream) throws DistributionException {
DistributionPackage distributionPackage;
final File file;
DigestOutputStream outputStream = null;
try {
String name;
// stable id
Map<String, Object> info = new HashMap<String, Object>();
DistributionPackageUtils.readInfo(stream, info);
Object remoteId = info.get(DistributionPackageUtils.PROPERTY_REMOTE_PACKAGE_ID);
if (remoteId != null) {
name = remoteId.toString();
log.debug("preserving remote id {}", name);
} else {
name = "distrpck-read-" + System.nanoTime();
log.debug("generating a new id {}", name);
}
file = File.createTempFile(name, "." + getType(), tempDirectory);
outputStream = openDigestOutputStream(new FileOutputStream(file), digestAlgorithm);
IOUtils.copy(stream, outputStream);
outputStream.flush();
String digestMessage = readDigestMessage(outputStream);
distributionPackage = new FileDistributionPackage(file, getType(), digestAlgorithm, digestMessage);
} catch (Exception e) {
throw new DistributionException(e);
} finally {
IOUtils.closeQuietly(outputStream);
}
return distributionPackage;
}
use of org.apache.sling.distribution.packaging.DistributionPackage in project sling by apache.
the class ResourceDistributionPackageBuilder method createPackageForAdd.
@Override
protected DistributionPackage createPackageForAdd(@Nonnull ResourceResolver resourceResolver, @Nonnull DistributionRequest request) throws DistributionException {
DistributionPackage distributionPackage;
try {
FileBackedMemoryOutputStream outputStream = null;
DigestOutputStream digestStream = null;
String digestMessage = null;
try {
outputStream = new FileBackedMemoryOutputStream(fileThreshold, memoryUnit, useOffHeapMemory, tempDirectory, "distrpck-create-", "." + getType());
if (digestAlgorithm != null) {
digestStream = openDigestOutputStream(outputStream, digestAlgorithm);
export(resourceResolver, request, digestStream);
} else {
export(resourceResolver, request, outputStream);
}
outputStream.flush();
if (digestAlgorithm != null) {
digestMessage = readDigestMessage(digestStream);
}
} finally {
IOUtils.closeQuietly(outputStream);
IOUtils.closeQuietly(digestStream);
}
Resource packagesRoot = DistributionPackageUtils.getPackagesRoot(resourceResolver, packagesPath);
InputStream inputStream = null;
Resource packageResource = null;
try {
inputStream = outputStream.openWrittenDataInputStream();
packageResource = uploadStream(resourceResolver, packagesRoot, inputStream, outputStream.size());
} finally {
IOUtils.closeQuietly(inputStream);
outputStream.clean();
}
distributionPackage = new ResourceDistributionPackage(packageResource, getType(), resourceResolver, digestAlgorithm, digestMessage);
} catch (IOException e) {
throw new DistributionException(e);
}
return distributionPackage;
}
Aggregations