PentahoReportEnvironment extends DefaultReportEnvironment { public String getEnvironmentProperty( final String key ) { if ( key == null ) { throw new NullPointerException(); } if ( "contentLink".equals( key ) ) { return clText; } if ( cache == null ) { cache = new HashMap<String, String>(); } final String cached = cache.get( key ); if ( cached != null ) { return cached; } final IPentahoSession session = PentahoSessionHolder.getSession(); if ( PentahoSystem.getApplicationContext() != null ) { final String fullyQualifiedServerUrl = PentahoSystem.getApplicationContext().getFullyQualifiedServerURL(); if ( "serverBaseURL".equals( key ) ) { final String baseServerURL = getBaseServerURL( fullyQualifiedServerUrl ); cache.put( key, baseServerURL ); return baseServerURL; } else if ( "pentahoBaseURL".equals( key ) ) { cache.put( key, fullyQualifiedServerUrl ); return fullyQualifiedServerUrl; } else if ( "solutionRoot".equals( key ) ) { final String solutionRoot = PentahoSystem.getApplicationContext().getSolutionPath( "" ); cache.put( key, solutionRoot ); return solutionRoot; } else if ( "hostColonPort".equals( key ) ) { final String hostColonPort = getHostColonPort( fullyQualifiedServerUrl ); cache.put( key, hostColonPort ); return hostColonPort; } else if ( "requestContextPath".equals( key ) ) { final String requestContextPath = PentahoRequestContextHolder.getRequestContext().getContextPath(); cache.put( key, requestContextPath ); } else if ( "selfURL".equals( key ) ) { String path = getRepositoryPath( true ); if ( path != null ) { final String selfURL = fullyQualifiedServerUrl + "api/repos/" + path + "/viewer?"; cache.put( key, selfURL ); return selfURL; } else { return null; } } } else { if ( "serverBaseURL".equals( key ) || "pentahoBaseURL".equals( key ) || "solutionRoot".equals( key ) || "hostColonPort".equals( key ) || "requestContextPath".equals( key ) ) { logger.warn( Messages.getInstance().getString( "ReportPlugin.warnNoApplicationContext" ) ); cache.put( key, null ); return null; } } if ( session != null ) { if ( "username".equals( key ) ) { final Authentication authentication = SecurityHelper.getInstance().getAuthentication(); if ( authentication == null ) { return null; } ITenantedPrincipleNameResolver tenantedUserNameUtils = PentahoSystem.get( ITenantedPrincipleNameResolver.class, "tenantedUserNameUtils", session ); String userName = null; if ( tenantedUserNameUtils != null ) { userName = tenantedUserNameUtils.getPrincipleName( authentication.getName() ); } else { userName = authentication.getName(); } cache.put( key, userName ); return userName; } else if ( "roles".equals( key ) ) { final Authentication authentication = SecurityHelper.getInstance().getAuthentication(); if ( authentication == null ) { return null; } final StringBuilder property = new StringBuilder(); final Collection<? extends GrantedAuthority> roles = authentication.getAuthorities(); ITenantedPrincipleNameResolver tenantedRoleNameUtils = PentahoSystem.get( ITenantedPrincipleNameResolver.class, "tenantedRoleNameUtils", session ); if ( roles == null ) { return null; } final CSVQuoter quoter = new CSVQuoter( ',', '"' ); for ( GrantedAuthority ga : roles ) { if ( property.length() > 0 ) { property.append( "," ); } String authority = null; if ( tenantedRoleNameUtils != null ) { authority = tenantedRoleNameUtils.getPrincipleName( ga.getAuthority() ); } else { authority = ga.getAuthority(); } property.append( quoter.doQuoting( authority ) ); } return property.toString(); } if ( key.startsWith( "session:" ) ) { final Object attribute = session.getAttribute( key.substring( "session:".length() ) ); return String.valueOf( attribute ); } else if ( key.startsWith( "global:" ) ) { final Object attribute = PentahoSystem.getGlobalParameters().getParameter( key.substring( "global:".length() ) ); return String.valueOf( attribute ); } } else { if ( key.startsWith( "session:" ) || key.equals( "username" ) || key.startsWith( "global:" ) || key.equals( "roles" ) ) { logger.warn( Messages.getInstance().getString( "ReportPlugin.warnNoSession" ) ); return null; } } final Object environmentProperty = super.getEnvironmentProperty( key ); if ( environmentProperty == null ) { return null; } return String.valueOf( environmentProperty ); } @Deprecated  PentahoReportEnvironment( final Configuration configuration ); @Deprecated  PentahoReportEnvironment( final Configuration configuration, final String clText );  PentahoReportEnvironment( final Configuration configuration, final String clText, final String repositoryPath ); String getEnvironmentProperty( final String key ); String getRepositoryPath();  }
@Test( expected = NullPointerException.class ) public void getNullProp() { new PentahoReportEnvironment( new DefaultConfiguration() ).getEnvironmentProperty( null ); }
@Test public void getCl() { final String clText = UUID.randomUUID().toString(); Assert.assertEquals( clText, new PentahoReportEnvironment( new DefaultConfiguration(), clText ) .getEnvironmentProperty( "contentLink" ) ); }
@Test public void testNotAppContext() { final String[] props = new String[] { "serverBaseURL", "pentahoBaseURL", "solutionRoot", "hostColonPort", "requestContextPath" }; for ( final String prop : props ) { Assert.assertNull( new PentahoReportEnvironment( new DefaultConfiguration() ) .getEnvironmentProperty( prop ) ); } }