Esempio n. 1
0
 public static Map<String, String> route(String method, String path, String headers, String host) {
   for (Route route : routes) {
     Map<String, String> args = route.matches(method, path, headers, host);
     if (args != null) {
       args.put("action", route.action);
       return args;
     }
   }
   return new HashMap<String, String>(16);
 }
  public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders) {
    if (mRoutes == null) {
      mRoutes = RoutesParser.init(mContext, mRouteRes);
    }

    for (Route r : mRoutes) {
      if (r.matches(request)) {
        return r.generateResponse(mContext, request);
      }
    }

    return ResponseGenerator.FACTORY.newHttpResponse(HTTP_1_1, 404, ResponseGenerator.CONTEXT);
  }
Esempio n. 3
0
 public static void routeOnlyStatic(Http.Request request) {
   for (Route route : routes) {
     try {
       if (route.matches(request.method, request.path, request.format, request.domain) != null) {
         break;
       }
     } catch (Throwable t) {
       if (t instanceof RenderStatic) {
         throw (RenderStatic) t;
       }
       if (t instanceof NotFound) {
         throw (NotFound) t;
       }
     }
   }
 }
Esempio n. 4
0
 public static Route route(Http.Request request) {
   if (Logger.isTraceEnabled()) {
     Logger.trace("Route: " + request.path + " - " + request.querystring);
   }
   // request method may be overriden if a x-http-method-override parameter is given
   if (request.querystring != null && methodOverride.matches(request.querystring)) {
     Matcher matcher = methodOverride.matcher(request.querystring);
     if (matcher.matches()) {
       if (Logger.isTraceEnabled()) {
         Logger.trace(
             "request method %s overriden to %s ", request.method, matcher.group("method"));
       }
       request.method = matcher.group("method");
     }
   }
   for (Route route : routes) {
     Map<String, String> args =
         route.matches(request.method, request.path, request.format, request.domain);
     if (args != null) {
       request.routeArgs = args;
       request.action = route.action;
       if (args.containsKey("format")) {
         request.format = args.get("format");
       }
       if (request.action.indexOf("{") > -1) { // more optimization ?
         for (String arg : request.routeArgs.keySet()) {
           request.action = request.action.replace("{" + arg + "}", request.routeArgs.get(arg));
         }
       }
       if (request.action.equals("404")) {
         throw new NotFound(route.path);
       }
       return route;
     }
   }
   // Not found - if the request was a HEAD, let's see if we can find a corresponding GET
   if (request.method.equalsIgnoreCase("head")) {
     request.method = "GET";
     Route route = route(request);
     request.method = "HEAD";
     if (route != null) {
       return route;
     }
   }
   throw new NotFound(request.method, request.path);
 }