Creating a page object in a Java class using a URL

In my Java class in the OSGi package, I have a page URI -

String pageUri = "/content/site/page.html" ; 

How to create a page object using this URL? I basically need to get the page properties also from the object later ...

I tried this code in my class:

 PageManager pm = new PageManager(); Page page = pm.getPage(pageUri); 

But this gives me a compilation error:

 Cannot instantiate the type PageManager 
+4
source share
3 answers

You should be able to insert an instance of ResourceResolverFactory into your component / service and from there enable the resource / page, as described in Obtaining resources and properties in Sling .

For instance:

 @Component(immediate = true) @Service(GetMeAPage.class) public class GetMeAPage { @Reference private ResourceResolverFactory resourceResolverFactory; private static final String pageUri = "/content/site/page.html"; /** * This method is executed at component startup rather than in the context of a request. */ @Activate public void getSpecificPage() { ResourceResolver resourceResolver = null; try { resourceResolver = resourceResolverFactory.getAdministrativeResourceResolver(null); Page page = getSpecificPage(resourceResolver); System.out.println(page.getTitle()); } catch (LoginException e) { e.printStackTrace(); } finally { if (resourceResolver != null) { resourceResolver.close(); } } } public Page getSpecificPage(ResourceResolver resourceResolver) { Resource resource = resourceResolver.resolve(pageUri); return resource.adaptTo(Page.class); } } 

Full code in this value

It uses an administrative login that is not perfect . I would recommend using ResourceResolver from the request. The easiest way to do this is to pass it as a method parameter to getSpecificPage(resourceResolver) (above) from your component / servlet.

Update: The correct way to do this in later versions of Sling (circa 2014, AEM6 +) is to use Sling Service Authentication . The getAdministrativeResourceResolver method is now deprecated.

+6
source

If you extend the SlingAllMethodsServlet with the "class in the OSGi package", you will have access to the request from which you can get the resolver / page manager.

 class Foo extends SlingAllMethodsServlet { @Override protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException { ResourceResolver resourceResolver = request.getResourceResolver(); PageManager pageManager = resourceResolver.adaptTo(PageManager.class); String pageUri = "/content/site/page.html" ; Page page = pageManager.getPage(pageUri); } } 

It depends on what your setup is inside this package.

+2
source

PageManager could not be created. To get an instance, you can adapt the resource resolver: resourceResolver.adaptTo (PageManager.class).

ResourceResolver can usually be obtained from the resource: resource.getResourceResolver (), you can also get it directly from the sling request

+1
source

Source: https://habr.com/ru/post/1499540/


All Articles