Beispiel #1
0
  void saveExceededSizeError(HttpRequest nettyRequest, Request request, Response response) {

    String warning = nettyRequest.getHeader(HttpHeaders.Names.WARNING);
    String length = nettyRequest.getHeader(HttpHeaders.Names.CONTENT_LENGTH);
    if (warning != null) {
      Logger.trace("saveExceededSizeError: begin");
      try {
        StringBuilder error = new StringBuilder();
        error.append("\u0000");
        // Cannot put warning which is play.netty.content.length.exceeded
        // as Key as it will result error when printing error
        error.append("play.netty.maxContentLength");
        error.append(":");
        String size = null;
        try {
          size = JavaExtensions.formatSize(Long.parseLong(length));
        } catch (Exception e) {
          size = length + " bytes";
        }
        error.append(Messages.get(warning, size));
        error.append("\u0001");
        error.append(size);
        error.append("\u0000");
        if (request.cookies.get(Scope.COOKIE_PREFIX + "_ERRORS") != null
            && request.cookies.get(Scope.COOKIE_PREFIX + "_ERRORS").value != null) {
          error.append(request.cookies.get(Scope.COOKIE_PREFIX + "_ERRORS").value);
        }
        String errorData = URLEncoder.encode(error.toString(), "utf-8");
        Http.Cookie c = new Http.Cookie();
        c.value = errorData;
        c.name = Scope.COOKIE_PREFIX + "_ERRORS";
        request.cookies.put(Scope.COOKIE_PREFIX + "_ERRORS", c);
        Logger.trace("saveExceededSizeError: end");
      } catch (Exception e) {
        throw new UnexpectedException("Error serialization problem", e);
      }
    }
  }
 public void handleJsRequest(String path, HttpRequest req, HttpResponse resp) {
   if (req.containsHeader(IF_NONE_MATCH) || req.containsHeader(IF_MODIFIED_SINCE)) {
     resp.setStatus(HttpResponseStatus.NOT_MODIFIED);
     return;
   }
   String jsName = path.substring(1);
   ChannelBuffer buf = jsBufferMap.get(jsName);
   if (buf == null) {
     tryReloadJs();
     if (jsName == null) {
       log.error("需要访问的JS名称为空。[req: " + req.getUri() + "]");
     }
     buf = jsBufferMap.get(jsName);
   }
   if (buf != null) {
     resp.setContent(buf);
     resp.setHeader(CONTENT_LENGTH, buf.capacity());
     resp.setHeader(CONTENT_TYPE, "text/javascript; charset=UTF-8");
     resp.setHeader(CONTENT_ENCODING, "gzip");
     //			resp.setHeader(EXPIRES,  System.currentTimeMillis() + expired);
     StringBuilder etagBuilder =
         new StringBuilder("W/\"")
             .append(buf.capacity())
             .append("-")
             .append(System.currentTimeMillis())
             .append("\"");
     resp.setHeader(ETAG, etagBuilder.toString());
     LogRequestParser lrp = logRequestParserThreadLocal.get();
     if (lrp == null) {
       lrp = new LogRequestParser();
     }
     lrp.cdate.setTime(System.currentTimeMillis() + expired);
     resp.setHeader(EXPIRES, lrp.sdf.format(lrp.cdate));
   } else {
     resp.setStatus(HttpResponseStatus.NOT_FOUND);
   }
 }