static InputStream openFile( String fileName ){ InputStream stream = open(new File(fileName)); final ClassLoader loader = Thread.currentThread().getContextClassLoader(); if ( stream == null ) stream = loader.getResourceAsStream( fileName ); if ( stream == null ) { if ( fileName.charAt( 0 ) == '/' ) fileName = fileName.substring(1); stream = loader.getResourceAsStream( fileName ); } return stream; }
static String readFileAsString( String fileName, String encoding) { try ( InputStream file = openFile( fileName ) ) { if ( file == null ) throw new IllegalStateException( "File not found: " + fileName ); return readFileAsString( file, encoding ); } catch ( IOException cause ) { throw new IllegalStateException( cause ); } }
static byte[] readFileAsBytes(InputStream inputStream) { return readFile(inputStream).toByteArray(); }
String readAndParseTemplate(){ final Map<String, Object> templateVariables = readTemplateVariables(); final String loginTemplatePage = config.getLoginTemplate(); final String template = SystemResource.readFileAsString( loginTemplatePage, "UTF-8" ); return applyVariables( template, templateVariables ); }
private void deployDefaultStaticRouting(final Config staticConfig, DeploymentContext context){ final String location = staticConfig.getString("location"); log.info( "Enabling static routing at folder: " + location ); final ResourceManager resourceManager = SystemResource.loadResourceManagerFor( location ); final HttpHandler fallbackHandler = context.fallbackHandler(); final ResourceHandler handler = new ResourceHandler(resourceManager, fallbackHandler); final String welcomeFile = staticConfig.getString("welcome-file", "index.html"); handler.setWelcomeFiles( welcomeFile ); context.fallbackHandler( handler ); } }
private void deployWelcomeFile(Config staticConfig, DeploymentContext context) { final String welcomeFile = staticConfig.getString("welcome-file", "index.html"); try { final String location = staticConfig.getString("location"); final String encoding = staticConfig.getString("encoding", "UTF-8"); final String fileContent = SystemResource.readFileAsString( location + "/" + welcomeFile, encoding); final WelcomeFileHandler welcomeFileHandler = new WelcomeFileHandler(fileContent, context.fallbackHandler()); context.fallbackHandler(welcomeFileHandler); } catch ( Throwable cause ) { log.debug( "Could not locate welcome file: " + welcomeFile, cause ); } }
private void deployWebJarStaticRouting(Config staticConfig, DeploymentContext context) { if ( staticConfig.getBoolean("webjar-enabled" ) ) { final String urlPrefix = staticConfig.getString("webjar-url-prefix").replaceFirst( "/$", "" ); log.info( "Enabling static routing for webjars at '" + urlPrefix + "'" ); final String webJarInternalLocation = staticConfig.getString("webjar-location", DEFAULT_WEBJAR_LOCATION); final ResourceManager resourceManager = SystemResource.loadResourceManagerFor( webJarInternalLocation ); final HttpHandler webjarHandler = new WebJarHttpHandler( new ResourceHandler(resourceManager, new WebJarNotFound( context.fallbackHandler() ) ), URLMatcher.compile( urlPrefix + "/{path}" ), context.fallbackHandler()); context.fallbackHandler( webjarHandler ); } }
static String readFileAsString(InputStream inputStream, String encoding) { try { return readFile(inputStream).toString(encoding); } catch ( UnsupportedEncodingException cause ) { throw new IllegalStateException( cause ); } }