Latest Posts

Archives [+]

Entries filed under 'sling'

    Posted by Gabriel Walt JUL 11, 2011

    Posted in conferences, cq5 and sling Comment 1

    A great tech meetup, sweetly named .adaptTo(Berlin) is being organized by Dominik Süß (pro!vision GmbH) on September 15th and 16th in the Betahaus, Berlin. Adobe is the main sponsor of this event, along with pro!vision GmbH who will act as host.

    The topic is the Apache Sling technical stack. It is adressed to all developers working with this open source stack, with the second day mainly focused on CQ5 and Sling-based solutions from other vendors.

    The agenda is still to be announced, but there will be several of our top Sling & CQ5 engineers talking. So not only will you certainly learn a lot, but this is also the opportunity for you to actively participate to the future of the Sling-stack if you wish.

    Register Now


    .adaptTo(Berlin)
     
    is planned as an interactive meetup with talks from the community. You too, contribute your knowledge to the community and talk during one of the 45 or 90 minute slots! Adobe developers will give some talks, but we would be pleased to have various speakers and encourage you to present your solutions.

    Speakers should submit their proposals as soon as possible, since the organizers want to publish a draft agenda at mid-August. Please post your topic to the wiki page till August 5th and send Dominik the slides, or a description of your talk till August 12th.

    Posted by Michael Marth DEC 17, 2010

    Posted in cq5, crx, java content repository, ria, sling and tutorials Comments 4

    Flex developers interested in working with CRX or CQ5 might want to have a look at a package I have recently uploaded to the public section of the Package Share. It is called "Flexling" and contains:

    • an Actionscript library for read and write access to CRX (as well as search)
    • a help file and API docs for the library
    • an example Flex application

    Preview of the package:

    file

    After installation of the package point your browser to localhost:(port)/apps/flexling/docs/flexling.html where the introductory document is located.

    file

    How it works

    The library basically wraps the Sling http API into an Actionscript object com.day.sling.Sling. There are three methods for reading and searching content: getContent (synchronous), loadContent (asynchronous) and search (asynchronous). Under the hood these leverage the json rendering of JCR content that Sling provides. In a similar fashion the methods saveContent and removeContent wrap the SlingPostServlet's functionality for writing to the repository (both are asynchronous).  All asynchronous methods dispatch com.day.sling.SlingEvent events when done (inherited from flash.events.Event).

    The method getContent exists basically for educational purposes - loadContent would be sufficient to read from the repository. getContent shows how to leverage the browser's XHR object for reading from the repository (using flash.external.ExternalInterface) while loadContent uses the flash.net.URLLoader.

    Alternatives

    Through flash.external.ExternalInterface there is actually a third possibility to interface the repository (though not implemented in this package): Sling provides a Javascript file located at /system/sling.js (Apache svn http://svn.apache.org/repos/asf/sling/trunk/bundles/servlets/post/src/main/resources/system/sling.js). You could use e.g. Sling.httpGet to retrieve content.

    This is exactly the approach taken by the Flex-based CQ5 standard slideshow component (see screenshot below). It ships with the complete Flex source code in the repository at /libs/foundation/components/slideshow/src.

    the slideshow component in the goemetrixx demo site

    Kudos to Josh Oransky who collaborated with me on the Flexling package.

    Posted by Federico Paparoni SEP 28, 2010

    Posted in graph and sling Add comment

    This post is cross-posted here.

    Yesterday I was looking an interesting javascript library, JIT (JavaScript InfoVis Toolkit), that is really useful to create some cool representation of your data with graphs, trees, charts and so on.

    This toolkit creates different data representation using JSON as input, so I thought it could be funny to integrate with Apache Sling to have a new representation of my repository.

    Unfortunately JSON translation in Apache Sling is a bit different from the one used in JIT, because it needs children node represented as an array (not in every demo contained in JIT but in the one I want so integrate with Sling).

    So I modified org.apache.sling.commons.json.jcr.JsonItemWriter, to encapsulate the nodes in a JSON array

    Iterator children = resource.listChildren();
    if (children.hasNext()) {
    w.key("children");
    w.array();
    while (children.hasNext()){
    Resource n = children.next();
    log.info("Giro "+n.getName());
    dump(n);
    }
    w.endArray();
    }


    Now I have to load some data on my instance of Apache Sling, so I create a simple JSON.

    {
    "id": "node01",
    "name": "0.2",
    "data":"",
    "jcr:primaryType":"sling:Folder",
    "node11": {
    "id": "node13",
    "name": "1.3",
    "data": "",
    "jcr:primaryType":"sling:Folder",
    "node111":{
    ...
    ...


    Finally I have to load this data into JIT, so I modified an example to load JSON data from an external source
    So I got a very cool representation of my data stored with JCR

    file

    Posted by Jean-Christophe Kautzmann SEP 13, 2010

    Posted in java content repository, jcr, jsr-283, sling and tutorial Comment 1

    When you develop an application on top of a JCR repository, you eventually need to access the following basic objects: the repository, a workspace, a session, a node, a property and some managers to perform operations like versioning, querying or controlling the access to the repository.
    I've put together here some code samples to let you do that.

    Getting the repository

    One way to access the repository is to use the JcrUtils.getRepository(Map) utility method of the jackrabbit-jcr-commons library:

    Map parameters = new HashMap();
    parameters.put(..., ...);
    Repository repository = JcrUtils.getRepository(parameters);

    Other ways to access the repository are described here.

    If you are using Sling, you can reference an existing service, called SlingRepository, by using the @scr.reference annotation as follows in your class:

    /** @scr.reference */
    private SlingRepository repository;


    Getting a session and a workspace

    Once you have accessed the repository, you can define a session:

    SimpleCredentials credentials = new SimpleCredentials(userID, password);
    Session session = repository.login(credentials);

    You can also define an administrative session by using the loginAdministrative() method of the SlingRepository interface:

    Session adminSession = repository.loginAdministrative(null);

    When using Sling, to get the user session within a servlet, you can use the following code (e.g. within the doGet() method):

    Session session = req.getResourceResolver().adaptTo(Session.class);
    // req is the SlingHttpServletRequest object passed to the servlet

    Note: all opened sessions should be properly closed when they are no longer needed. The recommended pattern is:

    Session session = repository.login(...);
    try {
      // use the session
    } finally {
      session.logout();
    }

    The only time you don't need this is when you're adapting the SlingHttpServletRequest instance to a Session, as Sling will automatically close that session once the request has been fully processed.

    To get a workspace:

    Workspace workspace = session.getWorkspace();        


    Getting a node, a property and a value

    To get a node:

    Node node = session.getNode(pathToNode);

    To get a property:

    Property property = session.getProperty(pathToProperty);

    To get the value of a single value property:

    if (!property.isMultiple()) {
        Value value = property.getValue();
        String myStringValue = value.getString();
    }

    To get the values of a multiple value property:

    if (property.isMultiple()) {
        Value[] values = property.getValues();
        for (int i = 0; i < values.length; i++) {
            Value value = values[i];
            // assuming the values are strings
            String myStringValue = value.getString();
        }
    }

    Getting managers

    To get managers for the features introduced by the JCR 2.0 specifications like locking, node type management, observation, querying, versioning, access control or retention:

    LockManager lockManager = workspace.getLockManager();
    NodeTypeManager nodeTypeManager = workspace.getNodeTypeManager();
    ObservationManager observationManager = workspace.getObservationManager();
    QueryManager queryManager = workspace.getQueryManager();
    VersionManager versionManager = workspace.getVersionManager();
    AccessControlManager accessControlManager = session.getAccessControlManager();
    RetentionManager retentionManager = session.getRetentionManager();

    For more details please refer to the JCR 2.0 API javadocs or to the JSR-283 specifications that define the JCR API.

    Posted by Kas Thomas SEP 08, 2010

    Posted in cq, crx, crx gems and sling Add comment

    Being able to modularize pages (build them up from individual pieces) using some sort of "include" mechanism is fundamental to web design. That's no less true in the Sling world than in any other environment. As it turns out, there are several techniques you can use for achieving componentized pages in Sling.

    In Java Server Pages, one of the handiest techniques for pulling together disparate page elements is the @include directive:

    <%@ include file="myScript.jsp" %>

    This line of code will tell the JSP compiler to include a complete file (namely, myScript.jsp) into the current file at compilation time, just as if the contents of the included file were pasted directly into the original file.

    In the Day Communiqué runtime environment, it's also possible to achieve this using a syntax of:

    <cq:include script="myScript.jsp" />

    The difference in this case is that the included file gets pulled in at runtime, rather than at servlet compile time.

    In the Sling environment (which of course also means the CRX environment), there's yet another possible technique for achieving inclusion, and that's one in which you do something like this:

    <% sling.include("/content/mypath/mynode") %>

    This line of code relies on the sling convenience object, which has an include() method. What's happening here is that you're essentially telling Sling to render the node "mynode" using whatever script Sling can find for that node based on the node's sling:resourceType. The rendered version of the node's content is what gets inserted into the original file.

    Let's take an example. Suppose we have a node at /content/test/mycontent which has a "content" property containing the string "This is some content." Furthermore, suppose there's a node at /content/test/myhead that has a "content" property containing the string value "This is a headline." Let's assume that both of these nodes have a sling:resourceType of "test," which means Sling will look under /apps/test/ to find any applicable scripts for rendering the nodes.

    Let's say we have a script designed to render content as an H1 head. We'll call it /apps/test/h1.jsp:

    <%@taglib prefix="sling" uri="http://sling.apache.org/taglibs/sling/1.0" %>
    <sling:defineObjects/>
    <h1>
    <%= currentNode.getProperty("content").getString() %>
    </h1>

    Suppose we have CRX running on port 7402. If we were now to aim our browser at http://localhost:7402/content/test/myhead.h1, the following content would be streamed out to the browser:

    <h1>This is a headline</h1>

    But suppose we want to include that line into an HTML file that also includes the (plain text) line "This is some content." We could do it with a JSP page that looks like this:

    <%@taglib prefix="sling" uri="http://sling.apache.org/taglibs/sling/1.0" %>
    <sling:defineObjects/>
    <html>
    <%
           sling.include("/content/test/myhead.h1");
    %>
    <%= currentNode.getProperty("content").getString() %>
    </html>

    All we have to do is give this script a name like html.jsp and put it under /apps/test/, then point the browser to http://localhost:7402/content/test/mycontent.html. The ".html" on the end of mycontent tells Sling to use html.jsp to render the page. The fully rendered page contains markup of:

    <html>
    <h1>This is a headline</h1>
    This is some content.
    </html>

    The sling.include() technique is very powerful. It not only lets you include programmatically rendered content into a file at runtime, but because it's scriptable, you can use sling.include() to conditionally pull content into a page (which is not true of the other inclusion techniques described above). For example, suppose you wanted to pull an iframe containing SVG (Scalable Vector Graphics) into a page, but only for page requests coming from Firefox. You could have code that looks something like:

     <%
        if ( sling.getRequest().getHeader("User-agent").indexOf("Firefox") != -1 )
            sling.include("/content/test/myhead.iframe");
        else
            sling.include("/content/test/myhead.h1");
    %>

    This assumes you've got a script, iframe.jsp, that contains something like:

    <iframe src="/content/test/myhead.svg" width="300" height="50" style="border:none" > </iframe>

    In this way, you can dynamically assemble a page that renders content one way for one type of browser and another way for another type of browser.