AEM Cloud Service - Accessing Geo Location Headers in Publish Instance


Geolocation headers added by Fastly CDN are sent in requests to Dispatcher. Well documented here, this post is just to show how to test it with local dispatcher SDK and demo on CS Publish..


1) Add a servlet to output the headers received by Publish 

package apps.eaem.sites.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 javax.servlet.Servlet;
import javax.servlet.ServletException;
import java.io.IOException;
import java.util.Enumeration;

@Component(
name = "Experience AEM Dump Request headers",
immediate = true,
service = Servlet.class,
property = {
"sling.servlet.methods=GET",
"sling.servlet.paths=/bin/eaem/headers"
}
)
public class DumpHeaders extends SlingAllMethodsServlet {
@Override
protected final void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws
ServletException, IOException {
try {
Enumeration<String> headers = request.getHeaderNames();

while(headers.hasMoreElements()){
String name = headers.nextElement();
response.getWriter().println(name + "=" + request.getHeader(name));
}
} catch (Exception e) {
response.setStatus(SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
}
}


2) Add "x-aem-client-country" and "x-aem-client-continent" in dispatcher\src\conf.dispatcher.d\clientheaders\clientheaders.any

#
# This file contains the request headers, and can be customized.
#
# By default, it includes the default client headers.
#

$include "./default_clientheaders.any"

"x-aem-client-country"
"x-aem-client-continent"


3) Add the following entry in dispatcher\src\conf.dispatcher.d\filters\filters.any to allow requests to the servlet

/0104 { /type "allow" /method "GET" /url "/bin/eaem/headers" }


4) Use PostMan for adding the headers manually, test if local Dispatcher SDK configuration is correct and passes the headers to Local Publish (on CS, Fastly CDN adds these headers to requests sent to dispatcher)


5) Running a CS build you should see the geo location headers added by Fastly to requests, output by the servlet...



No comments:

Post a Comment