⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clientmodifications.java

📁 实现了SyncML无线同步协议
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            Status statusCommand = new Status(                                              idGenerator.next()               ,                                              syncHeader.getMsgID()            ,                                              "0" /* command ref */            ,                                              "SyncHdr" /* see SyncML specs */ ,                                              targetRefs                       ,                                              sourceRefs                       ,                                              null /* credential */            ,                                              null /* challenge */             ,                                              new Data(StatusCode.OK)          ,                                               new Item[0]                                          );            commandList.add(statusCommand);                    //            // 2. The Status element MUST be included in SyncBody if requested by             //    the client. It is now used to indicate the general status of             //    the sync analysis and the status information related to data             //    items sent by the client (e.g., a conflict has happened.).             //            for (int i=0; (  isFlag(FLAG_SYNC_STATUS_REQUIRED)                          && (clientModificationsStatus != null)                           && (i<clientModificationsStatus.length) ); ++i) {                commandList.add(clientModificationsStatus[i]);            }        }                  //        // 3. The Sync element MUST be included in SyncBody, if earlier there        // were no occurred errors, which could prevent the server to process         // the sync analysis and to send its modifications back to the client.        //        for (int i=0; ((serverModifications != null) && (i<serverModifications.length)); ++i) {            commandList.add(serverModifications[i]);        }                //        // Constructs return message        //        Target target = new Target(syncHeader.getSource().getLocURI(),                                   syncHeader.getSource().getLocName());        Source source = new Source(syncHeader.getTarget().getLocURI(),                                   syncHeader.getTarget().getLocName());        SyncHdr responseHeader = new SyncHdr (                                        getDTDVersion()                      ,                                        getProtocolVersion()                 ,                                        syncHeader.getSessionID(),                                        syncHeader.getMsgID()    ,                                        target                                                    ,                                        source                                                    ,                                        null  /* response URI */                                  ,                                        false                                                     ,                                        null /* credentials */                                    ,                                        null /* meta data */                                                                     );        		AbstractCommand[] commands 	= 	null;        int size = commandList.size();        if (size == 0) {			 commands = new AbstractCommand[1];		} else {			commands = new AbstractCommand[size];		}        for (int i=0; i < size; i++) {            commands[i] = (AbstractCommand)commandList.get(i);        }        SyncBody responseBody = new SyncBody(                                    commands,                                    isFlag(FLAG_FINAL_MESSAGE) /* final */                                );        try {            return new SyncML(responseHeader, responseBody);        } catch (RepresentationException e) {            //            // It should never happen !!!!            //            throw new ProtocolException("Unexpected error", e);        }    }        /**     * Create a Status command for the Sync sent by the client.     *     * <b>NOTE</b>: the protocol does not specify any information about the format     * and the content of this message. By now a dummy status command is created     * and returned.     *     * @return a StatusCommand object     */    public Status createSyncStatusCommand() {        return new Status( idGenerator.next()                       ,                                  "0" /* message id; TO DO */              ,                            clientSyncCmdId.getCmdID()               ,                           Sync.COMMAND_NAME                        ,                                  (TargetRef[])null /* target refs */      ,                                  (SourceRef[])null /* source refs */      ,                                  null /* credential */                    ,                                  null /* chal */                          ,                                  null /* Data */                          ,                                  null /* items */                         );                                      }        /** For the Sync element, there are the following requirements.     * <ul>     *   <li> CmdID is required.     *   <li> The response can be required for the Sync command. (See the Caching of Map Item,     *        Chapter 2.3.1)     *   <li> Target is used to specify the target database.     *   <li> Source is used to specify the source database.     * </ul>     *     * 5. If there is any modification in the server after the previous sync,     * there are following requirements for the operational elements (e.g.,     * Replace, Delete, and Add 4 ) within the Sync element.     * <ul>     *   <li> CmdID is required.     *   <li> The response can be required for these operations.     *   <li> Source MUST be used to define the temporary GUID (See Definitions)     *        of the data item in the server if the operation is an addition.     *        If the operation is not an addition, Source MUST NOT be included.     *   <li> Target MUST be used to define the LUID (See Definitions) of the     *        data item if the operation is not an addition. If the operation is     *        an addition, Target MUST NOT be included.     *   <li> The Data element inside Item is used to include the data itself if     *        the operation is not a seletion.     *   <li> The Type element of the MetaInf DTD MUST be included in the Meta     *        element to indicate the type of the data item (E.g., MIME type).     *        The Meta element inside an operation or inside an item can be used.     * </ul>     * @param db the database to be synchronized     * @return a Sync command     * @throws ProtocolException if any protocol requirement is not respected     */    public Sync createSyncCommand(Database db)     throws ProtocolException {        CmdID syncId = idGenerator.next();                AbstractCommand[] commands = null;		// if db.getMethod is One_Way_Sync_CLIENT		// no synccommand from Server to Client		if(db.getMethod() != AlertCode.ONE_WAY_FROM_CLIENT){			commands = prepareCommands(db);		}                return new Sync(            syncId                             ,            isFlag(FLAG_SYNC_RESPONSE_REQUIRED),            null                               , /* credentials */            db.getTarget()                     ,            db.getSource()                     ,            null                               , /* Meta        */            0,            commands                                   );                }        /**     * Returns an array of synchronization commands (Add, Copy, Delete, Exec,      * Replace) based on the content of the given database.     *     * @param db the database to be synchronized     *     * @return an array of AbstractCommand     */    public AbstractCommand[] prepareCommands(Database db) {        ArrayList commands = new ArrayList();                Meta meta = new Meta();        meta.setType(db.getType());                Item[] items = null;  // reused many times                //        // Add        //        items = db.getAddItems();        if (items != null) {            commands.add(                new Add(                    idGenerator.next()                          ,                    isFlag(FLAG_MODIFICATIONS_RESPONSE_REQUIRED),                    null /* credentials */                      ,                    meta                                        ,                    items                                       )            );        }                //        // Copy        //        items = db.getCopyItems();        if (items != null) {            commands.add(                new Copy(                    idGenerator.next()                          ,                    isFlag(FLAG_MODIFICATIONS_RESPONSE_REQUIRED),                    null /* credentials */                      ,                    meta                                        ,                    items                                       )            );        }                //        // Delete        //        items = db.getDeleteItems();        if (items != null) {            commands.add(                new Delete(                    idGenerator.next()                          ,                    isFlag(FLAG_MODIFICATIONS_RESPONSE_REQUIRED),                    isFlag(FLAG_ARCHIVE_DATA)                   ,                    isFlag(FLAG_SOFT_DELETE)                    ,                    null /* credentials */                      ,                    meta                                        ,                    items                                       )            );        }                //        // Exec        //        items = db.getExecItems();                for (int i=0; ((items != null) && (i<items.length)); ++i) {            commands.add(                new Exec(                    idGenerator.next()                          ,                    isFlag(FLAG_MODIFICATIONS_RESPONSE_REQUIRED),                    null /* credentials */                      ,                    items[i]                                    )            );        }                //        // Replace        //        items = db.getReplaceItems();        if (items != null) {            commands.add(                new Replace(                    idGenerator.next()                          ,                    isFlag(FLAG_MODIFICATIONS_RESPONSE_REQUIRED),                    null /* credentials */                      ,                    meta                                        ,                    items                                       )            );        }        int size = commands.size();        AbstractCommand [] aCommands = new AbstractCommand[size];        for (int i=0; i < size; i++) {            aCommands[i] = (AbstractCommand)commands.get(i);        }        return aCommands;    }        // --------------------------------------------------------- Private methods        /**     * Checks if the requested status for server capabilities has been specified.     * <p>     *     * @throws ProtocolException     */    private void checkServerCapabilitiesStatus()     throws ProtocolException {        //        // If serverCapabilitiesCmdId is null no serverCapabilities status is required        //        if (serverCapabilitiesCmdId == null) return;                List list = ProtocolUtil.filterCommands(clientCommands         ,                                                Status.class    ,                                                serverCapabilitiesCmdId);                if (list.size() == 0) {            Object[] args = new Object[] { serverCapabilitiesCmdId.getCmdID() };            throw new ProtocolException(ClientModificationsRequirements.ERRMSG_MISSING_STATUS_COMMAND, args);        }                serverCapabilitiesStatus = (Status)list.get(0);    }        /**     * Checks if the result for client capabilities has been given.     * <p>     *     * @throws ProtocolException     */    private void checkClientCapabilitiesResult()     throws ProtocolException {        //        // If clientCapabilitiesCmdId is null no client capabilities were required        //        if (clientCapabilitiesCmdId == null) return;                List list = ProtocolUtil.filterCommands(clientCommands         ,                                                Results.class   ,                                                clientCapabilitiesCmdId);                if (list.size() == 0) {            Object[] args = new Object[] { clientCapabilitiesCmdId.getCmdID() };            throw new ProtocolException(ClientModificationsRequirements.ERRMSG_MISSING_RESULTS_COMMAND, args);        }                Results results = (Results)list.get(0);                ClientModificationsRequirements.checkCapabilities(            results,             ClientModificationsRequirements.CLIENT_CAPABILITIES        );                clientCapabilitiesResults = results;    }        /**     * Checks the Sync command.     * <p>Filters out the Sync Messages from client to Server with     * ONE_WAY_SYNC_SERVER     *     * @throws ProtocolException     */    private void checkSyncCommand()     throws ProtocolException {        List list = ProtocolUtil.filterCommands(clientCommands   ,                                                Sync.class);                if (list.size() == 0) {            clientSyncCommands = new Sync[0];            return;        }        clientSyncCommands = (Sync[])list.toArray(new Sync[list.size()]);    }    }

⌨️ 快捷键说明

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