Esempio n. 1
0
  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);
  }
Esempio n. 2
0
  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);
  }