Jump to content
We've recently updated our Privacy Statement, available here ×

Displaying jasper report in a jsp using REST V1


salma.zejli

Recommended Posts

 

I'm trying to retrieve a jasper report with RESTV1 webservices and then display it.I used this code:

 

<%@ page import="java.io.*" %><%

%><%@ page import="javax.xml.parsers.*" %><%

%><%@ page import="org.w3c.dom.*" %><%

%><%@ page import="org.xml.sax.*" %><%

%><%@ page import="javax.xml.transform.*" %><%

%><%@ page import="javax.xml.transform.dom.*" %><%

%><%@ page import="javax.xml.transform.stream.*" %><%

%><%@ page import="org.apache.commons.httpclient.HttpClient" %><%

%><%@ page import="org.apache.commons.httpclient.HttpStatus" %><%

%><%@ page import="org.apache.commons.httpclient.methods.PostMethod" %><%

%><%@ page import="org.apache.commons.httpclient.methods.PutMethod" %><%

%><%@ page import="org.apache.commons.httpclient.methods.GetMethod" %><%

%><%@page contentType="application/pdf" %><%

 

String serverURL = "http://pc-demo-bi:8090/jasperserver-pro/";

String reportName = "/public/folder1/1._Geographic_Results_by_Segment_Report";

String reportFormat ="pdf";

        String res=null;

HttpClient client = new HttpClient();

 

// Setting Login URL in a POST method

String loginURL = serverURL+"rest/login";

PostMethod postMethod = new PostMethod(loginURL);

 

// Set authentication parameters

postMethod.addParameter("j_username", "superuser");

postMethod.addParameter("j_password", "superuser");

 

// 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;

}

System.out.println("I/'m happy");

 

// 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());

}

System.out.println("I/'m much happy");

        

// Get the response body as String

String descriptorSource = getMethod.getResponseBodyAsString();

System.out.println(descriptorSource);

//Transform descriptor from String into XML Document

Document descriptorXML = stringToDom(descriptorSource);

 

// Use this method if you need to send parameters to the report

// These are added to the XML Document

//addParameter(descriptorXML, "personID", "1244");

 

// 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(descriptorSource);

 

// 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());

}

System.out.println("I'm very very happy");

 

// Get the response body

String reportSource = putMethod.getResponseBodyAsString();

System.out.println(reportSource);

// 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();

System.out.println(reportUuid);

// 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());

}

System.out.println("I'm very very very happy");

 

 

/* InputStreamReader isr = new InputStreamReader(getMethodFile.getResponseBodyAsStream());

         BufferedReader in = new BufferedReader(isr);

         String str;

         StringBuffer accumule = new StringBuffer();

         while ((str = in.readLine()) != null) {

             System.out.println(str);

             accumule.append(str);

         }

         in.close();

         res = accumule.toString();

         PrintWriter writer = response.getWriter();

  writer.write( res);

  writer.flush();

         out.println(res); */

 

// Getting the report body

InputStream is = getMethodFile.getResponseBodyAsStream();

BufferedInputStream bis = new BufferedInputStream( is );

 

String datastr = null;

StringBuffer sb = new StringBuffer();

byte[] bytes = new byte[ 8192 ];

 

int count = bis.read( bytes );

while( count != -1 && count <= 8192 ){

out.print(new String(bytes, 0, count));

count = bis.read( bytes );

}

bis.close(); 

 

%><%!

public static Document stringToDom(String xmlSource) throws SAXException, ParserConfigurationException, IOException {

   DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

   DocumentBuilder builder = factory.newDocumentBuilder();

StringReader sr = new StringReader(xmlSource);

   InputSource is = new InputSource(sr);

   return builder.parse(is);

}

 

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().replaceAll("n|r", "");

}

 

//Adds node like <parameter name ="exampleParm1" >value</parameter>

public static void addParameter(Document doc,String name, String value){

Element element = doc.getDocumentElement();

Element node = doc.createElement("parameter");

node.setAttribute("name",name);

CDATASection cdata = doc.createCDATASection(value);

node.appendChild(cdata);

element.appendChild(node);

}

 

%>

I have succeded to display a report in html and PDF but I can't visualize it's content.The report is given in the format required but empty.Any suggestions please ?Thanks in advance

 

Link to comment
Share on other sites

  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Popular Days

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...