Is there something like servlet filters but for pages for AEM cloud? | Community
Skip to main content
Level 8
May 27, 2021
Solved

Is there something like servlet filters but for pages for AEM cloud?

  • May 27, 2021
  • 2 replies
  • 2299 views

if you use your own (sling)servlets in AEM, you can also write filters to do things like log request or add headers to any servlet based on a pattern, e.g. 

@8220494 @SlingServletFilter(scope = {SlingServletFilterScope.REQUEST}, pattern = "/bin/.*", methods = {"GET","POST"}) public class CorrelationLoggingFilter implements Filter {

Is something like this possible for pages?  

We want to implement things like dynamic url rewriting/routing, but not do it in the dispatcher (as this cannot be easily tested on local author env, and to deploy to cloud to test a one line change takes anything between  2 hours and 2 days.

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 MarkusBullaAdobe

Hi @tb3dock!

 

In general, filters do apply to all requests so the answer to your question if filters can be included into request processing for pages is: yes. Please refer to the example filter implementation of the AEM Maven Archetype for an example [1].

 

That being said, I strongly discourage to build a custom rewriting or ruling mechanism into AEM. This will probably interfere with various AEM and Sling features and mechanisms and potentially break things. You really don't want to go down that rabbit whole.

 

There are two alternative approaches here:

  1. Use the according Apache httpd and dispatcher features
    It's totally possible to test this on a local environment and the AEM Cloud SDK even includes a Dispatcher. I recommend to have at least one (local or server) dev environment in every project that includes a full setup with author and publish instances plus dispatcher for this kind of development.
    Handling rewriting and redirects on the dispatcher level comes with performance advantages and reduces load to the AEM instances. Shying away from this approach because you just don't want to setup a local dispatcher is IMHO not a very resilient argument against it. You will have a dispatcher in production and you really should have a proper setup to develop and test your configurations on the lower/lowest stages as well.
  2. Use the existing AEM features for Resource Mapping [3] such as
    1. JCR Resource Resolver and/or
    2. AEM Mapping Definitions (/etc/map)

Personally, I prefer a combination of rewrite rules on the dispatcher level and link rewriting on the AEM side as outlined in this article [4] .

Please note that this always has two components:

  • Routing incoming requests to the correct path/resource (e. g. mysite.com/home.html to /content/mysite/de/de/home) and
  • Rewriting links inside the page/components (e. g. shortening /content/mysite/de/de/home.html to /home.html in href attributes)

 

Hope that helps!

 

[1] https://github.com/adobe/aem-project-archetype/blob/develop/src/main/archetype/core/src/main/java/core/filters/LoggingFilter.java

[2] https://experienceleague.adobe.com/docs/experience-manager-cloud-service/implementing/developing/aem-as-a-cloud-service-sdk.html?lang=en

[3] https://experienceleague.adobe.com/docs/experience-manager-65/deploying/configuring/resource-mapping.html?lang=en

[4] https://helpx.adobe.com/in/experience-manager/kb/multi-domain-management-aem-mappings-for-url-shortening---aem-6-.html

2 replies

Asutosh_Jena_
Community Advisor
Community Advisor
May 27, 2021

Hi @tb3dock 

 

Ideally filters should log all the request. Is it not logging the page requests?

You can change the path and try it out.

MarkusBullaAdobe
Adobe Employee
MarkusBullaAdobeAdobe EmployeeAccepted solution
Adobe Employee
May 28, 2021

Hi @tb3dock!

 

In general, filters do apply to all requests so the answer to your question if filters can be included into request processing for pages is: yes. Please refer to the example filter implementation of the AEM Maven Archetype for an example [1].

 

That being said, I strongly discourage to build a custom rewriting or ruling mechanism into AEM. This will probably interfere with various AEM and Sling features and mechanisms and potentially break things. You really don't want to go down that rabbit whole.

 

There are two alternative approaches here:

  1. Use the according Apache httpd and dispatcher features
    It's totally possible to test this on a local environment and the AEM Cloud SDK even includes a Dispatcher. I recommend to have at least one (local or server) dev environment in every project that includes a full setup with author and publish instances plus dispatcher for this kind of development.
    Handling rewriting and redirects on the dispatcher level comes with performance advantages and reduces load to the AEM instances. Shying away from this approach because you just don't want to setup a local dispatcher is IMHO not a very resilient argument against it. You will have a dispatcher in production and you really should have a proper setup to develop and test your configurations on the lower/lowest stages as well.
  2. Use the existing AEM features for Resource Mapping [3] such as
    1. JCR Resource Resolver and/or
    2. AEM Mapping Definitions (/etc/map)

Personally, I prefer a combination of rewrite rules on the dispatcher level and link rewriting on the AEM side as outlined in this article [4] .

Please note that this always has two components:

  • Routing incoming requests to the correct path/resource (e. g. mysite.com/home.html to /content/mysite/de/de/home) and
  • Rewriting links inside the page/components (e. g. shortening /content/mysite/de/de/home.html to /home.html in href attributes)

 

Hope that helps!

 

[1] https://github.com/adobe/aem-project-archetype/blob/develop/src/main/archetype/core/src/main/java/core/filters/LoggingFilter.java

[2] https://experienceleague.adobe.com/docs/experience-manager-cloud-service/implementing/developing/aem-as-a-cloud-service-sdk.html?lang=en

[3] https://experienceleague.adobe.com/docs/experience-manager-65/deploying/configuring/resource-mapping.html?lang=en

[4] https://helpx.adobe.com/in/experience-manager/kb/multi-domain-management-aem-mappings-for-url-shortening---aem-6-.html

TB3dockAuthor
Level 8
May 28, 2021

Thank you for this informative reply. Agreed doing URL rewriting/routing it in dispatcher would be easy, but this is static and requires releases to change. As our release cycle is typically months, this wont work as we need to change routing every time we get a new affiliate or want to exclude a specific country for example. We cant use OSGI config parameters as access to these is removed in cloud version. We need to build some custom code and some UI in the CMS or external system to manage it.

The other big requirement is the ability to route based on country (if the ip).  with EpiServer we did this based on the coutry header Cloufflare adds to every request, and is not bypassable.  For AEM, we are not sure how we can do this yet, possibly we need to import a static ID database, or call a 3rd party such as neustar.