fixed #67
This commit is contained in:
parent
18303642f2
commit
2115c82330
@ -24,7 +24,7 @@
|
||||
<dependency>
|
||||
<groupId>commons-httpclient</groupId>
|
||||
<artifactId>commons-httpclient</artifactId>
|
||||
<version>3.1</version>
|
||||
<version>3.0</version>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
||||
@ -43,8 +43,8 @@ import com.foxinmy.weixin4j.model.Consts;
|
||||
import com.foxinmy.weixin4j.util.StringUtil;
|
||||
|
||||
/**
|
||||
* Apache HttpComponent 3.x
|
||||
*
|
||||
* Requires commons-httpclient 3.0 or higher
|
||||
*
|
||||
* @className HttpComponent3
|
||||
* @author jinyu(foxinmy@gmail.com)
|
||||
* @date 2015年8月18日
|
||||
@ -116,8 +116,11 @@ public class HttpComponent3 extends AbstractHttpClient {
|
||||
httpClient.getHttpConnectionManager().getParams()
|
||||
.setSendBufferSize(params.getChunkSize());
|
||||
httpMethod.getParams().setSoTimeout(params.getSocketTimeout());
|
||||
httpMethod.getParams().setHttpElementCharset(Consts.UTF_8.name());
|
||||
httpMethod.getParams().setUriCharset(Consts.UTF_8.name());
|
||||
httpMethod.getParams().setHttpElementCharset(
|
||||
Consts.UTF_8.name());
|
||||
httpMethod.getParams().setParameter(
|
||||
"http.protocol.uri-charset", Consts.UTF_8.name());
|
||||
// httpMethod.getParams().setUriCharset(Consts.UTF_8.name());
|
||||
httpMethod.getParams().setContentCharset(Consts.UTF_8.name());
|
||||
}
|
||||
if (useSSL) {
|
||||
@ -143,8 +146,7 @@ public class HttpComponent3 extends AbstractHttpClient {
|
||||
if (!headers.containsKey(HttpHeaders.USER_AGENT)) {
|
||||
headers.set(HttpHeaders.USER_AGENT, "apache/httpclient3");
|
||||
}
|
||||
for (Entry<String, List<String>> header : headers
|
||||
.entrySet()) {
|
||||
for (Entry<String, List<String>> header : headers.entrySet()) {
|
||||
if (HttpHeaders.COOKIE.equalsIgnoreCase(header.getKey())) {
|
||||
httpMethod.setRequestHeader(header.getKey(),
|
||||
StringUtil.join(header.getValue(), ';'));
|
||||
|
||||
@ -3,8 +3,8 @@ package com.foxinmy.weixin4j.http.factory;
|
||||
import org.apache.commons.httpclient.HttpClient;
|
||||
|
||||
/**
|
||||
* 使用Apache的HttpClient3.x
|
||||
*
|
||||
* 使用commons-httpclient3.x
|
||||
*
|
||||
* @className HttpComponent3Factory
|
||||
* @author jinyu(foxinmy@gmail.com)
|
||||
* @date 2015年8月12日
|
||||
|
||||
@ -12,8 +12,8 @@ import com.foxinmy.weixin4j.http.HttpStatus;
|
||||
import com.foxinmy.weixin4j.http.HttpVersion;
|
||||
|
||||
/**
|
||||
* HttpComponent3 Response
|
||||
*
|
||||
* HttpComponent3 Response:Requires commons-httpclient 3.0 or higher
|
||||
*
|
||||
* @className HttpComponent3Response
|
||||
* @author jinyu(foxinmy@gmail.com)
|
||||
* @date 2015年8月17日
|
||||
|
||||
@ -18,7 +18,7 @@ import com.foxinmy.weixin4j.xml.XmlStream;
|
||||
* @date 2015年1月9日
|
||||
* @since JDK 1.6
|
||||
*/
|
||||
public class FileTokenStorager implements TokenStorager {
|
||||
public class FileTokenStorager extends TokenStorager {
|
||||
|
||||
private final String cachePath;
|
||||
|
||||
@ -37,7 +37,7 @@ public class FileTokenStorager implements TokenStorager {
|
||||
if (token.getCreateTime() < 0) {
|
||||
return token;
|
||||
}
|
||||
if ((token.getCreateTime() + (token.getExpiresIn() * 1000l) - CUTMS) > System
|
||||
if ((token.getCreateTime() + (token.getExpiresIn() * 1000l) - ms()) > System
|
||||
.currentTimeMillis()) {
|
||||
return token;
|
||||
}
|
||||
@ -83,7 +83,7 @@ public class FileTokenStorager implements TokenStorager {
|
||||
@Override
|
||||
public boolean accept(File file) {
|
||||
return file.isFile()
|
||||
&& file.getName().startsWith(PREFIX)
|
||||
&& file.getName().startsWith(prefix())
|
||||
&& "xml".equals(FileUtil.getFileExtension(file
|
||||
.getName()));
|
||||
}
|
||||
|
||||
@ -17,7 +17,7 @@ import com.whalin.MemCached.SockIOPool;
|
||||
* @since JDK 1.6
|
||||
* @see
|
||||
*/
|
||||
public class MemcacheTokenStorager implements TokenStorager {
|
||||
public class MemcacheTokenStorager extends TokenStorager {
|
||||
|
||||
private final MemCachedClient mc;
|
||||
|
||||
@ -36,7 +36,7 @@ public class MemcacheTokenStorager implements TokenStorager {
|
||||
if (token.getExpiresIn() > 0) {
|
||||
mc.set(cacheKey, token,
|
||||
new Date(token.getCreateTime() + token.getExpiresIn()
|
||||
* 1000 - CUTMS));
|
||||
* 1000 - ms()));
|
||||
} else {
|
||||
mc.set(cacheKey, token);
|
||||
}
|
||||
|
||||
@ -14,7 +14,7 @@ import com.foxinmy.weixin4j.model.Token;
|
||||
* @since JDK 1.6
|
||||
* @see
|
||||
*/
|
||||
public class MemoryTokenStorager implements TokenStorager {
|
||||
public class MemoryTokenStorager extends TokenStorager {
|
||||
|
||||
private final Map<String, Token> CONMAP;
|
||||
|
||||
@ -26,7 +26,7 @@ public class MemoryTokenStorager implements TokenStorager {
|
||||
public Token lookup(String cacheKey) {
|
||||
Token token = this.CONMAP.get(cacheKey);
|
||||
if (token != null) {
|
||||
if ((token.getCreateTime() + (token.getExpiresIn() * 1000l) - CUTMS) > System
|
||||
if ((token.getCreateTime() + (token.getExpiresIn() * 1000l) - ms()) > System
|
||||
.currentTimeMillis()) {
|
||||
return token;
|
||||
}
|
||||
|
||||
@ -19,7 +19,7 @@ import com.foxinmy.weixin4j.model.Token;
|
||||
* @date 2015年1月9日
|
||||
* @since JDK 1.6
|
||||
*/
|
||||
public class RedisTokenStorager implements TokenStorager {
|
||||
public class RedisTokenStorager extends TokenStorager {
|
||||
|
||||
private JedisPool jedisPool;
|
||||
|
||||
@ -79,7 +79,7 @@ public class RedisTokenStorager implements TokenStorager {
|
||||
jedis.hmset(cacheKey, token2map(token));
|
||||
if (token.getExpiresIn() > 0) {
|
||||
jedis.expire(cacheKey, token.getExpiresIn()
|
||||
- (int) (CUTMS / 1000l));
|
||||
- (int) (ms() / 1000l));
|
||||
}
|
||||
} finally {
|
||||
if (jedis != null) {
|
||||
@ -130,7 +130,7 @@ public class RedisTokenStorager implements TokenStorager {
|
||||
Jedis jedis = null;
|
||||
try {
|
||||
jedis = jedisPool.getResource();
|
||||
Set<String> cacheKeys = jedis.keys(String.format("%s*", PREFIX));
|
||||
Set<String> cacheKeys = jedis.keys(String.format("%s*", prefix()));
|
||||
if (!cacheKeys.isEmpty()) {
|
||||
Pipeline pipeline = jedis.pipelined();
|
||||
for (String cacheKey : cacheKeys) {
|
||||
|
||||
@ -15,13 +15,22 @@ import com.foxinmy.weixin4j.model.Token;
|
||||
* @see RedisTokenStorager
|
||||
* @see MemcacheTokenStorager
|
||||
*/
|
||||
public interface TokenStorager extends CacheStorager<Token> {
|
||||
public abstract class TokenStorager implements CacheStorager<Token> {
|
||||
/**
|
||||
* 考虑到临界情况,在实际有效时间上减去60秒
|
||||
* 考虑到临界情况,实际token的有效时间减去该毫秒数
|
||||
*
|
||||
* @return 默认为60秒
|
||||
*/
|
||||
final long CUTMS = 60 * 1000l;
|
||||
public long ms() {
|
||||
return 60 * 1000l;
|
||||
}
|
||||
|
||||
/**
|
||||
* 缓存key的前缀
|
||||
*
|
||||
* @return 默认为weixin4j_
|
||||
*/
|
||||
final String PREFIX = "weixin4j_";
|
||||
public String prefix() {
|
||||
return "weixin4j_";
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user