Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

how to create a sling servlet which works across sites (with the sites URL), and cross language?

Avatar

Level 9

We created some sling servlents following this tutorial: https://aem.redquark.org/2018/10/day-17-working-with-query-builder-api.html.  They make calls to a backend (player database), and pull in some content, and output json for the react page.  e.g. oursite.com/bin/api/oursevlet

 

This will work well for our first site (aka tenant?)

 

However, if we create a new site, with a new URL, we will want the same servlet, but with the new sites URL, e.g. ourothersite.com/bin/api/ourservlet, but this should pull in different content.

 

Presumably, we could duplicate the servlet, but it might also be possible to do a redirect or similar behind the scenes?  in this case, how would the servlet know which sites content to lookup?  We could pass in the URL as a parameter, assuming a servlet in one tenant can access content from another.

 

To a lesser extent, would translations be an issue, as the servlet would need to know which language content to look for?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

I would use the same servlet for easy maintenance (even though you can have a redirect to a different servlet at CDN or dispatcher level)

 

  1. Same servlet and same query logic based on path (should pass in as a request param)
  2. If you are maintaining multiple sites then you've different farms tied to farm specific rewrites. Therefore, you just need to intercept this servlet request and append the path query param to the servlet
  3. This way, you've one servlet serving to both/more sites and search would be contained to those sites vs global

 

Let me know if you've any questions.

 

Thanks,

Singaiah

View solution in original post

8 Replies

Avatar

Level 5

You can get the URL from the request.  You could map those URLs to a property (OSGI configuration for the Servlet) that you can use to determine what data to send back or for processing down a different path. Translations would have to be done regardless - request would have to have something to let the servlet know.  You could put the translations into the Database or consider swapping out the database for certain content and use content fragments which are easily translatable. You would need to also allow the path in your dispatcher rules for that domain.

Avatar

Administrator
@sdouglasmc, thank you for sharing the solution with AEM community. Keep it up. Looking forward for more such contributions from you.


Kautuk Sahni

Avatar

Correct answer by
Community Advisor

I would use the same servlet for easy maintenance (even though you can have a redirect to a different servlet at CDN or dispatcher level)

 

  1. Same servlet and same query logic based on path (should pass in as a request param)
  2. If you are maintaining multiple sites then you've different farms tied to farm specific rewrites. Therefore, you just need to intercept this servlet request and append the path query param to the servlet
  3. This way, you've one servlet serving to both/more sites and search would be contained to those sites vs global

 

Let me know if you've any questions.

 

Thanks,

Singaiah

Avatar

Level 9
Hi, thanks for the reply. The only issue with using the same servlet, is the domain will be wrong. E.g. i I host the serviet on mydomain.com/bin/servlet, then on site myotherdomain.com, it will be pointing to a different domain. IT will still work (assuming cors etc is setup), but doesnt look great, and might be blocked by ad-blockers.

Avatar

Community Advisor
I was assuming both of these sites are hosted on same AEM server(s). If yes then you/IT should be able to allow to grab the resources on these domains (no CORS) at the Apache level.

Avatar

Community Advisor
Allowing CORS for their own internal/external domains is common practice

Avatar

Level 9
Yes, both these sites are hosted on the same aem cloud instance. But as they are different brands, we cant have one brand calling urls from another. So in this case we would need to setup a 3rd site just to host this api, with a neutral name. This is not ideal, so may be duplicating the API code for each site, or some sort of internal redirect, might be better.

Avatar

Community Advisor
I don't know your specific requirement and this is just one way of doing it and of course you can skin this many different ways. If you can't allow this then duplicating would be better. But allowing only specific paths for API calls are common.