示例#1
1
  private static HttpResponse addEtag(
      HttpRequest nettyRequest, HttpResponse httpResponse, File file) {
    if (Play.mode == Play.Mode.DEV) {
      httpResponse.setHeader(CACHE_CONTROL, "no-cache");
    } else {
      String maxAge = Play.configuration.getProperty("http.cacheControl", "3600");
      if (maxAge.equals("0")) {
        httpResponse.setHeader(CACHE_CONTROL, "no-cache");
      } else {
        httpResponse.setHeader(CACHE_CONTROL, "max-age=" + maxAge);
      }
    }
    boolean useEtag = Play.configuration.getProperty("http.useETag", "true").equals("true");
    long last = file.lastModified();
    final String etag = "\"" + last + "-" + file.hashCode() + "\"";
    if (!isModified(etag, last, nettyRequest)) {
      if (nettyRequest.getMethod().equals(HttpMethod.GET)) {
        httpResponse.setStatus(HttpResponseStatus.NOT_MODIFIED);
      }
      if (useEtag) {
        httpResponse.setHeader(ETAG, etag);
      }

    } else {
      httpResponse.setHeader(LAST_MODIFIED, Utils.getHttpDateFormatter().format(new Date(last)));
      if (useEtag) {
        httpResponse.setHeader(ETAG, etag);
      }
    }
    return httpResponse;
  }
示例#2
0
  @Override
  public NettyHttpResponse error(Throwable error) {
    if (error instanceof TooLongFrameException) {
      response.setStatus(HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE);
    } else {
      response.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
    }
    String message = getStackTrace(error);
    header("Content-Type", "text/plain");
    content(message);
    flushResponse();

    exceptionHandler.uncaughtException(
        Thread.currentThread(), WebbitException.fromException(error, ctx.getChannel()));

    return this;
  }
示例#3
0
  public NettyHttpResponse error(Throwable error) {
    response.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
    String message = getStackTrace(error);
    header("Content-Type", "text/plain");
    content(message);
    flushResponse();

    exceptionHandler.uncaughtException(
        Thread.currentThread(), AradonRuntimeException.fromException(error, ctx.getChannel()));

    return this;
  }
  @Override
  public NettyHttpResponse error(Throwable error) {
    response.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
    String message = getStackTrace(error);
    header("Content-Type", "text/plain");
    header("Content-Length", message.length());
    content(message);
    flushResponse();

    exceptionHandler.uncaughtException(Thread.currentThread(), error);

    return this;
  }
示例#5
0
 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);
   }
 }
示例#6
0
 public void fire(Object something) {
   try {
     String result = (String) something;
     if (result.equals(RedisServer.TIME_OUT_STRING)) {
       logger.error("fire message timeout error, message id:" + getIdentify());
       response.setContent(
           ChannelBuffers.wrappedBuffer(
               JResultUtil.getFailureJsonString(CoreProxyHandler.RESULT_TIMEOUT).getBytes()));
       response.setStatus(HttpResponseStatus.OK);
     } else {
       logger.info("fire message success, message id:" + getIdentify());
       response.setContent(
           ChannelBuffers.wrappedBuffer(JResultUtil.getOkJsonString(result).getBytes()));
       response.setStatus(HttpResponseStatus.OK);
     }
   } catch (Exception e) {
     logger.error("fire message error, e:" + e.getMessage() + " result:" + something);
     response.setContent(
         ChannelBuffers.wrappedBuffer(
             JResultUtil.getFailureJsonString(CoreProxyHandler.RESULT_ERROR).getBytes()));
     response.setStatus(HttpResponseStatus.OK);
   }
   NettyHttpOutput.output(response, event.getChannel());
 }
 private void writeHead() {
   if (!headWritten) {
     HttpResponseStatus status =
         statusMessage == null
             ? HttpResponseStatus.valueOf(statusCode)
             : new HttpResponseStatus(statusCode, statusMessage);
     response.setStatus(status);
     if (chunked) {
       response.setHeader(Names.TRANSFER_ENCODING, HttpHeaders.Values.CHUNKED);
     } else if (contentLength == 0) {
       response.setHeader(HttpHeaders.Names.CONTENT_LENGTH, "0");
     }
     writeFuture = conn.write(response);
     headWritten = true;
   }
 }
示例#8
0
 @Override
 public NettyHttpResponse status(int status) {
   response.setStatus(HttpResponseStatus.valueOf(status));
   return this;
 }
 @Override
 public void setStatus(int sc, String msg) {
   response.setStatus(HttpResponseStatus.valueOf(sc));
 }