Export with OZ Server

Use RequestDispatcher

  1. param.put("connection.inputjson", inputData)

  2. request.setAttribute("OZViewerExportParam", param)

  3. getRequestDispatcher("/server")

  4. request.getAttribute("OZViewerExportResult")

OZViewerExport.jsp
<%@ page import="java.util.*,java.io.*"%>
<%@ page contentType="text/html;charset=UTF-8" %>
<%request.setCharacterEncoding("UTF-8");%>
<%
    String ozrName	= request.getParameter("ozrName");
    String exportFormat	= request.getParameter("exportFormat");
    String inputData	= request.getParameter("inputData");
    // Export env
    String exportPath = "C:\\Program Files\\Apache Software Foundation\\Tomcat 9.0\\webapps\\exported\\demo\\";
    String exportFile = ozrName + "_OZViewerExport." + exportFormat; // KYC_Form.ozr
    String exportFilePath = exportPath + exportFile;
    String ozrPath = "demo\\" + ozrName + ".ozr";
    // OZ parameters 	
    Hashtable param = new Hashtable();	
    param.put("connection.reportname", ozrPath);
    param.put("connection.inputjson", inputData); // data to bind with ozr
    param.put("export.mode", "silent");
    param.put("export.confirmsave", "flase"); // no confirm = silent
    param.put("export.format", exportFormat);
    param.put("export.path", exportPath);
    param.put("export.filename", exportFile);
    param.put("pdf.fontembedding", "true"); // embed the font used in the OZR into the target pdf
    // request OZ server to bind and export
    request.setAttribute("OZViewerExportParam", param);
    RequestDispatcher dispatcher = pageContext.getServletContext().getRequestDispatcher("/server");
    dispatcher.include(request, response);
try {
    // get result from OZ server 
    Object obj = request.getAttribute("OZViewerExportResult");
    if (obj != null) {
	Hashtable t = (Hashtable) obj;
	byte[] bytes = (byte[]) t.get(exportFilePath);
	if (bytes != null && writefile(bytes, exportFilePath)) { 
	    out.println("OZViewerExportResult:\n" + exportFilePath);
	} 
    } else {
	Throwable t = (Throwable) request.getAttribute("OZViewerExportError");
	if(t != null) {
	    throw t;
	} else {
	    throw new Exception("No result from OZ Server.");
	}
    }
} catch (Throwable e) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    e.printStackTrace(pw);
    response.sendError(500, sw.getBuffer().toString());
} finally {
}
%>
<%!
public boolean writefile(byte[] b, String path)
{
    BufferedOutputStream fout = null;
    boolean result = false;
    try{
	fout = new BufferedOutputStream(new FileOutputStream(path));
	fout.write(b);
	fout.flush();
	fout.close();
	fout = null;
	result = true;
    }catch(Exception e){
	result = false; 
    }finally{
	if(fout!=null) try{fout.close();}catch(Exception e){}
    }
    return result;
}
%>

Example: http://oz.ozeform.io/oz/export/export.html

Last updated

Was this helpful?