Rendering out a page from an OSGI Bundle
Hi,
I've run into an issue while trying to create a custom OSGI bundle that solves the following problem:
We have a list of several thousand keys in a CSV file that need to point to specific pages, and then render the content of that page.
Conceptually, I believe I understand how I'm supposed to do it:
- The OSGI bundle runs in the background, available for a *.JSP to reference.
- The bundle loads the CSV*, parsing the key->value from the JSP that was referenced. (eg. 54, /path/to/the/requested/resource)
- The bundle then renders the resource out as part of the request.
* I know the second step needs refinement (it should be loaded after the server loads, and remain in memory until it's shut down; we don't want to thrash the HDD and memory is much faster but for now this is mainly for a proof of concept.)
I should add that I'm pretty lousy when it comes to Java programming but using various tutorials I've managed to return a string so far. From that I've then tried to resolve the resource inside of the Implementation class for the bundle.
Code as follows (var names changed):
ExampleService.java:
package com.adobe.cq; public interface ExampleService { public void csvLocation(String val); public void setKey(String val); public void main(); }ExampleServiceImpl.java:
package com.adobe.cq; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; // Required Felix files import org.apache.felix.scr.annotations.Component; import org.apache.felix.scr.annotations.Reference; import org.apache.felix.scr.annotations.Service; // Required Sling Files import org.apache.sling.api.resource.ResourceResolver; import org.apache.sling.api.resource.ResourceResolverFactory; import org.apache.sling.api.resource.LoginException; // Required CQ Files import com.day.cq.wcm.api.Page; import com.day.cq.wcm.api.PageManager; // @Component // @Service public class ExampleServiceImpl implements ExampleService { private String csvLocation = "C:\\AdobeCQ\\LargeEntry.csv"; private String key = "0"; private ResourceResolverFactory resolverFactory; public void setKey(String val) { this.key = val; } public void csvLocation(String val) { this.csvLocation = val; } public void main() { BufferedReader br = null; String pageUrl = null; // // This part loads the CSV file // try { String sCurrentLine; br = new BufferedReader(new FileReader(csvLocation)); while ((sCurrentLine = br.readLine()) != null) { String[] split = sCurrentLine.split(","); if (split[0].equals(key)) { pageUrl = (split[1]).toString(); } } } catch (IOException e) { e.printStackTrace(); } finally { try { if (br != null) { br.close(); } } catch (IOException ex) { ex.printStackTrace(); } } // // Page load request // try { ResourceResolver resourceResolver = resolverFactory.getAdministrativeResourceResolver(null); PageManager pageManager = resourceResolver.adaptTo(PageManager.class); // perform actions here Page page = pageManager.getPage(pageUrl); // Right here: // This is where the issue exists-- // what would you do here to render the // contents of a retrieved page out to // the browser? resourceResolver.close(); } catch (LoginException e) { e.printStackTrace(); } } }Please, please help me here. I've been tearing my hair out trying to figure out how to output a rendered page after retrieving a resource. What am I missing? What am I doing wrong here?
Thanks for any assistance.