コード例 #1
0
ファイル: ServletWrapper.java プロジェクト: raff/play
  public static Request parseRequest(HttpServletRequest httpServletRequest) throws Exception {

    URI uri = new URI(httpServletRequest.getRequestURI());
    String method = httpServletRequest.getMethod().intern();
    String path = uri.getRawPath();
    if (path != null) {
      path = URLDecoder.decode(path, "utf-8");
    }
    String querystring =
        httpServletRequest.getQueryString() == null ? "" : httpServletRequest.getQueryString();

    if (Logger.isTraceEnabled()) {
      Logger.trace("httpServletRequest.getContextPath(): " + httpServletRequest.getContextPath());
      Logger.trace("request.path: " + path + ", request.querystring: " + querystring);
    }

    String contentType = null;
    if (httpServletRequest.getHeader("Content-Type") != null) {
      contentType =
          httpServletRequest.getHeader("Content-Type").split(";")[0].trim().toLowerCase().intern();
    } else {
      contentType = "text/html".intern();
    }

    if (httpServletRequest.getHeader("X-HTTP-Method-Override") != null) {
      method = httpServletRequest.getHeader("X-HTTP-Method-Override").intern();
    }

    InputStream body = httpServletRequest.getInputStream();
    boolean secure = httpServletRequest.isSecure();

    String url =
        uri.toString()
            + (httpServletRequest.getQueryString() == null
                ? ""
                : "?" + httpServletRequest.getQueryString());
    String host = httpServletRequest.getHeader("host");
    int port = 0;
    String domain = null;
    if (host.contains(":")) {
      port = Integer.parseInt(host.split(":")[1]);
      domain = host.split(":")[0];
    } else {
      port = 80;
      domain = host;
    }

    String remoteAddress = httpServletRequest.getRemoteAddr();

    boolean isLoopback = host.matches("^127\\.0\\.0\\.1:?[0-9]*$");

    final Request request =
        Request.createRequest(
            remoteAddress,
            method,
            path,
            querystring,
            contentType,
            body,
            url,
            host,
            isLoopback,
            port,
            domain,
            secure,
            getHeaders(httpServletRequest),
            getCookies(httpServletRequest));

    Request.current.set(request);
    Router.routeOnlyStatic(request);

    return request;
  }