📄 simplesessionhandler.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.server.session;import java.util.logging.Logger;import java.util.logging.Level;import java.util.ArrayList;import java.util.Iterator;import java.util.HashMap;import java.util.TreeMap;import java.util.Arrays;import java.util.List;import sync4j.framework.core.*;import sync4j.framework.core.Map;import sync4j.framework.protocol.*;import sync4j.framework.database.Database;import sync4j.server.engine.Sync4jEngine;import sync4j.framework.engine.SyncItemImpl;import sync4j.framework.engine.SyncOperation;import sync4j.framework.engine.SyncEngineFactory;import sync4j.framework.engine.source.MemorySyncSource;import sync4j.framework.logging.Sync4jLogger;import sync4j.framework.security.SecurityConstants;import sync4j.framework.security.Sync4jPrincipal;import sync4j.framework.server.session.SessionHandler;import sync4j.framework.server.error.ServerException;import sync4j.framework.server.SyncTimestamp;import sync4j.framework.server.LastTimestamp;import sync4j.framework.server.ClientMapping;import sync4j.framework.engine.SyncEngine;import sync4j.framework.engine.SyncItemState;import sync4j.framework.security.JAASOfficer;import sync4j.framework.server.error.InvalidCredentialsException;import sync4j.framework.server.store.NotFoundException;import sync4j.framework.server.store.PersistentStore;import sync4j.framework.server.store.PersistentStoreException;import sync4j.framework.server.session.SyncState;/** * This class represents the handler for a SyncML session. It coordinates and * handles the packages and messages as dictated by the protocol. * <p> * The entry point is <i>processMessage()</i>, which determines which message is * expected and what processing has to be done (depending on the value of * <i>currentState</i>). If an error accours, the session goes to the state * <i>STATE_ERROR</i>; in this state no other messages but initialization can be * performed. * <p> * In the current implementation separate initialization is required. * <p> * <i>SessionHandler</i> makes use of a <i>SyncEngine</i> for all * tasks not related to the handling of the protocol. * See <i>sync4j.framework.engine.SyncEngine</i> for more information. * * LOG NAME: sync4j.handler * * @see sync4j.framework.engine.SyncEngine * * @author Stefano Fornari @ Funambol * * @version $Id: SimpleSessionHandler.java,v 1.6 2004/06/03 13:00:56 luigiafassina Exp $ * */public class SimpleSessionHandler implements SessionHandler, java.io.Serializable { // --------------------------------------------------------------- Constants public static final int STATE_START = 0x0000; public static final int STATE_END = 0x0001; public static final int STATE_ERROR = 0xFFFF; public static final int STATE_INITIALIZATION_PROCESSING = 0x0010; public static final int STATE_INITIALIZATION_PROCESSED = 0x0011; public static final int STATE_SYNCHRONIZATION_PROCESSING = 0x0012; public static final int STATE_SYNCHRONIZATION_PROCESSED = 0x0013; public static final int STATE_SYNCHRONIZATION_COMPLETION = 0x0014; // ------------------------------------------------------------ Private data private int currentState = STATE_START; // // This data is true for slow sync // private boolean slow = false; /** * Gets the current state * * @return the current state */ public int getCurrentState() { return currentState; } private long creationTimestamp = -1; /** * Gets the creation timestamp of the session * * @return the creation timestamp */ public long getCreationTimestamp() { return creationTimestamp; } private transient Logger log = Sync4jLogger.getLogger("handler"); /** * SyncTimestamp for the current synchronization */ private SyncTimestamp nextTimestamp = null; private transient SyncInitialization syncInit = null; private transient ClientModifications modifications = null; /** * When action command received from client set to true */ private boolean syncCommandFromClient = false; /** * This Map contains all the mapping for the databases involved in the * synchronization for the current logged principal.<br> * The database names are used as keys and the corresponding mapping * contains the luig-guid mapping. The map is created and initialized * in <i>getClientMappings()</i> */ private java.util.Map clientMappings = null; /** * Contain the client device Id for the current session */ private String clientDeviceId = null; /** * The databases that have to be synchronized. It is set in the initialization * process. */ Database[] dbs = null; /** * The flag for established if credentials are valid */ private boolean validCredentials = false; // -------------------------------------------------------------- Properties /** * The session id - read only */ private String sessionId = null; public String getSessionId() { return this.sessionId; } /** * The command id generator (it defaults ro a <i>CommandIdGenerator</i> instance) */ private CommandIdGenerator cmdIdGenerator = new CommandIdGenerator(); public void setCommandIdGenerator(CommandIdGenerator cmdIdGenerator) { this.cmdIdGenerator = cmdIdGenerator; } public CommandIdGenerator getCommandIdGenerator() { return this.cmdIdGenerator; } /** * The cmdIdGenerator must be reset each time the process * of a message is starting */ private void resetIdGenerator() { this.cmdIdGenerator.reset(); syncEngine.setCommandIdGenerator(this.cmdIdGenerator); } /** * The message id generator (it defaults ro a <i>SimpleIdGenerator</i> instance) */ private SimpleIdGenerator msgIdGenerator = new SimpleIdGenerator(); /** * The Last message Id from the client */ private String lastMsgIdFromClient = new String(); /** * The factory for the <i>SyncEngine</i> object */ private SyncEngineFactory syncEngineFactory = null; /** * The <i>SyncEngine</i> */ private Sync4jEngine syncEngine = null; /** * Authenticated credential. If null no credential has authenticated. */ private Cred loggedCredential = null; private Sync4jPrincipal loggedPrincipal = null; private boolean guestEnabled = false; /** * The SyncState for the syncronization process */ private SyncState syncState = null; // ---------------------------------------------------------- Public methods /** * Sets the property <i> syncEngineFactory</i> and creates the instance of * the <i>SyncEngine</i> objects. * * @param syncEngineFactory the factory */ public void setSyncEngineFactory(SyncEngineFactory syncEngineFactory) { this.syncEngineFactory = syncEngineFactory; try { this.syncEngine = (Sync4jEngine) syncEngineFactory.getSyncEngine(); } catch (Sync4jException e) { log.throwing(getClass().getName(), "setSyncEngineFacytory", e); } if (log.isLoggable(Level.FINEST)) { log.finest(syncEngineFactory + " sync engine factory set"); log.finest(syncEngine + " sync engine created"); } } public SyncEngineFactory getSyncEngineFactory() { return this.syncEngineFactory; } public SyncEngine getSyncEngine() { return this.syncEngine; } /** * Indicates if the session is a new session */ private boolean newSession = true; public void setNew(boolean newSession) { this.newSession = newSession; } public boolean isNew() { return this.newSession; } // ------------------------------------------------------------ Constructors /** * Creates a new instance of SimpleSessionHandler */ public SimpleSessionHandler() { this.creationTimestamp = System.currentTimeMillis(); } /** * Creates a new instance of SimpleSessionHandler with a given session id */ public SimpleSessionHandler(String sessionId) { this(); this.sessionId = sessionId; } // ---------------------------------------------------------- Public methods /** * Returns true if the sessione has been authenticated. * * @return true if the sessione has been authenticated, false otherwise */ public boolean isAuthenticated() { return loggedCredential != null; } /** * Returns true if the session has not been authenticated because of an * expired account. * * @return true if the account is expired */ public boolean isAccountExpired() { JAASOfficer officer = (JAASOfficer) syncEngine.getOfficer(); return officer.isLoginExpired(); } /** * Is this the last message? */ private boolean finalMsg = true; /** * Processes the given message. See the class description for more * information. * * @param message the message to be processed * * @return the response message * * @throws ProtocolException */ public SyncML processMessage(SyncML message) throws ProtocolException, InvalidCredentialsException { boolean syncWithInit = false; SyncML response = null; SyncML syncResponse = null; // // Reset the cmdIdGenerator has specified in the spec // resetIdGenerator(); // // Each time a message is received for a particular session adjust the message ID // msgIdGenerator.next(); // // We maintain the message Id from client // lastMsgIdFromClient = message.getSyncHdr().getMsgID(); // // Initialize the device ID from the client request // clientDeviceId = message.getSyncHdr().getSource().getLocURI(); if (log.isLoggable(Level.FINE)) { log.fine("current state: " + getStateName(currentState)); } try { switch (currentState) { case STATE_ERROR: // in case of error you can start a new initialization case STATE_START: nextTimestamp = new SyncTimestamp(); nextTimestamp.start = System.currentTimeMillis(); nextTimestamp.tag = String.valueOf(nextTimestamp.start); syncState = new SyncState(); moveTo(STATE_INITIALIZATION_PROCESSING); login(message.getSyncHdr().getCred(), clientDeviceId); if (isAuthenticated()) { try { readPrincipal(loggedPrincipal); } catch (NotFoundException e) { validCredentials = false; loggedCredential = null; moveTo(STATE_INITIALIZATION_PROCESSING); } } case STATE_INITIALIZATION_PROCESSING: response = processInitMessage(message); // // To sort StatusCommand // response = mergeResponse(response, null); if (!isAuthenticated()) { moveTo(STATE_START); break; } // // Checking for message with Sync with Initialization // If yes, set CurrentState to STATE_SYNCHRONIZATION_PROCESSING // and proceed ahead for synchornization... // syncWithInit = checkSyncInit(message); if (syncWithInit) { moveTo(STATE_INITIALIZATION_PROCESSED); if (log.isLoggable(Level.FINE)) { log.fine("Sync message without separate initialization"); } } else { if (log.isLoggable(Level.FINE)) { log.fine("Sync with separate initalization"); } if (message.getSyncBody().isFinalMsg()) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -