How to get the current pagepath in service

Avatar

Avatar

vijithar4145255

Avatar

vijithar4145255

vijithar4145255

08-02-2021

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;

}

View Entire Topic

Avatar

Avatar

Anudeep_Garnepudi

MVP

Avatar

Anudeep_Garnepudi

MVP

Anudeep_Garnepudi
MVP

08-02-2021

@vijithar4145255 

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();

 

vijithar4145255

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;

path.PNG

Anudeep_Garnepudi
@vijithar4145255 In my example I am using Servlet configured with a component resource type. I dropped that component in a page and hitting that component path. The registered servlet with take the request in context of that component and will return the page path in which component is dropped. Please let me know what exactly your use case is.
vijithar4145255

Hi @Anudeep_Garnepudi @Manjunath_K, In my case I have one method in service and that method we are calling in multiple Model files.If I pass request as method argument in modal I am able to get the current page path.Dow have any option with out changing code in modal. I have tried as per your suggestions its working fine. 

****************working *************
public class TestModal1 {
@Override
public LinkItem getLinkItem() { return testLinkService.getLinkItem(Url, true, request); }
}

public class TestModal2 {
@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)
{
PageManager pageManager = request.getResourceResolver().adaptTo(PageManager.class);
Page currentPage = pageManager.getContainingPage(request.getResource());
String pagePath = currentPage.getPath();
if (path.contains("****") ) {
linkItem.setTarget("_parent");
}
}
}
****************working *************

Do we have any other option instead of changing model code.Since I am using that getLinkItem method in mutple models.

****************not working *************
public class TestModal1 {
@Override
public LinkItem getLinkItem() { return testLinkService.getLinkItem(Url, true); }
}

public class TestModal2 {
@Override
public LinkItem getLinkItem() { return testLinkService.getLinkItem(Url, true); }
}


public class testLinkService {
@Override
public String getPath(final SlingHttpServletRequest request) {
PageManager pageManager = request.getResourceResolver().adaptTo(PageManager.class);
Page currentPage = pageManager.getContainingPage(request.getResource());
String pagePath = currentPage.getPath();
return pagePath;
}
public LinkItem getLinkItem(final String url, final boolean isFragment) {
String path= getPath(request); ----if i declare local varable in this method i need intialize here if i intialize it will take null value ----
if (path.contains("****") ) {
linkItem.setTarget("_parent");
}
}
}
****************not working *************

Manjunath_K

Hi @vijithar4145255

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)
{

}

}

Anudeep_Garnepudi

@vijithar4145255, No other way you need to update you Service and Sling Models. Better get the current page path in Sling Model and passing it as an argument to your service and update your Service accordingly.

//Service
String getLinkItem(final String url, final boolean isFragment,String pagePath) {
 ...
}
// Service Call in Sling Model
testLinkService.getLinkItem(Url, true, currentPage.getPath());

 

vijithar4145255
Thnak you @@Anudeep_Garnepudi