Exemplo n.º 1
0
  /** @see org.wyona.yanel.core.api.attributes.VersionableV2#getView(String, String) */
  public View getView(String viewId, String revisionName) throws Exception {
    // TODO: Check first whether revision of node exists...
    View view = new View();

    view.setInputStream(getNode().getRevision(revisionName).getInputStream());
    view.setMimeType(getMimeType());
    view.setEncoding(getResourceConfigProperty("encoding"));

    return view;
  }
Exemplo n.º 2
0
  /** @see org.wyona.yanel.core.api.attributes.ViewableV2#getView(String) */
  public View getView(String viewId) throws Exception {
    if (!exists()) {
      throw new ResourceNotFoundException("No such repository node: " + getRepoPath());
    }

    View view = new View();
    view.setMimeType(getMimeType());
    view.setEncoding(getResourceConfigProperty("encoding"));

    String range = getEnvironment().getRequest().getHeader("Range");
    if (range
        != null) { // INFO: Also see
                   // http://stackoverflow.com/questions/12768812/video-streaming-to-ipad-does-not-work-with-tapestry5, http://balusc.blogspot.ch/2009/02/fileservlet-supporting-resume-and.html
      if (!range.equals("bytes=0-")) {
        log.warn("DEBUG: Specific range requested for node '" + getRepoPath() + "': " + range);
        String[] ranges = range.split("=")[1].split("-");
        int from = Integer.parseInt(ranges[0]);
        int to = Integer.parseInt(ranges[1]);
        int len = to - from + 1;
        view.setResponse(false); // INFO: In this case we write directly into the response...
        HttpServletResponse response = getEnvironment().getResponse();
        response.setStatus(206);
        response.setHeader("Accept-Ranges", "bytes");
        String responseRange = String.format("bytes %d-%d/%d", from, to, getSize());
        response.setHeader("Connection", "close");
        response.setHeader("Content-Range", responseRange);
        log.debug("Content-Range:" + responseRange);
        response.setDateHeader("Last-Modified", new Date().getTime());
        response.setContentLength(len);
        log.debug("Content length: " + len);

        OutputStream os = response.getOutputStream();
        InputStream is = new java.io.BufferedInputStream(getNode().getInputStream());

        try {
          byte[] buf = new byte[4096];
          is.skip(from);
          while (len != 0) {
            int read = is.read(buf, 0, len >= buf.length ? buf.length : len);
            if (read != -1) {
              os.write(buf, 0, read);
              len -= read;
            }
          }
        } catch (Exception e) {
          log.error(
              "Exception '"
                  + e.getMessage()
                  + "' while handling request '"
                  + getRepoPath()
                  + "' (Range: "
                  + range
                  + "), whereas see stack trace for details...");
          log.error(e, e);
        } finally {
          if (is != null) {
            is.close();
            log.warn(
                "DEBUG: Making sure to close input stream of '"
                    + getNode().getPath()
                    + "' (Range: "
                    + range
                    + ").");
          }
        }
        return view;
      } else {
        // log.debug("Range requested for node '" + getRepoPath()+ "': " + range);
      }
    } else {
      // log.debug("No range requested for node: " + getRepoPath());
    }

    view.setInputStream(getNode().getInputStream());

    return view;
  }