Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package blog.techrevel.servlet;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;
import javax.jcr.query.Query;
import javax.jcr.query.QueryManager;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import org.apache.jackrabbit.jcr2dav.Jcr2davRepositoryFactory;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(service = Servlet.class, property = { "sling.servlet.paths=/bin/testRemoteConnection",
"sling.servlet.methods=GET" })
public class RemoteConnectionServlet extends SlingSafeMethodsServlet {
private static final Logger LOG = LoggerFactory.getLogger(RemoteConnectionServlet.class);
@Override
protected void doGet(final SlingHttpServletRequest req, final SlingHttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html");
try {
//Connect to repository
Jcr2davRepositoryFactory repoFactory = new Jcr2davRepositoryFactory();
Map<String, String> params = new HashMap<String, String>();
params.put("org.apache.jackrabbit.repository.uri", "http://localhost:4502/crx/server");
Repository repository = repoFactory.getRepository(params);
Session session = repository.login(new SimpleCredentials("admin", "admin".toCharArray()), "crx.default");
//Execute Query
QueryManager qm = session.getWorkspace().getQueryManager();
String stmt = "select * from [cq:Component] where isdescendantnode('/apps')";
Query q = qm.createQuery(stmt, Query.JCR_SQL2);
NodeIterator results = q.execute().getNodes();
while (results.hasNext()) {
Node node = (Node) results.next();
resp.getWriter().write("<br />" + node.getPath());
}
session.logout();
} catch (RepositoryException e) {
LOG.error("Exception while connecting to AEM", e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.