thundr-proxy

Skeleton thundr module

License

License

GroupId

GroupId

com.threewks.thundr
ArtifactId

ArtifactId

thundr-proxy
Last Version

Last Version

0.0.4
Release Date

Release Date

Type

Type

jar
Description

Description

thundr-proxy
Skeleton thundr module
Project URL

Project URL

http://github.com/kuhnza/thundr-proxy/
Project Organization

Project Organization

3wks
Source Code Management

Source Code Management

https://github.com/kuhnza/thundr-proxy

Download thundr-proxy

How to add to project

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

Dependencies

compile (5)

Group / Artifact Type Version
com.threewks.thundr : thundr jar 0.9.11
com.threewks.thundr : thundr-http jar 0.9.11
commons-io : commons-io jar 2.4
com.google.guava : guava jar 14.0.1
com.google.http-client : google-http-client jar 1.17.0-rc

provided (1)

Group / Artifact Type Version
javax.servlet : servlet-api jar 2.5

test (3)

Group / Artifact Type Version
junit : junit jar 4.11
org.hamcrest : hamcrest-all jar 1.3
org.mockito : mockito-all jar 1.9.5

Project Modules

There are no modules declared in this project.

Thundr Proxy

An intercepting reverse proxy module for Thundr.

Build Status

Usage

In your ApplicationModule:

@Override
public void requires(DependencyRegistry dependencyRegistry) {
    super.requires(dependencyRegistry);

    dependencyRegistry.addDependency(ProxyModule.class);
}

Configuring Routes

Thundr Proxy registers a new ActionResolver which means that you register routes you're interested proxying in your routes.json like so:

{
  "/foo/**": {
    "GET": "proxy",
    "POST": "proxy",
    "PUT": "proxy",
    "DELETE": "proxy"
  }
}

Of course you can omit any HTTP verbs that you'd prefer to ignore.

Adding Rules

Once you've defined a route you'd like to proxy you'll need to define a proxy rule. A rule allows you to define what to do when your app receives a request for one of the routes you've registered.

Let's say you wanted to reverse proxy requests beginning with the path /foo over to the upstream server http://www.example.org/ instead of handling it in your app. Using the included SimpleProxyRule class in your injection configuration add the following:

SimpleProxyRule rule = new SimpleProxyRule("/foo", "http://www.example.org/");
ProxyActionResolver resolver = injectionContext.get(ProxyActionResolver.class);
resolver.registerProxyRule(rule);

Now with this rule registered any requests to /foo will be proxied to the upstream server. So therefore:

/foo/bar/baz -> http://www.example.org/bar/baz

Registering interceptors

Rules also support interceptors. Let's say you want to block all requests to the path /foo/bar/qux and return a 403 without hitting the upstream server.

Expanding on the code in our injection configuration above we can create a new ProxyInterceptor to intercept those calls and modify the response. For this purpose we will extend the BaseProxyInterceptor which provides noop stubs for the ProxyInterceptor interface so we only have to implement the interceptor method we're interested in:

ProxyInterceptor interceptor = new BaseProxyInterceptor() {
  /* Here we use the 'before' method which intercepts the request before it has been 
   * proxied to the upstream server. */
  public Response before(Request inboundRequest, Request proxyRequest) {
    if (inboundRequest.path().startsWith("/foo/bar/qux")) {
      return new Response().status(403);  // terminate proxying immediately and return 403 response to user
    }
    return null;  // null response signals that we should continue proxying request
  }
};
rule.addInterceptor(interceptor);

Interceptor events

The interceptor framework supports 3 events.

  • before - intercepts request before being proxied to the upstream server
  • after - intercepts response from upstream server before it has been proxied back to the requestor
  • exception - intercepts exceptions occurring in any rule or interceptor

Custom rule classes

If the SimpleProxyRule class just isn't cutting it then creating your own is easy. Just extend BaseProxyRule or implement the ProxyRule interface.

Versions

Version
0.0.4
0.0.3
0.0.2
0.0.1