URL Accessibility for Reports Sample
This sample explains how to create Accessible URLs for reports. You might also find the URL Accessibility For Searches Sample useful. We will be reusing the code of the Handler Sample and reusing logic of the Asset Models Sample in this example. This sample requires Apache Maven. See Installation Prerequisites for links on how to install it.
Instructions
1. Navigate to GREG_HOME/
samples/handler/src
to find the source code of the Handler Sample.
2. Add the following dependency to your POM file. Make sure you update the current version of carbon governance (e.g. 4.6.3).
Project Object Model (POM) is an XML representation of a Maven project held in a file named pom.xml. You should find POM file by the name pom.xml inside <G-REG_HOME>/
samples/handler/src/
directory.
<dependency> <groupId>org.wso2.carbon.governance</groupId> <artifactId>org.wso2.carbon.governance.api</artifactId> <version>{current version of the carbon.governance}</version> </dependency>
3. Add a new Java Class named URIForReportHandler
at
GREG_HOME/samples/handler/src/src/main/java/org/wso2/carbon/registry/samples/handler/URIForReportHandler.java
with the following source:
package org.wso2.carbon.registry.samples.handler; import org.wso2.carbon.governance.api.exception.GovernanceException; import org.wso2.carbon.governance.api.generic.GenericArtifactFilter; import org.wso2.carbon.governance.api.generic.GenericArtifactManager; import org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact; import org.wso2.carbon.governance.api.util.GovernanceUtils; import org.wso2.carbon.registry.core.ActionConstants; import org.wso2.carbon.registry.core.exceptions.RegistryException; import org.wso2.carbon.registry.core.jdbc.handlers.Handler; import org.wso2.carbon.registry.core.jdbc.handlers.RequestContext; import org.wso2.carbon.registry.core.session.CurrentSession; import org.wso2.carbon.registry.core.utils.RegistryUtils; import org.wso2.carbon.user.core.UserStoreException; import javax.xml.namespace.QName; public class URIForReportHandler extends Handler { private String resourceServletPath; public void setRegistryURL(String registryURL) { resourceServletPath = registryURL + (registryURL.endsWith("/") ? "resource" : "/resource"); } public void put(RequestContext requestContext) throws RegistryException { GovernanceUtils.setTenantGovernanceSystemRegistry(CurrentSession.getTenantId()); GenericArtifactManager manager = new GenericArtifactManager( GovernanceUtils.getGovernanceSystemRegistry(null), "uri"); String path = requestContext.getResourcePath().getPath(); final String uri = resourceServletPath + path; GenericArtifact[] artifacts = manager.findGenericArtifacts(new GenericArtifactFilter() { public boolean matches(GenericArtifact artifact) throws GovernanceException { return uri.matches(artifact.getAttribute("overview_uri")); } }); if (artifacts.length == 0) { GenericArtifact artifact = manager.newGovernanceArtifact(new QName(RegistryUtils.getResourceName(path))); artifact.setAttribute("overview_type", "Generic"); artifact.setAttribute("overview_uri", uri); manager.addGenericArtifact(artifact); try { CurrentSession.getUserRealm().getAuthorizationManager().authorizeRole( "wso2.anonymous.role", path, ActionConstants.GET); } catch (UserStoreException e) { throw new RegistryException("Unable to set role authorizations", e); } } } public void delete(RequestContext requestContext) throws RegistryException { GovernanceUtils.setTenantGovernanceSystemRegistry(CurrentSession.getTenantId()); GenericArtifactManager manager = new GenericArtifactManager( GovernanceUtils.getGovernanceSystemRegistry(null), "uri"); String path = requestContext.getResourcePath().getPath(); final String uri = resourceServletPath + path; GenericArtifact[] artifacts = manager.findGenericArtifacts(new GenericArtifactFilter() { public boolean matches(GenericArtifact artifact) throws GovernanceException { return uri.matches(artifact.getAttribute("overview_uri")); } }); if (artifacts.length > 0) { manager.removeGenericArtifact(artifacts[0].getId()); try { CurrentSession.getUserRealm().getAuthorizationManager().clearRoleAuthorization( "wso2.anonymous.role", path, ActionConstants.GET); } catch (UserStoreException e) { throw new RegistryException("Unable to clear role authorizations", e); } } } }
4. Compile the source code by running the following command inside GREG_HOME/
samples/handler/src
:
mvn clean install
The command mvn clean install
will trigger an Apache Maven Build in your command line. This requires you having installed Apache Maven. See Installation Prerequisites for links on how to install it.
A successful run of Apache Maven will generate a report similar to the following:
5. Copy the <G-REG_HOME>/
samples/handler/src/target/
org.wso2.carbon.registry.samples.handler-5.2.0.jar
file into the <G-REG_HOME>/repository/components/dropins/
directory.
6. Edit the registry.xml
file which is in GREG_HOME/repository/conf
folder and add the following XML snippet.
<handler class="org.wso2.carbon.registry.samples.handler.URIForReportHandler"> <filter class="org.wso2.carbon.registry.core.jdbc.handlers.filters.URLMatcher"> <property name="pattern">/_system/governance/reports/.*</property> </filter> <property name="registryURL">https://localhost:9443/registry</property> </handler>
See also Handler Configuration Details.
7. Start the WSO2 Governance Registry. See Running the Product for more information.
8. Run the Asset Models Sample for Applications and create a few Applications in Governance Registry (to facilitate the generation of meaningful reports).
9. Navigate to the Manage Reports page, and Schedule the Application Report.
10. Fill in the following parameters and schedule the report generation:
- Cron Expression - 0/10 * * * * ?
- Registry URL - https://localhost:9443/registry
- Username - admin
- Password - admin
- Resource Path - /_system/governance/reports/ApplicationReport.pdf
Click the "Schedule" button to finish scheduling the report.
You should find a line similar to the following printed on your command prompt, which indicates that you task has successfully deployed.
[2012-09-17 00:42:49,204] INFO {org.wso2.carbon.ntask.core.impl.AbstractQuartzTaskManager} - Task scheduled: ApplicationReport
11. Finally navigate to the List URI page to see your URI added.