Integrate BIRT in existing webapp - java

I would like to add the BIRT reporting engine to an existing webapp in Tomcat. I don't need the BIRT viewer, I really only want to be able to run the reports from a url like http://localhost:8080/birt/output?__report=test.rptdesign&sample=my+parameter and use the different export options pdf, xls, doc, html.
The integration guides I've found so far all include the viewer and writing my own servlets to handle different formats.
I was hoping someone knew simply which servlet mappings from the report-engine web.xml file I needed and which jars I would need to include from the lib directory for this barebones BIRT implementation in existing webapp.

I was hoping someone knew simply which servlet mappings from the
report-engine web.xml file I needed and which jars I would need to
include from the lib directory for this barebones BIRT implementation
in existing webapp.
I didn't necessarily want to write my own servlet I just wanted to integrate the existing reporting runtime from its own standalone webapp (here under the "runtime" button) into my existing webapp, so that I don't have to distribute 2 webapps to support running BIRT reports. Sorry if that wasn't clearer.
I did work this out though, in the simplest possible fashion, in case anyone has a similar question (using BIRT runtime 3.7.1):
All you need is the following servlet mapping added to your own webapp\web-inf\web.xml file:
<!-- Engine Servlet -->
<servlet>
<servlet-name>EngineServlet</servlet-name>
<servlet-class>org.eclipse.birt.report.servlet.BirtEngineServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>EngineServlet</servlet-name>
<url-pattern>/output</url-pattern>
</servlet-mapping>
Include all jars from the web-inf\lib directory of the runtime into your own webapp\web-inf\lib directory.
You can then run .rptdesign files using the output BIRT report url from your own webapp, and specifying whatever format you want, e.g.:
http://localhost:8080/myOwnWebapp/output?__report=test.rptdesign&__format=pdf
http://localhost:8080/myOwnWebapp/output?__report=test.rptdesign&__format=html
http://localhost:8080/myOwnWebapp/output?__report=test.rptdesign&__format=xls
http://localhost:8080/myOwnWebapp/output?__report=test.rptdesign&__format=doc
http://localhost:8080/myOwnWebapp/output?__report=test.rptdesign&__format=ppt

As i understand you, you are trying to generate a birt report form a servlet where you have the *.rptdesign in somewhere location.
Good, look at the following code
this.bundle = ResourceBundle.getBundle("com.tts.mersal.resources.MersalResources");
this.config = new EngineConfig();
this.config.setEngineHome(bundle.getString("BIRT_ENGINE_HOME"));
this.config.setLogConfig(bundle.getString("BIRT_LOGGING_FOLDER_PATH"), Level.ALL);
Platform.startup(config);
this.factory = (IReportEngineFactory)Platform.createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);
this.engine = factory.createReportEngine( config );
this.engine.changeLogLevel(Level.ALL);
ContentReader contentReader = Repository.getServiceRegistry(FacesContext.getCurrentInstance()).getFileFolderService().getReader(MersalOutboundReportDialogBean.this.dialogReportNode.getNodeRef());
IReportRunnable report = MersalOutboundReportDialogBean.this.getEngine().openReportDesign(contentReader.getContentInputStream());
ReportDesignHandle designHandle = (ReportDesignHandle)report.getDesignHandle();
OdaDataSource source = (OdaDataSource)designHandle.getModule().findDataSource(DATA_SOURCE_NAME);
source.setProperty(source.getPropertyDefn("FILELIST"), buildUrl((String)source.getProperty(designHandle.getModule(), "FILELIST")));
IRunAndRenderTask runAndRenderTask = MersalOutboundReportDialogBean.this.getEngine().createRunAndRenderTask(report);
HTMLRenderOption render = new HTMLRenderOption();
render.setOutputFileName("G:/Render.html");
render.setOutputFormat("html");
runAndRenderTask.setRenderOption(render);
runAndRenderTask.run();
runAndRenderTask.close();
As you can see the first thing you must prepare the birt engine and then get an instance of report from type IReportRunnable, so you can after that set the location of the output by using therender option which will be changed based on your request.
You have multiple chocies, HTMLRenderOption, PDFRenderOption and others.
I hope that will serve you.
Thanks.

Related

Spark Framework and relative path

I'm using the Spark framework to create a web app and I've been going through the tutorials on their site. At the moment, I'm working on their templates section, however I can't seem to get the project to recognize that my css and my templates are in my resources folder.
I'm using Netbeans and Maven to manage my dependencies.
Can anyone help me figure out how to set up my relative paths/create my project folders appropriately in this environment? I'm a newbie to both Maven and Spark, so go easy please.
Static Files:
If your resources directory looked like this:
resources
└───public
├───css
│ style.css
├───html
│ hello.html
└───templates
template.ftl
You could use staticFiles.location("/public"). This would make /public the root staticFiles directory.
You could then access hello.html like this: http://{host}:{port}/html/hello.html
If you wanted to use an external location on the filesystem, you could use staticFiles.externalLocation(...), which works pretty much the same way above.
Note: staticFiles.externalLocation(...) can be set to your project's resources directory, which means that the files will be automatically refreshed (useful for development)
A more in depth explanation can be found in the spark documentation
Configuring your template engine:
If you have already set the staticFiles location, but spark is still having trouble finding your templates, try this.
Note: These examples are for the FreeMarker engine, though they should apply to other engines with minor tweaking.
After looking through the examples, it seems that by default, a new FreemarkerEngine() looks for templates in spark/template/freemarker, and not your staticFiles location.
You have two options:
1: Move all of your templates to that directory
or
2: Configure your own engine, and pass it instead when defining routes
FreeMarkerEngine freemarker = new FreeMarkerEngine();
Configuration config = new Configuration();
config.setTemplateLoader(
new ClassTemplateLoader(YOUR_CLASS.class, "/templatedir"));
freemarker.setConfiguration(config);

Spring RESTful Service as a WAR instead of JAR in Tomcat

I am in the process of creating a REST web service in Java Spring. I've successfully loaded STS and the example detailed at :
"This guide walks you through the process of creating a "hello world" RESTful web service with Spring."
http://spring.io/guides/gs/rest-service/
However that tutorial only goes so far.. I want to create a WAR file instead of a self running jar containing a servlet, and deploy that WAR file. I then found this tutorial, and attempted to just modify the first tutorials build.gradle file.
"Converting a Spring Boot JAR Application to a WAR"
http://spring.io/guides/gs/convert-jar-to-war/
It seemed to build just fine into a .war file.. the service is running in my TOMCAT instance's manager.. but I get 404's once I attempt to use the service.
URL 404'd
http://localhost:8080/gs-rest-service-0.1.0/dbgreeting?name=MyName
Do I need to modify the mapping?
DataBaseController.java
#RequestMapping("/dbgreeting")
public #ResponseBody DataBaseGreeter dbgreeting(
#RequestParam(value="name", required=false, defaultValue="World") String name) {
return new DataBaseGreeter(counter.incrementAndGet(),String.format(template, name));
}
Now I have the .war file created according to a blending of things.. and worried I perhaps missed something.
I've since discovered XAMPP on OSX doesn't contain a webapp/ folder, which has forced me to load Bitnami's Tomcat stack instead. Do people generally switch between XAMPP and other stacks based on this? or did I miss something to get webapp folder created in XAMPP?
A WAR is just a JAR with special properites. It needs to have a WEB-INF, under which you need a web.xml to describe your deployment, any app server dependentXML configuration files, and usually a lib, classes, and other odds and ends.
The easiest way would be to use Maven to create your WAR. I think you should be able to simply change the project type in the pom.xml from JAR to WAR. The tutorial you followed seems to use Gradle, which in turn uses Maven I believe, so you should have one there somewhere. Other than that, google for tutorials on how to construct a WAR. I don't believe that Tomcat requires any special deployment descriptors, so you should only need the web
.xml.
(Answer from OP moved from question to here)
Boy I feel really dumb.. Found there was more to the tutorial after changing the gradle instructions.. including the very needed Auto Configuration that supercedes/replaces the need for a web.xml
Solution
Initialize the servlet
Previously, the application contained a public static void main() method which the spring-boot-gradle-plugin was configured to run when using the java -jar command.
By converting this into a WAR file with no XML files, you need a different signal to the servlet container on how to launch the application.
src/main/java/hello/HelloWebXml.java
package hello;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.SpringBootServletInitializer;
public class HelloWebXml extends SpringBootServletInitializer {
#Override
protected void configure(SpringApplicationBuilder application) {
application.sources(Application.class);
}
}
Will give credit to the first answer, but you both were correct that the web.xml (or what Spring-Boot uses to replace it) was needed.
I would expect to see some INFO output when a spring boot application starts so some ideas are:
Try a regular tomcat instance
Download and extract the zip distribution.
Start tomcat with bin/startup.sh
Copy your war to the webapps directory
Check the logs... hope to see some evidence of spring starting up
Manually inspect the war file
Unzip your war file
Expect to see WEB-INF/web.xml

How to create a folder in the same directory as the web application?

Let's say my web application's on development stage in Eclipse, at path C:\eclipse\workspace\myapp (Windows).
I intend to make it generate a folder at the same directory level (C:\eclipse\workspace\myappfiles) to store uploaded files (via new File()) in it.
However, at production stage, my web application will be at path /var/lib/tomcat7/webapps/myapp (Linux). It should then store files in /var/lib/tomcat7/webapps/myappfiles.
How can I detect those paths to write the folder and files?
I experimented with some java.io.File methods, but it seemed to not have a default method that suits me for the task.
EDIT :
I need the path like that so I could map the path for direct file access via URL.
As david99world says in his comment: This is a really bad idea. Plus, it might not even work on future versions of tomcat or on different configurations: Tomcat can be set up not to have "exploded" webapplications at all - e.g. run from zipped *.war files. Also, tomcat might not have write permissions to its own webapps directory (a basic precaution done to harden the system).
Imagine someone is uploading a file named "explorer.jsp" with a basic file explorer implemented in a jsp: Without taking care of it, tomcat will happily interpret this as code that needs to be executed server-side.
What you want to do is:
Find a upload directory, somewhere, and store your files there.
Write a download servlet that serves the files from that location.
You'll need a little bit more work this way, but the resulting installation is so much more secure out-of-the-box.
There are no standard ways to get the Tomcat path from Tomcat itself. You can try to find the working directory of the Tomcat user, but this may change based on where you start the server from. The safest way to do this is add the location of any files your application creates to your configuration, and then have your webapp check the config for this location.
One simple way and portable way to do this is to place the location of myappfiles in your web.xml:
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.example.mycompany.MyServlet</servlet-class>
<init-param>
<param-name>appfilesdir</param-name>
<param-value>/var/lib/tomcat7/webapps/myappfiles</param-value>
</init-param>
</servlet>
Then use GenericServlet.getInitParameter from within your servlet to read it and use it:
public class MyServlet extends HttpServlet
{
private String myAppFilesDir;
public void init(ServletConfig config)
throws ServletException
{
myAppFilesDir = getInitParameter("appfilesdir");
}
}
It should get the job done :
File currentDirectory = new File("");
String commonPath = currentDirectory.getAbsolutePath();
commonPath = commonPath.substring(path.lastIndexOf(File.separator) + 1);
File myFilesDir = new File(commonPath + "myappfiles");
myFilesDir.mkdirs();

Servlet/Tomcat relative file path

Where to put files in a Tomcat Servlet application, such that they are relatively visible to the page??
More detail:
I am developing a servlet page while using an external library. That library depends massively on external loaded XML files (relative file paths). So I need to put these XML files in the running directory of the servlet.
Is there a way in Tomcat server, where files can be accessible relatively?
When a web application is deployed to Tomcat, the root of the web application ends up being $CATALINA_HOME/webapps/YOUR_WEB_APP/
As such, if using a servlet to access an XML file located on a path within the root of your web application, you can just use the following:
request.getServletContext().getResourceAsStream("PATH/TO/YOUR/XML_FILE.xml")
This will load the XML file as an InputStream. Of course, if you want access to the file itself, you can always use the getResource(String resource) method to obtain a URL, from which a File object can be obtained like so (alternative methods included):
File f;
try {
f = new File(url.toURI());
} catch(URISyntaxException e) {
f = new File(url.getPath());
}
EDIT: To make them relatively visible to a web browser, simply keep them out of the ./WEB-INF and ./META-INF directories.
If this library you are talking about is going to search for the file on the classpath (like Hibernate or Log4J would do), you will have to put your XML file in WEB-INF. However, I suggest you do not do this manually. You can put the file in a source directory of you application, which will make sure the file gets deployed on the right spot.
This is an old question. I'm working on Tomcat 9. I've been quite successful with taking the Tomcat installation directory as the base. (CATALINA_HOME) The relative path to a file in ROOT for example is then, "webapps/ROOT/someDir/fileName"
The place to complain about repeated answers is to deal with repeated questions.

how to view JSP java file when using runjettyrun plugin

I am using Eclipse Indigo with runjettyrun plugin (latest). The webapp deploys successfully, but on non-trivial page I get a JSP custom tag compilation error.
I am getting the following JSP compilation error:
java.lang.IllegalStateException: STREAM
at org.eclipse.jetty.server.Response.getWriter(Response.java:683)
I want to look at the java file that got created from the Tag file. In the stacktrace, I can see that the Tag is being compiled to a file name:
xyz_Tag.java
I would like to view this java file. Any idea where I can find it?
you can instruct Jetty to keep generated java source files. There are many ways to configure this. Easiest way to do it is passing init parameter keepgenerated as true for org.apache.jasper.servlet.JspServlet in web.xml.
<init-param>
<param-name>keepgenerated</param-name>
<param-value>>true</param-value>
</init-param>
More details you can find in: http://wiki.eclipse.org/Jetty/Howto/Configure_JSP

Categories

Resources