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

Commit

Permalink
Http: code clean up and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jaguililla committed Jul 6, 2015
1 parent e0512e9 commit 79323de
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 36 deletions.
11 changes: 3 additions & 8 deletions http/src/main/java/sabina/Fault.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package sabina;

import static java.lang.String.format;
import static sabina.util.Checks.checkArgument;

import java.util.function.BiConsumer;
Expand All @@ -40,13 +41,7 @@ public Fault (final Class<T> exception, final BiConsumer<T, Request> handler) {
this.exception = exception;
}

/**
* Invoked when an exception that is mapped to this handler occurs during routing
*
* @param exception The exception that was thrown during routing
* @param request The request object providing information about the HTTP request
*/
public void handle (final T exception, final Request request) {
handler.accept (exception, request);
@Override public String toString () {
return format ("Fault: %s", exception.getName ());
}
}
15 changes: 2 additions & 13 deletions http/src/main/java/sabina/Route.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public final class Route {
public final String path;
public final String acceptType;
public final HttpMethod method;
private final Handler handler;
public final Handler handler;

public final List<String> routeParts;

Expand Down Expand Up @@ -99,18 +99,7 @@ public boolean isFilter () {
return method == AFTER || method == BEFORE;
}

/**
* Invoked when a req is made on this route's corresponding path e.g. '/hello'.
*
* @param req The request object providing information about the HTTP request.
*
* @return The content to be set in the response.
*/
public Object handle (final Request req) {
return handler.apply (req);
}

@Override public String toString () {
return format ("%s %s [%s]", method, path, acceptType);
return format ("Route: %s %s (%s)", method, path, acceptType);
}
}
2 changes: 1 addition & 1 deletion http/src/main/java/sabina/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public final class Server implements Router {
private static final int DEFAULT_PORT = 4567;
private static final String DEFAULT_HOST = "0.0.0.0";

public static Server server (int port) {
public static Server server (final int port) {
return new Server (port);
}

Expand Down
18 changes: 9 additions & 9 deletions http/src/main/java/sabina/route/SimpleRouteMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package sabina.route;

import static java.util.Collections.EMPTY_LIST;
import static java.util.Collections.singletonList;
import static java.util.logging.Logger.getLogger;
import static java.util.stream.Collectors.toList;
Expand All @@ -34,10 +35,9 @@
* @author Per Wendel
*/
final class SimpleRouteMatcher implements RouteMatcher {
private static final Logger LOG = getLogger (SimpleRouteMatcher.class.getName ());
private static final List<Route> EMPTY = new ArrayList<> (0);
// private static final Logger LOG = getLogger (SimpleRouteMatcher.class.getName ());

private final Map<HttpMethod, List<Route>> routes = new HashMap<> ();
private final Map<HttpMethod, List<Route>> routeMap = new HashMap<> ();

/** Holds a map of Exception classes and associated handlers. */
private final Map<Class<? extends Exception>, Fault<?>> exceptionMap = new HashMap<> ();
Expand All @@ -49,9 +49,9 @@ final class SimpleRouteMatcher implements RouteMatcher {
*/
@Override public void processRoute (Route target) {
HttpMethod method = target.method;
if (!routes.containsKey (method))
routes.put (method, new ArrayList<> ());
routes.get (method).add (target);
if (!routeMap.containsKey (method))
routeMap.put (method, new ArrayList<> ());
routeMap.get (method).add (target);
}

/**
Expand Down Expand Up @@ -170,11 +170,11 @@ private boolean routeWithGivenAcceptType (String bestMatch) {
private List<Route> findTargetsForRequestedRoute (
HttpMethod httpMethod, String path) {

return routes.containsKey (httpMethod)?
routes.get(httpMethod).stream ()
return routeMap.containsKey (httpMethod)?
routeMap.get(httpMethod).stream ()
.filter (entry -> matches (entry, path))
.collect (toList ()) :
EMPTY;
EMPTY_LIST;
}

// TODO: I believe this feature has impacted performance. Optimization?
Expand Down
8 changes: 4 additions & 4 deletions http/src/main/java/sabina/server/MatcherFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public MatcherFilter (
String bodyContent = null;

try {
bodyContent = onFilter (BEFORE, httpReq, httpRes, uri, acceptType, bodyContent);
bodyContent = onFilter (BEFORE, httpReq, httpRes, uri, acceptType, null);

final HttpMethod httpMethod = HttpMethod.valueOf (httpMethodStr);
RouteMatch match = routeMatcher.findTarget (httpMethod, uri, acceptType);
Expand Down Expand Up @@ -174,7 +174,7 @@ private String handleTargetRoute (
if (!aTarget.isFilter ()) {
request = new Request (aMatch, aHttpReq, aHttpRes);

Object element = aTarget.handle (request);
Object element = aTarget.handler.apply (request);
result = element != null? element.toString () : null;
}
if (result != null) {
Expand All @@ -187,7 +187,7 @@ private String handleTargetRoute (
catch (Exception e) {
Fault<Exception> handler = (Fault<Exception>)routeMatcher.findHandler (e.getClass ());
if (handler != null && request != null) {
handler.handle (e, request);
handler.handler.accept (e, request);
}
else {
LOG.severe (e.getMessage ());
Expand All @@ -214,7 +214,7 @@ private String onFilter (

for (RouteMatch filterMatch : matchSet) {
final Request request = new Request (filterMatch, httpRequest, httpResponse);
filterMatch.entry.handle (request);
filterMatch.entry.handler.apply (request);

final String bodyAfterFilter = request.response.body ();
if (bodyAfterFilter != null)
Expand Down
2 changes: 1 addition & 1 deletion http/src/test/java/sabina/RouteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ public void routeWithEmptyAcceptType () {
assert action.path.equals ("path");
assert action.acceptType.equals ("type");
assert action.method.equals (AFTER);
assert action.toString ().equals ("AFTER path [type]");
assert action.toString ().equals ("Route: AFTER path (type)");
}
}

0 comments on commit 79323de

Please sign in to comment.