10 March 2011

RAP + BIRT on Websphere

I used the blog from Tim Pietrusky to get started: http://blog.tim-pietrusky.de/2009/rich-ajax-platform-rap/integrating-birt-into-rap-applications/

Perhaps I did something wrong but all the image links in the resulting HTML were broken. The URL to my dynamically-generated images was invalid. Even the examples from the RAP wiki showed the same problem.

In the end I decided to store my images under /rwt-resources/birt as I noticed that all the images from RWT are loaded from this address. I also changed the URL to use a context-relative address and not a fully-defined URL with host and port. On Websphere the result was an IP address and the port of the AppServer which was not accessible directly from the client.

I set the resources extension point as follows:

<extension point="org.eclipse.equinox.http.registry.resources">
  <resource
    alias="/resources/birt"
    base-name="/rwt-resources/birt">
  </resource>
</extension>


The modified View part looks like this (sorry about the formatting):


public class ChartView extends ViewPart {
public static final String ID = "com.acme.fair.views.ChartView";

private static final String reportName = "simplechart.rptdesign";
private static final String reportDirLocal = "/resources/birt/";
private static final String reportDirServer = "/rwt-resources/birt/";

private Browser browser;
private String reportDesign; // Full path to the local BIRT report file
private String URL;

private IReportEngine engine = null;
private EngineConfig config = null;
private IReportRunnable design = null;
private IRunAndRenderTask task;

@SuppressWarnings("restriction")
public void createPartControl(Composite parent) {
this.setPartName("BIRT in RAP");
browser = new Browser(parent, SWT.NONE);
configureURL();

try {
config = new EngineConfig();
IReportEngineFactory factory = (IReportEngineFactory) Platform
.createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);
engine = factory.createReportEngine(config);

InputStream fs = null;
try {
URL url = Activator.getDefault().getBundle().getResource("/resources/birt/simplechart.rptdesign");
fs = url.openStream();
} catch (MalformedURLException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}

design = engine.openReportDesign(fs);

// Create task to run and render the report
task = engine.createRunAndRenderTask(design);

// Set the dataset for the chart as report parameter
task.setParameterValue("CHART_DATASET", MesseSql.getFairsPerYear());

// Render the report to HTML
HTMLRenderOption renderOptions = new HTMLRenderOption();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
renderOptions.setOutputFormat(HTMLRenderOption.HTML);
renderOptions.setOutputStream(outputStream);

// Get the path to the local context directory to save the rendered
// chart image
String webAppBase = (ContextProvider.getWebAppBase()).replaceAll("\\\\", "/");
String path = webAppBase + reportDirServer + RWT.getSessionStore().getId();

// Handle the image path
renderOptions.setImageHandler(new HTMLServerImageHandler());
renderOptions.setImageDirectory(path);
renderOptions.setBaseImageURL(RWT.getRequest().getContextPath() + reportDirServer + RWT.getSessionStore().getId());

task.setRenderOption(renderOptions);
task.run();
task.close();

// Set the HTML output to the Browser widget
browser.setText(outputStream.toString());

engine.destroy();

} catch (Exception ex) {
ex.printStackTrace();
}
}

// Set the URL for the BIRT report for different environments.
private void configureURL() {

// Protocol, ip & port
ServletContext sc = RWT.getRequest().getSession().getServletContext();
String realPath = sc.getRealPath("/");
HttpServletRequest request = RWT.getRequest();
String protocol = "http://";
String ip = request.getLocalAddr();
int port = request.getLocalPort();
URL = protocol + ip + ":" + port;
String webAppName = "";

if (realPath == null) { // Start application locally within Eclipse
// (Windows)
reportDesign = URL + reportDirLocal + reportName;
} else { // Start application on a server (Apache Tomcat or so)
webAppName = sc.getContextPath();
reportDesign = URL + webAppName + reportDirLocal + reportName;
URL = URL + webAppName;
}
}

public void setFocus() {
}

/**
* Execute the given script, for example to print the

* content of the Browser
*
* @param script
* String to be executed
*/
public void executeScript(String script) {
browser.execute(script);
}
}

No comments:

Post a Comment