Skip to content
This repository has been archived by the owner on Mar 4, 2021. It is now read-only.

Commit

Permalink
Refactor and runner implementation (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaguililla committed Oct 3, 2014
1 parent efd4438 commit bf0e13a
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 172 deletions.
10 changes: 7 additions & 3 deletions core/src/main/java/sabina/Action.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@

package sabina;

import static com.google.common.base.Strings.isNullOrEmpty;
import static java.lang.String.format;

/**
* Functionality used in both Route and Filter.
* TODO Move empty to utilities
*
* @author Per Wendel
*/
Expand All @@ -26,13 +28,15 @@ abstract class Action {
public final HttpMethod method;

protected Action (final HttpMethod method, final String path, final String acceptType) {
if (empty (path) || empty (acceptType) || method == null)
if (isNullOrEmpty (path) || isNullOrEmpty (acceptType) || method == null)
throw new IllegalArgumentException ();

this.path = path;
this.acceptType = acceptType;
this.method = method;
}

private boolean empty (String str) { return str == null || str.isEmpty (); }
@Override public String toString () {
return format ("%s %s [%s]", method, path, acceptType);
}
}
211 changes: 50 additions & 161 deletions core/src/main/java/sabina/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public final class Context {
public final Request request;
Expand All @@ -30,150 +31,62 @@ public final class Context {
this.response = res;
}

public Map<String, String> params () {
return request.params ();
}

public String cookie (final String name) {
return request.cookie (name);
}

public Session session () {
return request.session ();
}

public int port () {
return request.port ();
}

public Object attribute (final String name) {
return request.attribute (name);
}

public String queryString () {
return request.queryString ();
}

public String userAgent () {
return request.userAgent ();
}

public QueryParams queryMap () {
return request.queryMap ();
}

public Set<String> queryParams () {
return request.queryParams ();
}

public Set<String> attributes () {
return request.attributes ();
}

/*
* Request delegates
*/
public Map<String, String> params () { return request.params (); }
public String cookie (final String name) { return request.cookie (name); }
public Session session () { return request.session (); }
public int port () { return request.port (); }
public Object attribute (final String name) { return request.attribute (name); }
public String queryString () { return request.queryString (); }
public String userAgent () { return request.userAgent (); }
public QueryParams queryMap () { return request.queryMap (); }
public Set<String> queryParams () { return request.queryParams (); }
public Set<String> attributes () { return request.attributes (); }
public Session session (final boolean create) { return request.session (create); }
public String pathInfo () { return request.pathInfo (); }
public String contextPath () { return request.contextPath (); }
public String servletPath () { return request.servletPath (); }
public String params (final String name) { return request.params (name); }
public String host () { return request.host (); }
public String scheme () { return request.scheme (); }
public String headers (final String name) { return request.headers (name); }
public String contentType () { return request.contentType (); }
public Set<String> headers () { return request.headers (); }
public Map<String, String> cookies () { return request.cookies (); }
public int contentLength () { return request.contentLength (); }
public String url () { return request.url (); }
public String[] splat () { return request.splat (); }
public String queryParams (final String name) { return request.queryParams (name); }
public QueryParams queryMap (final String key) { return request.queryMap (key); }
public String ip () { return request.ip (); }
public HttpServletRequest requestRaw () { return request.raw (); }
public String requestBody () { return request.body (); }
public String requestMethod () { return request.requestMethod (); }
public void attribute (final String name, final Object value) {
request.attribute (name, value);
}

public Session session (final boolean create) {
return request.session (create);
}

public String pathInfo () {
return request.pathInfo ();
}

public String contextPath () {
return request.contextPath ();
}

public String servletPath () {
return request.servletPath ();
}

public String params (final String name) {
return request.params (name);
}

public String host () {
return request.host ();
}

public String scheme () {
return request.scheme ();
}

public String headers (final String name) {
return request.headers (name);
}

public String contentType () {
return request.contentType ();
}

public Set<String> headers () {
return request.headers ();
}

public Map<String, String> cookies () {
return request.cookies ();
}

public int contentLength () {
return request.contentLength ();
}

public String url () {
return request.url ();
}

public String[] splat () {
return request.splat ();
}

public String queryParams (final String name) {
return request.queryParams (name);
}

public QueryParams queryMap (final String key) {
return request.queryMap (key);
}

public String ip () {
return request.ip ();
}

public HttpServletRequest requestRaw () {
return request.raw ();
}

public String requestBody () {
return request.body ();
}

public String requestMethod () {
return request.requestMethod ();
}

public void status (final int statusCode) {
response.status (statusCode);
}
/*
* Response delegates
*/
public void body (final String body) { response.body (body); }
public void redirect (final String location) { response.redirect (location); }
public void type (final String contentType) { response.type (contentType); }
public HttpServletResponse responseRaw () { return response.raw (); }
public String responseBody () { return response.body (); }
public void status (final int statusCode) { response.status (statusCode); }
public void removeCookie (final String name) { response.removeCookie (name); }

public void cookie (final String name, final String value, final int maxAge) {
response.cookie (name, value, maxAge);
}

public void removeCookie (final String name) {
response.removeCookie (name);
}

public void header (final String name, final String value) {
response.header (name, value);
}

public void body (final String body) {
response.body (body);
}

public void cookie (
final String path,
final String name,
Expand All @@ -198,30 +111,12 @@ public void cookie (
response.cookie (name, value, maxAge, secured);
}

public void redirect (final String location) {
response.redirect (location);
}

public void type (final String contentType) {
response.type (contentType);
}

public HttpServletRequest responseRaw () {
return request.raw ();
}

public String responseBody () {
return request.body ();
}

/**
* Immediately stops a request within a filter or route
* NOTE: When using this don't catch exceptions of type HaltException, or if catched,
* re-throw otherwise halt will not work.
*/
public void halt () {
throw new HaltException ();
}
public void halt () { throw new HaltException (); }

/**
* Immediately stops a request within a filter or route with specified status code
Expand All @@ -230,9 +125,7 @@ public void halt () {
*
* @param status the status code.
*/
public void halt (final int status) {
throw new HaltException (status);
}
public void halt (final int status) { throw new HaltException (status); }

/**
* Immediately stops a request within a filter or route with specified body content
Expand All @@ -241,9 +134,7 @@ public void halt (final int status) {
*
* @param body The body content.
*/
public void halt (final String body) {
throw new HaltException (body);
}
public void halt (final String body) { throw new HaltException (body); }

/**
* Immediately stops a request within a filter or route with specified status code and body
Expand All @@ -258,13 +149,11 @@ public void halt (final int status, final String body) {
throw new HaltException (status, body);
}

// TODO Implement these methods!

/*
* TODO Implement these methods!
*/
public void pass () {}

public void redirect () {}

public void template (final String template, final Object params) {}

public void template (final String template, final String layout, final Object params) {}
}
7 changes: 4 additions & 3 deletions core/src/main/java/sabina/Filter.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package sabina;

import static com.google.common.base.Strings.isNullOrEmpty;
import static sabina.HttpMethod.*;

import java.util.function.Consumer;
Expand All @@ -31,7 +32,7 @@
public final class Filter extends Action {
public static final String ALL_PATHS = "+/*paths";

private static final String DEFAUT_CONTENT_TYPE = "text/html";
private static final String DEFAULT_CONTENT_TYPE = "text/html";

public static FilterNode after (Consumer<Context> handler) {
return new FilterNode (after, handler);
Expand Down Expand Up @@ -72,7 +73,7 @@ public static PathNode before (String path, String contentType, Consumer<Context
* @param path The filter path which is used for matching. (e.g. /hello, users/:name).
*/
Filter (final HttpMethod method, final String path, final Consumer<Context> handler) {
this (method, path, DEFAUT_CONTENT_TYPE, handler);
this (method, path, DEFAULT_CONTENT_TYPE, handler);
}

Filter (
Expand All @@ -81,7 +82,7 @@ public static PathNode before (String path, String contentType, Consumer<Context
final String acceptType,
final Consumer<Context> handler) {

super (method, path, acceptType);
super (method, path, isNullOrEmpty (acceptType)? DEFAULT_CONTENT_TYPE : acceptType);

if (handler == null)
throw new IllegalArgumentException ();
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/sabina/Route.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package sabina;

import static com.google.common.base.Strings.isNullOrEmpty;
import static sabina.HttpMethod.*;

import java.util.function.Consumer;
Expand Down Expand Up @@ -219,7 +220,7 @@ public static PathNode trace (
final String acceptType,
final Function<Context, Object> handler) {

super (method, path, acceptType);
super (method, path, isNullOrEmpty (acceptType)? DEFAULT_ACCEPT_TYPE : acceptType);

if (handler == null)
throw new IllegalArgumentException ();
Expand Down
19 changes: 15 additions & 4 deletions core/src/main/java/sabina/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ public static void serve (List<Parameter<?>> parameters, Node... aHandler) {

public Server (Node... nodes) {
List<Action> actions = getActions (nodes);

for (Action a : actions)
addRoute (a);
}
Expand All @@ -158,12 +159,19 @@ Action getAction (MethodNode node) {
String aPath = "";

for (Node p = node.parent; p != null; p = p.parent)
if (p instanceof PathNode)
aPath = ((PathNode)p).path + aPath;
else if (p instanceof ContentTypeNode)
// TODO Handle '/' properly at the beginning (and also at the end /b/ != /b)
if (p instanceof PathNode) {
String nodePath = ((PathNode)p).path;
aPath = nodePath.endsWith ("/")?
nodePath + aPath :
nodePath + '/' + aPath;
}
else if (p instanceof ContentTypeNode) {
aContentType += ((ContentTypeNode)p).contentType;
else
}
else {
throw new IllegalStateException ("Unsupported node type");
}

return node instanceof FilterNode?
new Filter (node.method, aPath, aContentType, ((FilterNode)node).handler) :
Expand Down Expand Up @@ -268,6 +276,9 @@ public synchronized void stop () {
}

protected synchronized void addRoute (Action action) {
// LOG.fine (">>> " + action);
System.out.println (">>> " + action);

routeMatcher.parseValidateAddRoute (
action.method + " '" + action.path + "'", action.acceptType, action);
}
Expand Down

0 comments on commit bf0e13a

Please sign in to comment.