WebAdminInterceptionServlet extends HttpServlet { @Override protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException { boolean responseSet = false; boolean validRequest = Arrays.asList( CSS_STYLESHEET, BANNER_IMAGE ).contains( req.getServletPath()); if( ! validRequest ) this.logger.severe( "An unexpected resource was asked to Roboconf's interception servlet: " + req.getServletPath()); if( ! Utils.isEmptyOrWhitespaces( this.karafEtc )) { File resource = null; if( CSS_STYLESHEET.equals( req.getServletPath())) resource = new File( this.karafEtc, OVERRIDE_CSS ); else if( BANNER_IMAGE.equals( req.getServletPath())) resource = new File( this.karafEtc, OVERRIDE_IMAGE ); if( resource != null && resource.isFile()) { this.logger.fine( "A custom resource was found for " + req.getServletPath()); Utils.copyStream( resource, resp.getOutputStream()); responseSet = true; } } if( validRequest && ! responseSet ) { InputStream in = getServletContext().getResourceAsStream( req.getServletPath()); try { if( in != null ) { Utils.copyStreamUnsafelyUseWithCaution( in, resp.getOutputStream()); responseSet = true; } } finally { Utils.closeQuietly( in ); } } if( ! responseSet ) { if( validRequest ) resp.sendError( HttpServletResponse.SC_NOT_FOUND ); else resp.sendError( HttpServletResponse.SC_FORBIDDEN ); } }    }
@Test public void testInterceptionServlet_noKarafEtc_notFound() throws Exception { HttpServletResponse resp = Mockito.mock( HttpServletResponse.class ); HttpServletRequest req = Mockito.mock( HttpServletRequest.class ); Mockito.when( req.getServletPath()).thenReturn( CSS_STYLESHEET ); ServletConfig sc = Mockito.mock( ServletConfig.class ); ServletContext ctx = Mockito.mock( ServletContext.class ); Mockito.when( sc.getServletContext()).thenReturn( ctx ); WebAdminInterceptionServlet servlet = new WebAdminInterceptionServlet(); servlet.init( sc ); servlet.karafEtc = ""; servlet.doGet( req, resp ); Mockito.verify( req, Mockito.atLeast( 1 )).getServletPath(); Mockito.verify( resp, Mockito.only()).sendError( HttpServletResponse.SC_NOT_FOUND ); }
@Test public void testInterceptionServlet_noKarafEtc_forbidden() throws Exception { HttpServletResponse resp = Mockito.mock( HttpServletResponse.class ); HttpServletRequest req = Mockito.mock( HttpServletRequest.class ); Mockito.when( req.getServletPath()).thenReturn( "invalid-resource" ); ServletConfig sc = Mockito.mock( ServletConfig.class ); ServletContext ctx = Mockito.mock( ServletContext.class ); Mockito.when( sc.getServletContext()).thenReturn( ctx ); WebAdminInterceptionServlet servlet = new WebAdminInterceptionServlet(); servlet.init( sc ); servlet.karafEtc = ""; servlet.doGet( req, resp ); Mockito.verify( req, Mockito.atLeast( 1 )).getServletPath(); Mockito.verify( resp, Mockito.only()).sendError( HttpServletResponse.SC_FORBIDDEN ); }
@Test public void testInterceptionServlet_noKarafEtc_defaultFound() throws Exception { HttpServletResponse resp = Mockito.mock( HttpServletResponse.class ); Mockito.when( resp.getOutputStream()).thenReturn( Mockito.mock( ServletOutputStream.class )); HttpServletRequest req = Mockito.mock( HttpServletRequest.class ); Mockito.when( req.getServletPath()).thenReturn( CSS_STYLESHEET ); ServletConfig sc = Mockito.mock( ServletConfig.class ); ServletContext ctx = Mockito.mock( ServletContext.class ); Mockito.when( ctx.getResourceAsStream( Mockito.anyString())).thenReturn( new ByteArrayInputStream( new byte[ 0 ])); Mockito.when( sc.getServletContext()).thenReturn( ctx ); WebAdminInterceptionServlet servlet = new WebAdminInterceptionServlet(); servlet.init( sc ); servlet.doGet( req, resp ); Mockito.verify( req, Mockito.atLeast( 1 )).getServletPath(); Mockito.verify( resp, Mockito.only()).getOutputStream(); }
@Test public void testInterceptionServlet_withKarafEtc_overrideCss() throws Exception { HttpServletResponse resp = Mockito.mock( HttpServletResponse.class ); Mockito.when( resp.getOutputStream()).thenReturn( Mockito.mock( ServletOutputStream.class )); HttpServletRequest req = Mockito.mock( HttpServletRequest.class ); Mockito.when( req.getServletPath()).thenReturn( CSS_STYLESHEET ); ServletConfig sc = Mockito.mock( ServletConfig.class ); ServletContext ctx = Mockito.mock( ServletContext.class ); Mockito.when( sc.getServletContext()).thenReturn( ctx ); WebAdminInterceptionServlet servlet = new WebAdminInterceptionServlet(); servlet.init( sc ); File dir = this.folder.newFolder(); servlet.karafEtc = dir.getAbsolutePath(); File f = new File( dir, OVERRIDE_CSS ); Utils.writeStringInto( "something", f ); Assert.assertTrue( f.exists()); servlet.doGet( req, resp ); Mockito.verify( req, Mockito.atLeast( 1 )).getServletPath(); Mockito.verify( resp, Mockito.only()).getOutputStream(); }
@Test public void testInterceptionServlet_withKarafEtc_overrideBanner() throws Exception { HttpServletResponse resp = Mockito.mock( HttpServletResponse.class ); Mockito.when( resp.getOutputStream()).thenReturn( Mockito.mock( ServletOutputStream.class )); HttpServletRequest req = Mockito.mock( HttpServletRequest.class ); Mockito.when( req.getServletPath()).thenReturn( BANNER_IMAGE ); ServletConfig sc = Mockito.mock( ServletConfig.class ); ServletContext ctx = Mockito.mock( ServletContext.class ); Mockito.when( sc.getServletContext()).thenReturn( ctx ); WebAdminInterceptionServlet servlet = new WebAdminInterceptionServlet(); servlet.init( sc ); File dir = this.folder.newFolder(); servlet.karafEtc = dir.getAbsolutePath(); File f = new File( dir, OVERRIDE_IMAGE ); Utils.writeStringInto( "something", f ); Assert.assertTrue( f.exists()); servlet.doGet( req, resp ); Mockito.verify( req, Mockito.atLeast( 1 )).getServletPath(); Mockito.verify( resp, Mockito.only()).getOutputStream(); }
@Test public void testInterceptionServlet_withKarafEtc_overrideCss_cssIsNotAFile() throws Exception { HttpServletResponse resp = Mockito.mock( HttpServletResponse.class ); Mockito.when( resp.getOutputStream()).thenReturn( Mockito.mock( ServletOutputStream.class )); HttpServletRequest req = Mockito.mock( HttpServletRequest.class ); Mockito.when( req.getServletPath()).thenReturn( CSS_STYLESHEET ); ServletConfig sc = Mockito.mock( ServletConfig.class ); ServletContext ctx = Mockito.mock( ServletContext.class ); Mockito.when( sc.getServletContext()).thenReturn( ctx ); WebAdminInterceptionServlet servlet = new WebAdminInterceptionServlet(); servlet.init( sc ); File dir = this.folder.newFolder(); servlet.karafEtc = dir.getAbsolutePath(); File f = new File( dir, OVERRIDE_CSS ); Assert.assertTrue( f.mkdir()); servlet.doGet( req, resp ); Mockito.verify( req, Mockito.atLeast( 1 )).getServletPath(); Mockito.verify( resp, Mockito.only()).sendError( HttpServletResponse.SC_NOT_FOUND ); }
@Test public void testInterceptionServlet_withKarafEtc_invalidRequestedFile() throws Exception { HttpServletResponse resp = Mockito.mock( HttpServletResponse.class ); Mockito.when( resp.getOutputStream()).thenReturn( Mockito.mock( ServletOutputStream.class )); HttpServletRequest req = Mockito.mock( HttpServletRequest.class ); Mockito.when( req.getServletPath()).thenReturn( "unexpected" ); ServletConfig sc = Mockito.mock( ServletConfig.class ); ServletContext ctx = Mockito.mock( ServletContext.class ); Mockito.when( sc.getServletContext()).thenReturn( ctx ); WebAdminInterceptionServlet servlet = new WebAdminInterceptionServlet(); servlet.init( sc ); File dir = this.folder.newFolder(); servlet.karafEtc = dir.getAbsolutePath(); servlet.doGet( req, resp ); Mockito.verify( req, Mockito.atLeast( 1 )).getServletPath(); Mockito.verify( resp, Mockito.only()).sendError( HttpServletResponse.SC_FORBIDDEN ); }