Zhenya
Zhenya

Reputation: 6198

How to set sling.servlet.resourceTypes in a Sling serlvet to a path relative to itself (to serlvet's resource)?

In case I have asked a question in a wrong way I will first say what I want to achieve:

I have the following structure of a component in CQ5: /apps/TEST/components/DatabaseConnection (DatabaseConnection is a component name) /apps/TEST/components/DatabaseConnection/src/ServletDatabaseConnection (This is the my serlvet for processing POST requests from TEST/components/DatabaseConnection.

In my servlet sling.servlet.resourceTypes is set to TEST/components/DatabaseConnection)

So, I have set a relative path of sling.servlet.resourceTypes, and the serlvet will look for a resourceType under /apps/TEST/components/DatabaseConnection and under /libs/TEST/components/DatabaseConnection/

But I want to be able to move my component DatabaseConnection to any other folder and not change anything neither in the servlet nor in the application. But if I do it now, I will have to change sling.servlet.resourceTypes to a new resourceType

Taking into account, that everything in Sling is a resource, than my servlet is a resource too, right? So, probably it would be possible to set sling.servlet.resourceTypes relatively to the resourceType of the servlet?

So, can I set sling.servlet.resourceTypes relatively to this servlet's resource type? If no, are there any other ways to make my component "moveable", so that I won't have to change anything in a servlet?

I have found this two resources to be very useful: http://www.pro-vision.de/content/medialib/pro-vision/production/adaptto/2012/adaptto2012-apache-sling-basic-concepts-rainer-bartl-peter-manne/_jcr_content/renditions/rendition.file/adaptto2012-apache-sling-basic-concepts-rainer-bartl-peter-mannel.pdf http://sling.apache.org/site/resources.html But I still can't come up with a solution

Upvotes: 3

Views: 3654

Answers (2)

Zhenya
Zhenya

Reputation: 6198

7 months have passed and I have found out how to achieve what I was trying to achieve after looking at my colleague's code :)

So I wanted to make the component moveable, together with its servlet.

To do that we don't need to use resourceType but we need to use sling.servlet.paths. http://sling.apache.org/documentation/the-sling-engine/servlets.html

To make it work we need to do three things.

1) In the form which we want to send to the Servlet, set some action. Example:

<form name="name" id="id" action="/someaction/dothis" method="POST">
<button name="submit"> Submit </button>
</form>

2) In the servlet set the sling.servlet.paths. In our case:

@Component(immediate = true)
@Service(value=javax.servlet.Servlet.class)
@Properties(value = {
@Property(name="sling.servlet.methods", value={ "POST" }),
//
@Property(name="sling.servlet.paths", value={"/someaction/dothis"})
})
public class ServletEvaluation extends SlingAllMethodsServlet {
...
}

3) In OSGi console /system/console/configMgr configure Apache Sling Servlet/Resource Resolver: Add sling.servlet.path to Execution paths. In our case set execution path to /someaction

Upvotes: 1

Batavia
Batavia

Reputation: 2497

did you try setting an absolute path instead of resourceType? For example if you set the path to /bin/TEST/DatabaseConnection it doesn't matter what the resource type is.

Upvotes: 0

Related Questions