Exemplo n.º 1
0
  protected boolean httpServeFile(String strContType) throws IOException {
    String strPath = uri.getPath();
    // if ( !strPath.startsWith("/apps") )
    //	strPath = "/apps" + strPath;

    LOG.TRACE("httpServeFile: " + strPath);

    if (!isDbFilesPath(strPath)) {
      if (strContType.equals("application/javascript")) {
        responseData = RhoRuby.loadFile(strPath);
        if (responseData == null) {
          String str = "";
          responseData = new ByteArrayInputStream(str.getBytes());
        }
      } else responseData = RhoRuby.loadFile(strPath);
    } else {
      if (strPath.startsWith("/apps/app/db/db-files"))
        strPath = strPath.substring(9); // remove /apps/app
      else strPath = strPath.substring(5); // remove /apps
    }

    if (responseData == null) {

      SimpleFile file = null;
      try {
        file = RhoClassFactory.createFile();
        String strFileName = strPath;
        //				if ( strFileName.startsWith("/apps") )
        //					strFileName = strPath.substring(5);

        file.open(strFileName, true, true);
        responseData = file.getInputStream();
        if (responseData != null) {
          contentLength = (int) file.length();
        }
        m_file = file;
      } catch (Exception exc) {
        if (file != null) file.close();
      }
    } else {
      if (responseData != null) {
        contentLength = responseData.available();
      }
    }

    if (responseData == null) return false;

    if (strContType.length() > 0) resHeaders.addProperty("Content-Type", strContType);

    resHeaders.addProperty("Content-Length", Integer.toString(contentLength));

    // resHeaders.addProperty("Date",getLocalHttpTimeString());
    // resHeaders.addProperty("Cache-control", "public, max-age=3600" );
    // resHeaders.addProperty("Expires", "Thu, 01 Dec 2010 16:00:00 GMT" );

    return true;
  }
Exemplo n.º 2
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";
    }
  }