Exemplo n.º 1
0
	    //{"source_name":"SampleAdapter","client_id":1,"create":{"1":{"brand":"Apple","name":"iPhone","price":"199.99"}}}
	    //{"source_name":"SampleAdapter","client_id":1,"update":{"1":{"brand":"Apple","name":"iPhone","price":"199.99"}}}
	    //{"source_name":"SampleAdapter","client_id":1,"delete":{"1":{"brand":"Apple","name":"iPhone","price":"199.99"}}}
	    //{"source_name":"SampleAdapter","client_id":1,"delete":{"3":{"brand":"HTC","name":"Fuze","price":"299.99"}},"create":{"1":{"brand":"Apple","name":"iPhone","price":"199.99"}},"update":{"2":{"brand":"Android","name":"G2","price":"99.99"}}}
	    String makePushBody_Ver3( String strUpdateType, boolean isSync)
	    {
		    String strBody = "";
	        getDB().Lock();
	    
	        if ( isSync )
	    	    getDB().updateAllAttribChanges();
	    
	        IDBResult res = getDB().executeSQL("SELECT attrib, object, value, attrib_type "+
	            "FROM changed_values where source_id=? and update_type =? and sent<=1 ORDER BY object", getID(), strUpdateType );

	        if ( res.isEnd() )
	        {
	    	    res.close();
	            getDB().Unlock();
	            return strBody;
	        }

	        String strCurObject = "";
	        boolean bFirst = true;
	        for( ; !res.isEnd(); res.next() )
	        {
	            String strAttrib = res.getStringByIdx(0);
	            String strObject = res.getStringByIdx(1);
	            String value = res.getStringByIdx(2);
	            String attribType = res.getStringByIdx(3);

                if ( m_hashIgnorePushObjects.containsKey(strObject) )
    	        	continue;

	            if ( attribType.compareTo("blob.file") == 0 )
	            {
	                NetRequest.MultipartItem oItem = new NetRequest.MultipartItem();
	                oItem.m_strFilePath = RHODESAPP().resolveDBFilesPath(value);
	                oItem.m_strContentType = "application/octet-stream";
	                oItem.m_strName = strAttrib + "-" + strObject;

	                m_arBlobAttrs.addElement(strAttrib);
	                m_arMultipartItems.addElement(oItem);
	            }

	            if ( strBody.length() == 0 )
	            {
	                if ( !isSync )
	                    strBody += "{";
	                else
	                    strBody += "\"" + strUpdateType + "\":{";
	            }

	            if ( strObject.compareTo(strCurObject) != 0 )
	            {
	                if ( strCurObject.length() > 0 )
	                {
	                    if ( !bFirst )
	                        strBody += "}";
	                    strBody += ",";
	                }

	                bFirst = true;
	                strBody += JSONEntry.quoteValue(strObject);
	                strCurObject = strObject;
	            }
	            
	            if (!bFirst)
	                strBody += ",";

	            if ( strAttrib.length() > 0  )
	            {
	                if ( bFirst )
	                    strBody += ":{";

	                strBody += JSONEntry.quoteValue(strAttrib) + ":" + JSONEntry.quoteValue(value);
	                bFirst = false;
	            }
	        }

	        if ( strBody.length() > 0 )
	        {
	            if ( !bFirst )
	                strBody += "}";

	            strBody += "}";
	        }

	        if ( isSync )	    
	    	    getDB().executeSQL("UPDATE changed_values SET sent=1 WHERE source_id=? and update_type=? and sent=0", getID(), strUpdateType );
	    
	        getDB().Unlock();
	    
	        return strBody;
	    }
Exemplo n.º 2
0
            public void execute()
            {
                NetResponse resp = null;
     	        try
     	        {
	                switch( m_eCmd )
	                {
	                    case hcGet:
	            	        ///m_pNetRequest.setIgnoreSuffixOnSim(false);
	                        resp = m_pNetRequest.doRequest( m_params.getString("http_command", "GET"), 
	                        m_params.getString("url"), m_params.getString("body"), null, m_mapHeaders);
	                        break;
	                    case hcPost:
	            	        ///m_pNetRequest.setIgnoreSuffixOnSim(false);
	                        resp = m_pNetRequest.doRequest(m_params.getString("http_command", "POST"), 
	                        m_params.getString("url"), m_params.getString("body"), null, m_mapHeaders);
	                        break;
	
	                    case hcDownload:
	                        resp = m_pNetRequest.pullFile(m_params.getString("url"), m_params.getString("filename"), null, m_mapHeaders);
	                        break;  
	
	            case hcUpload:
	                {
                        Vector<NetRequest.MultipartItem>/*Ptr<net::CMultipartItem*>*/ arMultipartItems = new Vector<NetRequest.MultipartItem>();
	
	                    RhoParamArray arParams = new RhoParamArray( m_params, "multipart");
	                    if ( arParams.size() > 0 )
	                    {
	                        for( int i = 0; i < arParams.size(); i++)
	                        {
	                            RhoParams oItem = arParams.getItem(i);

                                NetRequest.MultipartItem pItem = new NetRequest.MultipartItem();
	                            String strBody = oItem.getString("body");
	                            if ( strBody.length() > 0 )
	                            {
	                                pItem.m_strBody = strBody;
	                                pItem.m_strContentType = oItem.getString("content_type", "application/x-www-form-urlencoded");
	                            }
	                            else
	                            {
	                                pItem.m_strFilePath = oItem.getString("filename");
	                                pItem.m_strContentType = oItem.getString("content_type", "application/octet-stream");
	                            }
	
	                            pItem.m_strName = oItem.getString("name");
	                            pItem.m_strFileName = oItem.getString("filename_base");
	                            arMultipartItems.addElement(pItem);
	                        }
	                    }else
	                    {
                            NetRequest.MultipartItem pItem = new NetRequest.MultipartItem();
	                        pItem.m_strFilePath = m_params.getString("filename");
	                        pItem.m_strContentType = m_params.getString("file_content_type", "application/octet-stream");
	                        pItem.m_strName = m_params.getString("name");
	                        pItem.m_strFileName = m_params.getString("filename_base");
	                        arMultipartItems.addElement(pItem);
	
	                        String strBody = m_params.getString("body");
	                        if ( strBody.length() > 0 )
	                        {
                                NetRequest.MultipartItem pItem2 = new NetRequest.MultipartItem();
	                            pItem2.m_strBody = strBody;
	                            pItem2.m_strContentType = (String)m_mapHeaders.get("content-type");
	                            arMultipartItems.addElement(pItem2);
	                        }
	                    }
	
	                    resp = m_pNetRequest.pushMultipartData( m_params.getString("url"), arMultipartItems, null, m_mapHeaders );
	                    break;
	                }
	            }
	
	            if ( !m_pNetRequest.isCancelled())
	            {
				    processResponse(resp);
	                callNotify(resp,0);
	            }
    	    }catch(IOException exc)
    	    {
    	    	LOG.ERROR("command failed: " + m_eCmd + "url: " + m_params.getString("url"), exc);
    	    	callNotify(null, RhoAppAdapter.ERR_NETWORK);
    	    }catch(Exception exc)
    	    {
    	    	LOG.ERROR("command crashed: " + m_eCmd + "url: " + m_params.getString("url"), exc);
    	    	callNotify(null, RhoAppAdapter.ERR_RUNTIME);
    	    }
        }
Exemplo n.º 3
0
	    void doSyncClientChanges()
	    {
	        String[] arUpdateTypes = {"create", "update", "delete"};
	        boolean[] arUpdateSent = {false, false, false};

	        m_arMultipartItems.removeAllElements();
	        m_arBlobAttrs.removeAllElements();
	        String strBody = "{\"source_name\":" + JSONEntry.quoteValue(getName()) + ",\"client_id\":" + JSONEntry.quoteValue(getSync().getClientID());
	        boolean bSend = false;
	        int i = 0;
	        getDB().Lock();
	        try{
		        checkIgnorePushObjects();
		    
	            for( i = 0; i < 3 && getSync().isContinueSync(); i++ )
	            {
	                String strBody1;
	                strBody1 = makePushBody_Ver3(arUpdateTypes[i], true);
	                if (strBody1.length() > 0)
	                {
	                    strBody += "," + strBody1;

	                    String strBlobAttrs = "";
	                    for ( int j = 0; j < (int)m_arBlobAttrs.size(); j++)
	                    {
	                        if ( strBlobAttrs.length() > 0 )   
	                            strBlobAttrs += ",";

	                        strBlobAttrs += JSONEntry.quoteValue((String)m_arBlobAttrs.elementAt(j));
	                    }

	                    if ( strBlobAttrs.length() > 0 )
	                        strBody += ",\"blob_fields\":[" + strBlobAttrs + "]";

	                    arUpdateSent[i] = true;
	                    bSend = true;
	                }
	            }
	            strBody += "}";
            }
            finally
            {
                getDB().Unlock();
            }

	        if ( bSend )
	        {
	            LOG.INFO( "Push client changes to server. Source: " + getName() + "Size :" + strBody.length() );
	            LOG.TRACE("Push body: " + strBody);		

	            try{
		            if ( m_arMultipartItems.size() > 0 )
		            {
		                NetRequest.MultipartItem oItem = new NetRequest.MultipartItem();
		                oItem.m_strBody = strBody;
		                //oItem.m_strContentType = getProtocol().getContentType();
		                oItem.m_strName = "cud";
		                m_arMultipartItems.addElement(oItem);
	
		                NetResponse resp = getNet().pushMultipartData( getProtocol().getClientChangesUrl(), m_arMultipartItems, getSync(), null );
		                if ( !resp.isOK() )
		                {
		                    getSync().setState(SyncEngine.esStop);
		                    m_nErrCode = RhoAppAdapter.ERR_REMOTESERVER;
		                    m_strError = resp.getCharData();
		                }
		            }else
		            {
		                NetResponse resp = getNet().pushData( getProtocol().getClientChangesUrl(), strBody, getSync());
		                if ( !resp.isOK() )
		                {
		                    getSync().setState(SyncEngine.esStop);
		                    m_nErrCode = RhoAppAdapter.ERR_REMOTESERVER;
		                    m_strError = resp.getCharData();
		                }
		            }
		        }catch(Exception exc)
		        {
		    	    m_nErrCode = RhoAppAdapter.getNetErrorCode(exc);
		    	    throw exc;
		        }
		        
	        }

	        for( i = 0; i < 3 && getSync().isContinueSync(); i++ )
	        {
	            if ( arUpdateSent[i] )
	            {
	                //oo conflicts
                    if (i < 1 && !SyncEngine.getSourceOptions().getBoolProperty(getID(), "pass_through") ) //create
	                    getDB().executeSQL("UPDATE changed_values SET sent=2 WHERE source_id=? and update_type=? and sent=1", getID(), arUpdateTypes[i] );
	                else
	                //
	                    getDB().executeSQL("DELETE FROM changed_values WHERE source_id=? and update_type=? and sent=1", getID(), arUpdateTypes[i] );
	            }
	        }

	        m_arMultipartItems.removeAllElements();
	        m_arBlobAttrs.removeAllElements();
	    }
Exemplo n.º 4
0
            public void execute()
            {
                NetResponse resp = null;

                try
                {
                    switch (m_eCmd)
                    {
                    case hcGet:
                        ///m_pNetRequest.setIgnoreSuffixOnSim(false);
                        resp = m_pNetRequest.doRequest(m_params.getString("http_command", "GET"),
                                                       m_params.getString("url"), m_params.getString("body"), null, m_mapHeaders);
                        break;

                    case hcPost:
                        ///m_pNetRequest.setIgnoreSuffixOnSim(false);
                        resp = m_pNetRequest.doRequest(m_params.getString("http_command", "POST"),
                                                       m_params.getString("url"), m_params.getString("body"), null, m_mapHeaders);
                        break;

                    case hcDownload:
                        resp = m_pNetRequest.pullFile(m_params.getString("url"), m_params.getString("filename"), null, m_mapHeaders);
                        break;

                    case hcUpload:
                    {
                        Vector <NetRequest.MultipartItem> /*Ptr<net::CMultipartItem*>*/ arMultipartItems = new Vector <NetRequest.MultipartItem>();

                        RhoParamArray arParams = new RhoParamArray(m_params, "multipart");
                        if (arParams.size() > 0)
                        {
                            for (int i = 0; i < arParams.size(); i++)
                            {
                                RhoParams oItem = arParams.getItem(i);

                                NetRequest.MultipartItem pItem = new NetRequest.MultipartItem();
                                String strBody = oItem.getString("body");
                                if (strBody.length() > 0)
                                {
                                    pItem.m_strBody        = strBody;
                                    pItem.m_strContentType = oItem.getString("content_type", "application/x-www-form-urlencoded");
                                }
                                else
                                {
                                    pItem.m_strFilePath    = oItem.getString("filename");
                                    pItem.m_strContentType = oItem.getString("content_type", "application/octet-stream");
                                }

                                pItem.m_strName     = oItem.getString("name");
                                pItem.m_strFileName = oItem.getString("filename_base");
                                arMultipartItems.addElement(pItem);
                            }
                        }
                        else
                        {
                            NetRequest.MultipartItem pItem = new NetRequest.MultipartItem();
                            pItem.m_strFilePath    = m_params.getString("filename");
                            pItem.m_strContentType = m_params.getString("file_content_type", "application/octet-stream");
                            pItem.m_strName        = m_params.getString("name");
                            pItem.m_strFileName    = m_params.getString("filename_base");
                            arMultipartItems.addElement(pItem);

                            String strBody = m_params.getString("body");
                            if (strBody.length() > 0)
                            {
                                NetRequest.MultipartItem pItem2 = new NetRequest.MultipartItem();
                                pItem2.m_strBody        = strBody;
                                pItem2.m_strContentType = (String)m_mapHeaders.get("content-type");
                                arMultipartItems.addElement(pItem2);
                            }
                        }

                        resp = m_pNetRequest.pushMultipartData(m_params.getString("url"), arMultipartItems, null, m_mapHeaders);
                        break;
                    }
                    }

                    if (!m_pNetRequest.isCancelled())
                    {
                        processResponse(resp);
                        callNotify(resp, 0);
                    }
                }catch (IOException exc)
                {
                    LOG.ERROR("command failed: " + m_eCmd + "url: " + m_params.getString("url"), exc);
                    callNotify(null, RhoAppAdapter.ERR_NETWORK);
                }catch (Exception exc)
                {
                    LOG.ERROR("command crashed: " + m_eCmd + "url: " + m_params.getString("url"), exc);
                    callNotify(null, RhoAppAdapter.ERR_RUNTIME);
                }
            }