Demystifying the Fear of AEM Dispatcher — Part 2
Understanding the /clientheaders section
If you have noticed the farm file under available_farms folder, the first section that we have is the /clientheaders section. The /clientheaders section specifies which HTTP headers from the client request (Dispatcher) are allowed to be forwarded to the AEM publish instance. Properly configuring this section is crucial for effective caching and request handling.
Pre-Requisite (Optional)
To follow along the steps mentioned in this article, I highly recommend to setup AEM dispatcher by going through:
Practical Example Walkthrough
Let’s walk through adding a custom header to your local AEM Dispatcher Setup.
Adding the custom header in ‘clientheaders.any’ file
- Navigate to the folder ‘src/conf.dispatcher.d/clientheaders’ and open ‘clientheaders.any’ file, you will see inclusion of another file ‘default_clientheaders.any’. Open ‘default_clientheaders.any’ and you will see a list of default headers that are allowed by AEM. Since, it is an immutable file, we cannot edit it.
- As specified in the ‘default_clientheaders.any’ file, if we need to add our custom header we are allowed to modify clientheader.any file. So, lets go ahead and add a header as below:
- Save the above changes and restart AEM dispatcher server.
How to verify your change
There are 2 ways in which you can verify the above change
Method 1: Using AEM logs
- Change the log level to ‘Debug’ for ‘request.log’ file from http://localhost:4503/system/console/slinglog
- Run the curl command in CLI: curl -I -H “X-custom-header: TestValue” http://localhost:8080/content/wknd/us/en.html
- Check the request.log file, you should find this header in the logs.
However, I wasn’t able to verify using the above method as I faced some issues. Even though I changed the log level to ‘Debug’, I wasn’t able to see any DEBUG logs in the request.log file. Therefore, I followed Method 2. Please comment below if you are able to verify using the logs method :)
Method 2: Using a Servlet
- Create a servlet in your repo as below:
package com.adobe.aem.guides.wknd.core.servlets;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.propertytypes.ServiceDescription;
import javax.servlet.Servlet;
import java.io.IOException;
import java.util.Enumeration;
import static org.apache.sling.api.servlets.ServletResolverConstants.SLING_SERVLET_PATHS;
@Component(
service = { Servlet.class },
property = {SLING_SERVLET_PATHS + "=/bin/headers"
}
)
@ServiceDescription("Get Client Headers")
public class GetClientHeaders extends SlingAllMethodsServlet {
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
response.setContentType("text/plain");
Enumeration<String> headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
String headerName = headerNames.nextElement();
String headerValue = request.getHeader(headerName);
response.getWriter().println(headerName + ": " + headerValue);
}
}
}
- Since, by default the servlets are not allowed in the dispatcher configuration, if you try to call the servlet using URL http://localhost:8080/bin/headers, you will get 404 error. To fix this, we need to allow it in the filters
- Navigate to folder ‘src/conf.dispatcher.d/filters’, open filters.any file and allow the servlet, note that the number /0064 should be a unique number. Save it and restart the dispatcher server.
- The final step would be to call this servlet using dispatcher url by running a curl command (curl -H “X-custom-header: TestValue” http://localhost:8080/bin/header) or Postman. You should be able to see the custom header that we added to the dispatcher in the output.
- Now, Lets try passing a random header that is not added to the client headers list of dispatcher, you will not see its value displayed in the output as dispatcher is not allowed to do so. For example: I have added a new header ‘X-custom-header-2’ in the below postman call, it is not displayed in the output because it wasn’t passed to AEM publisher.