/url-scheme-registry

Java java.net.URL Scheme Registry Helper

Primary LanguageJavaApache License 2.0Apache-2.0

Library to make it easy to register new URL schemes for java.net.URL. Consider:

@Test
public void testRegisterHandler() throws Exception
{
    UrlSchemeRegistry.register("dinner", DinnerHandler.class);

    assertThat(read(new URL("dinner://steak"))).isEqualTo("steak");
}

which uses the URL handler:

public class DinnerHandler extends URLStreamHandler
{
  @Override
  protected URLConnection openConnection(URL u) throws IOException
  {
    final String breakfast = u.getHost();
    return new URLConnection(u)
    {
      @Override
      public void connect() throws IOException { }

      @Override
      public InputStream getInputStream() throws IOException
      {
          return new ByteArrayInputStream(breakfast.getBytes());
      }
    };
  }
}

The library uses the java.protocol.handler.pkgs system property and runtime generated classes following the correct package and class name conventions to accomplish this. It does not use URL.setURLStreamHandlerFactory(...); for this (so code using this library should run fine inside Tomcat which does use that method).

Releases are distributed via Maven Central. There are two variants, one with a dependency on cglib-2.2.2

<dependency>
    <groupId>org.skife.url</groupId>
    <artifactId>url-scheme-registry</artifactId>
    <version>0.0.1</version>
</dependency>

or and one which renamespaces and bundles cglib-2.2.2.

<dependency>
    <groupId>org.skife.url</groupId>
    <artifactId>url-scheme-registry</artifactId>
    <classifier>nodep</classifier>
    <version>0.0.1</version>
</dependency>

Good luck!