protected boolean httpGetFile(String strContType) throws IOException { if (!isDbFilesPath(uri.getPath()) && !isKnownExtension(uri.getPath()) && strContType.length() == 0) { String strTemp = FilePath.join(uri.getPath(), "/"); if (RhoSupport.findClass(strTemp + "controller") != null) return false; int nPos = findIndex(uri.getPath()); if (nPos >= 0) { String url = uri.getPath(); // + (nPos == 0 ? ".iseq" : ""); Properties reqHash = new Properties(); this.doDispatch(reqHash, url); // RubyValue res = RhoRuby.processIndexRequest(url);//erb-compiled should load from class // processResponse(res); // RhodesApp.getInstance().keepLastVisitedUrl(url_external); return true; } if (httpGetIndexFile()) return true; } return httpServeFile(strContType); }
/** Construct connection using URI * */ public RhoConnection(URI _uri) { url_external = _uri.toString(); uri_orig = _uri; uri = new URI(url_external); if (!uri.getPath().startsWith("/apps")) uri.setPath("/apps" + uri.getPath()); else uri.setPath(uri.getPath()); }
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; }
public void resetUrl(String url) { url_external = url; uri = new URI(url_external); if (!uri.getPath().startsWith("/apps")) uri.setPath("/apps" + uri.getPath()); else uri.setPath(uri.getPath()); method = ""; responseCode = 200; responseMsg = "OK"; contentLength = -1; reqHeaders.clear(); resHeaders.clear(); requestProcessed = false; try { clean(); } catch (IOException exc) { LOG.ERROR("clean failed.", exc); } }
protected boolean httpGetIndexFile() { String strIndex = null; String slash = ""; if (uri.getPath() != null && uri.getPath().length() > 0) slash = uri.getPath().charAt(uri.getPath().length() - 1) == '/' ? "" : "/"; for (int i = 0; i < m_arIndexes.length; i++) { String name = uri.getPath() + slash + m_arIndexes[i]; String nameClass = name; if (nameClass.endsWith(".iseq")) nameClass = nameClass.substring(0, nameClass.length() - 5); if (RhoSupport.findClass(nameClass) != null || RhoRuby.resourceFileExists(name)) { strIndex = name; break; } } if (strIndex == null) return false; respondMoved(strIndex); return true; }
void respondNotFound(String strError) { responseCode = HTTP_NOT_FOUND; responseMsg = "Not found"; if (strError != null && strError.length() != 0) responseMsg += ".Error: " + strError; String strBody = "Page not found: " + uri.getPath(); contentLength = strBody.length(); responseData = new ByteArrayInputStream(strBody.getBytes()); resHeaders.addProperty("Content-Type", "text/html"); resHeaders.addProperty("Content-Length", Integer.toString(contentLength)); }
public String getFile() { LOG.TRACE("getFile"); if (uri != null) { String path = uri.getPath(); if (path != null) { int s0 = path.lastIndexOf('/'); int s1 = path.lastIndexOf('\\'); if (s1 > s0) s0 = s1; if (s0 < 0) s0 = 0; return path.substring(s0); } } return null; }
void doDispatch(Properties reqHash, String strIndex) throws IOException { reqHash.setProperty("request-method", this.method); reqHash.setProperty("request-uri", uri.getPath()); reqHash.setProperty("request-query", uri.getQueryString()); if (postData != null && postData.size() > 0) { if (!RHOCONF().getBool("log_skip_post")) LOG.TRACE(postData.toString()); reqHash.setProperty("request-body", postData.toString()); } RubyValue res = RhoRuby.processRequest(reqHash, reqHeaders, resHeaders, strIndex); processResponse(res); RhodesApp.getInstance().keepLastVisitedUrl(url_external); LOG.INFO("dispatch end"); }
public void processRequest() throws IOException { if (!requestProcessed) { String strErr = ""; LOG.TRACE("processRequest: " + getURL()); String strReferer = reqHeaders != null ? reqHeaders.getPropertyIgnoreCase("Referer") : ""; if (getRef() != null && getRef().length() > 0 && strReferer != null && strReferer.equalsIgnoreCase(uri_orig.getPathNoFragment())) { respondNotModified(); } else { String strContType = getContentType(); if (uri.getPath().startsWith("/apps/public")) { httpServeFile(strContType); } else { if (this.method.equals("POST") || strContType.length() == 0 || strContType.indexOf("application/x-www-form-urlencoded") >= 0) { if (dispatch()) { requestProcessed = true; return; } } if ( /*this.method == "GET" &&*/ httpGetFile(strContType)) { // }else if ( dispatch() ){ } else { respondNotFound(strErr); } } } requestProcessed = true; } }
String getContentType() { String contType = reqHeaders.getProperty("Content-Type"); if (contType == null || contType.length() == 0) contType = reqHeaders.getProperty("content-type"); if (contType != null && contType.length() > 0) return contType; String path = uri.getPath(); int nPoint = path.lastIndexOf('.'); String strExt = ""; if (nPoint > 0) strExt = path.substring(nPoint + 1); if (strExt.equals("png")) return "image/png"; else if (strExt.equals("jpeg")) return "image/jpeg"; else if (strExt.equals("jpg")) return "image/jpg"; else if (strExt.equals("js")) return "application/javascript"; else if (strExt.equals("css")) return "text/css"; else if (strExt.equals("gif")) return "image/gif"; else if (strExt.equals("html") || strExt.equals("htm")) return "text/html"; else if (strExt.equals("txt")) return "text/plain"; return ""; }
protected boolean dispatch() throws IOException { // LOG.INFO("dispatch start : " + uri.getPath()); UrlParser up = new UrlParser(uri.getPath()); String apps = up.next(); String application; if (apps.equalsIgnoreCase("apps")) application = up.next(); else application = apps; String model = up.next(); if (model == null || model.length() == 0) return false; if (checkRhoExtensions(application, model)) return true; // Convert CamelCase to underscore_case StringBuffer cName = new StringBuffer(); byte[] modelname = model.getBytes(); char ch; for (int i = 0; i != model.length(); ++i) { if (modelname[i] >= (byte) 'A' && modelname[i] <= (byte) 'Z') { ch = (char) (modelname[i] + 0x20); if (i != 0) cName.append('_'); } else ch = (char) modelname[i]; cName.append(ch); } String controllerName = cName.toString(); String strCtrl = "apps/" + application + '/' + model + '/' + controllerName + "_controller"; if (RhoSupport.findClass(strCtrl) == null) { strCtrl = "apps/" + application + '/' + model + '/' + "controller"; if (RhoSupport.findClass(strCtrl) == null) return false; } Properties reqHash = new Properties(); String actionid = up.next(); String actionnext = up.next(); if (actionid != null && actionid.length() > 0) { if (actionid.length() > 6 && actionid.startsWith("%7B") && actionid.endsWith("%7D")) actionid = "{" + actionid.substring(3, actionid.length() - 3) + "}"; if (actionid.length() > 2 && actionid.charAt(0) == '{' && actionid.charAt(actionid.length() - 1) == '}') { reqHash.setProperty("id", actionid); reqHash.setProperty("action", actionnext); } else { reqHash.setProperty("id", actionnext); reqHash.setProperty("action", actionid); } } reqHash.setProperty("application", application); reqHash.setProperty("model", model); doDispatch(reqHash, null); if (actionid != null && actionid.length() > 2 && actionid.charAt(0) == '{' && actionid.charAt(actionid.length() - 1) == '}') SyncThread.getInstance().addobjectnotify_bysrcname(model, actionid); return true; }