Skip to content

Instantly share code, notes, and snippets.

@miere
Last active October 19, 2017 13:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miere/4294d1d64f806a39bdae8642c548f55f to your computer and use it in GitHub Desktop.
Save miere/4294d1d64f806a39bdae8642c548f55f to your computer and use it in GitHub Desktop.

WebJar

I just created the bellow class.

public interface WebJarHandler {

  static HttpHandler withLocation( webJarInternalLocation ) {
    final ResourceManager resourceManager = loadResourceManagerFor( webJarInternalLocation );
    return new ResourceHandler(resourceManager, new WebJarNotFound( context.fallbackHandler() ) )
  }

  static ResourceManager loadResourceManagerFor(String location) {
    final File locationAsFile = new File(location);
    if ( locationAsFile.exists() ) {
      final boolean isCaseSensitive = !OS_NAME.contains("win");
      return new FileResourceManager(locationAsFile, 100, isCaseSensitive );
    }
    final ClassLoader classLoader = SystemResource.class.getClassLoader();
    return new ClassPathResourceManager( classLoader, location );
  }
}

And used it just like this:

final String DEFAULT_WEBJAR_LOCATION = "META-INF/resources/webjars/";
final Undertow server = Undertow.builder()
  .addHttpListener(8080, "localhost")
  .setHandler( WebJarHandler.withLocation(DEFAULT_WEBJAR_LOCATION) ).build();
server.start();
@miere
Copy link
Author

miere commented Oct 19, 2017

// TODO: check syntax

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment