📄 impssession.java
字号:
/* * Copyright (C) 2007 Esmertec AG. * Copyright (C) 2007 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.android.im.imps;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import com.android.im.engine.Contact;import com.android.im.engine.ImErrorInfo;import com.android.im.engine.ImException;import com.android.im.engine.LoginInfo;import com.android.im.imps.ImpsConnectionConfig.CirMethod;import com.android.im.imps.ImpsConnectionConfig.TransportType;/** * Represents the context of an IMPS session. The IMPS session is a framework in * which the IMPS services are provided to the IMPS client. It's established * when the client logs in and terminated when either the client logs out or the * SAP decides to disconnect the session. */public class ImpsSession { private static final String KEY_CIR_HTTP_ADDRESS = "cirHttpAddress"; private static final String KEY_CIR_TCP_PORT = "cirTcpPort"; private static final String KEY_CIR_TCP_ADDRESS = "cirTcpAddress"; private static final String KEY_CIR_METHOD = "CirMethod"; private static final String KEY_SERVER_POLL_MIN = "serverPollMin"; private static final String KEY_PASSWORD = "password"; private static final String KEY_USERNAME = "username"; private static final String KEY_KEEP_ALIVE_TIME = "keepAliveTime"; private static final String KEY_SESSION_COOKIE = "sessionCookie"; private static final String KEY_SESSION_ID = "sessionId"; private static final int DEFAULT_TCP_PORT = 3171; private ImpsConnection mConnection; private String mId; private String mCookie; private long mKeepAliveTime; private CirMethod mCurrentCirMethod; private String mCirTcpAddress; private int mCirTcpPort = DEFAULT_TCP_PORT; private long mServerPollMin; private String mCirHttpAddress; private LoginInfo mLoginInfo; private boolean mCapablityRequest; private List<CirMethod> mSupportedCirMethod; private Contact mLoginUser; PrimitiveElement mServiceTree; /** * Flag that indicates this is a new created session or not. */ private boolean mNew; ImpsSession(ImpsConnection connection, LoginInfo info) throws ImException{ mConnection = connection; setLoginInfo(info); mNew = true; mCookie = ImpsUtils.genSessionCookie(); mCirTcpPort = DEFAULT_TCP_PORT; mServerPollMin = connection.getConfig().getDefaultServerPollMin(); } ImpsSession(ImpsConnection connection, HashMap<String, String> values) throws ImException { mConnection = connection; mNew = false; mId = values.get(KEY_SESSION_ID); if (mId == null || mId.length() == 0) { throw new ImException(ImErrorInfo.INVALID_SESSION_CONTEXT, "Missing session id"); } mCookie = values.get(KEY_SESSION_COOKIE); if (mCookie == null || mCookie.length() == 0) { throw new ImException(ImErrorInfo.INVALID_SESSION_CONTEXT, "Missing session cookie"); } try { mKeepAliveTime = Long.parseLong(values.get(KEY_KEEP_ALIVE_TIME)); } catch (NumberFormatException e) { throw new ImException(ImErrorInfo.INVALID_SESSION_CONTEXT, "Invalid keepAliveTime"); } try { mServerPollMin = Long.parseLong(values.get(KEY_SERVER_POLL_MIN)); } catch (NumberFormatException e) { throw new ImException(ImErrorInfo.INVALID_SESSION_CONTEXT, "Invalid serverPollMin"); } String username = values.get(KEY_USERNAME); String password = values.get(KEY_PASSWORD); // Empty password might be valid if (username == null || username.length() == 0 || password == null) { throw new ImException(ImErrorInfo.INVALID_SESSION_CONTEXT, "Invalid username or password"); } setLoginInfo(new LoginInfo(username, password)); mCurrentCirMethod = CirMethod.valueOf(values.get(KEY_CIR_METHOD)); if (mCurrentCirMethod == CirMethod.STCP) { mCirTcpAddress = values.get(KEY_CIR_TCP_ADDRESS); if (mCirTcpAddress == null || mCirTcpAddress.length() == 0) { throw new ImException(ImErrorInfo.INVALID_SESSION_CONTEXT, "Missing CirTcpAddress"); } try { mCirTcpPort = Integer.parseInt(values.get(KEY_CIR_TCP_PORT)); } catch (NumberFormatException e) { throw new ImException(ImErrorInfo.INVALID_SESSION_CONTEXT, "Invalid CirTcpPort"); } } else if (mCurrentCirMethod == CirMethod.SHTTP) { mCirHttpAddress = values.get(KEY_CIR_HTTP_ADDRESS); } } public HashMap<String, String> getContext() { HashMap<String, String> values = new HashMap<String, String>(); values.put(KEY_SESSION_ID, mId); values.put(KEY_SESSION_COOKIE, mCookie); values.put(KEY_KEEP_ALIVE_TIME, Long.toString(mKeepAliveTime)); values.put(KEY_USERNAME, mLoginInfo.getUserName()); values.put(KEY_PASSWORD, mLoginInfo.getPassword()); values.put(KEY_SERVER_POLL_MIN, Long.toString(mServerPollMin)); values.put(KEY_CIR_METHOD, mCurrentCirMethod.name()); if(mCurrentCirMethod == CirMethod.STCP) { values.put(KEY_CIR_TCP_ADDRESS, mCirTcpAddress); values.put(KEY_CIR_TCP_PORT, Integer.toString(mCirTcpPort)); } else if (mCurrentCirMethod == CirMethod.SHTTP) { values.put(KEY_CIR_HTTP_ADDRESS, mCirHttpAddress); } return values; } /** * Gets the unique id of the session. * * @return the unique id of the session. */ public String getID() { return mId; } public void setId(String id) { mId = id; } public String getCookie() { return mCookie; } public String getUserName() { return mLoginInfo.getUserName(); } public String getPassword() { return mLoginInfo.getPassword(); } public LoginInfo getLoginInfo() { return mLoginInfo; } /** * Gets the auto logout timer value. * * @return the auto logout timer value. */ public long getKeepAliveTime() { return mKeepAliveTime; } public void setKeepAliveTime(long keepAliveTime) { mKeepAliveTime = keepAliveTime; } /** * Gets if further capability request is required in the session. * * @return <code>true</code> if further capability request is required. */ public boolean isCapablityRequestRequired() { return mCapablityRequest || mNew; } public void setCapablityRequestRequired(boolean required) { mCapablityRequest = required; } public ImpsUserAddress getLoginUserAddress() { return (ImpsUserAddress) mLoginUser.getAddress(); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -