Expand my Community achievements bar.

Enhance your AEM Assets & Boost Your Development: [AEM Gems | June 19, 2024] Improving the Developer Experience with New APIs and Events

AEM 6550 - Stream Binary or Text Content of Provided URL thru AEM for bypassing CORS Issues | AEM Community Blog Seeding




AEM 6550 - Stream Binary or Text Content of Provided URL thru AEM for bypassing CORS Issues by Sreekanth Choudry Nalabotu


If you are encountering CORS (Access-Control-Allow-Origin) issues when making client side ajax calls to another server (which does not provide Access-Control-Allow-Origin header in response) from browser, a quick alternative is to proxy the URL from AEM... the following post proxies images or xml content (useful when accessing Scene7 Render Server images...)

Add a servlet apps.experienceaem.assets.ProxyContentServlet to read the url, format and proxy the response through AEM...

package apps.experienceaem.assets;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.fluent.Request;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.osgi.services.HttpClientBuilderFactory;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import java.io.*;

name = "Experience AEM Image Proxy Servlet",
immediate = true,
service = Servlet.class,
property = {
public class ProxyContentServlet extends SlingAllMethodsServlet{
private static final Logger log = LoggerFactory.getLogger(ProxyContentServlet.class);

private static String IMAGE = "IMAGE";
private static String XML = "XML";

private HttpClientBuilderFactory httpClientBuilderFactory;

private CloseableHttpClient httpClient;

protected void activate(ComponentContext ctx) {
HttpClientBuilder builder = httpClientBuilderFactory.newBuilder();

RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30000)


httpClient = builder.build();

protected final void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws
ServletException, IOException {
try {
String url = request.getParameter("url");
String fileName = request.getParameter("fileName");
String format = request.getParameter("format");

throw new Exception("Empty url");

format = IMAGE;

streamImage(response, url, fileName);
}else if(format.equalsIgnoreCase(XML)){
streamXML(response, url);
} catch (Exception e) {
log.error("Could not getting binary response", e);

private void streamImage(SlingHttpServletResponse response, String url, String fileName) throws Exception{
fileName = "eaem.jpg";

response.setHeader("Content-disposition","attachment; filename=" + fileName);

writeContent(response, url);

private void streamXML(SlingHttpServletResponse response, String url) throws Exception{

writeContent(response, url);

private void writeContent(SlingHttpServletResponse response, String url) throws Exception{
byte[] image = Request.Get(url).execute().returnContent().asBytes();

InputStream in = new ByteArrayInputStream(image);

OutputStream out = response.getOutputStream();

IOUtils.copy(in, out);



Read Full Blog

AEM 6550 - Stream Binary or Text Content of Provided URL thru AEM for bypassing CORS Issues


Please use this thread to ask the related questions.

Kautuk Sahni

Topics help categorize Community content and increase your ability to discover relevant content.

0 Replies