PentahoXmlaServlet extends DynamicDatasourceXmlaServlet { @Override protected RepositoryContentFinder makeContentFinder( String dataSourcesUrl ) { @SuppressWarnings ( "rawtypes" ) Set keys = cacheMgr.getAllKeysFromRegionCache( CACHE_REGION ); if ( !keys.contains( dataSourcesUrl ) ) { cacheMgr.putInRegionCache( CACHE_REGION, dataSourcesUrl, new DynamicContentFinder( dataSourcesUrl ) { @Override public String getContent() { try { String original = generateInMemoryDatasourcesXml(); EntityResolver loader = new PentahoEntityResolver(); Document originalDocument = XmlDom4JHelper.getDocFromString( original, loader ); if ( PentahoXmlaServlet.logger.isDebugEnabled() ) { PentahoXmlaServlet.logger .debug( Messages.getInstance().getString( "PentahoXmlaServlet.DEBUG_ORIG_DOC", originalDocument.asXML() ) ); } Document modifiedDocument = (Document) originalDocument.clone(); List<Node> nodesToRemove = getNodesToRemove( modifiedDocument ); if ( PentahoXmlaServlet.logger.isDebugEnabled() ) { PentahoXmlaServlet.logger.debug( Messages.getInstance().getString( "PentahoXmlaServlet.DEBUG_NODES_TO_REMOVE", String.valueOf( nodesToRemove.size() ) ) ); } for ( Node node : nodesToRemove ) { node.detach(); } if ( PentahoXmlaServlet.logger.isDebugEnabled() ) { PentahoXmlaServlet.logger.debug( Messages.getInstance().getString( "PentahoXmlaServlet.DEBUG_MOD_DOC", modifiedDocument.asXML() ) ); } return modifiedDocument.asXML(); } catch ( XmlParseException e ) { PentahoXmlaServlet.logger.error( Messages.getInstance().getString( "PentahoXmlaServlet.ERROR_0004_UNABLE_TO_GET_DOCUMENT_FROM_STRING" ), e ); return null; } } private List<Node> getNodesToRemove( Document doc ) { List<Node> nodesToRemove = doc.selectNodes( "/DataSources/DataSource/Catalogs/Catalog" ); CollectionUtils.filter( nodesToRemove, new Predicate() { @Override public boolean evaluate( Object o ) { Element el = ( (DefaultElement) o ).element( "DataSourceInfo" ); if ( el == null || el.getText() == null || el.getTextTrim().length() == 0 ) { throw new XmlaException( SERVER_FAULT_FC, UNKNOWN_ERROR_CODE, UNKNOWN_ERROR_FAULT_FS, new MondrianException( "DataSourceInfo not defined for " + ( (DefaultElement) o ).attribute( "name" ).getText() ) ); } return el.getText().matches( "(?i).*EnableXmla=['\"]?false['\"]?" ); } } ); return nodesToRemove; } } ); } return (RepositoryContentFinder) cacheMgr.getFromRegionCache( CACHE_REGION, dataSourcesUrl ); } PentahoXmlaServlet(); @Override void init( ServletConfig servletConfig );  }
@Test public void testMakeContentFinderHandlesXmlaEnablement() throws Exception { ISecurityHelper securityHelper = mock( ISecurityHelper.class ); SecurityHelper.setMockInstance( securityHelper ); when( securityHelper.runAsSystem( any( ( Callable.class ) ) ) ).thenReturn( DATASOURCE_XML ); Document content = XmlDom4JHelper.getDocFromString( new PentahoXmlaServlet().makeContentFinder( "fakeurl" ).getContent(), new PentahoEntityResolver() ); assertEquals( 2, content.selectNodes( "/DataSources/DataSource/Catalogs/Catalog" ).size() ); assertNotNull( content.selectNodes( "/DataSources/DataSource/Catalogs/Catalog[@name='EnabledCatalog']" ) ); assertNotNull( content.selectNodes( "/DataSources/DataSource/Catalogs/Catalog[@name='FoodMart']" ) ); }
@Test public void testInvalidDataSourceInfo() throws Exception { ISecurityHelper securityHelper = mock( ISecurityHelper.class ); SecurityHelper.setMockInstance( securityHelper ); when( securityHelper.runAsSystem( any( ( Callable.class ) ) ) ).thenReturn( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<DataSources>\n" + "<DataSource>\n" + "<DataSourceName>Pentaho</DataSourceName>\n" + "<DataSourceDescription>Pentaho BI Platform Datasources</DataSourceDescription>\n" + "<URL>http: + "<DataSourceInfo>Provider=mondrian</DataSourceInfo>\n" + "<ProviderName>PentahoXMLA</ProviderName>\n" + "<ProviderType>MDP</ProviderType>\n" + "<AuthenticationMode>Unauthenticated</AuthenticationMode>\n" + "<Catalogs>\n" + "<Catalog name=\"SampleData\">\n" + "<DataSourceInfo></DataSourceInfo>\n" + "<Definition>mondrian:/SampleData</Definition>\n" + "</Catalog>\n" + "</Catalogs>\n" + "</DataSource>\n" + "</DataSources>\n" ); try { new PentahoXmlaServlet().makeContentFinder( "fakeurl" ).getContent(); } catch ( MondrianException e ) { assertTrue( e.getCause().getCause().getMessage().contains( "DataSourceInfo not defined for SampleData" ) ); return; } fail( "Did not throw expected exception." ); }