PentahoURLRewriter implements URLRewriter { public String rewrite( final ContentEntity contentEntry, final ContentEntity dataEntity ) throws URLRewriteException { try { final ArrayList<String> entityNames = new ArrayList<String>(); entityNames.add( dataEntity.getName() ); ContentLocation location = dataEntity.getParent(); while ( location != null ) { entityNames.add( location.getName() ); location = location.getParent(); } final ArrayList<String> contentNames = new ArrayList<String>(); location = dataEntity.getRepository().getRoot(); while ( location != null ) { contentNames.add( location.getName() ); location = location.getParent(); } while ( ( contentNames.isEmpty() == false ) && ( entityNames.isEmpty() == false ) ) { final String lastEntity = (String) entityNames.get( entityNames.size() - 1 ); final String lastContent = (String) contentNames.get( contentNames.size() - 1 ); if ( lastContent.equals( lastEntity ) == false ) { break; } entityNames.remove( entityNames.size() - 1 ); contentNames.remove( contentNames.size() - 1 ); } final StringBuffer b = new StringBuffer(); for ( int i = entityNames.size() - 1; i >= 0; i-- ) { final String name = (String) entityNames.get( i ); b.append( name ); if ( i != 0 ) { b.append( "/" ); } } if ( pattern == null ) { return b.toString(); } return MessageFormat.format( pattern, new Object[] { b.toString() } ); } catch ( ContentIOException cioe ) { throw new URLRewriteException(); } } PentahoURLRewriter( final String pattern ); String rewrite( final ContentEntity contentEntry, final ContentEntity dataEntity );  }
@Test public void testRewriteWithNullPattern() throws Exception { String pattern = null; PentahoURLRewriter rewriter = new PentahoURLRewriter( pattern ); File dataDirectory = new File(TEST_SRC + "/solution/test/reporting/"); FileRepository dataRepository = new FileRepository( dataDirectory ); File contentEntryBackend = new File(TEST_SRC + "/solution/test/reporting/contentEntryBackend"); File dataEntityBackend = new File(TEST_SRC + "/solution/test/reporting/dataEntityBackend"); ContentEntity contentEntry = new FileContentItem( dataRepository.getRoot(), contentEntryBackend ); ContentEntity dataEntity = new FileContentItem( dataRepository.getRoot(), dataEntityBackend ); String result = rewriter.rewrite( contentEntry, dataEntity ); assertEquals( "dataEntityBackend", result ); }
@Test public void testRewriteWithSimplePattern() throws Exception { String pattern = TEST_SRC + "/solution/test/reporting/system"; PentahoURLRewriter rewriter = new PentahoURLRewriter( pattern ); File dataDirectory = new File(TEST_SRC + "/solution/test/reporting/"); FileRepository dataRepository = new FileRepository( dataDirectory ); File contentEntryBackend = new File(TEST_SRC + "/solution/test/reporting/contentEntryBackend"); File dataEntityBackend = new File(TEST_SRC + "/solution/test/reporting/dataEntityBackend"); ContentEntity contentEntry = new FileContentItem( dataRepository.getRoot(), contentEntryBackend ); ContentEntity dataEntity = new FileContentItem( dataRepository.getRoot(), dataEntityBackend ); String result = rewriter.rewrite( contentEntry, dataEntity ); assertEquals( pattern, result ); }
@Test public void testRewriteWithPatternParam() throws Exception { String pattern = TEST_SRC + "/solution/test/reporting/system/{0}/param"; PentahoURLRewriter rewriter = new PentahoURLRewriter( pattern ); File dataDirectory = new File(TEST_SRC + "/solution/test/reporting/"); FileRepository dataRepository = new FileRepository( dataDirectory ); File contentEntryBackend = new File(TEST_SRC + "/solution/test/reporting/contentEntryBackend"); File dataEntityBackend = new File(TEST_SRC + "/solution/test/reporting/dataEntityBackend"); ContentEntity contentEntry = new FileContentItem( dataRepository.getRoot(), contentEntryBackend ); ContentEntity dataEntity = new FileContentItem( dataRepository.getRoot(), dataEntityBackend ); String result = rewriter.rewrite( contentEntry, dataEntity ); assertEquals(TEST_SRC + "/solution/test/reporting/system/dataEntityBackend/param", result ); }
@Test public void testRewriteRoot() throws Exception { String pattern = TEST_SRC + "/solution/test/reporting/system/{0}/param"; PentahoURLRewriter rewriter = new PentahoURLRewriter( pattern ); File dataDirectory = new File( "/" ); FileRepository dataRepository = new FileRepository( dataDirectory ); File contentEntryBackend = new File(TEST_SRC + "/solution/test/reporting/contentEntryBackend"); File dataEntityBackend = new File(TEST_SRC + "/solution/test/reporting/dataEntityBackend"); ContentEntity contentEntry = new FileContentItem( dataRepository.getRoot(), contentEntryBackend ); ContentEntity dataEntity = new FileContentItem( dataRepository.getRoot(), dataEntityBackend ); String result = rewriter.rewrite( contentEntry, dataEntity ); assertEquals(TEST_SRC + "/solution/test/reporting/system/dataEntityBackend/param", result ); }