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

📄 syncinitialization.java

📁 实现了SyncML无线同步协议
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        Alert[] alerts = new Alert[size];        for (int i=0; i < size; i++) {            alerts[i] = (Alert)alertList.get(i);             listAlerts.add(alerts[i]);         }        for (int i=0; (alerts != null) && (i < alerts.length); ++i) {             InitializationRequirements.checkAlertCommand(alerts[i]);        }  // next i                //        // All alerts are OK => they can be cached        //        clientAlerts = (Alert[])listAlerts.toArray(new Alert[0]);        mergedClientCommands.addAll(listAlerts);                ArrayList clientCapabilitiesList =             ProtocolUtil.filterCommands(allClientCommands, Put.class);        if ((clientCapabilities != null) && (clientCapabilitiesList.size()>0)) {            InitializationRequirements.checkCapabilities((Put)clientCapabilitiesList.get(0)     ,                                                          InitializationRequirements.CLIENT_CAPABILITIES);            clientCapabilities = (Put)clientCapabilitiesList.get(0);        }        mergedClientCommands.addAll(clientCapabilitiesList);        ArrayList capabilitiesRequest =             ProtocolUtil.filterCommands(allClientCommands, Get.class);        if ((capabilitiesRequest != null) && (capabilitiesRequest.size()>0)) {            InitializationRequirements.checkCapabilitiesRequest((Get)capabilitiesRequest.get(0));            serverCapabilitiesRequest = (Get)capabilitiesRequest.get(0);                }        mergedClientCommands.addAll(capabilitiesRequest);                clientCommands =             (AbstractCommand[])mergedClientCommands.toArray(new AbstractCommand[0]);            }    /**     * Constructs a proper response message.<br>     * NOTES     * <ul>     *  <li> If server capabilities are not required, they are not sent (in     *       the SyncML protocol the server MAY send not required capabilities)     * </ul>     *     * @return the response message     *     * @throws ProtocolException in case of error or inconsistency     */    public SyncML getResponse() throws ProtocolException {        ArrayList commandList = new ArrayList();        //        // Constructs all required response commands.        //        // NOTE: if NoResp is specified in the header element, than no         //       response commands must be returned regardless NoResp is         //       specified or not in subsequent commands        //                if (syncHeader.isNoResp() == false) {            //            // Session authorization            //            TargetRef[] targetRefs = new TargetRef[] { new TargetRef(syncHeader.getTarget().getLocURI()) };            SourceRef[] sourceRefs = new SourceRef[] { new SourceRef(syncHeader.getSource().getLocURI()) };            //            // If the session is not authenticated, a Chal element must be returned            //            Chal chal = null;            if (authorizedStatusCode != StatusCode.AUTHENTICATION_ACCEPTED) {                chal = Chal.getBasicChal();            }            Status statusCommand = new Status(                                    idGenerator.next()                            ,                                    syncHeader.getMsgID()                         ,                                    "0" /* command ref */                         ,                                    "SyncHdr" /* see SyncML specs */              ,                                    targetRefs                                    ,                                    sourceRefs                                    ,                                    null /* credential */                         ,                                    chal                                          ,                                    new Data(String.valueOf(authorizedStatusCode)),                                    new Item[0]                                   );            commandList.add(statusCommand);            //            // Status for each command that requested it (it is supposed each             // command has bean already checked).            //            for (int i=0; ((clientCommands != null) && (i < clientCommands.length)); ++i) {                if (clientCommands[i].isNoResp()) continue;                targetRefs = null;                sourceRefs = null;                if (clientCommands[i] instanceof ItemizedCommand) {                    Item[] items = (Item[])((ItemizedCommand)clientCommands[i]).getItems().toArray(new Item[0]);                                        ArrayList trefs = new ArrayList();                    ArrayList srefs = new ArrayList();                    Target t;                    Source s;                    for (int j=0; j<items.length; ++j) {                        t = items[j].getTarget();                        s = items[j].getSource();                                    if (t != null) {                            trefs.add(new TargetRef(t));                        }                        if (s != null) {                            srefs.add(new SourceRef(s));                        }                    }  // next j                                        if (trefs.size() > 0) {                        targetRefs = (TargetRef[])trefs.toArray(new TargetRef[trefs.size()]);                    }                    if (srefs.size() > 0) {                        sourceRefs = (SourceRef[])srefs.toArray(new SourceRef[srefs.size()]);                    }                }                                String commandReference = clientCommands[i].getCmdID().getCmdID();                int status = getStatusCodeForCommand(clientCommands[i], StatusCode.OK);                Item[] items = new Item[0];                				///				//	Within Response of Alert, Item must contain the NEXT Anchor.                // NOTE: a database represents the server point of view so that                //       the target is the client database and the source the                //       server database.				//                if (clientCommands[i] instanceof Alert) {                    for(int j=0; (databases != null) && (j<databases.length) ; ++j) {                        if((databases[j].getSource().getLocURI()).equals(targetRefs[0].getValue())){                            items = new Item[1];                                                        Anchor alertAnchor =                                        new Anchor(null, databases[j].getNext());                                                        ComplexData data = new ComplexData();                            data.setAnchor(alertAnchor);                                                        items[0] = new Item(                               null,  // target                               null,  // source                               null, // meta                               data,                               false //MoreData                            );                                                       break;                        }                    }                }                statusCommand = new Status(                                    idGenerator.next()          ,                                    syncHeader.getMsgID()       ,                                    commandReference            ,                                    clientCommands[i].getName() ,                                    targetRefs                  ,                                    sourceRefs                  ,                                    null /* credential */       ,                                    null /* challenge */        ,                                    new Data(status)            ,                                    items                                );                commandList.add(statusCommand);            }  // next i            //            // Status for client capabilities            //            if (clientCapabilities != null) {                String commandReference = clientCapabilities.getCmdID().getCmdID();                targetRefs = new TargetRef[] {};                sourceRefs = new SourceRef[] {                     new SourceRef(InitializationRequirements.CAPABILITIES_SOURCE)                 };                Data data = new Data(String.valueOf(clientCapabilitiesStatusCode));                statusCommand = new Status(                                    idGenerator.next()          ,                                    syncHeader.getMsgID()       ,                                    commandReference            ,                                    clientCapabilities.getName(),                                     targetRefs                  ,                                    sourceRefs                  ,                                    null /* credential */       ,                                    null /* challenge */        ,                                    data                        ,                                    null /* items */                                );                commandList.add(statusCommand);            }        }  // end if syncHeader.getNoResponse() == false        //        // Server capabilities        //        if ((serverCapabilitiesRequest != null) && (authorizedStatusCode == StatusCode.AUTHENTICATION_ACCEPTED)) {            if (serverCapabilities == null) {                throw new ProtocolException("Error in creating a response: server capabilities not set (use setServerCapabilities())");            }            String commandReference =                 serverCapabilitiesRequest.getCmdID().getCmdID();                        Meta meta = serverCapabilitiesRequest.getMeta();            if (meta == null) {                meta = new Meta();                meta.setType(Constants.MIMETYPE_SYNCML_DEVICEINFO_XML);            }                        ComplexData data = new ComplexData();            data.setDevInf(serverCapabilities);                                    Source source   = ProtocolUtil.target2Source(                                  ((Item)(serverCapabilitiesRequest.getItems().get(0))).getTarget()                              );            Item[] capabilities = new Item[] { new Item(null, source, null, data, false) };                                                  Results resultsCommand = new Results(                                                idGenerator.next()   ,                                                syncHeader.getMsgID(),                                                commandReference     ,                                                meta /* meta */      ,                                                null /* target ref */,                                                null /* source ref */,                                                capabilities                                                                  );            commandList.add(resultsCommand);        }        //        // Alerts for each database to be synchronized        //        for (int i=0; (databases != null) &&                       (i<databases.length); ++i ) {                                      if (databases[i].getStatusCode() == StatusCode.OK) {                            Alert alertCommand =                 ProtocolUtil.createAlertCommand(idGenerator.next(),                                                false             ,                                                null              ,                                                databases[i]      );                commandList.add(alertCommand);            }        }        //        // If client capabilities are required but not provided, a get command        // must be added.        //        if (clientCapabilitiesRequired && (clientCapabilities == null)) {            Meta meta = new Meta();            meta.setType(Constants.MIMETYPE_SYNCML_DEVICEINFO_XML);            Target target = new Target( InitializationRequirements.CAPABILITIES_TARGET,                                         InitializationRequirements.CAPABILITIES_TARGET);            Item[] items = new Item[1];             items[0] = new Item(                           target,                           null  , /* source */                            null  , /* meta   */                           null  , /* data   */                           false   /* moreData*/                           );            Get getCommand = new Get(                                idGenerator.next()     ,                                false /* no response */,                                null  /* language    */,                                null  /* credentials */,                                meta                   ,                                items                             );            commandList.add(getCommand);        }        //        // 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 */   ,                                        syncHeader.getMeta()                                                                     );        int size = commandList.size();        AbstractCommand [] aCommands = new AbstractCommand[size];        for (int i=0; i < size; i++) {            aCommands[i] = (AbstractCommand)commandList.get(i);            if (authorizedStatusCode != StatusCode.AUTHENTICATION_ACCEPTED) {                if (aCommands[i] instanceof Status) {                    Status sc = (Status)aCommands[i];                    sc.setData(new Data(String.valueOf(authorizedStatusCode)));                }            }                    }        SyncBody responseBody = new SyncBody(                                   aCommands,                                   isFlag(Flags.FLAG_FINAL_MESSAGE)                                );                        try {            return new SyncML(responseHeader, responseBody);        } catch (RepresentationException e) {            //            // It should never happen !!!!            //            throw new ProtocolException("Unexpected error", e);        }    }        // --------------------------------------------------------- Private methods        }

⌨️ 快捷键说明

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