Пример #1
0
  public NetResponse pullFile(
      String strUrl, String strFileName, IRhoSession oSession, Hashtable headers) throws Exception {
    IRAFile file = null;
    NetResponse resp = null;

    m_nMaxPacketSize = RhoClassFactory.getNetworkAccess().getMaxPacketSize();
    m_bFlushFileAfterWrite = RhoConf.getInstance().getBool("use_persistent_storage");
    m_bCancel = false;

    try {

      if (!strFileName.startsWith("file:")) {
        try {
          strFileName = FilePath.join(RhoClassFactory.createFile().getDirPath(""), strFileName);
        } catch (IOException x) {
          LOG.ERROR("getDirPath failed.", x);
          throw x;
        }
      }

      file = RhoClassFactory.createFSRAFile();
      file.open(strFileName, "rw");
      file.seek(file.size());

      do {
        resp = pullFile1(strUrl, file, file.size(), oSession, headers);
      } while (!m_bCancel
          && (resp == null || resp.isOK())
          && m_nCurDownloadSize > 0
          && m_nMaxPacketSize > 0);

    } finally {
      if (file != null) {
        file.close();
        file = null;
      }
    }

    copyHashtable(m_OutHeaders, headers);

    return resp != null && !m_bCancel
        ? resp
        : makeResponse("", IHttpConnection.HTTP_INTERNAL_ERROR);
  }
Пример #2
0
  NetResponse pullFile1(
      String strUrl, IRAFile file, long nStartPos, IRhoSession oSession, Hashtable headers)
      throws Exception {
    String strRespBody = null;
    InputStream is = null;
    int code = -1;

    try {
      closeConnection();
      m_connection = RhoClassFactory.getNetworkAccess().connect(strUrl, true);

      if (oSession != null) {
        String strSession = oSession.getSession();
        if (strSession != null && strSession.length() > 0)
          m_connection.setRequestProperty("Cookie", strSession);
      }

      m_connection.setRequestProperty("Connection", "keep-alive");

      if (nStartPos > 0 || m_nMaxPacketSize > 0) {
        if (m_nMaxPacketSize > 0)
          m_connection.setRequestProperty(
              "Range", "bytes=" + nStartPos + "-" + (nStartPos + m_nMaxPacketSize - 1));
        else m_connection.setRequestProperty("Range", "bytes=" + nStartPos + "-");
      }
      writeHeaders(headers);

      m_connection.setRequestMethod(IHttpConnection.GET);

      code = m_connection.getResponseCode();

      LOG.INFO("getResponseCode : " + code);

      m_nCurDownloadSize = 0;
      readHeaders(headers);

      if (code == IHttpConnection.HTTP_RANGENOTSATISFY) code = IHttpConnection.HTTP_PARTIAL_CONTENT;
      else {
        if (code != IHttpConnection.HTTP_OK && code != IHttpConnection.HTTP_PARTIAL_CONTENT) {
          LOG.ERROR("Error retrieving data: " + code);
          if (code == IHttpConnection.HTTP_UNAUTHORIZED) oSession.logout();

          if (code != IHttpConnection.HTTP_INTERNAL_ERROR) {
            is = m_connection.openInputStream();
            strRespBody = readFully(is, getResponseEncoding());
          }
        } else {
          int nRead = 0;

          is = m_connection.openInputStream();
          byte[] byteBuffer = new byte[1024 * 20];

          do {
            nRead = /*bufferedReadByByte(m_byteBuffer, is);*/ is.read(byteBuffer);
            if (nRead > 0) {
              file.write(byteBuffer, 0, nRead);

              if (m_bFlushFileAfterWrite) file.sync();

              m_nCurDownloadSize += nRead;
            }
          } while (!m_bCancel && nRead >= 0);

          if (code == IHttpConnection.HTTP_OK
              || (code == IHttpConnection.HTTP_PARTIAL_CONTENT && isFinishDownload()))
            m_nCurDownloadSize = 0;
        }
      }
    } finally {
      if (is != null)
        try {
          is.close();
        } catch (IOException exc) {
        }

      closeConnection();
    }

    return makeResponse(strRespBody != null ? strRespBody : "", code);
  }