Example #1
0
 /** 假定js都是只读的,如有更新必然体现在文件名上标记版本。 */
 public void tryReloadJs() {
   if (System.currentTimeMillis() - lastReloadTime < jsReloadPeriodMs) {
     return;
   }
   lastReloadTime = System.currentTimeMillis();
   Map<String, ChannelBuffer> newBufferMap = null; // = new HashMap<String, ChannelBuffer>();
   for (File f : new File(confAdmin.getFullPathRelatedToConfRoot("js")).listFiles()) {
     String name = f.getName();
     if (name.endsWith(".js")) {
       if (jsBufferMap.containsKey(name)) {
         continue;
       }
       if (newBufferMap == null) {
         newBufferMap = new HashMap<String, ChannelBuffer>(jsBufferMap);
       }
       // TODO: use new io
       InputStream in = null;
       try {
         in = new FileInputStream(f);
         ByteArrayOutputStream bao = new ByteArrayOutputStream();
         GZIPOutputStreamEx gzo = new GZIPOutputStreamEx(bao, Deflater.BEST_COMPRESSION);
         gzo.write(trackUrlExpBa);
         byte[] buf = new byte[1024];
         int c = 0;
         while ((c = in.read(buf)) != -1) {
           gzo.write(buf, 0, c);
         }
         gzo.close();
         in.close();
         byte[] ba = bao.toByteArray();
         ChannelBuffer cbuf = ChannelBuffers.directBuffer(ba.length);
         cbuf.writeBytes(ba);
         newBufferMap.put(name, cbuf);
       } catch (IOException e) { // never happen
         log.error(e);
       }
     }
   }
   if (newBufferMap != null) {
     jsBufferMap = newBufferMap;
   }
 }
Example #2
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);
   }
 }