Created
October 9, 2015 04:29
-
-
Save nateyolles/6302cfb1b21c27e59cfe to your computer and use it in GitHub Desktop.
Get the HTML markup for a resource in Apache Sling.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.nateyolles.sling; | |
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 javax.servlet.ServletException; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import java.io.IOException; | |
@SlingServlet(paths={"/bin/foo"}) | |
public class SlingResourceResolutionServlet extends SlingSafeMethodsServlet { | |
/** Service to process requests through Sling */ | |
@Reference | |
private SlingRequestProcessor requestProcessor; | |
@Override | |
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException { | |
/* The resource path to resolve. Use any selectors or extension. */ | |
String requestPath = "/content/myapp/us/en/index/jcr:content/myparsys/mycomponent_f93d.html"; | |
/* | |
* Create a fake request and fake response. The response adds a method to make the HTML accessible. | |
* You need the following three files from Apache Sling: | |
* | |
* https://github.com/apache/sling/blob/trunk/testing/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/HttpRequest.java | |
* https://github.com/apache/sling/blob/trunk/testing/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/HttpResponse.java | |
* https://github.com/apache/sling/blob/trunk/testing/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/TestServletOutputStream.java | |
*/ | |
final HttpRequest req = new HttpRequest(requestPath); | |
final HttpResponse resp = new HttpResponse(); | |
/* Process request through Sling */ | |
requestProcessor.processRequest(req, resp, request.getResourceResolver()); | |
String html = resp.getContent(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment