public void handle(Request request, Response response) {
   String targetClean = request.getTarget().toLowerCase();
   if (targetClean.indexOf('?') >= 0) {
     targetClean = targetClean.substring(0, targetClean.indexOf('?'));
   }
   if (targetClean.startsWith("/")) {
     targetClean = targetClean.substring(1);
   }
   if (targetClean.endsWith("/")) {
     targetClean = targetClean.substring(0, targetClean.length() - 1);
   }
   Container obj;
   try {
     obj = containers.get(URLDecoder.decode(targetClean.trim(), "UTF-8").toLowerCase());
     if (obj == null) {
       String tmp = targetClean;
       while (tmp.contains("/") && obj == null) {
         tmp = tmp.substring(0, tmp.lastIndexOf('/'));
         obj = containers.get(URLDecoder.decode(tmp.trim(), "UTF-8").toLowerCase() + "/*");
       }
       if (obj != null) {
         targetClean = tmp;
       }
     }
     System.out.println(
         "Request: "
             + request.getTarget()
             + " ("
             + URLDecoder.decode(targetClean.trim(), "UTF-8").toLowerCase()
             + ") -> "
             + (obj != null ? obj.getClass().getSimpleName() : "null"));
     if (obj != null) {
       obj.handle(request, response);
       return;
     } else {
       try {
         PrintStream body = response.getPrintStream();
         response.setCode(404);
         body.println(
             "Not found: " + URLDecoder.decode(targetClean.trim(), "UTF-8").toLowerCase());
       } catch (Exception e2) {
       }
     }
   } catch (Exception e) {
     try {
       PrintStream body = response.getPrintStream();
       response.setCode(503);
       e.printStackTrace(body);
     } catch (Exception e2) {
     }
   }
   try {
     response.getPrintStream().close();
   } catch (Exception e) {
   }
 }
Ejemplo n.º 2
0
 private void setResponseFields(Response resp, ResponseParams rp) {
   final long time = System.currentTimeMillis();
   String contentType = getContentType(rp.staticFile, rp.message);
   resp.setContentType(contentType);
   resp.setValue("Server", "Mock");
   resp.setDate("Date", time);
   resp.setDate("Last-Modified", time);
   resp.setCode(rp.responseCode);
   for (Map.Entry<String, String> header : rp.headers.entrySet()) {
     resp.setValue(header.getKey(), header.getValue());
   }
 }