private void processResponse(RubyValue res) { if (res != null && res != RubyConstant.QNIL && res instanceof RubyHash) { RubyHash resHash = (RubyHash) res; RubyValue resBody = null; RubyArray arKeys = resHash.keys(); RubyArray arValues = resHash.values(); for (int i = 0; i < arKeys.size(); i++) { String strKey = arKeys.get(i).toString(); if (strKey.equals("request-body")) resBody = arValues.get(i); else if (strKey.equals("status")) responseCode = arValues.get(i).toInt(); else if (strKey.equals("message")) responseMsg = arValues.get(i).toString(); else resHeaders.addProperty(strKey, arValues.get(i).toString()); } String strBody = ""; if (resBody != null && resBody != RubyConstant.QNIL) strBody = resBody.toRubyString().toString(); if (!RHOCONF().getBool("log_skip_post")) LOG.TRACE(strBody); try { responseData = new ByteArrayInputStream(strBody.getBytes("UTF-8")); } catch (java.io.UnsupportedEncodingException exc) { LOG.ERROR("Error getting utf-8 body :", exc); } if (responseData != null) contentLength = Integer.parseInt(resHeaders.getPropertyIgnoreCase("Content-Length")); } }
void setSearchNotification(String strUrl, String strParams) throws Exception { LOG.INFO("Set search notification. Url :" + strUrl + "; Params: " + strParams); String strFullUrl = getNet().resolveUrl(strUrl); if (strFullUrl.length() > 0) { synchronized (m_mxSyncNotifications) { m_pSearchNotification = new SyncNotification(strFullUrl, strParams, true); } LOG.INFO(" Done Set search notification. Url :" + strFullUrl + "; Params: " + strParams); } }
void callLoginCallback(SyncNotification oNotify, int nErrCode, String strMessage) { try { if (getSync().isStoppedByUser()) return; String strBody = "error_code=" + nErrCode; strBody += "&error_message=" + URI.urlEncode(strMessage != null ? strMessage : ""); strBody += "&rho_callback=1"; LOG.INFO("Login callback: " + oNotify.toString() + ". Body: " + strBody); callNotify(oNotify, strBody); } catch (Exception exc) { LOG.ERROR("Call Login callback failed.", exc); } }
private void createBrowserField() { LOG.INFO("Use BrowserField5"); m_oBrowserField = new BrowserField(m_oConfig); m_oBrowserField .getRenderingOptions() .setProperty(RenderingOptions.CORE_OPTIONS_GUID, RenderingOptions.ALLOW_POPUPS, true); m_oController = new RhoProtocolController(m_oBrowserField); m_oConfig.setProperty(BrowserFieldConfig.CONTROLLER, m_oController); BrowserFieldListener _listener = new BrowserFieldListener() { public void documentLoaded(BrowserField browserField, org.w3c.dom.Document document) throws Exception { synchronized (Application.getEventLock()) { m_oMainScreen.deleteAll(); m_oMainScreen.add(m_oBrowserField); } } /* public void downloadProgress(BrowserField browserField, net.rim.device.api.browser.field.ContentReadEvent event)throws Exception { //Add your code here. }*/ }; m_oBrowserField.addListener(_listener); }
public double GetLongitude() { LOG.TRACE("GeoLocation.GetLongitude"); if (locImpl != null) return locImpl.GetLongitude(); return 0.0; }
public static void stopSync() throws Exception { LOG.INFO("STOP sync"); if (getSyncEngine().isSyncing()) { getSyncEngine().stopSyncByUser(); getInstance().stopWait(); int nWait = 0; // while( nWait < 30000 && getSyncEngine().getState() != SyncEngine.esNone ) while (nWait < 30000 && DBAdapter.isAnyInsideTransaction()) try { Thread.sleep(100); nWait += 100; } catch (Exception e) { } // if (getSyncEngine().getState() != SyncEngine.esNone) if (DBAdapter.isAnyInsideTransaction()) { getSyncEngine().exitSync(); getInstance().stop(0); RhoClassFactory ptrFactory = getInstance().getFactory(); m_pInstance = null; Create(ptrFactory); } } }
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"); }
void clearNotification(SyncSource src) { LOG.INFO("Clear notification. Source : " + (src != null ? src.getName() : "")); synchronized (m_mxSyncNotifications) { if (getSync().isSearch()) m_pSearchNotification = null; else m_mapSyncNotifications.remove(src.getID()); } }
public String getHeaderField(int index) throws IOException { LOG.TRACE("getHeaderField: " + index); processRequest(); if (index >= resHeaders.size()) { return null; } return resHeaders.getValueAt(index); }
public String getEncoding() { LOG.TRACE("getEncloding"); try { return getHeaderField("content-encoding"); } catch (IOException x) { return null; } }
public String getType() { LOG.TRACE("getType"); try { return getHeaderField("content-type"); } catch (IOException x) { return null; } }
void clearSyncNotification(int source_id) { LOG.INFO("Clear notification. Source ID: " + source_id); synchronized (m_mxSyncNotifications) { if (source_id == -1) // Clear all m_pAllNotification = null; else m_mapSyncNotifications.remove(new Integer(source_id)); } }
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 long getLength() { LOG.TRACE("getLength: " + contentLength); try { processRequest(); } catch (IOException ioe) { // Fall through to return -1 for length } return contentLength; }
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; }
SyncThread(RhoClassFactory factory) throws Exception { super(factory); ThreadQueue.setLogCategory(LOG.getLogCategory()); if (RhoConf.getInstance().isExist("sync_poll_interval")) setPollInterval(RhoConf.getInstance().getInt("sync_poll_interval")); m_oSyncEngine = new SyncEngine(); m_oSyncEngine.setFactory(factory); m_oSyncEngine.setFactory(factory); LOG.INFO("sync_poll_interval: " + RhoConf.getInstance().getInt("sync_poll_interval")); LOG.INFO("syncserver: " + RhoConf.getInstance().getString("syncserver")); LOG.INFO("bulksync_state: " + RhoConf.getInstance().getInt("bulksync_state")); ClientRegister.Create(factory); if (RhoConf.getInstance().getString("syncserver").length() > 0) start(epLow); }
public int getHeaderFieldInt(String name, int def) throws IOException { LOG.TRACE("getHeaderFieldInt: " + name); processRequest(); try { return Integer.parseInt(getHeaderField(name)); } catch (IllegalArgumentException iae) { // fall through } catch (NullPointerException npe) { // fall through } return def; }
public void processConnection(HttpConnection connection, Object e) { try { if (connection.getResponseCode() == HttpConnection.HTTP_NOT_MODIFIED) return; } catch (java.io.IOException exc) { LOG.ERROR("processConnection - getResponseCode failed.", exc); } synchronized (Application.getEventLock()) { createBrowserField(); m_oBrowserField.displayContent(connection, (e != null ? (String) e : "")); } }
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"; } }
public long getHeaderFieldDate(String name, long def) throws IOException { LOG.TRACE("getHeaderFieldDate: " + name); processRequest(); try { return DateTimeTokenizer.parse(getHeaderField(name)); } catch (NumberFormatException nfe) { // fall through } catch (IllegalArgumentException iae) { // fall through } catch (NullPointerException npe) { // fall through } return def; }
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; }
public NetResponse pullCookies(String strUrl, String strBody, IRhoSession oSession) throws Exception { Hashtable headers = new Hashtable(); m_bIgnoreSuffixOnSim = false; m_bCancel = false; NetResponse resp = doRequest /*Try*/("POST", strUrl, strBody, oSession, headers); if (resp.isOK()) { resp.setCharData(resp.getCookies()); LOG.INFO("pullCookies: " + resp.getCharData()); } return resp; }
void showGeoLocation() { String location = ""; try { IRhoRubyHelper helper = RhoClassFactory.createRhoRubyHelper(); location = helper.getGeoLocationText(); } catch (Exception exc) { LOG.ERROR("getGeoLocationText failed", exc); } respondOK(); contentLength = location.length(); responseData = new ByteArrayInputStream(location.getBytes()); resHeaders.addProperty("Content-Type", "text/html"); resHeaders.addProperty("Content-Length", Integer.toString(contentLength)); }
public void setRequestMethod(String method) throws IOException { LOG.TRACE("setRequestMethod: " + method); /* * The request method can not be changed once the output stream has been * opened. */ // if (streamConnection != null) { // throw new IOException("connection already open"); // } if (!method.equals(HEAD) && !method.equals(GET) && !method.equals(POST)) { throw new IOException("unsupported method: " + method); } this.method = method; }
/* void setSyncNotification(int source_id, String strUrl, String strParams )throws Exception { LOG.INFO( "Set notification. Source ID: " + source_id + "; Url :" + strUrl + "; Params: " + strParams ); String strFullUrl = getNet().resolveUrl(strUrl); if ( source_id == -1 ) { synchronized(m_mxSyncNotifications) { m_pAllNotification = new SyncNotification( strFullUrl, strParams, false ); } LOG.INFO( " Done Set notification for all sources; Url :" + strFullUrl + "; Params: " + strParams ); }else { if ( strFullUrl.length() > 0 ) { synchronized(m_mxSyncNotifications){ m_mapSyncNotifications.put(new Integer(source_id),new SyncNotification( strFullUrl, strParams, true ) ); } LOG.INFO( " Done Set notification. Source ID: " + source_id + "; Url :" + strFullUrl + "; Params: " + strParams ); } } }*/ void setSyncNotification(int source_id, SyncNotification pNotify) { LOG.INFO( "Set notification. Source ID: " + source_id + ";" + (pNotify != null ? pNotify.toString() : "")); if (source_id == -1) { synchronized (m_mxSyncNotifications) { m_pAllNotification = pNotify; } } else { synchronized (m_mxSyncNotifications) { m_mapSyncNotifications.put(new Integer(source_id), pNotify); } } }
public void reportSyncStatus(String status, int error, String strDetails) { synchronized (m_mxSyncNotifications) { if (m_syncStatusListener != null && (isReportingEnabled() || error == RhoAppAdapter.ERR_SYNCVERSION)) { if (error == RhoAppAdapter.ERR_SYNCVERSION) status = RhoAppAdapter.getErrorText(error); else { if (strDetails.length() == 0) strDetails = RhoAppAdapter.getErrorText(error); status += (strDetails.length() > 0 ? RhoAppAdapter.getMessageText("details") + strDetails : ""); } LOG.INFO("Status: " + status); m_syncStatusListener.reportStatus(status, error); } } }
public NetResponse pullFile( String strUrl, String strFileName, IRhoSession oSession, Hashtable headers) throws Exception { IRAFile file = null; NetResponse resp = null; m_nMaxPacketSize = RhoClassFactory.getNetworkAccess().getMaxPacketSize(); m_bFlushFileAfterWrite = RhoConf.getInstance().getBool("use_persistent_storage"); m_bCancel = false; try { if (!strFileName.startsWith("file:")) { try { strFileName = FilePath.join(RhoClassFactory.createFile().getDirPath(""), strFileName); } catch (IOException x) { LOG.ERROR("getDirPath failed.", x); throw x; } } file = RhoClassFactory.createFSRAFile(); file.open(strFileName, "rw"); file.seek(file.size()); do { resp = pullFile1(strUrl, file, file.size(), oSession, headers); } while (!m_bCancel && (resp == null || resp.isOK()) && m_nCurDownloadSize > 0 && m_nMaxPacketSize > 0); } finally { if (file != null) { file.close(); file = null; } } copyHashtable(m_OutHeaders, headers); return resp != null && !m_bCancel ? resp : makeResponse("", IHttpConnection.HTTP_INTERNAL_ERROR); }
private static String makeClientCookie(Hashtable headers) throws IOException { if (headers == null) return ""; // ParsedCookie cookie = new ParsedCookie(); String strRes = ""; Enumeration valsHeaders = headers.elements(); Enumeration keysHeaders = headers.keys(); while (valsHeaders.hasMoreElements()) { String strName = (String) keysHeaders.nextElement(); String strValue = (String) valsHeaders.nextElement(); if (strName.equalsIgnoreCase("Set-Cookie")) { LOG.INFO("Set-Cookie: " + strValue); strRes += URI.parseCookie(strValue); } } return strRes; }
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); } }
public int getLastPollInterval() { try { long nowTime = (TimeInterval.getCurrentTime().toULong()) / 1000; long latestTimeUpdated = 0; Vector /*<String>*/ arPartNames = DBAdapter.getDBAllPartitionNames(); for (int i = 0; i < (int) arPartNames.size(); i++) { DBAdapter dbPart = DBAdapter.getDB((String) arPartNames.elementAt(i)); IDBResult res = dbPart.executeSQL("SELECT last_updated from sources"); for (; !res.isEnd(); res.next()) { long timeUpdated = res.getLongByIdx(0); if (latestTimeUpdated < timeUpdated) latestTimeUpdated = timeUpdated; } } return latestTimeUpdated > 0 ? (int) (nowTime - latestTimeUpdated) : 0; } catch (Exception exc) { LOG.ERROR("isStartSyncNow failed.", exc); } return 0; }