|
| 1 | +package me.chanjar.weixin.cp.api; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | + |
| 5 | +import me.chanjar.weixin.common.bean.WxAccessToken; |
| 6 | +import me.chanjar.weixin.common.util.http.ApacheHttpClientBuilder; |
| 7 | +import redis.clients.jedis.Jedis; |
| 8 | +import redis.clients.jedis.JedisPool; |
| 9 | + |
| 10 | +/** |
| 11 | + * Jedis client implementor for wechat config storage |
| 12 | + * |
| 13 | + * @author gaigeshen |
| 14 | + */ |
| 15 | +public class WxCpJedisConfigStorage implements WxCpConfigStorage { |
| 16 | + |
| 17 | + /* Redis keys here */ |
| 18 | + private static final String ACCESS_TOKEN_KEY = "WX_CP_ACCESS_TOKEN"; |
| 19 | + private static final String ACCESS_TOKEN_EXPIRES_TIME_KEY = "WX_CP_ACCESS_TOKEN_EXPIRES_TIME"; |
| 20 | + private static final String JS_API_TICKET_KEY = "WX_CP_JS_API_TICKET"; |
| 21 | + private static final String JS_API_TICKET_EXPIRES_TIME_KEY = "WX_CP_JS_API_TICKET_EXPIRES_TIME"; |
| 22 | + |
| 23 | + private volatile String corpId; |
| 24 | + private volatile String corpSecret; |
| 25 | + |
| 26 | + private volatile String token; |
| 27 | + private volatile String aesKey; |
| 28 | + private volatile String agentId; |
| 29 | + |
| 30 | + private volatile String oauth2redirectUri; |
| 31 | + |
| 32 | + private volatile String http_proxy_host; |
| 33 | + private volatile int http_proxy_port; |
| 34 | + private volatile String http_proxy_username; |
| 35 | + private volatile String http_proxy_password; |
| 36 | + |
| 37 | + private volatile File tmpDirFile; |
| 38 | + |
| 39 | + private volatile ApacheHttpClientBuilder apacheHttpClientBuilder; |
| 40 | + |
| 41 | + /* Redis clients pool */ |
| 42 | + private final JedisPool jedisPool; |
| 43 | + |
| 44 | + /** |
| 45 | + * |
| 46 | + * |
| 47 | + * @param jedis |
| 48 | + */ |
| 49 | + public WxCpJedisConfigStorage(String host, int port) { |
| 50 | + this.jedisPool = new JedisPool(host, port); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * |
| 55 | + * This method will be destroy jedis pool |
| 56 | + */ |
| 57 | + public void destroy() { |
| 58 | + this.jedisPool.destroy(); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public String getAccessToken() { |
| 63 | + try (Jedis jedis = this.jedisPool.getResource()) { |
| 64 | + return jedis.get(ACCESS_TOKEN_KEY); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public boolean isAccessTokenExpired() { |
| 70 | + try (Jedis jedis = this.jedisPool.getResource()) { |
| 71 | + String expiresTimeStr = jedis.get(ACCESS_TOKEN_EXPIRES_TIME_KEY); |
| 72 | + |
| 73 | + if (expiresTimeStr != null) { |
| 74 | + Long expiresTime = Long.parseLong(expiresTimeStr); |
| 75 | + return System.currentTimeMillis() > expiresTime; |
| 76 | + } |
| 77 | + |
| 78 | + return true; |
| 79 | + |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public void expireAccessToken() { |
| 85 | + try (Jedis jedis = this.jedisPool.getResource()) { |
| 86 | + jedis.set(ACCESS_TOKEN_EXPIRES_TIME_KEY, "0"); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public synchronized void updateAccessToken(WxAccessToken accessToken) { |
| 92 | + this.updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn()); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public synchronized void updateAccessToken(String accessToken, int expiresInSeconds) { |
| 97 | + try (Jedis jedis = this.jedisPool.getResource()) { |
| 98 | + jedis.set(ACCESS_TOKEN_KEY, accessToken); |
| 99 | + |
| 100 | + jedis.set(ACCESS_TOKEN_EXPIRES_TIME_KEY, |
| 101 | + (System.currentTimeMillis() + (expiresInSeconds - 200) * 1000L) + ""); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public String getJsapiTicket() { |
| 107 | + try (Jedis jedis = this.jedisPool.getResource()) { |
| 108 | + return jedis.get(JS_API_TICKET_KEY); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public boolean isJsapiTicketExpired() { |
| 114 | + |
| 115 | + try (Jedis jedis = this.jedisPool.getResource()) { |
| 116 | + String expiresTimeStr = jedis.get(JS_API_TICKET_EXPIRES_TIME_KEY); |
| 117 | + |
| 118 | + if (expiresTimeStr != null) { |
| 119 | + Long expiresTime = Long.parseLong(expiresTimeStr); |
| 120 | + return System.currentTimeMillis() > expiresTime; |
| 121 | + } |
| 122 | + |
| 123 | + return true; |
| 124 | + |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public void expireJsapiTicket() { |
| 130 | + try (Jedis jedis = this.jedisPool.getResource()) { |
| 131 | + jedis.set(JS_API_TICKET_EXPIRES_TIME_KEY, "0"); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public synchronized void updateJsapiTicket(String jsapiTicket, int expiresInSeconds) { |
| 137 | + |
| 138 | + try (Jedis jedis = this.jedisPool.getResource()) { |
| 139 | + jedis.set(JS_API_TICKET_KEY, jsapiTicket); |
| 140 | + |
| 141 | + jedis.set(JS_API_TICKET_EXPIRES_TIME_KEY, |
| 142 | + (System.currentTimeMillis() + (expiresInSeconds - 200) * 1000L + "")); |
| 143 | + } |
| 144 | + |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public String getCorpId() { |
| 149 | + return this.corpId; |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public String getCorpSecret() { |
| 154 | + return this.corpSecret; |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + public String getAgentId() { |
| 159 | + return this.agentId; |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public String getToken() { |
| 164 | + return this.token; |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + public String getAesKey() { |
| 169 | + return this.aesKey; |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + public long getExpiresTime() { |
| 174 | + try (Jedis jedis = this.jedisPool.getResource()) { |
| 175 | + String expiresTimeStr = jedis.get(ACCESS_TOKEN_EXPIRES_TIME_KEY); |
| 176 | + |
| 177 | + if (expiresTimeStr != null) { |
| 178 | + Long expiresTime = Long.parseLong(expiresTimeStr); |
| 179 | + return expiresTime; |
| 180 | + } |
| 181 | + |
| 182 | + return 0L; |
| 183 | + |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + @Override |
| 188 | + public String getOauth2redirectUri() { |
| 189 | + return this.oauth2redirectUri; |
| 190 | + } |
| 191 | + |
| 192 | + @Override |
| 193 | + public String getHttp_proxy_host() { |
| 194 | + return this.http_proxy_host; |
| 195 | + } |
| 196 | + |
| 197 | + @Override |
| 198 | + public int getHttp_proxy_port() { |
| 199 | + return this.http_proxy_port; |
| 200 | + } |
| 201 | + |
| 202 | + @Override |
| 203 | + public String getHttp_proxy_username() { |
| 204 | + return this.http_proxy_username; |
| 205 | + } |
| 206 | + |
| 207 | + @Override |
| 208 | + public String getHttp_proxy_password() { |
| 209 | + return this.http_proxy_password; |
| 210 | + } |
| 211 | + |
| 212 | + @Override |
| 213 | + public File getTmpDirFile() { |
| 214 | + return this.tmpDirFile; |
| 215 | + } |
| 216 | + |
| 217 | + @Override |
| 218 | + public ApacheHttpClientBuilder getApacheHttpClientBuilder() { |
| 219 | + return this.apacheHttpClientBuilder; |
| 220 | + } |
| 221 | + |
| 222 | + public void setCorpId(String corpId) { |
| 223 | + this.corpId = corpId; |
| 224 | + } |
| 225 | + |
| 226 | + public void setCorpSecret(String corpSecret) { |
| 227 | + this.corpSecret = corpSecret; |
| 228 | + } |
| 229 | + |
| 230 | + public void setToken(String token) { |
| 231 | + this.token = token; |
| 232 | + } |
| 233 | + |
| 234 | + public void setAesKey(String aesKey) { |
| 235 | + this.aesKey = aesKey; |
| 236 | + } |
| 237 | + |
| 238 | + public void setAgentId(String agentId) { |
| 239 | + this.agentId = agentId; |
| 240 | + } |
| 241 | + |
| 242 | + // ============================ Setters below |
| 243 | + |
| 244 | + public void setOauth2redirectUri(String oauth2redirectUri) { |
| 245 | + this.oauth2redirectUri = oauth2redirectUri; |
| 246 | + } |
| 247 | + |
| 248 | + public void setHttp_proxy_host(String http_proxy_host) { |
| 249 | + this.http_proxy_host = http_proxy_host; |
| 250 | + } |
| 251 | + |
| 252 | + public void setHttp_proxy_port(int http_proxy_port) { |
| 253 | + this.http_proxy_port = http_proxy_port; |
| 254 | + } |
| 255 | + |
| 256 | + public void setHttp_proxy_username(String http_proxy_username) { |
| 257 | + this.http_proxy_username = http_proxy_username; |
| 258 | + } |
| 259 | + |
| 260 | + public void setHttp_proxy_password(String http_proxy_password) { |
| 261 | + this.http_proxy_password = http_proxy_password; |
| 262 | + } |
| 263 | + |
| 264 | + public void setTmpDirFile(File tmpDirFile) { |
| 265 | + this.tmpDirFile = tmpDirFile; |
| 266 | + } |
| 267 | + |
| 268 | + public void setApacheHttpClientBuilder(ApacheHttpClientBuilder apacheHttpClientBuilder) { |
| 269 | + this.apacheHttpClientBuilder = apacheHttpClientBuilder; |
| 270 | + } |
| 271 | + |
| 272 | +} |
0 commit comments