2016-05-29 72 views
1

我正在努力使客戶端休息服務與jasperserver生成報告。我使用下面的代碼使:JasperServer REST客戶端路徑如何?

我在設置服務器的URL和報告路徑, 服務器的URL我把http://localhost:8081/jasperserver/ 並在接下來的圖像顯示,我把報表路徑rest/report/mytest/my_report但我得到404不是問題發現錯誤行

File remoteFile = resource.get(File.class); 

那麼我怎樣才能從jasperserver得到正確的報告路徑?

Repository screenshot

public class App2 { 

private final static String serverUrl = "http://localhost:8081 
    /jasperserver/"; 
private final static String serverUser = "jasperadmin"; 
private final static String serverPassword = "jasperadmin"; 

public static void main(String arg[]) throws Exception { 
    Report reporte = new Report(); 
    reporte.setFormat("pdf"); 
    reporte.setOutputFolder("/home/ali/images"); 
    ClientConfig clientConfig; 
    Map<String, String> resourceCache=new HashMap<String, String>(); 
    clientConfig = new DefaultApacheHttpClientConfig(); 
    clientConfig.getProperties().put(ClientConfig.PROPERTY_FOLLOW_REDIRECTS, true); 
    clientConfig.getProperties().put(ApacheHttpClientConfig.PROPERTY_HANDLE_COOKIES, true); 
    ApacheHttpClient client = ApacheHttpClient.create(clientConfig); 
    client.addFilter(new HTTPBasicAuthFilter(serverUser, serverPassword)); 
    String describeResourcePath = "/rest/resource" + "/mytest/my_report/"; 
    String generateReportPath = "/rest/report" + "/mytest/my_report/" + "?RUN_OUTPUT_FORMAT=" + reporte.getFormat(); 
    WebResource resource = null; 
    String resourceResponse = null; 
    if (resourceCache.containsKey(describeResourcePath)) { 
     resourceResponse = resourceCache.get(describeResourcePath); 
    } else { 
     resource = client.resource(serverUrl); 
     resource.accept(MediaType.APPLICATION_XML); 
     resourceResponse = resource.path(describeResourcePath).get(String.class); 
     resourceCache.put(describeResourcePath, resourceResponse); 
    } 
    Document resourceXML = parseResource(resourceResponse); 
    resourceXML = addParametersToResource(resourceXML, reporte); 
    resource = client.resource(serverUrl + generateReportPath); 
    resource.accept(MediaType.TEXT_XML); 
    System.out.println(resource); 
    String reportResponse = resource.put(String.class, serializetoXML(resourceXML)); 
    String urlReport = parseReport(reportResponse); 
    resource = client.resource(urlReport); 
    System.out.println(resource); 
    File destFile = null; 
    try { 
     File remoteFile = resource.get(File.class); 
     File parentDir = new File(reporte.getOutputFolder()); 
     destFile = File.createTempFile("report_", "." + getExtension(reporte.getFormat()), parentDir); 
     FileUtils.copyFile(remoteFile, destFile); 
    } catch (IOException e) { 
     throw e; 
    } 
} 

/** 
* 
* @return 
* @throws DocumentException 
*/ 
private static Document parseResource(String resourceAsText) throws Exception { 
    // LOGGER.debug("parseResource:\n" + resourceAsText); 
    Document document; 
    try { 
     document = DocumentHelper.parseText(resourceAsText); 
    } catch (DocumentException e) { 
     throw e; 
    } 
    return document; 
} 

/** 
* 
*/ 
private static String parseReport(String reportResponse) throws Exception { 
    String urlReport = null; 
    try { 
     Document document = DocumentHelper.parseText(reportResponse); 
     Node node = document.selectSingleNode("/report/uuid"); 
     String uuid = node.getText(); 
     node = document.selectSingleNode("/report/totalPages"); 
     Integer totalPages = Integer.parseInt(node.getText()); 
     if (totalPages == 0) { 
      throw new Exception("Error generando reporte"); 
     } 
     urlReport = serverUrl + "/report/" + uuid + "?file=report"; 
    } catch (DocumentException e) { 
     throw e; 
    } 
    return urlReport; 
} 

/** 
* 
* @param resource 
* @param reporte 
* @return 
*/ 
private static Document addParametersToResource(Document resource, Report reporte) { 
    // LOGGER.debug("addParametersToResource"); 

    Element root = resource.getRootElement(); 
    Map<String, String> params = reporte.getParams(); 
    for (Map.Entry<String, String> entry : params.entrySet()) { 
     String key = entry.getKey(); 
     String value = entry.getValue(); 
     if (key != null && value != null) { 
      root.addElement("parameter").addAttribute("name", key).addText(value); 
     } 
    } 
    // LOGGER.debug("resource:" + resource.asXML()); 
    return resource; 
} 

/** 
* 
* @param aEncodingScheme 
* @throws IOException 
* @throws Exception 
*/ 
private static String serializetoXML(Document resource) throws Exception { 
    OutputFormat outformat = OutputFormat.createCompactFormat(); 
    ByteArrayOutputStream out = new ByteArrayOutputStream(); 
    outformat.setEncoding("ISO-8859-1"); 

    try { 
     XMLWriter writer = new XMLWriter(out, outformat); 
     writer.write(resource); 
     writer.flush(); 
    } catch (IOException e) { 
     throw e; 
    } 
    return out.toString(); 
} 

/** 
* 
* @param format 
* @return 
*/ 
private static String getExtension(String format) { 
    String ext = null; 
    if (format.equals(Report.FORMAT_PDF)) { 
     ext = "pdf"; 
    } else if (format.equals(Report.FORMAT_EXCEL)) { 
     ext = "xls"; 
    } 
    return ext; 
} 

} 

回答

0

我有固定的,我需要改變路徑

urlReport = serverUrl + "/report/" + uuid + "?file=report"; 

在parseReport方法
urlReport = serverUrl + "/rest/report/" + uuid + "?file=report"; 

private static String parseReport(String reportResponse) throws Exception { 
String urlReport = null; 
try { 
    Document document = DocumentHelper.parseText(reportResponse); 
    Node node = document.selectSingleNode("/report/uuid"); 
    String uuid = node.getText(); 
    node = document.selectSingleNode("/report/totalPages"); 
    Integer totalPages = Integer.parseInt(node.getText()); 
    if (totalPages == 0) { 
     throw new Exception("Error generando reporte"); 
    } 
    urlReport = serverUrl + "/report/" + uuid + "?file=report"; 
} catch (DocumentException e) { 
    throw e; 
} 
return urlReport; 

}