Exemple #1
0
  private void setHeaders(RequestContext rc, String fileName, Boolean cache, Map options)
      throws Exception {
    HttpServletRequest req = rc.getReq();
    HttpServletResponse res = rc.getRes();

    String characterEncoding = MapUtil.getNestedValue(options, "characterEncoding");
    characterEncoding = (characterEncoding != null) ? characterEncoding : "UTF-8";

    String contentType = MapUtil.getNestedValue(options, "contentType");
    contentType = (contentType != null) ? contentType : servletContext.getMimeType(fileName);
    contentType = (contentType != null) ? contentType : FileUtil.getExtraMimeType(fileName);

    req.setCharacterEncoding(characterEncoding);
    res.setContentType(contentType);

    // TODO: needs to support "cache=true"

    // --------- Set Cache --------- //

    if (cache) {
      /*
       * NOTE: for now we remove this, in the case of a CSS, we do not know the length, since it is a template
       * contentLength = resourceFile.length();
       *
       * if (contentLength < Integer.MAX_VALUE) { res.setContentLength((int) contentLength); } else {
       * res.setHeader("content-length", "" + contentLength); }
       */
      // This content will expire in 1 hours.
      final int CACHE_DURATION_IN_SECOND = 60 * 60 * 1; // 1 hours
      final long CACHE_DURATION_IN_MS = CACHE_DURATION_IN_SECOND * 1000;
      long now = System.currentTimeMillis();

      res.addHeader("Cache-Control", "max-age=" + CACHE_DURATION_IN_SECOND);
      res.addHeader("Cache-Control", "must-revalidate"); // optional
      res.setDateHeader("Last-Modified", now);
      res.setDateHeader("Expires", now + CACHE_DURATION_IN_MS);
    } else {
      res.setHeader("Pragma", "No-cache");
      res.setHeader("Cache-Control", "no-cache,no-store,max-age=0");
      res.setDateHeader("Expires", 1);
    }

    // --------- Set Cache --------- //
  }
Exemple #2
0
 /**
  * Return the value in the model map (the m.**) with the appropriate type and fall back value.
  *
  * @param <T>
  * @param namePath path deliminated with the ".". Note that the "m." should not be in this
  *     namePath.
  * @param cls The type of the value to be casted to
  * @param defaultValue The fall back value
  * @return
  */
 public <T> T getModelValue(String namePath, Class<T> cls, T defaultValue) {
   return MapUtil.getNestedValue(webMap, namePath, cls, defaultValue);
 }