Example #1
1
  /**
   * Still experimental
   *
   * @param rc
   * @param fileName
   * @param content
   * @param cache (for now, not supported, only no-cache)
   * @param options
   * @throws Exception
   */
  public void writeStringContent(
      RequestContext rc, String fileName, Reader contentReader, boolean cache, Map options)
      throws Exception {

    setHeaders(rc, fileName, cache, options);

    // --------- Stream File --------- //
    Writer ow = null;
    int realLength = 0;
    try {

      // create the reader/writer
      ow = rc.getRes().getWriter();

      char[] buffer = new char[BUFFER_SIZE];
      int readLength = contentReader.read(buffer);

      while (readLength != -1) {
        realLength += readLength;
        ow.write(buffer, 0, readLength);
        readLength = contentReader.read(buffer);
      }
    } catch (Exception e) {
      logger.error(e.getMessage());
    } finally {
      contentReader.close();
      if (ow != null) {
        ow.close();
      }
    }
  }
Example #2
0
  public void writeBinaryContent(
      RequestContext rc, String fileName, InputStream contentIS, boolean cache, Map options)
      throws Exception {
    OutputStream os = rc.getRes().getOutputStream();

    BufferedInputStream bis = new BufferedInputStream(contentIS);

    int realLength = 0;

    try {
      byte[] buffer = new byte[BUFFER_SIZE];
      int len = buffer.length;
      while (true) {
        len = bis.read(buffer);
        realLength += len;
        if (len == -1) break;
        os.write(buffer, 0, len);
      }

    } catch (Exception e) {
      logger.error(e.getMessage());
    } finally {
      os.close();
      contentIS.close();
      bis.close();
    }
  }
Example #3
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 --------- //
  }
  public static Map<?, ?> buildRequestModel(RequestContext rc) {
    HttpServletRequest request = rc.getReq();
    HttpServletResponse response = rc.getRes();

    HashMap<String, Object> requestMap = new HashMap<String, Object>();

    requestMap.put(MODEL_KEY_REQUEST_CONTEXT, rc);

    /*--------- Include user and auth ---------*/
    /**/
    Auth<?> auth = rc.getAuth();
    if (auth != null) {
      requestMap.put(MODEL_KEY_AUTH, auth);
      requestMap.put(MODEL_KEY_USER, auth.getUser());
    }

    /*--------- /Include user and auth ---------*/

    /* --------- Include the HTTPRequest and HTTPResponse/ --------- */
    requestMap.put(MODEL_KEY_HTTP_REQUEST, request);
    requestMap.put(MODEL_KEY_HTTP_RESPONSE, response);

    // add the context path
    requestMap.put(MODEL_KEY_CONTEXT_PATH, request.getContextPath());

    // add the pathInfo
    String pathInfo = rc.getPathInfo();
    requestMap.put(MODEL_KEY_PATH_INFO, pathInfo);

    // add the fullPath
    StringBuilder fullPathSB = new StringBuilder(request.getContextPath());
    fullPathSB.append(pathInfo);
    requestMap.put(MODEL_KEY_FULL_PATH, fullPathSB.toString());

    /* --------- Include the Request Params --------- */
    requestMap.put(MODEL_KEY_PARAMS, rc.getParamMap());
    /* --------- /Include the Request Params --------- */

    String queryString = request.getQueryString();
    requestMap.put(MODEL_KEY_QUERY_STRING, queryString);

    if (queryString != null && queryString.length() > 0) {
      requestMap.put(MODEL_KEY_HREF, fullPathSB.append('?').append(queryString));
    } else {
      requestMap.put(MODEL_KEY_HREF, fullPathSB.toString());
    }

    /* --------- Include Headers --------- */
    HashMap<String, Object> headersMap = new HashMap<String, Object>();
    requestMap.put(MODEL_KEY_HEADERS, headersMap);

    Enumeration headerNames = request.getHeaderNames();
    while (headerNames.hasMoreElements()) {
      String headerName = (String) headerNames.nextElement();
      String headerValue = request.getHeader(headerName);
      headersMap.put(headerName, headerValue);
    }
    /* --------- /Include Headers --------- */

    /* --------- Include Cookies --------- */
    requestMap.put(MODEL_KEY_COOKIES, rc.getCookieMap());
    /* --------- /Include Cookies --------- */

    // --------- Include the WebState --------- //
    requestMap.put("webState", new WebStateProxy(rc));
    // --------- Include the WebState --------- //

    return requestMap;
  }