Exemple #1
0
  void processMultipartItems(Vector /*Ptr<CMultipartItem*>&*/ arItems) throws Exception {
    for (int i = 0; i < (int) arItems.size(); i++) {
      MultipartItem oItem = (MultipartItem) arItems.elementAt(i);

      if (oItem.m_strName.length() == 0) oItem.m_strName = "blob";

      if (oItem.m_strFileName.length() == 0) {
        if (oItem.m_strFilePath.length() > 0) {
          FilePath oPath = new FilePath(oItem.m_strFilePath);
          oItem.m_strFileName = oPath.getBaseName();
        }
        // else
        //    oItem.m_strFileName = "doesnotmatter.txt";
      }

      oItem.m_strDataPrefix = i > 0 ? "\r\n" : "";
      oItem.m_strDataPrefix +=
          "------------A6174410D6AD474183FDE48F5662FCC5\r\n"
              + "Content-Disposition: form-data; name=\"";
      oItem.m_strDataPrefix += oItem.m_strName + "\"";
      if (oItem.m_strFileName.length() > 0)
        oItem.m_strDataPrefix += "; filename=\"" + oItem.m_strFileName + "\"";
      oItem.m_strDataPrefix += "\r\n";
      if (oItem.m_strContentType != null && oItem.m_strContentType.length() > 0)
        oItem.m_strDataPrefix += "Content-Type: " + oItem.m_strContentType + "\r\n";

      long nContentSize = 0;
      if (oItem.m_strFilePath.length() > 0) {
        SimpleFile file = null;
        try {
          file = RhoClassFactory.createFile();
          file.open(oItem.m_strFilePath, true, true);
          nContentSize = file.length();
          if (!file.isOpened()) {
            LOG.ERROR("File not found: " + oItem.m_strFilePath);
            throw new RuntimeException("File not found:" + oItem.m_strFilePath);
          }
        } finally {
          if (file != null)
            try {
              file.close();
            } catch (IOException e) {
            }
        }
      } else nContentSize = oItem.m_strBody.length();

      if (oItem.m_strContentType != null && oItem.m_strContentType.length() > 0)
        oItem.m_strDataPrefix += "Content-Length: " + nContentSize + "\r\n";

      oItem.m_strDataPrefix += "\r\n";
    }
  }
Exemple #2
0
  public void saveToFile() {
    String strData = saveToString();
    SimpleFile oFile = null;

    try {
      oFile = RhoClassFactory.createFile();
      oFile.delete(getConfFilePath());

      oFile.open(getConfFilePath(), false, false);
      oFile.write(0, strData.getBytes());
      oFile.close();
    } catch (Exception exc) {
      if (oFile != null)
        try {
          oFile.close();
        } catch (IOException exc2) {
        }
    }
  }
Exemple #3
0
  void loadFromFile() {
    SimpleFile oFile = null;
    try {
      oFile = RhoClassFactory.createFile();
      oFile.open(getConfFilePath(), true, false);

      if (oFile.isOpened()) {
        String strSettings = oFile.readString();
        oFile.close();
        loadFromString(strSettings);
      }

    } catch (Exception exc) {
      if (oFile != null)
        try {
          oFile.close();
        } catch (IOException exc2) {
        }
    }
  }
Exemple #4
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);
  }