Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn More

View all

Sign in to view all badges

How to get the current pagepath in service

Avatar

Avatar
Level 1
vijithar4145255
Level 1

Likes

0 likes

Total Posts

15 posts

Correct Reply

0 solutions
View profile

Avatar
Level 1
vijithar4145255
Level 1

Likes

0 likes

Total Posts

15 posts

Correct Reply

0 solutions
View profile
vijithar4145255
Level 1

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;

}

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Validate 1
MVP
raj_mandalapu
MVP

Likes

124 likes

Total Posts

203 posts

Correct Reply

72 solutions
Top badges earned
Validate 1
Contributor
Shape 1
Give Back 5
Give Back 3
View profile

Avatar
Validate 1
MVP
raj_mandalapu
MVP

Likes

124 likes

Total Posts

203 posts

Correct Reply

72 solutions
Top badges earned
Validate 1
Contributor
Shape 1
Give Back 5
Give Back 3
View profile
raj_mandalapu
MVP

08-02-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

@reference
private ResourceResolverFactory resolverFactory;

public void testMethod(String resourcePath ) {
try {

Resource res = resourceResolver.getResource(resourcePath);

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

 

 

Answers (5)

Answers (5)

Avatar

Avatar
Establish
MVP
BrianKasingli
MVP

Likes

613 likes

Total Posts

590 posts

Correct Reply

231 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile

Avatar
Establish
MVP
BrianKasingli
MVP

Likes

613 likes

Total Posts

590 posts

Correct Reply

231 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile
BrianKasingli
MVP

08-02-2021

@vijithar4145255 

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;
}

 

 

Avatar

Avatar
Give Back 3
MVP
Manjunath_K
MVP

Likes

139 likes

Total Posts

188 posts

Correct Reply

61 solutions
Top badges earned
Give Back 3
Give Back
Boost 50
Boost 5
Boost 3
View profile

Avatar
Give Back 3
MVP
Manjunath_K
MVP

Likes

139 likes

Total Posts

188 posts

Correct Reply

61 solutions
Top badges earned
Give Back 3
Give Back
Boost 50
Boost 5
Boost 3
View profile
Manjunath_K
MVP

08-02-2021

Hi @vijithar4145255 ,

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

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

}

}

kautuk_sahni
@Manjunath_K, thank you for sharing the answer. This would help AEM Community in posterity. Keep doing the great work in the AEM community.

Avatar

Avatar
Springboard
Level 7
KiranVedantam1992
Level 7

Likes

174 likes

Total Posts

183 posts

Correct Reply

55 solutions
Top badges earned
Springboard
Give Back 5
Ignite 1
Affirm 50
Validate 1
View profile

Avatar
Springboard
Level 7
KiranVedantam1992
Level 7

Likes

174 likes

Total Posts

183 posts

Correct Reply

55 solutions
Top badges earned
Springboard
Give Back 5
Ignite 1
Affirm 50
Validate 1
View profile
KiranVedantam1992
Level 7

08-02-2021

Hi @vijithar4145255,

 

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.

Avatar

Avatar
Ignite 1
MVP
SureshDhulipudi
MVP

Likes

163 likes

Total Posts

174 posts

Correct Reply

49 solutions
Top badges earned
Ignite 1
Give Back 5
Give Back 3
Give Back 10
Give Back
View profile

Avatar
Ignite 1
MVP
SureshDhulipudi
MVP

Likes

163 likes

Total Posts

174 posts

Correct Reply

49 solutions
Top badges earned
Ignite 1
Give Back 5
Give Back 3
Give Back 10
Give Back
View profile
SureshDhulipudi
MVP

08-02-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/");

Avatar

Avatar
Establish
MVP
Anudeep_Garnepudi
MVP

Likes

274 likes

Total Posts

320 posts

Correct Reply

87 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile

Avatar
Establish
MVP
Anudeep_Garnepudi
MVP

Likes

274 likes

Total Posts

320 posts

Correct Reply

87 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile
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