Coding

Include external resource in AEM

Sometimes, when you are involved in AEM projects, it could come up a requirement that you will need to load some content from an external resource provided by the client in a component to be rendered in pages using some javascript logic.

In this post, we are going to show you the best solutions for avoiding cross site issues retrieving an external resource to be used on a HTML in AEM.  The example which we will use it’s based on the following statement: “You want to use an external XML for getting the necesary information and put it in a page.“, but it would be use for other cases.

So let’s see some options you have to achieve your goal.

DEVELOPMENT OF A QUARTZ JOB

Preparing a job on AEM which will be in charge of downloading an external resource file, put it in AEM and activate that resource for being updated on publish environment. Normally, the best choice for saving on AEM a resource is on DAM.  Check below a sample code for this job:

As you can see, it was defined some configuration properties you can set in System Console:

CONFIGURATION OF WEBSERVER

Instead of developing a job in AEM, it could be possible to do something much simpler if there is no requirement to process the external resource (you need only to show it). This option is to configure a webserver rule mapping a URL in your domain with the external one.

Check below a sample rewrite rule to be added for apache for this purpose:

With this rule, when a client is requesting /myfolder/ from your server, it will request http://other.example.com/anotherfolder/ and send the response from that server back to the client. The options inside [] means:

  • P(proxy): Force the substitution URL to be internally sent as a proxy request.
  • NE(noescape): Prevent mod_rewrite from applying hexcode escaping of special characters in the result of the rewrite.
  • L(last): Stop the rewriting process immediately and don’t apply any more rules.

In the following schema, we are going to show how it works the rewrite rule:

CONCLUSIONS

With both options, you are reducing a lot the requests on your AEM instances compared to developing a proxy servlet, that could maybe the first option in your mind.

First option could be more indicated if you need to elaborate data read from the external source (so you do not have to re-elaborate at every request, but could not be appropriate if the external resource is changing very often.

Second option could be your best choice if you just want to display the external resource as it is (no elaboration needed) or the external data is changing so often that you caching in a local resource, would result in displaying not updated data.

AUTHORS

Alessandro Paolinelli, Javier Reyes

Do you want more information?

You Might Also Like

1 Comment

  • Reply
    Ashish
    March 12, 2017 at 6:15 pm

    Nice blog post.
    In my view an easier solution would be to leverage AEM’s importer framework. Authors can easily add or edit configurations (/etc/polling/importers), unlike Felix console which is only available to super/admin users.
    I believe some of AEM’s features (Sitecatalyst and other integrations) rely on this framework.

Leave a Reply