Code Snippets
To get list of selectors in an array
String[] selectors = slingRequest.getRequestPathInfo().getSelectors();To get a page from a path
Page page = pageManager.getPage(path);To get any containing page for a resource
Page page = pageManager.getContainingPage(resourceResolver.getResource(path));To get a session in JSP
final SlingRepository repos = sling.getService(SlingRepository.class);session = repos.loginAdministrative(null);
To get query object and run a query
build query using search in crx explorer or crxdeliteString stmt = "select * from cq:Page where jcr:path like '/content/training/%' and contains(*, '" + slingRequest.getParameter("q") + "') order by jcr:score desc";
Query query = currentNode.getSession().getWorkspace().getQueryManager().createQuery(stmt, Query.SQL);
QueryResult results = query.execute();
Get node from path
Node n = session.getItem(path); // where session = JCR sessionsession.save();
resourceResolver.commit();
session.logout();
n.setProperty(“prop-name”,”value”);
EventAdmin eventAdmin
public void sendEventTo(){
org.osgi.service.event.Event event = new Event(EventTopic, <object>);
eventAdmin.sendEvent(event);
}
SlingRepository repository;
session.logout();
Add a property to node
Node n = parent.addNode(…);n.setProperty(“prop-name”,”value”);
Java Code
Creating a logger instance
private static final Logger LOGGER = LoggerFactory.getLogger(ReplicationLogger.class);Sending an Event
@ReferenceEventAdmin eventAdmin
public void sendEventTo(){
org.osgi.service.event.Event event = new Event(EventTopic, <object>);
eventAdmin.sendEvent(event);
}
To get page manager
final PageManager pm = resourceResolver.adaptTo(PageManager.class);To get repository
@ReferenceSlingRepository repository;
Nhận xét
Đăng nhận xét