Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sridharjayakumar/e5367c9154a4bda7f527af5ac4acb16c to your computer and use it in GitHub Desktop.
Save sridharjayakumar/e5367c9154a4bda7f527af5ac4acb16c to your computer and use it in GitHub Desktop.
Get the HTML markup for a resource in AEM / CQ.
package com.nateyolles.aem;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.apache.sling.engine.SlingRequestProcessor;
import com.day.cq.contentsync.handler.util.RequestResponseFactory;
import com.day.cq.wcm.api.WCMMode;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@SlingServlet(paths={"/bin/foo"})
public class AemResourceResolutionServlet extends SlingSafeMethodsServlet {
/** Service to create HTTP Servlet requests and responses */
@Reference
private RequestResponseFactory requestResponseFactory;
/** Service to process requests through Sling */
@Reference
private SlingRequestProcessor requestProcessor;
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/plain");
// This path works Just Fine - since the static.css is in the JCR
// String path = "/etc/designs/geometrixx/static.css";
// This path doesn't
String path = "/etc/designs/geometrixx/clientlibs.css";
/* Setup request */
HttpServletRequest req = requestResponseFactory.createRequest("GET", requestPath);
WCMMode.DISABLED.toRequest(req);
/* Setup response */
ByteArrayOutputStream out = new ByteArrayOutputStream();
HttpServletResponse resp = requestResponseFactory.createResponse(out);
/* Process request through Sling */
requestProcessor.processRequest(req, resp, request.getResourceResolver());
String html = new String(out.toByteArray(), "UTF-8");
response.getWriter().println(html);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment