Search in sources :

Example 36 with DistributionRequest

use of org.apache.sling.distribution.DistributionRequest in project sling by apache.

the class SimpleDistributionPackage method fromIdString.

public static SimpleDistributionPackage fromIdString(String id, String type) {
    if (!id.startsWith(PACKAGE_START)) {
        return null;
    }
    id = id.substring(PACKAGE_START.length());
    String[] parts = id.split(Pattern.quote(DELIM));
    if (parts.length < 1 || parts.length > 2) {
        return null;
    }
    String actionString = parts[0];
    String pathsString = parts.length < 2 ? null : parts[1];
    DistributionRequestType distributionRequestType = DistributionRequestType.fromName(actionString);
    SimpleDistributionPackage distributionPackage = null;
    if (distributionRequestType != null) {
        String[] paths = pathsString == null ? new String[0] : pathsString.split(PATH_DELIM);
        DistributionRequest request = new SimpleDistributionRequest(distributionRequestType, paths);
        distributionPackage = new SimpleDistributionPackage(request, type);
    }
    return distributionPackage;
}
Also used : SimpleDistributionRequest(org.apache.sling.distribution.SimpleDistributionRequest) DistributionRequest(org.apache.sling.distribution.DistributionRequest) SimpleDistributionRequest(org.apache.sling.distribution.SimpleDistributionRequest) DistributionRequestType(org.apache.sling.distribution.DistributionRequestType)

Example 37 with DistributionRequest

use of org.apache.sling.distribution.DistributionRequest in project sling by apache.

the class PersistedJcrEventDistributionTrigger method processEvent.

@Override
protected DistributionRequest processEvent(Event event) throws RepositoryException {
    log.debug("processing event {}", event);
    DistributionRequest distributionRequest = null;
    Session session = getSession();
    if (!session.nodeExists(nuggetsPath)) {
        initializeNuggetsPath(session);
    }
    if (session.hasPermission(nuggetsPath, Session.ACTION_ADD_NODE)) {
        log.debug("persisting event under {}", nuggetsPath);
        Node nuggetsNode = session.getNode(nuggetsPath);
        if (nuggetsNode != null) {
            String nodeName = String.valueOf(System.nanoTime());
            Node createdNode = nuggetsNode.addNode(nodeName, "nt:unstructured");
            if (createdNode != null) {
                String path = createdNode.getPath();
                createdNode.setProperty("identifier", event.getIdentifier());
                createdNode.setProperty("path", event.getPath());
                createdNode.setProperty("date", event.getDate());
                createdNode.setProperty("type", event.getType());
                createdNode.setProperty("userData", event.getUserData());
                createdNode.setProperty("userID", event.getUserID());
                @SuppressWarnings({ "unchecked", "rawtypes" }) Set<Map.Entry> set = event.getInfo().entrySet();
                Collection<String> values = new ArrayList<String>();
                for (@SuppressWarnings("rawtypes") Map.Entry entry : set) {
                    values.add(String.valueOf(entry.getKey()) + ":" + String.valueOf(entry.getValue()));
                }
                createdNode.setProperty("info", values.toArray(new String[values.size()]));
                session.save();
                log.info("event {} persisted at {}", event, path);
                distributionRequest = new SimpleDistributionRequest(DistributionRequestType.ADD, path);
            } else {
                log.warn("could not create node {}", nuggetsPath + "/" + nodeName);
            }
        } else {
            log.warn("could not get node {} to persist event", nuggetsPath);
        }
    } else {
        log.warn("not enough privileges to persist the event {} under {}", event, nuggetsPath);
    }
    return distributionRequest;
}
Also used : SimpleDistributionRequest(org.apache.sling.distribution.SimpleDistributionRequest) DistributionRequest(org.apache.sling.distribution.DistributionRequest) SimpleDistributionRequest(org.apache.sling.distribution.SimpleDistributionRequest) Node(javax.jcr.Node) ArrayList(java.util.ArrayList) Map(java.util.Map) Session(javax.jcr.Session)

Example 38 with DistributionRequest

use of org.apache.sling.distribution.DistributionRequest in project sling by apache.

the class DistributionAgentServlet method doPost.

@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
    response.setContentType("application/json");
    DistributionRequest distributionRequest = RequestUtils.fromServletRequest(request);
    log.debug("distribution request : {}", distributionRequest);
    DistributionAgent agent = request.getResource().adaptTo(DistributionAgent.class);
    ResourceResolver resourceResolver = request.getResourceResolver();
    if (agent != null) {
        try {
            DistributionResponse distributionResponse = agent.execute(resourceResolver, distributionRequest);
            ServletJsonUtils.writeJson(response, distributionResponse);
            log.debug("distribution response : {}", distributionResponse);
        } catch (Throwable e) {
            log.error("an unexpected error has occurred", e);
            ServletJsonUtils.writeJson(response, 503, "an unexpected error has occurred", null);
        }
    } else {
        ServletJsonUtils.writeJson(response, 404, "agent not found", null);
    }
}
Also used : DistributionRequest(org.apache.sling.distribution.DistributionRequest) DistributionAgent(org.apache.sling.distribution.agent.DistributionAgent) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) DistributionResponse(org.apache.sling.distribution.DistributionResponse)

Example 39 with DistributionRequest

use of org.apache.sling.distribution.DistributionRequest in project sling by apache.

the class DistributionTriggerServlet method doGet.

@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
    String secondsParameter = request.getParameter("sec");
    int seconds = secondsParameter != null && secondsParameter.length() > 0 ? Integer.parseInt(secondsParameter) : DEFAULT_NUMBER_OF_SECONDS;
    if (seconds > MAX_NUMBER_OF_SECONDS) {
        seconds = MAX_NUMBER_OF_SECONDS;
    } else if (seconds < 0) {
        seconds = DEFAULT_NUMBER_OF_SECONDS;
    }
    DistributionTrigger distributionTrigger = request.getResource().adaptTo(DistributionTrigger.class);
    // setup SSE headers
    response.setContentType("text/event-stream");
    response.setCharacterEncoding("UTF-8");
    response.setHeader("Cache-Control", "no-cache");
    response.setHeader("Connection", "keep-alive");
    // needed to allow e.g. the JavaScript EventSource API to make a call from author to this server and listen for the events
    //        response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin")); // allowed origins should be explicitly configured
    //        response.setHeader("Access-Control-Allow-Credentials", "true");
    final PrintWriter writer = response.getWriter();
    DistributionRequestHandler distributionRequestHandler = new DistributionRequestHandler() {

        public void handle(@Nullable ResourceResolver resourceResolver, @Nonnull DistributionRequest request) {
            writeEvent(writer, request);
        }
    };
    try {
        distributionTrigger.register(distributionRequestHandler);
        try {
            Thread.sleep(seconds * 1000);
        } catch (InterruptedException e) {
            log.error("thread interrupted", e);
        }
        distributionTrigger.unregister(distributionRequestHandler);
    } catch (DistributionException e) {
        response.setStatus(400);
        response.getWriter().write("error while (un)registering trigger " + e.toString());
    }
}
Also used : DistributionRequestHandler(org.apache.sling.distribution.trigger.DistributionRequestHandler) DistributionRequest(org.apache.sling.distribution.DistributionRequest) Nonnull(javax.annotation.Nonnull) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) DistributionTrigger(org.apache.sling.distribution.trigger.DistributionTrigger) DistributionException(org.apache.sling.distribution.common.DistributionException) Nullable(javax.annotation.Nullable) PrintWriter(java.io.PrintWriter)

Example 40 with DistributionRequest

use of org.apache.sling.distribution.DistributionRequest in project sling by apache.

the class KryoContentSerializerTest method testBuildAndInstallOnSingleShallowPath.

@Test
public void testBuildAndInstallOnSingleShallowPath() 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, "/libs/sub");
    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"));
}
Also used : FileDistributionPackageBuilder(org.apache.sling.distribution.packaging.impl.FileDistributionPackageBuilder) SimpleDistributionRequest(org.apache.sling.distribution.SimpleDistributionRequest) DistributionRequest(org.apache.sling.distribution.DistributionRequest) SimpleDistributionRequest(org.apache.sling.distribution.SimpleDistributionRequest) DistributionPackage(org.apache.sling.distribution.packaging.DistributionPackage) FileDistributionPackageBuilder(org.apache.sling.distribution.packaging.impl.FileDistributionPackageBuilder) DistributionPackageBuilder(org.apache.sling.distribution.packaging.DistributionPackageBuilder) Resource(org.apache.sling.api.resource.Resource) DistributionContentSerializer(org.apache.sling.distribution.serialization.DistributionContentSerializer) Test(org.junit.Test)

Aggregations

DistributionRequest (org.apache.sling.distribution.DistributionRequest)52 Test (org.junit.Test)42 SimpleDistributionRequest (org.apache.sling.distribution.SimpleDistributionRequest)31 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)22 DistributionPackage (org.apache.sling.distribution.packaging.DistributionPackage)16 SlingRepository (org.apache.sling.jcr.api.SlingRepository)14 ResourceResolverFactory (org.apache.sling.api.resource.ResourceResolverFactory)13 Session (javax.jcr.Session)11 List (java.util.List)10 DefaultDistributionLog (org.apache.sling.distribution.log.impl.DefaultDistributionLog)10 DistributionPackageBuilder (org.apache.sling.distribution.packaging.DistributionPackageBuilder)10 Event (javax.jcr.observation.Event)9 Scheduler (org.apache.sling.commons.scheduler.Scheduler)9 DistributionPackageProcessor (org.apache.sling.distribution.packaging.DistributionPackageProcessor)9 DistributionResponse (org.apache.sling.distribution.DistributionResponse)7 TreeMap (java.util.TreeMap)6 Resource (org.apache.sling.api.resource.Resource)6 DistributionPackageExporter (org.apache.sling.distribution.packaging.DistributionPackageExporter)6 DistributionPackageInfo (org.apache.sling.distribution.packaging.DistributionPackageInfo)6 FileDistributionPackageBuilder (org.apache.sling.distribution.packaging.impl.FileDistributionPackageBuilder)6