ctpconfig.cpp

来自「funambol window mobile客户端源代码」· C++ 代码 · 共 339 行

CPP
339
字号
/*
 * Funambol is a mobile platform developed by Funambol, Inc. 
 * Copyright (C) 2003 - 2007 Funambol, Inc.
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License version 3 as published by
 * the Free Software Foundation with the addition of the following permission
 * added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
 * WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE
 * WARRANTY OF NON INFRINGEMENT  OF THIRD PARTY RIGHTS.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 
 * details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program; if not, see http://www.gnu.org/licenses or write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301 USA.
 * 
 * You can contact Funambol, Inc. headquarters at 643 Bair Island Road, Suite
 * 305, Redwood City, CA 94063, USA, or at email address info@funambol.com.
 * 
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU Affero General Public License version 3.
 * 
 * In accordance with Section 7(b) of the GNU Affero General Public License
 * version 3, these Appropriate Legal Notices must retain the display of the
 * "Powered by Funambol" logo. If the display of the logo is not reasonably
 * feasible for technical reasons, the Appropriate Legal Notices must display
 * the words "Powered by Funambol".
 */    
#include "CTPConfig.h"    
#include "base/stringUtils.h"
#include "base/util/StringBuffer.h"
#include "spdm/constants.h"
#include "pim/ClientSettings.h"

#define CTP_QUEUE_PUSH                  "queuePush"
#define CTP_RETRY                       "ctpRetry"
#define CTP_MAX_RETRY_TIMEOUT           "maxCtpRetry"
#define CTP_CMD_TIMEOUT                 "ctpCmdTimeout"
#define CTP_CONN_TIMEOUT                "ctpConnTimeout"
#define CTP_PORT                        "ctpPort"
#define CTP_READY                       "ctpReady"
#define PROPERTY_CTP_SERVER             "ctpServer"
#define PROPERTY_NOTIFY_TIMEOUT         "notifyTimeout"



CTPConfig::CTPConfig(const char* application_uri)
                    : DMTClientConfig(application_uri) {
}
        
CTPConfig::~CTPConfig() {}

char* CTPConfig::decodePassword(const char* password) {
    
    char* decripted = NULL;
    if (password && strlen(password) > 0) {
         decripted = decryptData(password, PASS_KEY); 
    }    

    if (decripted == NULL || getLastErrorCode() == 801) {
        decripted = new char[1];
        decripted[0] = 0;
        setError(ERR_NONE, "");
    }
    return decripted;       
}

StringBuffer CTPConfig::encodePassword(const char* password) {
    
    StringBuffer buffer("");
    if (password && strlen(password) > 0) {
        char* encoded = encryptData(password, PASS_KEY);        
        buffer = encoded;        
    }        
    return buffer;   
}
        
void CTPConfig::readCTPConfig() {

    if (!open()) {
        LOG.error("Impossible read the ctp configuration. exit");
        return;
    }

    if (readAccessConfig(*syncMLNode)) {
        setUsername(accessConfig.getUsername());
        // urlTo is loaded from 'ctpServer' key   
        // nonce is read calling 'readAccessConfig()'
    }

    char* passDecoded = decodePassword(accessConfig.getPassword());
    accessConfig.setPassword(passDecoded);
    if (passDecoded) { delete [] passDecoded; }

    if (readDeviceConfig(*syncMLNode)) {
        setDeviceId(deviceConfig.getDevID());

    }
    
    // now read the single CTP properties
    ManagementNode* node;

    // read parameter of the client only
    node = dmt->readManagementNode(rootContext);
    if (node) {
        char* tmp;        
        tmp = node->readPropertyValue(PROPERTY_PUSH_NOTIFICATION);
        if (tmp) {
            setPush(atoi(tmp));
        } else {
            setPush(0);
        }        
        delete [] tmp;
             
        tmp = node->readPropertyValue(PROPERTY_POLLING_NOTIFICATION);
        if (tmp) {
            setPolling(atoi(tmp));
        } else {
            setPolling(0);
        }                
        delete [] tmp;

        tmp = node->readPropertyValue(CTP_QUEUE_PUSH);
        if (tmp) {
            setQueuePush(atoi(tmp) == 0 ? false : true);
        } else {
            setQueuePush(false);
        }                
        delete [] tmp;

        tmp = node->readPropertyValue(PROPERTY_NOTIFY_TIMEOUT);
        if (tmp) {
            setNotifyTimeout(atoi(tmp));
        } else {
            setNotifyTimeout(180);
        }                
        delete [] tmp;

        tmp = node->readPropertyValue(CTP_RETRY);
        if (tmp) {
            setCtpRetry(atoi(tmp));
        } else {
            setCtpRetry(5);
        }                
        delete [] tmp;

        tmp = node->readPropertyValue(CTP_MAX_RETRY_TIMEOUT);
        if (tmp) {
            setMaxCtpRetry(atoi(tmp));
        } else {
            setMaxCtpRetry(900); // 15 min
        }                
        delete [] tmp;

        tmp = node->readPropertyValue(CTP_CMD_TIMEOUT);
        if (tmp) {
            setCtpCmdTimeout(atoi(tmp));
        } else {
            setCtpCmdTimeout(0); 
        }                
        delete [] tmp;

        tmp = node->readPropertyValue(CTP_CONN_TIMEOUT);
        if (tmp) {
            setCtpConnTimeout(atoi(tmp));
        } else {
            setCtpConnTimeout(0); 
        }                
        delete [] tmp;

        tmp = node->readPropertyValue(CTP_PORT);
        if (tmp) {
            setCtpPort(atoi(tmp));
        } else {
            setCtpPort(0); 
        }                
        delete [] tmp;
        
        tmp = node->readPropertyValue(CTP_READY);
        if (tmp) {
            setCtpReady(atoi(tmp));
        } else {
            setCtpReady(5);
        }                
        delete [] tmp;

        // server url is loaded from regstry key 'ctpServer'
        tmp = node->readPropertyValue(PROPERTY_CTP_SERVER);
        if (tmp && strlen(tmp)>0) {
            // *** workaround for prefix: remove this when not necessary! ***
            string url = checkPrefix(tmp);
            LOG.debug("urlTo = %s", url.c_str());
            setUrlTo(url);
        } else {
            // If 'ctpServer' reg value is empty, extract the url from 'syncUrl'
            setUrlTo(getHostName(accessConfig.getSyncURL()));
        }                
        delete [] tmp;


        delete node;
        node = NULL;
    }
    
    close();
    return;

}


void CTPConfig::saveCTPConfig() { 
    
    ManagementNode* node = NULL;
    if (!open()) {
        return;
    }
  
    StringBuffer buffer = encodePassword(accessConfig.getPassword());
    accessConfig.setPassword(buffer.c_str());
    // Save the nonce: save AuthConfig properties
    char nodeName[DIM_MANAGEMENT_PATH];
    sprintf(nodeName, "%s%s%s", APPLICATION_URI, CONTEXT_SPDS_SYNCML, CONTEXT_AUTH);
    node = dmt->readManagementNode(nodeName);
    if (node) {
        saveAuthConfig(*syncMLNode, *node);
        delete node;
    }


    char* passDecoded = decodePassword(accessConfig.getPassword());
    accessConfig.setPassword(passDecoded);
    if (passDecoded) { delete [] passDecoded; }
    close();
}
                      

string CTPConfig::getHostName(string syncUrl) {

    std::string::size_type start, end;
    std::string host;
    // Extract the hostName from syncUrl: "http://<hostName>:8080/funambol/ds"    
    start = syncUrl.find("://", 0);
    if (start != std::string::npos) {
        start += 3;
    } else {
        // try to extract hostname from <hostName>:8080/funambol/ds"  
        start = 0;
    }
    
    end = syncUrl.find_first_of(":/", start);       // stop if ":" or "/" found
    if (end == std::string::npos) { // so the url is only <hostName>
        end = syncUrl.length();
    }

    if (end > start) {
        host = syncUrl.substr(start, end-start);        
    }      

    return host;
}

int CTPConfig::getHostPort(string syncUrl) {

    std::string::size_type start, endSlash, endColon, urlLength;
    string hostPort = "";
    urlLength = syncUrl.length();
    int port = 0;
    // Extract the port from syncUrl: "http://<hostName>:8080/funambol/ds"    
    start = syncUrl.find("://", 0);
    if (start != std::string::npos) {
        start += 3;
    } else {
        // try to extract the port from <hostName>:8080/funambol/ds"  
        start = 0;
    }
    
    endSlash = syncUrl.find_first_of("/", start);       // stop if ":" or "/" found
    endColon = syncUrl.find_first_of(":", start);       // stop if ":" or "/" found
    
    if (endSlash == std::string::npos && endColon == std::string::npos) {
        // there is no port
        // hostname
    } else if (endSlash != std::string::npos && endColon == std::string::npos) {
        // there is no port
        // hostname/funambol
    } else if (endSlash == std::string::npos && endColon != std::string::npos) {
        // there is port
        // hostname:8080            
        hostPort = syncUrl.substr(endColon + 1, urlLength - start);
    } else {
        if (endSlash > endColon) {
            // there is port
            // hostname:8080/funambol            
            hostPort = syncUrl.substr(endColon + 1, endSlash - endColon - 1);
        }
    }
    
    if (hostPort != "") {
        port = atoi(hostPort.c_str());
    }

    return port;
}
    

/// If passed 'url' contains "prefix:" the value is appended 
/// next to the host name: <prefix><hostName> is returned
/// (hostName is retrieved from SyncUrl)
/// Otherwise the passed 'url' is returned as string.
string CTPConfig::checkPrefix(char* url) {

    string ctpUrl = url;
    const string prefix = "prefix:";

    string::size_type pos = ctpUrl.find(prefix, 0);
    if (pos == string::npos) {
        // Not found: keep the url passed
        return ctpUrl;
    }

    // Go after the ":" 
    pos += prefix.length();
    if (pos != string::npos) {
        // Add the prefix
        ctpUrl = ctpUrl.substr(pos, ctpUrl.length());
        // Append the syncUrl
        ctpUrl += getHostName(accessConfig.getSyncURL());
    }

    return ctpUrl;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?