📄 syncinitialization.java
字号:
/** * Copyright (C) 2003-2004 Funambol * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * 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 General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package sync4j.framework.protocol;import java.util.ArrayList;import java.util.Hashtable;import java.security.Principal;import sync4j.framework.core.*;import sync4j.framework.database.Database;import sync4j.framework.protocol.SyncPackage;import sync4j.framework.protocol.ProtocolUtil;import sync4j.framework.protocol.v11.InitializationRequirements;import sync4j.framework.security.Sync4jPrincipal;/** * Represents the Initialization package of the SyncML protocol. * * Example: * <pre> * SyncInitialization syncInit = new SyncInitialization(header, body); * ... do something ... * syncInit.setServerCapabilities(serverCapabilities); * syncInit.setAuthorizedStatusCode(StatusCode.AUTHENTICATION_ACCEPTED); * syncInit.setClientCapabilitiesStatusCode(StatusCode.OK); * ... other initializations ... * Message response = syncInit.getResponse(); * </pre> * * @author Stefano Fornari @ Funambol * * @version $Id: SyncInitialization.java,v 1.32 2004/06/03 13:00:56 luigiafassina Exp $ * * @see SyncPackage */public class SyncInitializationextends SyncPackage{ /** * Contains the request for server capabilities sent by the client * (null means capabilities not required) */ private Get serverCapabilitiesRequest = null; /** * The device capabilities sent by the client. */ private Put clientCapabilities = null; public DevInf getClientDeviceInfo() throws ProtocolException { DevInfItem item = (DevInfItem)this.clientCapabilities.getItems().get(0); return item.getDevInfData().getDevInf(); } /** * Caches the commands sent by the client.It is set during the * checking of the requirements. */ private AbstractCommand[] clientCommands = null; public AbstractCommand[] getClientCommands() { return clientCommands; } /** * Caches the alert command sent by the client. It is set during the * checking of the requirements. */ private Alert[] clientAlerts = null; public Alert[] getClientAlerts() { return clientAlerts; } /** * Client Capabilities status code */ private int clientCapabilitiesStatusCode = -1; public int getClientCapabilitiesStatusCode() { return this.clientCapabilitiesStatusCode; } public void setClientCapabilitiesStatusCode(int clientCapabilitiesStatusCode) { this.clientCapabilitiesStatusCode = clientCapabilitiesStatusCode; } /** * Server capabilities */ private DevInf serverCapabilities = null; public void setServerCapabilities(DevInf capabilities) { this.serverCapabilities = capabilities; } public DevInf getServerCapabilities() { return this.serverCapabilities; } /** * Databases that the server wants to synchronized. They can be differnt * from the databases the client has requested to be synchronized. */ private Database[] databases = null; public void setDatabases(Database[] databases) { this.databases = databases; } public Database[] getDatabases() { return this.databases; } /** * Does the server require client capabilities? */ private boolean clientCapabilitiesRequired = false; public void setClientCapabilitiesRequired(boolean clientCapabilitiesRequired) { this.clientCapabilitiesRequired = clientCapabilitiesRequired; } public boolean isClientCapabilitiesRequired() { return this.clientCapabilitiesRequired; } /** * Authorized status code - used in building response message */ private int authorizedStatusCode = -1; public void setAuthorizedStatusCode(int authorizedStatusCode) { this.authorizedStatusCode = authorizedStatusCode; } // ---------------------------------------------------------- Command status /** * The map containing the status of the commands */ private Hashtable commandStatus = new Hashtable(); /** * Sets the status code for the given command. * * @param cmd the command * @param statusCode the status code */ public void setStatusCodeForCommand(AbstractCommand cmd, int statusCode) { setStatusCodeForCommand(cmd.getCmdID().getCmdID(), statusCode); } /** * Sets the status code for the command identified by the given id. * * @param cmdId the command id * @param statusCode the status code */ public void setStatusCodeForCommand(String cmdId, int statusCode) { commandStatus.put(cmdId, new Integer(statusCode)); } /** * Returns the status code for the given command. The status code must be * previously set with <i>setStatusCodeForCommand()</i>. If no status code * is associated to the given command, the default status code is returned. * * @param cmd the command * @param defaultCode the default status code * * @return the status code for the command if previously set, the default * status code otherwise */ public int getStatusCodeForCommand(AbstractCommand cmd, int defaultCode) { String cmdId = cmd.getCmdID().getCmdID(); return getStatusCodeForCommand(cmdId, defaultCode); } /** * The same as <i>getStatusCodeForCommand(AbstractCommand, int) but passing * in the command id instead of the command. * * @param cmdId the command id * @param defaultCode * * @return the status code for the command if previously set, the default * status code otherwise */ public int getStatusCodeForCommand(String cmdId, int defaultCode) { Integer statusCode = (Integer)commandStatus.get(cmdId); return (statusCode == null) ? defaultCode : statusCode.intValue(); } // ------------------------------------------------------------ Constructors /** * * @param syncHeader the header of the syncronization packet * @param syncBody the body of the syncronization packet * * @throws ProtocolException */ public SyncInitialization(final SyncHdr syncHeader, final SyncBody syncBody ) throws ProtocolException { super(syncHeader, syncBody); checkRequirements(); } // ------------------------------------------------------ Public methods /** * Alerts specifying the database to be synchronized could be more * then one. Each can contains more than one item, which specifies * a single database. This method selects the items containing the * databases regardless in what alert command they where included. * * @return an array of Database objects */ public Database[] getDatabasesToBeSynchronized() { ArrayList dbList = new ArrayList(); Cred c = null; Database db = null; Item[] items = null; Meta meta = null; for (int i=0; ((clientAlerts != null) && (i < clientAlerts.length)); ++i) { // // Only database synchronization alerts are selected // if (!AlertCode.isInitializationCode(clientAlerts[i].getData())) { continue; } items = (Item[])clientAlerts[i].getItems().toArray(new Item[0]); for (int j=0; ((items != null) && (j<items.length)); ++j) { meta = items[j].getMeta(); Anchor anchor = meta.getAnchor(); // // If the anchor does not exists, the alert does not represent // a database to be synchronized. // if (anchor == null) { continue; } c = syncHeader.getCred(); if (c == null) { c = Cred.getGuestCredential(); } Principal p = Sync4jPrincipal.fromCredential ( c.getData(), c.getType(), syncHeader.getSource().getLocURI()); // // NOTE: the target becomes the database source and vice-versa // db = new Database( items[j].getTarget().getLocURI() , null /* type */ , ProtocolUtil.source2Target(items[j].getSource()), ProtocolUtil.target2Source(items[j].getTarget()), anchor , p ); db.setMethod(clientAlerts[i].getData()); db.setAlertCommand(clientAlerts[i]); dbList.add(db); } // next j } // next i int dbSize = dbList.size(); Database[] dbArray = new Database[dbSize]; for (int i=0; i<dbSize; i++) { dbArray[i] = (Database)dbList.get(i); } return dbArray; } // ------------------------------------------------------------------------- /** * Checks that all requirements regarding the header of the initialization * packet are respected. * * @throws ProtocolException */ public void checkHeaderRequirements() throws ProtocolException { InitializationRequirements.checkDTDVersion (syncHeader.getVerDTD() ); InitializationRequirements.checkProtocolVersion(syncHeader.getVerProto()); InitializationRequirements.checkSessionId (syncHeader.getSessionID()); InitializationRequirements.checkMessageId (syncHeader.getMsgID() ); InitializationRequirements.checkTarget (syncHeader.getTarget() ); InitializationRequirements.checkSource (syncHeader.getSource() ); } /** * Checks that all requirements regarding the body of the initialization * packet are respected. * * @throws ProtocolException */ public void checkBodyRequirements() throws ProtocolException { ArrayList listAlerts = new ArrayList(); ArrayList mergedClientCommands = new ArrayList(); AbstractCommand[] allClientCommands = (AbstractCommand[])syncBody.getCommands().toArray( new AbstractCommand[0]); // // Extracts and checks alert commands // ArrayList alertList = ProtocolUtil.filterCommands(allClientCommands , Alert.class); int size = alertList.size();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -