コード例 #1
0
ファイル: NetRequest.java プロジェクト: JamieDelton/rhodes
  private void readHeaders(Hashtable headers) throws Exception {
    if (headers != null) {
      m_OutHeaders = new Hashtable();
      for (int i = 0; ; i++) {
        String strField = m_connection.getHeaderFieldKey(i);
        if (strField == null && i > 0) break;

        if (strField != null) {
          String header_field = m_connection.getHeaderField(i);
          m_OutHeaders.put(strField.toLowerCase(), header_field);
        }
      }
    }
  }
コード例 #2
0
ファイル: NetRequest.java プロジェクト: JamieDelton/rhodes
  public void closeConnection() {
    if (m_connection != null) {
      try {
        m_connection.close();
      } catch (IOException exc) {
        LOG.ERROR("There was an error close connection", exc);
      }
    }

    m_connection = null;
  }
コード例 #3
0
ファイル: NetRequest.java プロジェクト: JamieDelton/rhodes
 private void writeHeaders(Hashtable headers) throws Exception {
   if (headers != null && headers.size() > 0) {
     Enumeration valsHeaders = headers.elements();
     Enumeration keysHeaders = headers.keys();
     while (valsHeaders.hasMoreElements()) {
       String strName = (String) keysHeaders.nextElement();
       String strValue = (String) valsHeaders.nextElement();
       m_connection.setRequestProperty(strName, strValue);
     }
   }
 }
コード例 #4
0
ファイル: NetRequest.java プロジェクト: JamieDelton/rhodes
  private boolean isFinishDownload() throws IOException {
    String strContRange = m_connection.getHeaderField("Content-Range");
    if (strContRange != null) {
      int nMinus = strContRange.indexOf('-');
      if (nMinus > 0) {
        int nSep = strContRange.indexOf('/', nMinus);
        if (nSep > 0) {
          String strHigh = strContRange.substring(nMinus + 1, nSep);
          String strTotal = strContRange.substring(nSep + 1);

          if (Integer.parseInt(strHigh) + 1 >= Integer.parseInt(strTotal)) return true;
        }
      }
    }

    return false;
  }
コード例 #5
0
ファイル: NetRequest.java プロジェクト: JamieDelton/rhodes
  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);
  }
コード例 #6
0
ファイル: NetRequest.java プロジェクト: JamieDelton/rhodes
  public NetResponse pushMultipartData(
      String strUrl,
      Vector /*Ptr<CMultipartItem*>&*/ arItems,
      IRhoSession oSession,
      Hashtable /*<String,String>**/ headers)
      throws Exception {
    String strRespBody = null;
    InputStream is = null;
    OutputStream os = null;
    int code = -1;

    m_bCancel = false;

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

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

      m_connection.setRequestProperty("Connection", "keep-alive");
      m_connection.setRequestProperty("content-type", szMultipartContType);
      writeHeaders(headers);
      m_connection.setRequestMethod(IHttpConnection.POST);

      // PUSH specific
      processMultipartItems(arItems);
      os = m_connection.openOutputStream();
      // write all items
      for (int i = 0; i < (int) arItems.size(); i++) {
        MultipartItem oItem = (MultipartItem) arItems.elementAt(i);
        os.write(oItem.m_strDataPrefix.getBytes(), 0, oItem.m_strDataPrefix.length());

        if (oItem.m_strFilePath.length() > 0) {

          SimpleFile file = null;
          InputStream fis = null;

          try {
            file = RhoClassFactory.createFile();
            file.open(oItem.m_strFilePath, true, true);

            if (!file.isOpened()) {
              LOG.ERROR("File not found: " + oItem.m_strFilePath);
              throw new RuntimeException("File not found:" + oItem.m_strFilePath);
            }

            fis = file.getInputStream();
            byte[] byteBuffer = new byte[1024 * 4];
            int nRead = 0;
            do {
              nRead = fis.read(byteBuffer);
              if (nRead > 0) os.write(byteBuffer, 0, nRead);
            } while (nRead > 0);
          } finally {
            if (fis != null)
              try {
                fis.close();
              } catch (IOException e) {
              }

            if (file != null)
              try {
                file.close();
              } catch (IOException e) {
              }
          }

        } else {
          os.write(oItem.m_strBody.getBytes(), 0, oItem.m_strBody.length());
        }
      }
      os.write(szMultipartPostfix.getBytes(), 0, szMultipartPostfix.length());
      // os.flush();
      // PUSH specific

      is = m_connection.openInputStream();
      code = m_connection.getResponseCode();

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

      readHeaders(headers);
      copyHashtable(m_OutHeaders, headers);

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

        if (code != IHttpConnection.HTTP_INTERNAL_ERROR)
          strRespBody = readFully(is, getResponseEncoding());

      } else {
        long len = m_connection.getLength();
        LOG.INFO("fetchRemoteData data size:" + len);

        strRespBody = readFully(is, getResponseEncoding());

        LOG.INFO("fetchRemoteData data readFully.");
      }

    } finally {
      try {
        if (is != null) is.close();
        if (os != null) os.close();

        closeConnection();

      } catch (IOException exc2) {
      }
    }

    return makeResponse(strRespBody, code);
  }
コード例 #7
0
ファイル: NetRequest.java プロジェクト: JamieDelton/rhodes
  public NetResponse doRequest(
      String strMethod, String strUrl, String strBody, IRhoSession oSession, Hashtable headers)
      throws Exception {
    String strRespBody = null;
    InputStream is = null;
    OutputStream os = null;
    int code = -1;

    m_bCancel = false;
    try {
      closeConnection();
      m_connection = RhoClassFactory.getNetworkAccess().connect(strUrl, m_bIgnoreSuffixOnSim);
      LOG.INFO("connection done");

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

      // m_connection.setRequestProperty("Connection", "keep-alive");
      // m_connection.setRequestProperty("Accept",
      // "application/x-www-form-urlencoded,application/json,text/html");

      if (strBody != null && strBody.length() > 0) {
        if (oSession != null)
          m_connection.setRequestProperty("Content-Type", oSession.getContentType());
        else m_connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

        writeHeaders(headers);
        LOG.INFO("writeHeaders done");
        m_connection.setRequestMethod(IHttpConnection.POST);

        os = m_connection.openOutputStream();
        os.write(strBody.getBytes(), 0, strBody.length());
        LOG.INFO("write body done");
      } else {
        writeHeaders(headers);
        m_connection.setRequestMethod(strMethod);
      }

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

      is = m_connection.openInputStream();
      LOG.INFO("openInputStream done");

      readHeaders(headers);
      copyHashtable(m_OutHeaders, headers);

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

        // if ( code != IHttpConnection.HTTP_INTERNAL_ERROR )
        {
          strRespBody = readFully(is, getResponseEncoding());

          if (code == IHttpConnection.HTTP_MOVED_TEMPORARILY
              || code == IHttpConnection.HTTP_MOVED_PERMANENTLY)
            LOG.INFO("Response body: " + strRespBody);
          else LOG.TRACE("Response body: " + strRespBody);
        }
      } else {
        long len = m_connection.getLength();
        LOG.INFO("fetchRemoteData data size:" + len);

        strRespBody = readFully(is, getResponseEncoding());

        LOG.INFO("fetchRemoteData data readFully.");
      }

    } finally {
      if (is != null)
        try {
          is.close();
        } catch (IOException exc) {
        }
      if (os != null)
        try {
          os.close();
        } catch (IOException exc) {
        }

      closeConnection();

      m_bIgnoreSuffixOnSim = true;
    }

    return makeResponse(strRespBody, code);
  }