Giter Club home page Giter Club logo

juri's Introduction

JURI

URIs like they should be

URI parsing and manipulation for Java.

Parsing and reading

Pass any URL into the constructor (similiar to java.net.URI but without the bugs in parsing parameters, user info, etc.):

JURI uri = JURI.parse("http://user:[email protected]:81/path/index.html?q=books#fragment");
JURI uri = JURI.create(new URI("http://user:[email protected]:81/path/index.html?q=books#fragment"));

Use property methods to get at the various parts (like java.net.URI but more convenient):

uri.getScheme()          // http
uri.getUser()            // user
uri.getPassword()        // pass
uri.getHost()            // www.test.com
uri.getPort()            // 81
uri.getPath()            // /path/index.html
uri.getPathSegments()    // ["path","index.html"]
uri.getQueryParameters() // {"q": "books"} as Map<String, Collecion<String>>
uri.getFragment()        // fragment

Modifying and writing

JURI can modify and extend your URIs. Setters and other modifying methods are chainable:

JURI.parse(uri)
    .setScheme(scheme)
    .setUserInfo(user, password)
    .setHost(host)
    .setPort(port)
    .addPathSegment(directory)
    .addQueryParameter(key, value)
    .replaceQueryParameter(key, value)
    .setFragment()
    .toString();

Do magic with query parameters:

uri.addQueryParameter("q", "value");
uri.replaceQueryParameter("q", "value");
uri.removeQueryParameter("q");
uri.clearQueryParameters();
JURI.parse("...?q=X&q=Y").replaceQueryParameter("q", "value").toString() // -> "...?q=value"

Read, set and alter paths:

JURI uri = JURI.parse("blah/blub");
uri.addPathSegment("sub%20dir");
uri.getRawPath();                            // -> "blah/blub/sub%20dir"
uri.getPath();                               // -> "blah/blub/sub dir"
uri.navigate("../relative/path").toString(); // -> "blah/relative/path""

The navigate method tries to mimic browser behaviour: 'What happens if you are on the current URI and click on the (relative or absolute) link:

uri = JURI.parse("http://example.com?c=d#asdf");
uri.navigate("/a/b.html?a=b#hash").toString();              // -> "http://example.com/a/b.html?a=b#hash"
uri.navigate("../c.html").toString();                       // -> "http://example.com/c.html"
uri.navigate("g.html").toString();                          // -> "http://example.com/g.html"
uri.navigate("#anchor").toString();                         // -> "http://example.com/g.html#anchor"
uri.navigate("/a/b.html#anchor").toString();                // -> "http://example.com/a/b.html#anchor"
uri.navigate("http://www.google.com/search?q=2").toString() // -> "http://www.google.com/search?q=2"

Completely free (MIT license)

No need to worry about including the list of authors in your software product.

Dependencies

  • Guava for escaping.
  • A lib for Nullable etc.
  • StringUtils from Apache lang-commons.
  • slf4j-api for logging.

Building

Before building copy gradle.properties.example to gradle.properties. Build using gradle build.

Related work

Why should I use JURI instead of java.net.URI? URI helps when parsing URIs. Not when building or mutating them.

Why should I use JURI instead of java.net.URL? URL does many things. And many unexpected things. An example: What do you expect this line to do:

url1.equals(url2);

Do you expect it to do blocking DNS requests and compare IP adresses? If using java.net.URL you better should...

Slightly inspired by jsuri.

juri's People

Contributors

tec avatar wielmic avatar georgkoester avatar

Stargazers

Leonard 'Lenny' Daume avatar  avatar  avatar

Watchers

 avatar charles bourasseau avatar James Cloos avatar  avatar Lars Quentmeier avatar  avatar Philipp Sander avatar  avatar  avatar  avatar

Forkers

georgkoester tec

juri's Issues

Fat/Uber-Jar

It would be helpful to have a release version wich integrates all dependencies.
This can be achieved by

  • removing all dependencies from code or
  • including all dependencies in the jar / osgi bundle (so called uber-jar or fat-jar)

Osgi bundle

It would be great to have an OSGI bundle. It could be enough to add a snippet to the build.gradle like this, but this needs testing:

apply plugin: 'osgi'

jar {
    from "LICENSE.txt"
    from "README.md"
    manifest {
        instruction 'Bundle-Vendor', 'Pidoco GmbH'
        instruction 'Bundle-Description', '''URI manipulation made easy'''
        instruction 'Bundle-DocURL', 'github.com/pidoco/juri'
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.