Expand my Community achievements bar.

SOLVED

How to get page size information.

Avatar

Level 2

Hi All,

 

I am trying to get page size information in AEM, but cannot locate it. Can you tell from where I can get this information.

 

Thank you.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @muskan_mala ,

You can get the page size from input stream as belows.

public long getPageSize(Page page) {
Resource contentResource = page.getContentResource();
InputStream contentStream = contentResource.adaptTo(InputStream.class);
return contentStream != null ? contentStream.available() : 0;
}

Here, getPageSize() method takes a AEM Page object as its argument, and returns the size of the page's content in bytes.

Hope this could help you!

Regards

Shiv

Shiv Prakash

View solution in original post

5 Replies

Avatar

Community Advisor

Hello @muskan_mala 

Please give more context on what you want to achieve.

 

If you are looking for the size or length of metadata of your targeted page then you can get it programmatically using Java in this way

 

import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import org.apache.sling.api.resource.ResourceResolver;

ResourceResolver resourceResolver = slingRequest.getResourceResolver();
PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
Page targetedPage = pageManager.getPage("/content/mysite/homepage");

long pageSize = targetedPage.getContentResource().getResourceMetadata().getContentLength();

 

Thanks.

Avatar

Community Advisor

Hello @muskan_mala 

You can also get page information in this way

import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;

ResourceResolver resourceResolver = slingRequest.getResourceResolver();

Resource contentResource = resourceResolver.getResource(page.getPath() + "/jcr:content");

ValueMap valueMap = contentResource.adaptTo(ValueMap.class);

long pageSize = valueMap.get("jcr:data", byte[].class).length;

Avatar

Employee Advisor

Hi @muskan_mala ,

Can you please share for what use-case you want to fetch the page-size ?

Best Regards,

Milind Bachani

Avatar

Correct answer by
Community Advisor

Hi @muskan_mala ,

You can get the page size from input stream as belows.

public long getPageSize(Page page) {
Resource contentResource = page.getContentResource();
InputStream contentStream = contentResource.adaptTo(InputStream.class);
return contentStream != null ? contentStream.available() : 0;
}

Here, getPageSize() method takes a AEM Page object as its argument, and returns the size of the page's content in bytes.

Hope this could help you!

Regards

Shiv

Shiv Prakash

Avatar

Employee Advisor

This will only return the size of the jcr:content node in bytes, but that does not reflect the size of the rendered page.

Actually the size is only known when the complete page has been rendered. To have that information server-side, you would need to do a dummy-render process to have that information. In the browser it's probably much easier to achieve (you would only need to get the size of the HTML document).