routd

Sonatype helps open source projects to set up Maven repositories on https://oss.sonatype.org/

License

License

GroupId

GroupId

org.bigtesting
ArtifactId

ArtifactId

routd
Last Version

Last Version

1.0.7
Release Date

Release Date

Type

Type

jar
Description

Description

routd
Sonatype helps open source projects to set up Maven repositories on https://oss.sonatype.org/
Project URL

Project URL

http://maven.apache.org
Source Code Management

Source Code Management

http://github.com/lantunes/routd

Download routd

How to add to project

<!-- https://jarcasting.com/artifacts/org.bigtesting/routd/ -->
<dependency>
    <groupId>org.bigtesting</groupId>
    <artifactId>routd</artifactId>
    <version>1.0.7</version>
</dependency>
// https://jarcasting.com/artifacts/org.bigtesting/routd/
implementation 'org.bigtesting:routd:1.0.7'
// https://jarcasting.com/artifacts/org.bigtesting/routd/
implementation ("org.bigtesting:routd:1.0.7")
'org.bigtesting:routd:jar:1.0.7'
<dependency org="org.bigtesting" name="routd" rev="1.0.7">
  <artifact name="routd" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.bigtesting', module='routd', version='1.0.7')
)
libraryDependencies += "org.bigtesting" % "routd" % "1.0.7"
[org.bigtesting/routd "1.0.7"]

Dependencies

test (1)

Group / Artifact Type Version
junit : junit jar 4.5

Project Modules

There are no modules declared in this project.

Routd


Routd is a small and simple URL routing library for Java. It is currently not trie-based, and may not be as performant as trie-based implementations. However, it does contain a tree-based implementation, which should be sufficient for most cases.

Getting Started


Download the latest .jar. Or, add the following dependency to your pom.xml:

<dependency>
    <groupId>org.bigtesting</groupId>
    <artifactId>routd</artifactId>
    <version>1.0.7</version>
</dependency>

Or, if you are using Gradle:

dependencies {
	compile "org.bigtesting:routd:1.0.7"
}

Usage


There are two classes of interest in the Routd library: Route and Router.

First, you create Routes:

Route r1 = new Route("/");
Route r2 = new Route("/client/:name");
Route r3 = new Route("/customer/:id<[0-9]+>");
Route r4 = new Route("/user/*/account");

...and add them to a Router:

Router router = new TreeRouter();
router.add(r1);
router.add(r2);
router.add(r3);
router.add(r4);

Then you can retrieve the routes:

assertEquals(r1, router.route("/"));
assertEquals(r2, router.route("/client/Tim"));
assertEquals(r3, router.route("/customer/123"));
assertEquals(r4, router.route("/user/john/account"));

With the routes, you can get the parameter values from a path:

Route route = new Route("/customer/:id/named/:name/*");
String path = "/customer/1/named/John/Doe";

assertEquals("1", route.getNamedParameter("id", path));
assertEquals("John", route.getNamedParameter("name", path));
assertNull(route.getNamedParameter("blah", path));
assertEquals("Doe", route.splat(path)[0]);

...and you can also get the path parameter elements directly:

Route route = new Route("/customer/:id<[0-9]+>/named/:name/*");

List<PathElement> elements = route.getPathElements();
assertEquals(5, elements.size());

StaticPathElement firstStaticElement = (StaticPathElement)elements.get(0);
assertEquals("customer", firstStaticElement.name());
assertEquals(0, firstStaticElement.index());

NamedParameterElement firstNamedElement = (NamedParameterElement)elements.get(1);
assertEquals("id", firstNamedElement.name());
assertEquals(1, firstNamedElement.index());
assertEquals("[0-9]+", firstNamedElement.regex());

NamedParameterElement secondNamedElement = (NamedParameterElement)elements.get(3);
assertEquals("name", secondNamedElement.name());
assertEquals(3, secondNamedElement.index());
assertNull(secondNamedElement.regex());

SplatParameterElement splatElement = (SplatParameterElement)elements.get(4);
assertEquals(4, splatElement.index());

Notes


Routers currently expect URL paths to be undecoded. That is, paths should retain any URL encodings. The router will handle any URL decoding required.

Versions

Version
1.0.7
1.0.6
1.0.5
1.0.4
1.0.3
1.0.2
1.0.1
1.0.0