Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.

intercepting the page request in AEM-6.4

Avatar

Level 4

Dear All,

Can you please help me to understand the intercept concept in AEM (Do we have any Adobe help document??)

One of the my project requirement is intercepting the all page request under the path http://localhost:4502/editor.html/content/we-retail/104139ToshibaAmericaInc401SavingsPlanFeatures/03...

http://localhost:4502/sites.html/content/we-retail/104139ToshibaAmericaInc401SavingsPlanFeatures

For example I have below 2 pages under the above path.

http://localhost:4502/content/we-retail/104139ToshibaAmericaInc401SavingsPlanFeatures/0303TAMS-MRISA...

http://localhost:4502/content/we-retail/104139ToshibaAmericaInc401SavingsPlanFeatures/0202TAMSTUSTIN...

Can anybody please help me on this ?

9 Replies

Avatar

Employee Advisor

What do you mean with "intercept"? You can register a request filter (standard servlet API) and then filter according to all your requirements.

Or are you looking for something different?

Jörg

Avatar

Level 4

Dear Joerg,

Thanks for your help.

Actually My requirement is below.

Intercept HTTP request from thirdparty  (endpoint is abc-services/jax-rs/participant-service/secure/participant/search) to AEM 

Avatar

Employee Advisor

If it's a HTTP request coming into AEM, you can also use a servlet filter. Then you have the raw request object, but not a structured object (for the payload of this request) yet.

When you mention that path, is it the thirdparty system or a path living in AEM (and using JAX-RS in AEM)?

Jörg

Avatar

Level 4

Hi Joerg,

Thanks a lot for your help and appreciated.

Currently the path is living in AEM and now I have written the below logic for my below requirement using sling filter.

Now my requirement is that when somebody will hit the PlanA (/content/trainingproject/Plan/PlanA) then it will redirect to SunitaFirst.html ( http://localhost:4502/content/trainingproject/Plan/PlanA/Location1/SunitaFirst.html ).

1793339_pastedImage_0.png

I have written the below code for this. But when I am trying to redirect then I am getting below error.

@SlingFilter(label = "Sample Filter", description = "Sample Description", metatype = true, generateComponent = true, // True if you want to leverage activate/deactivate

generateService = true, order = 0, // The smaller the number, the earlier in the Filter chain (can go negative);

scope = SlingFilterScope.REQUEST) // REQUEST, INCLUDE, FORWARD, ERROR, COMPONENT (REQUEST, INCLUDE, COMPONENT)

public class CustomRedirectFilter implements Filter {

Logger log = LoggerFactory.getLogger(this.getClass());

@Override

public void init(FilterConfig filterConfig) throws ServletException {

// Usually, do nothing

}

@Override

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

if (!(request instanceof SlingHttpServletRequest) || !(response instanceof SlingHttpServletResponse)) {

// Not a SlingHttpServletRequest/Response, so ignore.

chain.doFilter(request, response); // This line would let you proceed to the rest of the filters.

return;

}

final SlingHttpServletResponse slingResponse = (SlingHttpServletResponse) response;

final SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) request;

final Resource resource = slingRequest.getResource();

log.info("resource is ----------- ====**** " + resource);

//String rootContentPath="/content/trainingproject/Plan/PlanA";

if (resource.getPath().startsWith("/content/trainingproject/Plan/PlanA")) {

// Is the SlingFilterScope is REQUEST, redirects can be issued.

// Write your custom code here.

slingResponse.sendRedirect("/Location1/SunitaFirst.html");

return;

}

// to proceed with the rest of the Filter chain

chain.doFilter(request, response);

}

@Override

public void destroy() {

// Usually, do Nothing

}

}

ERROR IS BELOW

1793340_pastedImage_3.png

Avatar

Employee Advisor

You are hitting a non-existing resource (the resource "/Location1/SunitaFirst" does not exist). But if you have requested "/content/trainingproject/Plan/PlanA" and got redirected here, your filter is working.

Avatar

Level 4

Hi Joerg,

thanks for your help. Sorry for late reply. Now redirect is working fine.

But now , When I am running my page with the selectors , it is working fine , as shown below.

1797301_pastedImage_2.png

When I am not giving selectors then my page is not working fine , as shown below.

1797299_pastedImage_0.png

1797305_pastedImage_0.png

My code is below. Am I missing something "or" this is the expected behavior that in author it will not work and in publisher it will work.

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.felix.scr.annotations.sling.SlingFilter;
import org.apache.felix.scr.annotations.sling.SlingFilterScope;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;

// http://localhost:4502/system/console/status-slingfilter
//  http://localhost:4502/content/workplace/vignettemigrationcontent/plan/plan-104105/location-9999/Firs...
// http://localhost:4502/content/workplace/vignettemigrationcontent/plan/plan-104139/location-0101/Seco...

@SlingFilter(order = -701, scope = SlingFilterScope.REQUEST)

public class PlanLocationURLParamsFilter implements Filter {

Logger log = LoggerFactory.getLogger(this.getClass());
String planIDinJCR1 = "104105";
String locationIDinJCR1 = "9999";
String planIDinJCR2 = "104139";
String locationIDinJCR2 = "0101";
String[] arrSplit;
int i;

@Override
public void init(FilterConfig filterConfig) throws ServletException {
  // Usually, do nothing
}

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
   throws IOException, ServletException {
  if (!(request instanceof SlingHttpServletRequest) || !(response instanceof SlingHttpServletResponse)) {
   // Proceed with the rest of the Filter chain
   chain.doFilter(request, response);
   return;
  }

  final SlingHttpServletResponse slingResponse = (SlingHttpServletResponse) response;
  final SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) request;
  log.info("request for {}, with selector {}", slingRequest.getRequestPathInfo().getResourcePath(),
    slingRequest.getRequestPathInfo().getSelectorString());
  String multiselector = slingRequest.getRequestPathInfo().getSelectorString();
  log.info("MULTISELECTOR is ----------- ====**** " + multiselector);
  final Resource resource = slingRequest.getResource();
  log.info("resource is ----------- ====**** " + resource);

  if (resource.getPath().startsWith("/content/workplace/vignettemigrationcontent/plan")) {

   PageManager pageManager = resource.getResourceResolver().adaptTo(PageManager.class);
   Page page = pageManager.getPage(resource.getPath());
   String templatePath = page.getContentResource().getValueMap().get("cq:template").toString();
   if (templatePath.equals("/apps/workplace-fdi/templates/migrationtemplateHTL")) {
    arrSplit = multiselector.split(" ");
    for (i = 0; i < arrSplit.length; i++) {
     log.info("multiselector INSIDE FOR STATEMENT is ====**** " + arrSplit[i]);
    }
    slingResponse.setHeader("Content-Type", "text/html");
    if (multiselector.contains(planIDinJCR1)) {
     // log.info("multiselector is ----------- ====**** " + planIDinJCR1 );
     slingResponse.getWriter().println("<h1>PlanID is " + planIDinJCR1 + "</h1>");
    }
    if (multiselector.contains(planIDinJCR2)) {
     slingResponse.getWriter().println("<h1>PlanID is " + planIDinJCR2 + "</h1>");
    }
    if (multiselector.contains(locationIDinJCR1)) {
     slingResponse.getWriter().println("<h1>LocationID is " + locationIDinJCR1 + "</h1>");
    }

    if (multiselector.contains(locationIDinJCR2)) {
     slingResponse.getWriter().println("<h1>LocationID is " + locationIDinJCR2 + "</h1>");
    }   
    return;
   }
  }
  chain.doFilter(request, response);
}

@Override
public void destroy() {
  // Usually, do Nothing
}

}

Please suggest.

Avatar

Employee Advisor

You have a NullPointer in your code. Unfortunately the screenshot does not indicate the exact line, and also your code misses the line numbers. But it should not be to hard to spot.

Jörg

Avatar

Level 4

Hi jorg,

I have put my contents and code in the my github branch.

GitHub - sunita197881/sunitaworkplace at Intercept-AEM-page

Getting null pointer exception for the below line 

String templatePath =  page.getContentResource().getValueMap().get("cq:template").toString();

But I am not able to figure out why it is coming null pointer.

Avatar

Community Advisor

The easiest way, please debug the code by breaking into multiple statements and check if the value exist in valuemap or not.

Resource contentRes =  page.getContentResource();

ValueMap map =  contentRes.getValueMap();

String templatePath =  map.get("cq:template").toString();

Put the null check and add debug statements or simply debug the code while hitting the servlet and check values. You'll get to know why null pointer!