Jump to content
We've recently updated our Privacy Statement, available here ×
  • How to interact with JasperReports Server RepositoryService from Jar file resource


    stasp
    • Version: v6.4, v6.4.2, v6.3 Product: JasperReports® Server

    Use case description

    This article provides general steps in form of guidelines for implementing a custom JAR file and invoking this JAR from a JRXML. The custom java code is required to interact with resources in repository, and we will look into more details for this last step.

    Suggested approach

    In order to implement some custom Java code that would be called from JRXML reports:

    1. Create a simple Java class, place it in a JAR and uploade to the repository as described here:

    http://community.jaspersoft.com/documentation/tibco-jasperreports-server-administration-guide/v62/file-resources-0#JAR_Files

    2. Invoke your java from a JRXML report by adding the JAR as a resource per these instructions and make sure that it works:

    http://community.jaspersoft.com/documentation/tibco-jasperreports-server-user-guide/v62/uploading-undetected-file-resources 

    3. Now you are going to have to implement access to resources in JasperReports Server repository. There are some code samples for interacting with repository resources via API in Jasperserver Ultimate Guide  (see section '5.4.2.3 Repository Resources' on page 63 if you are using PDF version of the guide for detailed info):

    https://community.jaspersoft.com/documentation/tibco-jasperreports-server-ultimate-guide/v640/repository-api

    However, 

    the guide leaves out the details on how to get a reference to the RepositoryService. For example like this:

    RepositoryService repositoryService = ...; // gets the instance of the

    // RepositoryService interface

    So below is one of possible solutions implemented by one of our users. This particular code snippet was written to read an XML file resource in the repository:

    public static String getDashboardMapping(String prid) throws IOException {

            String repoPath = "/public/my_resources/common/metadata/dashboard_mapping.xml";

            ApplicationContext ctx = StaticApplicationContext.getApplicationContext();

            RepositoryService repositoryService = ctx.getBean("repositoryService",RepositoryService.class);

            // Get the FileResource. This is just metadata about the resource such as name, description, modified date

            // but it does not contain any content

            FileResource resource = (FileResource)repositoryService.getResource(JasperServerUtil.getExecutionContext(),repoPath, FileResource.class);

            // To get file content we need to use fetch the FileResourceData object.

            FileResourceData fileResourceData = repositoryService.getResourceData(JasperServerUtil.getExecutionContext(),repoPath);

            // Convert the file contents to a String

            String content = getStringFromInputStream(fileResourceData.getDataStream());

            

            // Process the file to determine the correct path to return

     ...

    }


    User Feedback

    Recommended Comments

    There are no comments to display.



    Guest
    This is now closed for further comments

×
×
  • Create New...