KibanaExtensionServlet extends HttpServlet { @Override protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException { String content = null; String path = req.getRequestURI().substring( req.getServletPath().length()); resp.addHeader( "Access-Control-Allow-Origin","*" ); resp.addHeader( "Access-Control-Allow-Methods", "GET, DELETE, POST, OPTIONS" ); String reqHead = req.getHeader( "Access-Control-Request-Headers" ); if( ! Utils.isEmptyOrWhitespaces( reqHead )) { reqHead = URLEncoder.encode( reqHead, StandardCharsets.UTF_8.displayName()); resp.addHeader( "Access-Control-Allow-Headers", reqHead ); } if( Utils.isEmptyOrWhitespaces( path ) || "/".equals( path )) { List<Application> applications = new ArrayList<> (); for( ManagedApplication ma : this.manager.applicationMngr().getManagedApplications()) { applications.add( ma.getApplication()); } JtwigTemplate template = JtwigTemplate.classpathTemplate( "apps.twig" ); JtwigModel model = JtwigModel.newModel() .with( "kibanaLocation", this.appDashBoardUrl ) .with( "baseLocation", KibanaExtensionConstants.WEB_ADMIN_PATH ) .with( "apps", applications ); ByteArrayOutputStream os = new ByteArrayOutputStream(); template.render( model, os ); content = os.toString( "UTF-8" ); } else if( path.startsWith( "/" )) { String appName = path.substring( 1 ).trim(); Application app = this.manager.applicationMngr().findApplicationByName( appName ); if( app != null ) { List<String> instancePaths = new ArrayList<> (); for( Instance inst : InstanceHelpers.findAllScopedInstances( app )) { instancePaths.add( InstanceHelpers.computeInstancePath( inst )); } JtwigTemplate template = JtwigTemplate.classpathTemplate( "app.twig" ); JtwigModel model = JtwigModel.newModel() .with( "kibanaLocation", this.agentDashBoardUrl ) .with( "baseLocation", KibanaExtensionConstants.WEB_ADMIN_PATH ) .with( "appName", appName ) .with( "instancePaths", instancePaths ); ByteArrayOutputStream os = new ByteArrayOutputStream(); template.render( model, os ); content = os.toString( "UTF-8" ); } } if( content == null ) { resp.sendError( HttpServletResponse.SC_NOT_FOUND ); } else { ByteArrayInputStream in = new ByteArrayInputStream( content.getBytes( StandardCharsets.UTF_8 )); Utils.copyStreamUnsafelyUseWithCaution( in, resp.getOutputStream()); } } KibanaExtensionServlet( Manager manager ); void setAppDashBoardUrl( String appDashBoardUrl ); void setAgentDashBoardUrl( String agentDashBoardUrl );  }
@Test public void testDoGet_404() throws Exception { Manager manager = new Manager(); manager.configurationMngr().setWorkingDirectory( this.folder.newFolder()); HttpServletRequest req = Mockito.mock( HttpServletRequest.class ); HttpServletResponse resp = Mockito.mock( HttpServletResponse.class ); Mockito.when( req.getRequestURI()).thenReturn( "/roboconf-web-extension/kibana/invalid-app" ); Mockito.when( req.getServletPath()).thenReturn( "/roboconf-web-extension/kibana" ); KibanaExtensionServlet servlet = new KibanaExtensionServlet( manager ); servlet.doGet( req, resp ); Mockito.verify( resp ).sendError( HttpServletResponse.SC_NOT_FOUND ); }
@Test public void testDoGet_invalidPath() throws Exception { Manager manager = new Manager(); manager.configurationMngr().setWorkingDirectory( this.folder.newFolder()); HttpServletRequest req = Mockito.mock( HttpServletRequest.class ); HttpServletResponse resp = Mockito.mock( HttpServletResponse.class ); Mockito.when( req.getRequestURI()).thenReturn( "/roboconf-web-extension/kibana2" ); Mockito.when( req.getServletPath()).thenReturn( "/roboconf-web-extension/kibana" ); KibanaExtensionServlet servlet = new KibanaExtensionServlet( manager ); servlet.doGet( req, resp ); Mockito.verify( resp ).sendError( HttpServletResponse.SC_NOT_FOUND ); }
@Test public void testDoGet_baseRequest_noApplication() throws Exception { Manager manager = new Manager(); manager.configurationMngr().setWorkingDirectory( this.folder.newFolder()); HttpServletRequest req = Mockito.mock( HttpServletRequest.class ); HttpServletResponse resp = Mockito.mock( HttpServletResponse.class ); Mockito.when( req.getRequestURI()).thenReturn( "/roboconf-web-extension/kibana" ); Mockito.when( req.getServletPath()).thenReturn( "/roboconf-web-extension/kibana" ); TestServletOutputStream wrappedOs = new TestServletOutputStream(); Mockito.when( resp.getOutputStream()).thenReturn( wrappedOs ); KibanaExtensionServlet servlet = new KibanaExtensionServlet( manager ); servlet.appDashBoardUrl = "my-dashboard"; servlet.doGet( req, resp ); Mockito.verify( resp ).getOutputStream(); String s = wrappedOs.os.toString( "UTF-8" ); validateHtml( s ); Assert.assertTrue( s.contains( "\t<th>Agents List</th>\n" )); Assert.assertTrue( s.contains( "=\"my-dashboard" )); Assert.assertFalse( s.contains( "=\"#" + KibanaExtensionConstants.WEB_ADMIN_PATH )); }
@Test public void testDoGet_baseRequest_withApplications() throws Exception { Manager manager = new Manager(); manager.configurationMngr().setWorkingDirectory( this.folder.newFolder()); TestManagerWrapper wrapper = new TestManagerWrapper( manager ); TestApplication app = new TestApplication(); app.setDirectory( this.folder.newFolder()); wrapper.addManagedApplication( new ManagedApplication( app )); HttpServletRequest req = Mockito.mock( HttpServletRequest.class ); HttpServletResponse resp = Mockito.mock( HttpServletResponse.class ); Mockito.when( req.getRequestURI()).thenReturn( "/roboconf-web-extension/kibana" ); Mockito.when( req.getServletPath()).thenReturn( "/roboconf-web-extension/kibana" ); TestServletOutputStream wrappedOs = new TestServletOutputStream(); Mockito.when( resp.getOutputStream()).thenReturn( wrappedOs ); KibanaExtensionServlet servlet = new KibanaExtensionServlet( manager ); servlet.appDashBoardUrl = "my-dashboard"; servlet.doGet( req, resp ); Mockito.verify( resp ).getOutputStream(); String s = wrappedOs.os.toString( "UTF-8" ); validateHtml( s ); Assert.assertTrue( s.contains( "\t<th>Agents List</th>\n" )); Assert.assertTrue( s.contains( "=\"my-dashboard" )); Assert.assertTrue( s.contains( "=\"#" + KibanaExtensionConstants.WEB_ADMIN_PATH )); }
@Test public void testDoGet_baseRequestWithSlash() throws Exception { Manager manager = new Manager(); manager.configurationMngr().setWorkingDirectory( this.folder.newFolder()); HttpServletRequest req = Mockito.mock( HttpServletRequest.class ); HttpServletResponse resp = Mockito.mock( HttpServletResponse.class ); Mockito.when( req.getRequestURI()).thenReturn( "/roboconf-web-extension/kibana/" ); Mockito.when( req.getServletPath()).thenReturn( "/roboconf-web-extension/kibana" ); TestServletOutputStream wrappedOs = new TestServletOutputStream(); Mockito.when( resp.getOutputStream()).thenReturn( wrappedOs ); KibanaExtensionServlet servlet = new KibanaExtensionServlet( manager ); servlet.doGet( req, resp ); Mockito.verify( resp ).getOutputStream(); String s = wrappedOs.os.toString( "UTF-8" ); validateHtml( s ); Assert.assertTrue( s.contains( "\t<th>Agents List</th>\n" )); }
@Test public void testDoGet_requestForApplication() throws Exception { Manager manager = new Manager(); manager.configurationMngr().setWorkingDirectory( this.folder.newFolder()); TestManagerWrapper wrapper = new TestManagerWrapper( manager ); TestApplication app = new TestApplication(); app.setDirectory( this.folder.newFolder()); wrapper.addManagedApplication( new ManagedApplication( app )); HttpServletRequest req = Mockito.mock( HttpServletRequest.class ); HttpServletResponse resp = Mockito.mock( HttpServletResponse.class ); Mockito.when( req.getRequestURI()).thenReturn( "/roboconf-web-extension/kibana/" + app.getName()); Mockito.when( req.getServletPath()).thenReturn( "/roboconf-web-extension/kibana" ); TestServletOutputStream wrappedOs = new TestServletOutputStream(); Mockito.when( resp.getOutputStream()).thenReturn( wrappedOs ); KibanaExtensionServlet servlet = new KibanaExtensionServlet( manager ); servlet.agentDashBoardUrl = "my-dashboard"; servlet.doGet( req, resp ); Mockito.verify( resp ).getOutputStream(); String s = wrappedOs.os.toString( "UTF-8" ); validateHtml( s ); Assert.assertTrue( s.contains( "\t<th>Instance Path</th>\n" )); Assert.assertTrue( s.contains( "\t<th>Associated Dashboard</th>\n" )); Assert.assertTrue( s.contains( "=\"my-dashboard" )); Assert.assertTrue( s.contains( "<td>/mysql-vm</td>" )); Assert.assertTrue( s.contains( "=\"#" + KibanaExtensionConstants.WEB_ADMIN_PATH )); }