NIMSDK-AOS  10.9.76
NosTokenSceneConfig.java
浏览该文件的文档.
1 package com.netease.nimlib.sdk;
2 
3 import android.text.TextUtils;
4 
5 import com.netease.nimlib.NimNosSceneKeyConstant;
6 
7 import java.util.HashMap;
8 
9 /**
10  * SDK nos token场景配置,不配置的话,走默认值,默认值只有以下3种场景:
11  *
12  * @see NimNosSceneKeyConstant#NIM_DEFAULT_PROFILE
13  * @see NimNosSceneKeyConstant#NIM_DEFAULT_IM
14  * @see NimNosSceneKeyConstant#NIM_SYSTEM_NOS_SCENE
15  * <p>
16  * <p>
17  * 默认场景对应的默认过期时间为{@link #NEVER_EXPIRE},用户可以修改默认场景的过期时间。
18  * 同时用户可以新增自己的场景并指定对应的过期时间(sceneKey-> expireTimeByDay),目前最多10个.
19  * <p>
20  * 用户发送消息或直接上传文件等情况下都可以任意指定相应的场景 ,不指定走默认的。
21  * 如果要指定相应的场景,只需要指定相应的sceneKey 即可
22  * <p>
23  * 强烈建议在配置时,将相应的sceneKey常量化,以方便使用,参考{@link NimNosSceneKeyConstant}
24  */
25 public class NosTokenSceneConfig {
26 
27  /**
28  * 永不过期,资源一直在于服务器
29  */
30  public static final long NEVER_EXPIRE = 0L;
31 
32  public static final long DAY_SECOND = 60 * 60 * 24L;
33  private static final int MIN_CUSTOM_SCENE_COUNT = 10;
34  private static final int DEFAULT_CUSTOM_SCENE_COUNT = MIN_CUSTOM_SCENE_COUNT;
35  private final static HashMap<String, Long> DEFAULT_SCENE = new HashMap<>();
36 
37  private static int sDefaultSceneCount;
38  private static NosTokenSceneConfig sDefault = new NosTokenSceneConfig();
39 
40  private HashMap<String, Long> mNosTokenScene;
41 
42  private int maxCustomNosSceneCount = DEFAULT_CUSTOM_SCENE_COUNT;
43 
44  static {
45  DEFAULT_SCENE.put(NimNosSceneKeyConstant.NIM_DEFAULT_PROFILE, NEVER_EXPIRE);
46  DEFAULT_SCENE.put(NimNosSceneKeyConstant.NIM_DEFAULT_IM, NEVER_EXPIRE);
47  DEFAULT_SCENE.put(NimNosSceneKeyConstant.NIM_SYSTEM_NOS_SCENE, NEVER_EXPIRE);
48  DEFAULT_SCENE.put(NimNosSceneKeyConstant.NIM_SECURITY_PREFIX, NEVER_EXPIRE);
49  sDefaultSceneCount = DEFAULT_SCENE.size();
50  }
51 
52 
54  mNosTokenScene = new HashMap<>();
55  mNosTokenScene.putAll(DEFAULT_SCENE);
56  }
57 
58  /**
59  * 获取自定义NOS场景最大数量
60  * @return
61  */
63  return maxCustomNosSceneCount;
64  }
65 
66  /**
67  * 更新默认场景 {@link NimNosSceneKeyConstant#NIM_DEFAULT_PROFILE}对应的过期时间
68  *
69  * @param expireTime 过期时间,0表示永不过期,单位: 天*/
70  public void updateDefaultProfileSceneExpireTime(int expireTime) {
71  if (expireTime < 0) {
72  throw new IllegalArgumentException("expireTimeByDay must >= 0");
73  }
74  mNosTokenScene.put(NimNosSceneKeyConstant.NIM_DEFAULT_PROFILE, expireTime * DAY_SECOND);
75  }
76 
77  /**
78  * 更新默认场景 {@link NimNosSceneKeyConstant#NIM_DEFAULT_IM }对应的过期时间
79  *
80  * @param expireTime 过期时间,0表示永不过期,单位: 天*/
81  public void updateDefaultIMSceneExpireTime(int expireTime) {
82  if (expireTime < 0) {
83  throw new IllegalArgumentException("expireTimeByDay must >= 0");
84  }
85  mNosTokenScene.put(NimNosSceneKeyConstant.NIM_DEFAULT_IM, expireTime * DAY_SECOND);
86  }
87 
88 
89  /**
90  * 增加自定义场景
91  *
92  * @param sceneKey 自定义场景key,使用时需要带上相应的sceneKey
93  * @param expireTime 过期时间,0表示永不过期,单位: 天
94  * @return NosTokenSceneConfig
95  */
96  public NosTokenSceneConfig appendCustomScene(String sceneKey, int expireTime) {
97  return appendCustomSceneByMilliseconds(sceneKey, expireTime * DAY_SECOND);
98  }
99 
100  public NosTokenSceneConfig appendCustomSceneByMilliseconds(String sceneKey, long expireTime) {
101  if (expireTime < 0 || TextUtils.isEmpty(sceneKey)) {
102  throw new IllegalArgumentException("sceneKey must not empty and expireTimeByDay must >= 0");
103  }
104  if (mNosTokenScene.size() - sDefaultSceneCount >= maxCustomNosSceneCount) {
105  throw new IllegalStateException("the custom scene count must <= " + maxCustomNosSceneCount);
106  }
107  if (NimNosSceneKeyConstant.NIM_SYSTEM_NOS_SCENE.equals(sceneKey)) {
108  throw new IllegalArgumentException("the \"nim_system_nos_scene\" scene cannot be modified");
109  }
110  mNosTokenScene.put(sceneKey, expireTime);
111  return this;
112  }
113 
114  /**
115  * 获取所有场景副本
116  *
117  * @return 包含所有场景的HashMap
118  */
119  public HashMap<String, Long> getNosTokenScene() {
120  HashMap<String, Long> copy = new HashMap<>(mNosTokenScene.size());
121  copy.putAll(mNosTokenScene);
122  return copy;
123  }
124 
126  sDefault.mNosTokenScene = DEFAULT_SCENE;
127  return sDefault;
128  }
129 }
void updateDefaultProfileSceneExpireTime(int expireTime)
更新默认场景 NimNosSceneKeyConstant#NIM_DEFAULT_PROFILE对应的过期时间
static final long NEVER_EXPIRE
永不过期,资源一直在于服务器
NosTokenSceneConfig appendCustomSceneByMilliseconds(String sceneKey, long expireTime)
NosTokenSceneConfig appendCustomScene(String sceneKey, int expireTime)
增加自定义场景
HashMap< String, Long > getNosTokenScene()
获取所有场景副本
int getMaxCustomNosSceneCount()
获取自定义NOS场景最大数量
SDK nos token场景配置,不配置的话,走默认值,默认值只有以下3种场景:
void updateDefaultIMSceneExpireTime(int expireTime)
更新默认场景 NimNosSceneKeyConstant#NIM_DEFAULT_IM对应的过期时间