Posts Tagged “Apache Sling”

Creating a site footer in AEM 6 using Sightly and Sling Models

The Adobe-developed and [recently-donated-to-Apache-Sling](http://blogs.adobe.com/experiencedelivers/experience-management/donating-sightly-apache-sling/) project [Sightly](http://docs.adobe.com/docs/en/aem/6-0/develop/sightly.html) project has been out for a little under a year now, alongside Adobe Experience Manager 6, and has slowly been amassing documentation and gaining presence. The server-side templating language aims to give a facelift to the web development facet of Java-based software stacks, Adobe's...

CQ Haiku: JCR vs. Sling

Adapt to Node like Assembly inside Java Use Sling API Quite often I see code, especially from less experience developers, with something like this: Node node = resource.adaptTo(Node.class); String property = null; try{ property = node.getProperty("myProperty").getString(); } catch (RepositoryException re){ log.error("Exception accessing myProperty", re); } Or even scarier, there may be just a global exception handler for the particular method. Either way, this code is a lot more fragile than it needs to be. If...

ModifiableValueMap Not Found: Considerations in Using ModifiableValueMaps in an API

So let's just say you are designing an API which allows both reading and writing of attributes for a Resource.  Of course, you want to use consistent code to persist the values, so you use the (relatively) new CRUD features in the Sling API to both get and set the property values.  To save code, you might use a ModifiableValueMap for both operations, which works great in author as an administrative user.   The Problem At some...

Accessing Request Variables in Sightly

You can implement some interesting stuff in Sightly, the newest templating language for Adobe Experience Manager, including Tower of Hanoi.  At the same time, there are a few practical applications which are not available in Sightly, including accessing request scoped variables in Sightly.  While Gabriel's suggested approaches in the Stack Overflow post are absolutely correct in this situation, there are still certain situations where request variables can be very useful.   A good example is for sharing a variable...

Sling Models for Fun and Profit

Recently, I finally had a chance to leverage Sling Models for a new project.  If you are not familiar with Sling Models, they allow developers to inject methods and fields based on resource properties, other resources and OSGi services.  For those developing in AEM 6, Sling Models are available as part of the default installation, for those on older versions of AEM / CQ5, Sling Models are available as downloadable bundles on the Sling website.  Sling Models...

9 JCR-SQL2 Queries Every AEM Developer Should Know

JCR-SQL2 queries are useful items in every AEM Developer's toolbag.  They are relatively quick, aren't deprecated and have a familiar SQL like grammer. JCR-SQL2 queries can be invoked from the JCR API: Session session = ...QueryManager queryManager = session.getWorkspace.getQueryManager();Query query = queryManager.createQuery("{QUERY}", Query.JCR_SQL2);QueryResult result = query.execute(); or through the Sling API: ResourceResolver resolver = ...Iterator<Resource> result = resolver.findResources("{QUERY}", Query.JCR_SQL2); Although there is an official specification for JCR-SQL2, there aren't a ton of really good resources out...

New from Apache Sling: Testing Tools 1.0.8

The Apache Sling Project just announced the release of Testing Tools 1.0.8.  This new release brings some great new features and fixes to anyone using Apache Sling Testing Tools for integration tests.  There are two fixes which are certainly worth highlighting. SLING-3328:  Simplify SlingTestBase usage with an already running instance This issue makes it easier to connect the Sling Testing Tools to a running Sling / AEM instance without requiring to set the jar.executor.jar.folder, jar.executor.jar.options,...

Six Dimensions Supports the Apache Sling Project: Resource.hasChildren

Recently, I had the chance to shepherd a new feature into the Apache Sling API. This feature adds the method hasChildren to the Resource andResourceResolver. My colleague, Brian Warner suggested adding this method after struggling to do this in a clean, correct way.What's new? The hasChildren method is a convenient shortcut for developers to check whether or not a Resource has child resources. This method returns correctly across different Resource Providers unlike the lower-level API's such as Node.hasNodes(). And this method enables developers to...

New in AEM 5.6.1: Workflow Purge Scheduler

In previous versions of Adobe CQ5/AEM in order to purge old completed workflows, you needed to either write a custom job or install an additional package provided by DayCare.  Now in AEM 5.6.1, this functionality comes out of the box with the product.Why Purge Completed Workflows?So why would you want to purge completed workflows?  Workflow instances are stored as nodes inside CQ.  Generally, this doesn't pose a problem for author-generated workflows, at least not over...

Service Boss Level Part 2: Custom Web Console Plugins

You've got your awesome service.  It performs all sorts of complex tasks and maybe even uses a Sling Service Factory to provide multiple configurations.  But how do you know what it's doing?  Well, of course you need to have appropriate logging, but another tool you can leverage is the Apache Felix Web Console, by creating a custom Web Console Plugin.  Your plugin can be accessed through Adobe CQ and Apache Sling's Web Console and can display...

Service Boss Level: Service Factories

I've always wondered how the Apache Sling Logging Logger Configuration worked.  I like the idea of being able to configure multiple configuration instances through the OSGi Console and could see how this would cut down on the UI code I'd need to write in many circumstances.Since working on the Apache Sling project, I have taken the time to peruse the code for the Sling logger.  Unfortunately, the Sling logging code uses a fairly arcane method for registering the...

Six Dimensions Supports the Apache Sling Project: New Resource Access Tags

Recently, I had the chance to release a new version of the Sling JSP Taglib. This new version includes a large number of features and improvements which I hope will help developers in Sling-based platforms, including Adobe CQ, develop cleaner and more reusable JSP scripts.What's new? Well, the JSP Taglib allows for access to the Sling Repository without requiring any Java code.  With the previous version of the Sling Taglib, if you wanted to the get the...

New from Apache Sling: Resource Access Tags

Recently, I had the chance to release a new version of the Sling JSP Taglib. This new version includes a large number of features and improvments which I hope will help developers in Sling-based platforms, including Adobe CQ, develop cleaner and more reusable JSP scripts. What's new? Well, the JSP Taglib allows for access to the Sling Repository without requring any Java code. With the previous version of the Sling Taglib, if you wanted to...

My CQ JSP Development Best Practises

I would guess that many or most developers in the CQ development space have their own set of development best practises they operate with.  This is a topic very near and dear to my heart.  It is something that I have spent significant personal time pursuing.  At the end of the day I want readable, maintainable, extensible, bug free code that is not a burden.I have deliberately kept the list short.  These have been primarily...

CQ Resource Inclusion from a Servlet

Including resources from a JSP is super simple due to the many facilities within Sling and CQ.  But what if you wanted to include a CQ resource from a custom Sling Servlet?  All of this nice JSP based inclusion is built on top of the JavaEE API.  Take a look at the class: javax.servlet.jsp.PageContext and two of it's methods: public abstract void include(String relativeUrlPath) and public JspWriter pushBody(Writer writer). These two method will form the basis for all...

Beware of Export-Package

Quick - without checking the documentation, what does the Export-Package instruction do in the Maven Bundle Plugin?Did you say "sets what packages are exported by the bundle"?  If so, you're only half right.Recently, I was trying to diagnose some issues with a SOAP Web Services implementation in an existing code base.   Theoretically, everything was in place, however I was getting a ClassCastException when attempting to retrieve the OSGi Service for accessing the SOAP Web...

Ask the Experts: CQ5 Inheritance Model

In this series of blog posts we pose a question to experts in the Adobe CQ5 platform about Adobe CQ5, Enterprise Content Management and Web Development.What's the difference in CQ5's inheritance model versus standard Java?The main difference that I can think of is that in standard Java, you will write a class which inherits all the methods of a previous class. These methods can be modified and overwritten, and new methods even added to enhance...

EL Function Management

In my post Tagfile vs. Tag in a Sling or CQ5 Application we created the groundwork for developing Servlet artifacts: Tag files, Classic Tags, and EL Functions.  The definition of all these items require the file taglib.tld.  This defines the URL that identifies the library via the uri in a <%@taglib prefix=".." uri=".." %> JSP statement within a JSP.  Here we address creating EL functions and managing them.Additionally, I will provide some of my thoughts...

Tagfile vs. Tag in a Sling or CQ5 Application

Custom tags are cool, I guess.  The first couple I wrote were fun to write.  Kind of like driving through a new city, or part of the country.  The landscape is vibrant, new, unfamiliar, exciting.  When I came back several months later, things were just unfamiliar, and not so exciting.If you are taking the OCEJWCD test, there is no avoiding them.  However, my recommendation is, unless you are writing a Servlet framework, avoid them.  Unless...

Creating Integration Tests in Apache Sling

One of the lesser known features in Apache Sling Testing Tools framework is the SlingTestBase, this class can be extended to allow you to create tests against a Sling instance which will be automatically created, started and then shut down when the tests are complete.Why Test this Way?First, you should have some form of automatic testing on your Apache Sling and Adobe CQ5 projects.  Automatic testing gives you better coverage against regression and changes breaking code...

Ask the Experts: MVC in Adobe CQ5

In this series of blog posts we pose a question to experts in the Adobe CQ5 platform about Adobe CQ5, Enterprise Content Management and Web Development.How does CQ5's architecture compare to a standard MVC scenario?As we all know, MVC stands for Model, View, & Controller. This is the logical separation of work into those three buckets to allow for more modular and readable code. CQ follows this model (even if you may not notice you're...

Six Dimensions' Contribution to CQ 5.6

The new version of CQ5, Adobe Experience Manager 5.6, includes a contribution by Six Dimensions to the Apache Sling project.  This contribution adds a new method, getChildren to the Resource interface.  Now getChildren() in the Resource-API, available in AEM5.6 thanks to @klcodanr #sling #cq5 #aem dev.day.com/docs/en/cq/cur...- Feike Visser (@heervisscher) March 10, 2013This method allows you to use for-each loops to iterate through child resources and allows access the children of a resource using Expression Language and TagLibs....

Sling (sort of) Requires Javax Servlet

Recently I was refactoring some Maven POM's to reduce the number of dependencies and manage the dependency version from the reactor POM.   After I removed the duplicate dependencies I ran the build and almost immediately ran into this error:[ERROR] BUILD FAILURE[INFO] ------------------------------------------------------------------------[INFO] Compilation failure{...Class}.java:[178,50] cannot access javax.servlet.http.HttpServletRequest class file for javax.servlet.http.HttpServletRequest not found            RequestPathInfo requestInfo = httpRequest.getRequestPathInfo() ;Since the error was related to Java Servlets, I assumed it was an issue with the servlet project. ...

Servlets in Sling - the case of the disappearing Servlet path

I have to admit, coming from a plain old JavaEE background, I was very dubious over the prospect of not using a Servlet path when creating custom Sling Servlets.  What - no Servlet path?  How will I access my Servlet?  New ideas always go down hard, but once I saw the light, it ALL made sense.Some folks have been evangelizing the use of Sling Servlets that are resource defined as opposed to path defined.  This...

Handling File Upload in Adobe CQ

You need to handle a file upload in Adobe CQ.  So, you notice CQ already has Apache Commons FileUpload installed, so you decide to use it to handle the file upload.You write your servlet code and end up with something like this:// Check that we have a file upload requestfinal boolean isMultipart = ServletFileUpload.isMultipartContent(request);PrintWriter out = null;try {  out = response.getWriter();  if (isMultipart) {    // Create a new file upload handler    final ServletFileUpload upload =...

Your Custom Namespace Retort

So - In my last article, I stuck my new Namespace in an OSGi bundle where it did not belong.  I wanted to verify the approach for adding the Namespace, and in my thinking at the time, it was the quickest location to do that.  It worked.  Great - now how do I put it in the "correct" location.You cannot simply remove the Sling Namespace metadata from the POM and redeploy.  Nope.  Still there.  What I...

Fixing an out of sync CQ Cluster

Every so often a node in a CQ cluster will fall out of sync with its master.  This can happen due to a loss of network connectivity, power loss, or any number of other alternatives.  When the master and slave fall out of sync, it will be impossible to start both nodes (note start, if it was due to a network connectivity issue, it is possible that they can both remain running in a non-clustered...

Note to self: This is Mike. BAD Idea to ...

... throw your laptop through the glass block window.Yes.  I'm back at it in spades on something that should be pretty simple.  Problem is there is lots of rhetoric on how it should/could/would be done, but honestly nothing of practical use.  This was not simply 10 minutes of Googling, throw my hand up and "Ach du lieber gott!"  No no no.  My most egregious sin is once I get it in my head that it...

Gotcha: Sling Servlet Requires a Name

I recently had the opportunity to delve into the code for the Sling Resources to document the @SlingServlet annotation.  One of the more interesting things I discovered was that in addition to everything stated on the Sling documentation about what is required to register a servlet, there is one additional requirement.Each Servlet which is to be created and registered, must have an unique name.  The SlingServletResolver, which handles the resolution of Servlets, generates the name...

New in Apache Sling: getChildren

Recently I had the pleasure of contributing to the Apache Sling project.  Among other things, I contributed a suggested improvement to have Sling Resources return an Iterable<Resource> of the child resources.  Recently, the Apache Sling released an update to the Sling API,  including an implementation of this improvement.  With the new API change, developers will be able to leverage the enhanced for-each loops available in Java 6. This will allow developers to write code like:for(Resource child...

Loading Classes in Adobe CQ

So you've got a problem.  You need to load a resource from the Classpath or you need to dynamically load a class and you're using Adobe CQ.  Since Adobe CQ uses an OSGi container, a regular ClassLoader will not be able to retrieve the class information from the OSGi Bundles.Never fear, Apache Sling provides the Dynamic ClassLoader Service.  This OSGi Service allows you to retrieve a ClassLoader which will allow you to interact with classes...

Good to Know: The Sling PersistableValueMap

The Sling ValueMap makes retrieving properties from CQ easy and removes a lot of the error-prone code you have to use when using the JCR APIs, however it does not allow setting of properties.  This unfortunately results in developers using the Sling ValueMap to retrieve properties and JCR Nodes and Properties to set properties.  This doesn't have to be the case though!  Sling also provides an interface which allows for retrieving properties like a ValueMap...

New in CQ 5.5: Sling Adapters Console

Adobe CQ 5.5 has a new, long overdue feature in the Felix Console.  The Sling Adapters Console lists all of the available Sling Adapter Factories as well as their Adapter Classes, Adaptable Classes, condition information and Providing bundle.  This information can be invaluable for developers to determine what classes can be adapted and what they can be adapted into as well as checking the functioning of custom adapters.  Developers can access this information by logging...