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