Jasper server is a powerful server which is used to create a jasper report and to save the reports in server.
Which helps in compiling and generating the reports with huge data.
Mainly jasper server is used to store the reports.So, that we can clearly seperate reports and the application code.
Jasper server provides different ways to call a report that exists in the jasper server from a java program.
Jasper server by default provides a REST service to call/down load a report.
Below sample code explains how to call a report/down load a report that exists in jasper server.
Pre requisites:
I assume you have some knowledge on Jasper server and Jasper API.
public class OneMoreTry {
public static Document stringToDom(String xmlSource) throws SAXException, ParserConfigurationException, IOException {
ByteArrayInputStream stream = new ByteArrayInputStream(xmlSource.getBytes());
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
//StringReader sr = new StringReader(xmlSource);
//InputSource is = new InputSource(sr);
return builder.parse(stream);
}
public static String domToString( Document xml ) throws Exception{
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
StringWriter writer = new StringWriter();
transformer.transform(new DOMSource(xml), new StreamResult(writer));
return writer.getBuffer().toString();
}
public static void main(String[] args) throws Exception {
//URL on which jasper server is ruuning
String serverURL = "http://localhost:8080/jasperserver/";
//report name (in jasper sever we have some sample reports I am using some sample report for the example, below is the path to the report)
String reportName = "reports/samples/Department";
String reportFormat = "HTML";
//Use HTTP clinet,POST,PUT methods to consume REST service of the jasper server
HttpClient client = new HttpClient();
// Setting Login URL in a POST method
String loginURL = serverURL+"rest/login";
PostMethod postMethod = new PostMethod(loginURL);
// Set authentication parameters(To connect the jasper server through post methods)
postMethod.addParameter("j_username", "XXX");
postMethod.addParameter("j_password", "XXXX");
// Send POST with login request
int statusCode = client.executeMethod(postMethod);
// Check correct login process
if (statusCode != HttpStatus.SC_OK) {
System.out.println("Login failed: " + postMethod.getStatusLine());
return;
}
// Settting resource URL in a GET method to get descriptor
String resourceURL = serverURL+"rest/resource/"+reportName;
GetMethod getMethod = new GetMethod(resourceURL);
// Send GET request for descriptor
statusCode = client.executeMethod(getMethod);
// Check correct descriptor process
if (statusCode != HttpStatus.SC_OK) {
System.out.println("Descriptor failed: " + getMethod.getStatusLine());
}
// Get the response body as String
String descriptorSource = getMethod.getResponseBodyAsString();
//Transform descriptor from String into XML Document
Document descriptorXML = stringToDom(descriptorSource);
System.out.println("doc:" + descriptorXML.toString());
// Settting PUT method to run report, the url contains the RUN_OUTPUT_FORMAT parameter
String reportURL = serverURL+"rest/report/"+reportName+"?RUN_OUTPUT_FORMAT="+reportFormat;
PutMethod putMethod = new PutMethod(reportURL);
// Setting the request Body. The descriptor XML Document is transform to String and add to the request body.
putMethod.setRequestBody(domToString(descriptorXML));
// Send PUT request to execute the report.
statusCode = client.executeMethod(putMethod);
// Check correct report process
if (statusCode != 201) {
System.out.println("Report failed: " + putMethod.getStatusLine());
}
// Get the response body
String reportSource = putMethod.getResponseBodyAsString();
// Transform report information into XML Document.
Document reportXML = stringToDom(reportSource);
// Extrac from XML Document the report's UUID
NodeList nodes = reportXML.getElementsByTagName("uuid");
String reportUuid = nodes.item(0).getTextContent();
// Setting GET request to download the report
String reportFileURL = serverURL+"rest/report/"+reportUuid;//+"?file=report";
GetMethod getMethodFile = new GetMethod( reportFileURL );
//Send GET request to download the report
statusCode = client.executeMethod(getMethodFile);
// Check correct report process
if (statusCode != 200) {
System.out.println("Downlaod failed: " + putMethod.getStatusLine());
}
// Getting the report body
byte inbytes[] = new byte[1024];
InputStream is= getMethodFile.getResponseBodyAsStream();
is.read(inbytes);
String in = new String(inbytes, "cp1252");
DataOutputStream out = new DataOutputStream(new FileOutputStream("d:/Myjars/hai.pdf"));
out.writeUTF(in);
//File file = new File("d:/Myjars/hai.pdf");
//bis.close();
}
}
Hope this will help you..
Which helps in compiling and generating the reports with huge data.
Mainly jasper server is used to store the reports.So, that we can clearly seperate reports and the application code.
Jasper server provides different ways to call a report that exists in the jasper server from a java program.
Jasper server by default provides a REST service to call/down load a report.
Below sample code explains how to call a report/down load a report that exists in jasper server.
Pre requisites:
I assume you have some knowledge on Jasper server and Jasper API.
public class OneMoreTry {
public static Document stringToDom(String xmlSource) throws SAXException, ParserConfigurationException, IOException {
ByteArrayInputStream stream = new ByteArrayInputStream(xmlSource.getBytes());
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
//StringReader sr = new StringReader(xmlSource);
//InputSource is = new InputSource(sr);
return builder.parse(stream);
}
public static String domToString( Document xml ) throws Exception{
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
StringWriter writer = new StringWriter();
transformer.transform(new DOMSource(xml), new StreamResult(writer));
return writer.getBuffer().toString();
}
public static void main(String[] args) throws Exception {
//URL on which jasper server is ruuning
String serverURL = "http://localhost:8080/jasperserver/";
//report name (in jasper sever we have some sample reports I am using some sample report for the example, below is the path to the report)
String reportName = "reports/samples/Department";
String reportFormat = "HTML";
//Use HTTP clinet,POST,PUT methods to consume REST service of the jasper server
HttpClient client = new HttpClient();
// Setting Login URL in a POST method
String loginURL = serverURL+"rest/login";
PostMethod postMethod = new PostMethod(loginURL);
// Set authentication parameters(To connect the jasper server through post methods)
postMethod.addParameter("j_username", "XXX");
postMethod.addParameter("j_password", "XXXX");
// Send POST with login request
int statusCode = client.executeMethod(postMethod);
// Check correct login process
if (statusCode != HttpStatus.SC_OK) {
System.out.println("Login failed: " + postMethod.getStatusLine());
return;
}
// Settting resource URL in a GET method to get descriptor
String resourceURL = serverURL+"rest/resource/"+reportName;
GetMethod getMethod = new GetMethod(resourceURL);
// Send GET request for descriptor
statusCode = client.executeMethod(getMethod);
// Check correct descriptor process
if (statusCode != HttpStatus.SC_OK) {
System.out.println("Descriptor failed: " + getMethod.getStatusLine());
}
// Get the response body as String
String descriptorSource = getMethod.getResponseBodyAsString();
//Transform descriptor from String into XML Document
Document descriptorXML = stringToDom(descriptorSource);
System.out.println("doc:" + descriptorXML.toString());
// Settting PUT method to run report, the url contains the RUN_OUTPUT_FORMAT parameter
String reportURL = serverURL+"rest/report/"+reportName+"?RUN_OUTPUT_FORMAT="+reportFormat;
PutMethod putMethod = new PutMethod(reportURL);
// Setting the request Body. The descriptor XML Document is transform to String and add to the request body.
putMethod.setRequestBody(domToString(descriptorXML));
// Send PUT request to execute the report.
statusCode = client.executeMethod(putMethod);
// Check correct report process
if (statusCode != 201) {
System.out.println("Report failed: " + putMethod.getStatusLine());
}
// Get the response body
String reportSource = putMethod.getResponseBodyAsString();
// Transform report information into XML Document.
Document reportXML = stringToDom(reportSource);
// Extrac from XML Document the report's UUID
NodeList nodes = reportXML.getElementsByTagName("uuid");
String reportUuid = nodes.item(0).getTextContent();
// Setting GET request to download the report
String reportFileURL = serverURL+"rest/report/"+reportUuid;//+"?file=report";
GetMethod getMethodFile = new GetMethod( reportFileURL );
//Send GET request to download the report
statusCode = client.executeMethod(getMethodFile);
// Check correct report process
if (statusCode != 200) {
System.out.println("Downlaod failed: " + putMethod.getStatusLine());
}
// Getting the report body
byte inbytes[] = new byte[1024];
InputStream is= getMethodFile.getResponseBodyAsStream();
is.read(inbytes);
String in = new String(inbytes, "cp1252");
DataOutputStream out = new DataOutputStream(new FileOutputStream("d:/Myjars/hai.pdf"));
out.writeUTF(in);
//File file = new File("d:/Myjars/hai.pdf");
//bis.close();
}
}
Hope this will help you..
Hi Manu,
ReplyDeleteI am unable to find the package for NodeList and Document. Which packages/JAR do they belong to?
Regards,
Jatin
HI ,
DeletePlease find import statements for the nodelist and document.
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
Thanks,
Manu
Login failed: HTTP/1.1 404 (with valid username and password) plz any solution?
ReplyDelete