How to get the current pagepath in service | Community
Skip to main content
Level 3
February 8, 2021
Solved

How to get the current pagepath in service

  • February 8, 2021
  • 6 replies
  • 21356 views

I have used the below code to get the currentpagepath in modal but if I use same code in service it is not working nd currentpage value I am getting null. Can anyone help on this.

 

@ScriptVariable
protected Page currentPage;

 

public string getCurrenPath() {

String currentPagePath = currentPage.getPath();

return currentPagePath;

}

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 raj_mandalapu

You need to pass the current page object to the OSGi service.

public void readProperties(Page currentPage) {
ResourceResolver resolver = null;
InheritanceValueMap map = new HierarchyNodeInheritanceValueMap(currentPage.getContentResource());
}

In the sling model call OSGI service

  @Inject @Source("osgi-services")
  TestService testService1;

    @PostConstruct
    public void activate(){
        System.out.println("Inside Post Constructor Method");
        testService1.readProperties(currentPage);
    }

You can also pass page path as a parameter to the service method and inside service method, you need to write something like this

@3214626
private ResourceResolverFactory resolverFactory;

public void testMethod(String resourcePath ) {
try {

Resource res = resourceResolver.getResource(resourcePath);

resourceResolver.close();
} catch (LoginException e) {
// log the error
}
}

 

 

6 replies

Anudeep_Garnepudi
Community Advisor
Community Advisor
February 8, 2021

@vijitha 

You can not use Sling annotations in OSGI service. currentPage, currentNode etc.. are global sling objects which will be created while Sling resolving particular resource. As Sling model has all the capabilities of Sling, those variables/objects will be directly injected in to Sling Model in context of current request.

In OSGI service this is not that straight forward, you should get the current Page and then get path.

  • Get the ResourceResolver
  • Get current Resource
  • Adapt resourceResolver to PageManager
  • Get the current Page by passing current resource
  • Get path from Page

Try the below code snippet, might help. Here I am getting Resource and ResourceResolver from request.

PageManager pageManager = request.getResourceResolver().adaptTo(PageManager.class); Page currentPage = pageManager.getContainingPage(request.getResource()); String pagePath = currentPage.getPath();

 

vijithaAuthor
Level 3
February 8, 2021

Hi @anudeep_garnepudi Thank you for the detailed explanation. I have tried using the above code but I am getting null value in request in service.

 

SlingHttpServletRequest request;

SureshDhulipudi
Community Advisor
Community Advisor
February 8, 2021

In Service you have to user ResourceResolver , Resource objects

 

ResourceResolver resourceResolver = request.getResourceResolver();

PageManager pageManager = resourceResolver.adaptTo(PageManager.class);

Page currentPage = pageManager.getPage("/content/page/en/");

raj_mandalapu
raj_mandalapuAccepted solution
Level 7
February 8, 2021

You need to pass the current page object to the OSGi service.

public void readProperties(Page currentPage) {
ResourceResolver resolver = null;
InheritanceValueMap map = new HierarchyNodeInheritanceValueMap(currentPage.getContentResource());
}

In the sling model call OSGI service

  @Inject @Source("osgi-services")
  TestService testService1;

    @PostConstruct
    public void activate(){
        System.out.println("Inside Post Constructor Method");
        testService1.readProperties(currentPage);
    }

You can also pass page path as a parameter to the service method and inside service method, you need to write something like this

@3214626
private ResourceResolverFactory resolverFactory;

public void testMethod(String resourcePath ) {
try {

Resource res = resourceResolver.getResource(resourcePath);

resourceResolver.close();
} catch (LoginException e) {
// log the error
}
}

 

 

Kiran_Vedantam
Community Advisor
Community Advisor
February 8, 2021

Hi @vijitha,

 

As per your comments, assuming you have request object, you can get the page URL like this:

Enumeration values = request.getHeaders(REFERER);

while (values.hasMoreElements()) {
pageURL = values.nextElement().toString();
}

Once you get the URL, you can trim it and fetch the path.

 

Hope this helps.

 

Thanks,

Kiran Vedantam.

Manjunath_K
Level 7
February 8, 2021

Hi @vijitha ,

1. If you want to get current page path in OSGI Service class then pass request as an method argument to service class methods from model/servlet class & get page path using request present in the method parameter. 

 

2. If you want to get current page path in servlet then you can get it using code shared by @anudeep_garnepudi.

 

Hope this helps!

Manjunath_K
Level 7
February 9, 2021

Hi @vijitha

To get current page path in OSGI service method, we need current request. so, passing method argument to service class methods is the only way with below 2 option.

 

1. Recommended approach - duplicating code in each model class to fetch current page path before passing it to service methods will be reduced if you keep that logic in service methods by passing request as method argument.

public class TestModal1 {
@Override
public LinkItem getLinkItem() {
         

       return testLinkService.getLinkItem(Url, true, request); }
}

 

public class TestLinkService {
@Override
public LinkItem getLinkItem(final String url, final boolean isFragment,SlingHttpServletRequest request)
{
            //get current page path using current request 
}

}

 

2.

public class TestModal1 {
@Override
public LinkItem getLinkItem() {

          //get current page path using current request & pass page path to service methods.

          return testLinkService.getLinkItem(Url, true, currentPagePath); }
}

 

public class TestLinkService {
@Override
public LinkItem getLinkItem(final String url, final boolean isFragment,String currentPagePath)
{

}

}

BrianKasingli
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
February 8, 2021

@vijitha 

Assuming that your problem is from Sling Models...

There two be really important declarations you are not sharing.

What is the adaptables? Make sure the SlingHttpServletRequest.class value is an adaptable option

What is the actual Page object you are referencing to? making sure com.day.cq.wcm.api.Page object is being referenced.

I tested the code below and it works for me:

 

@Model(adaptables = {SlingHttpServletRequest.class}, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL) public class Component { @ScriptVariable private com.day.cq.wcm.api.Page currentPage; }