@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" )); }
@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 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 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 ); }
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 ); }
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 );  }