private static NanoResponse getResource ( RequestType request_type , String url ) { byte [ ] bytes = _cache . get ( url ) ; if ( bytes == null ) { // Try-with-resource try ( InputStream resource = water . init . JarHash . getResource2 ( url ) ) { if ( resource != null ) { try { bytes = toByteArray ( resource ) ; } catch ( IOException e ) { Log . err ( e ) ; } // PP 06-06-2014 Disable caching for now so that the browser // always gets the latest sources and assets when h2o-client is rebuilt. // TODO need to rethink caching behavior when h2o-dev is merged into h2o. // // if (bytes != null) { // byte[] res = _cache.putIfAbsent(url, bytes); // if (res != null) bytes = res; // Racey update; take what is in the _cache //} // } } catch ( IOException ignore ) { } } if ( bytes == null || bytes . length == 0 ) // No resource found? return response404 ( "Resource " + url , request_type ) ; int i = url . lastIndexOf ( ' ' ) ; String mime ; switch ( url . substring ( i + 1 ) ) { case "js" : mime = MIME_JS ; break ; case "css" : mime = MIME_CSS ; break ; case "htm" : case "html" : mime = MIME_HTML ; break ; case "jpg" : case "jpeg" : mime = MIME_JPEG ; break ; case "png" : mime = MIME_PNG ; break ; case "svg" : mime = MIME_SVG ; break ; case "gif" : mime = MIME_GIF ; break ; case "woff" : mime = MIME_WOFF ; break ; default : mime = MIME_DEFAULT_BINARY ; } NanoResponse res = new NanoResponse ( HTTP_OK , mime , new ByteArrayInputStream ( bytes ) ) ; res . addHeader ( "Content-Length" , Long . toString ( bytes . length ) ) ; return res ; }
Returns the response containing the given uri with the appropriate mime type .