Ejemplo n.º 1
0
 /**
  * Find a route for method and path.
  *
  * @param httpMethod http method.
  * @param pathInfo path.
  * @return the route found (null if no route found).
  */
 protected Route findRoute(String httpMethod, String pathInfo) {
   if (!routes.containsKey(HttpMethod.fromValue(httpMethod))) {
     return null;
   }
   for (Route route : routes.get(HttpMethod.fromValue(httpMethod))) {
     if (route.isThisPath(pathInfo)) {
       return route;
     }
   }
   return null;
 }
Ejemplo n.º 2
0
 /**
  * Write http response.
  *
  * @param request http request.
  * @param response http response.
  * @param handlerResponse response of route handler.
  * @throws IOException in case of IO error.
  */
 private void writeHttpResponse(
     HttpServletRequest request, HttpServletResponse response, Response<?> handlerResponse)
     throws IOException {
   if (handlerResponse.getStatus() != null) {
     response.setStatus(handlerResponse.getStatus());
   } else if (handlerResponse.getAnswer() == null) {
     response.setStatus(HttpMethod.fromValue(request.getMethod()).getDefaultStatusWithNoContent());
   } else {
     response.setStatus(HttpMethod.fromValue(request.getMethod()).getDefaultStatus());
   }
   if (handlerResponse.getAnswer() != null) {
     response.setContentType("application/json");
     response.getOutputStream().print(gson.toJson(handlerResponse.getAnswer()));
     response.getOutputStream().close();
   }
 }