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