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