Coder Social home page Coder Social 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"

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.

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

Watchers

James Cloos avatar Georg Koester avatar

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.