How to get the path of the component instance under a page, that's invoking a servlet? | Community
Skip to main content
Level 2
May 24, 2018
Solved

How to get the path of the component instance under a page, that's invoking a servlet?

  • May 24, 2018
  • 14 replies
  • 18723 views

Hi All,

Here's my requirement.

I have a component which is invoking the servlet via an Ajax call.

I would like to get the path of this component's instance under the page in the servlet.

If test is the page and comp1, comp2 are the instances of the same component dragged and dropped twice in the parsys called par,

I would like to get the path /content/test/jcr:content/par/comp1 in my servlet invoked from comp1, similarly /content/test/jcr:content/par/comp2 for comp2.

Alternatively, I am currently achieving this by using the resource HTL Global Object ${resource.path} in the JavaScript which gives the path of the current resource instance and passing it as a request parameter to the servlet.

But this is not admissible as I am trying to eliminate any query parameters being passed to the servlet due to caching constraints.

Is there a way to get path of the component instance under par which invokes the servlet at the back-end???

Please Help!!

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by joerghoh

Your requirement is quite easy to solve.

I assume that:

* the page is https://mycorp.com/content/mycorp/test.html

* and the servlet invoked is

** https://mycorp.com/bin/myservlet?comp=/content/mycorp/test/jcr:content/comp1 and

** https://mycorp.com/bin/myservlet?comp=/content/mycorp/test/jcr:content/comp2

and the servlet calls should be changed so the query string goes away.

Just change it in the way, that

* the page is https://mycorp.com/content/mycorp/test.html

* the servlet is called like this

** https://mycorp.com/content/mycorp/test/jcr:content/comp2.myservlet.json

** https://mycorp.com/content/mycorp/test/jcr:content/comp2.myservlet.json

And that's quite easy to achieve, because you have to bind the servlet as a selector to the resourcetype of "comp". The result is a perfectly cachable URL.

regards,

Jörg

14 replies

smacdonald2008
Level 10
May 24, 2018

What problem are you trying to solve by getting this path. I am trying to understand what value this  is within a web site.

edubey
Level 10
May 24, 2018

Ajax call in front end will be completely unware of component structure in AEM node.

You wil have to pass params.

One more, Even if you dont use params and  think of caching, Ideally your servlet response should not be cached else it will not execute again for another component.

Whats the use case?

S_BhavyaAuthor
Level 2
May 25, 2018

The actual requirement is cache a servlet response, which is returned by a third party rest-api. The url for this rest-api is fetched from the dialog proeprties of the component 'comp1'.

So, I was hoping if I could get the path of the component instance under par from which the servlet invoked, I can directly fetch the dialog property using PropertyIterator without having to pass query params to the servlet. It would help me cache the response, provided I have an extension for the  servlet path. And when the author changes the URL in the dialog for that particular component and activates the page, the dispatcher might invalidate the cache and invoke the servlet again to fetch fresh response.

@edubey

Yes I totally agree with your point. This would be the next challenge. Any suggestions?

I have tried considering registering it with resourceTypes, selectors, extension and suffix. But I cannot bind the servlet to a single page resource as I would be using this servlet on multiple pages through various components serving the same purpose. Also, we cannot use wildcards to define a regex for the selector value while registering a servlet.

Alternatively I have also tried dispatcher level configurations such as ignoreUrlParams, Mod Rewrite Rules and resource mapping using etc/maps/http. It doesn't seem to work for my requirement.

Any suggestions?

I'm clearly going forward based on trial and error method.

Kindly correct me if my understanding is wrong.

Level 2
May 25, 2018

Hi,

You can register your servlet using resourceType and can get component path in your servlet using :

smacdonald2008
Level 10
May 25, 2018

That will give you path to page - then you still need to figure out which component on the page sent the AJAX request.

arunpatidar
Community Advisor
Community Advisor
May 25, 2018

There is a way to achieve this by making call to Sling servlet (which is registered via resourcesType="componentPath") using java and get component path using request.getResource().getPath()

But this required credential to make a succesful request in Author, I think you can do it without credential(Authentication) in the Publish instance.

I don't know if this is feasible or not from your end but I am able to do it in my local instance.

Below is JS file which making kind a ajax call from component

// Server-side JavaScript for the ajax call

use(function () {

    var curRes = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()+resource.getPath();

    var getVar = new Packages.org.apache.commons.httpclient.methods.GetMethod(curRes);

    var client = new Packages.org.apache.commons.httpclient.HttpClient();

   // Authentication part

    var creds = new Packages.org.apache.commons.httpclient.UsernamePasswordCredentials("admin", "admin");

     client.getParams().setAuthenticationPreemptive(true);

    client.getState().setCredentials(org.apache.commons.httpclient.auth.AuthScope.ANY, creds);

   // Authentication part end

    client.executeMethod(getVar);

});

Thanks

Arun Patidar

Arun Patidar
joerghoh
Adobe Employee
joerghohAdobe EmployeeAccepted solution
Adobe Employee
May 27, 2018

Your requirement is quite easy to solve.

I assume that:

* the page is https://mycorp.com/content/mycorp/test.html

* and the servlet invoked is

** https://mycorp.com/bin/myservlet?comp=/content/mycorp/test/jcr:content/comp1 and

** https://mycorp.com/bin/myservlet?comp=/content/mycorp/test/jcr:content/comp2

and the servlet calls should be changed so the query string goes away.

Just change it in the way, that

* the page is https://mycorp.com/content/mycorp/test.html

* the servlet is called like this

** https://mycorp.com/content/mycorp/test/jcr:content/comp2.myservlet.json

** https://mycorp.com/content/mycorp/test/jcr:content/comp2.myservlet.json

And that's quite easy to achieve, because you have to bind the servlet as a selector to the resourcetype of "comp". The result is a perfectly cachable URL.

regards,

Jörg

Level 4
August 13, 2018

Hi Jorge,

Do you have any sample code for above approach?

Thanks,
Rajeev

joerghoh
Adobe Employee
Adobe Employee
August 14, 2018

see [1] for an example where the selector "tagwidget" is bound to all resource types; you might want to be more specific with the resource type.

[1] acs-aem-commons/TagWidgetConfigurationServlet.java at 763d2968169be1d86937bc1f4354cf2f8e201866 · Adobe-Consulting-Servic…