mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-10-15 18:55:13 +08:00
批量格式化代码
This commit is contained in:
@@ -7,13 +7,13 @@ import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* 标识某个字段是否是必填的
|
||||
*
|
||||
* <p>
|
||||
* Created by Binary Wang on 2016/9/25.
|
||||
* @author binarywang (https://github.com/binarywang)
|
||||
*
|
||||
* @author binarywang (https://github.com/binarywang)
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.FIELD)
|
||||
public @interface Required {
|
||||
|
||||
}
|
||||
}
|
||||
|
@@ -51,7 +51,7 @@ public class WxMediaUploadResult implements Serializable {
|
||||
@Override
|
||||
public String toString() {
|
||||
return "WxUploadResult [type=" + this.type + ", media_id=" + this.mediaId + ", thumb_media_id=" + this.thumbMediaId
|
||||
+ ", created_at=" + this.createdAt + "]";
|
||||
+ ", created_at=" + this.createdAt + "]";
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -14,7 +14,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
||||
public class StandardSessionManager implements WxSessionManager, InternalSessionManager {
|
||||
|
||||
protected static final StringManager sm =
|
||||
StringManager.getManager(Constants.Package);
|
||||
StringManager.getManager(Constants.Package);
|
||||
/**
|
||||
* The descriptive name of this Manager implementation (for logging).
|
||||
*/
|
||||
@@ -82,7 +82,7 @@ public class StandardSessionManager implements WxSessionManager, InternalSession
|
||||
public WxSession getSession(String sessionId, boolean create) {
|
||||
if (sessionId == null) {
|
||||
throw new IllegalStateException
|
||||
(sm.getString("sessionManagerImpl.getSession.ise"));
|
||||
(sm.getString("sessionManagerImpl.getSession.ise"));
|
||||
}
|
||||
|
||||
InternalSession session = findSession(sessionId);
|
||||
@@ -135,15 +135,15 @@ public class StandardSessionManager implements WxSessionManager, InternalSession
|
||||
public InternalSession createSession(String sessionId) {
|
||||
if (sessionId == null) {
|
||||
throw new IllegalStateException
|
||||
(sm.getString("sessionManagerImpl.createSession.ise"));
|
||||
(sm.getString("sessionManagerImpl.createSession.ise"));
|
||||
}
|
||||
|
||||
if ((this.maxActiveSessions >= 0) &&
|
||||
(getActiveSessions() >= this.maxActiveSessions)) {
|
||||
(getActiveSessions() >= this.maxActiveSessions)) {
|
||||
this.rejectedSessions++;
|
||||
throw new TooManyActiveSessionsException(
|
||||
sm.getString("sessionManagerImpl.createSession.tmase"),
|
||||
this.maxActiveSessions);
|
||||
sm.getString("sessionManagerImpl.createSession.tmase"),
|
||||
this.maxActiveSessions);
|
||||
}
|
||||
|
||||
// Recycle or create a Session instance
|
||||
|
@@ -21,7 +21,7 @@ package me.chanjar.weixin.common.session;
|
||||
* reached and the server is refusing to create any new sessions.
|
||||
*/
|
||||
public class TooManyActiveSessionsException
|
||||
extends IllegalStateException {
|
||||
extends IllegalStateException {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
|
@@ -13,25 +13,10 @@ import org.apache.commons.lang3.builder.ToStringStyle;
|
||||
*/
|
||||
public class ToStringUtils {
|
||||
public static final ToStringStyle THE_STYLE = new SimpleMultiLineToStringStyle();
|
||||
private static class SimpleMultiLineToStringStyle extends ToStringStyle {
|
||||
private static final long serialVersionUID = 4645306494220335355L;
|
||||
private static final String LINE_SEPARATOR = "\n";
|
||||
private static final String NULL_TEXT = "<null>";
|
||||
|
||||
public SimpleMultiLineToStringStyle() {
|
||||
super();
|
||||
this.setContentStart("[");
|
||||
this.setFieldSeparator(LINE_SEPARATOR + " ");
|
||||
this.setFieldSeparatorAtStart(true);
|
||||
this.setContentEnd(LINE_SEPARATOR + "]");
|
||||
this.setNullText(NULL_TEXT);
|
||||
this.setUseShortClassName(true);
|
||||
this.setUseIdentityHashCode(false);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 用于产生去掉空值属性并以换行符分割各属性键值的toString字符串
|
||||
*
|
||||
* @param obj
|
||||
*/
|
||||
public static String toSimpleString(Object obj) {
|
||||
@@ -58,4 +43,21 @@ public class ToStringUtils {
|
||||
|
||||
return result.deleteCharAt(result.length() - 1).toString();
|
||||
}
|
||||
|
||||
private static class SimpleMultiLineToStringStyle extends ToStringStyle {
|
||||
private static final long serialVersionUID = 4645306494220335355L;
|
||||
private static final String LINE_SEPARATOR = "\n";
|
||||
private static final String NULL_TEXT = "<null>";
|
||||
|
||||
public SimpleMultiLineToStringStyle() {
|
||||
super();
|
||||
this.setContentStart("[");
|
||||
this.setFieldSeparator(LINE_SEPARATOR + " ");
|
||||
this.setFieldSeparatorAtStart(true);
|
||||
this.setContentEnd(LINE_SEPARATOR + "]");
|
||||
this.setNullText(NULL_TEXT);
|
||||
this.setUseShortClassName(true);
|
||||
this.setUseIdentityHashCode(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,9 +1,9 @@
|
||||
package me.chanjar.weixin.common.util.crypto;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Created by Daniel Qian on 14/10/19.
|
||||
*/
|
||||
|
@@ -6,6 +6,10 @@
|
||||
* 针对org.apache.commons.codec.binary.Base64,
|
||||
* 需要导入架包commons-codec-1.9(或commons-codec-1.8等其他版本)
|
||||
* 官方下载地址:http://commons.apache.org/proper/commons-codec/download_codec.cgi
|
||||
* <p>
|
||||
* 针对org.apache.commons.codec.binary.Base64,
|
||||
* 需要导入架包commons-codec-1.9(或commons-codec-1.8等其他版本)
|
||||
* 官方下载地址:http://commons.apache.org/proper/commons-codec/download_codec.cgi
|
||||
*/
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
@@ -17,10 +21,10 @@
|
||||
*/
|
||||
package me.chanjar.weixin.common.util.crypto;
|
||||
|
||||
import java.io.StringReader;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Arrays;
|
||||
import java.util.Random;
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.xml.sax.InputSource;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.spec.IvParameterSpec;
|
||||
@@ -28,11 +32,10 @@ import javax.crypto.spec.SecretKeySpec;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.xml.sax.InputSource;
|
||||
import java.io.StringReader;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Arrays;
|
||||
import java.util.Random;
|
||||
|
||||
public class WxCryptUtil {
|
||||
|
||||
@@ -84,6 +87,66 @@ public class WxCryptUtil {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将一个数字转换成生成4个字节的网络字节序bytes数组
|
||||
*
|
||||
* @param number
|
||||
*/
|
||||
private static byte[] number2BytesInNetworkOrder(int number) {
|
||||
byte[] orderBytes = new byte[4];
|
||||
orderBytes[3] = (byte) (number & 0xFF);
|
||||
orderBytes[2] = (byte) (number >> 8 & 0xFF);
|
||||
orderBytes[1] = (byte) (number >> 16 & 0xFF);
|
||||
orderBytes[0] = (byte) (number >> 24 & 0xFF);
|
||||
return orderBytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* 4个字节的网络字节序bytes数组还原成一个数字
|
||||
*
|
||||
* @param bytesInNetworkOrder
|
||||
*/
|
||||
private static int bytesNetworkOrder2Number(byte[] bytesInNetworkOrder) {
|
||||
int sourceNumber = 0;
|
||||
for (int i = 0; i < 4; i++) {
|
||||
sourceNumber <<= 8;
|
||||
sourceNumber |= bytesInNetworkOrder[i] & 0xff;
|
||||
}
|
||||
return sourceNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* 随机生成16位字符串
|
||||
*/
|
||||
private static String genRandomStr() {
|
||||
String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
||||
Random random = new Random();
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for (int i = 0; i < 16; i++) {
|
||||
int number = random.nextInt(base.length());
|
||||
sb.append(base.charAt(number));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成xml消息
|
||||
*
|
||||
* @param encrypt 加密后的消息密文
|
||||
* @param signature 安全签名
|
||||
* @param timestamp 时间戳
|
||||
* @param nonce 随机字符串
|
||||
* @return 生成的xml字符串
|
||||
*/
|
||||
private static String generateXml(String encrypt, String signature,
|
||||
String timestamp, String nonce) {
|
||||
String format = "<xml>\n" + "<Encrypt><![CDATA[%1$s]]></Encrypt>\n"
|
||||
+ "<MsgSignature><![CDATA[%2$s]]></MsgSignature>\n"
|
||||
+ "<TimeStamp>%3$s</TimeStamp>\n" + "<Nonce><![CDATA[%4$s]]></Nonce>\n"
|
||||
+ "</xml>";
|
||||
return String.format(format, encrypt, signature, timestamp, nonce);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将公众平台回复用户的消息加密打包.
|
||||
* <ol>
|
||||
@@ -119,7 +182,7 @@ public class WxCryptUtil {
|
||||
byte[] randomStringBytes = randomStr.getBytes(CHARSET);
|
||||
byte[] plainTextBytes = plainText.getBytes(CHARSET);
|
||||
byte[] bytesOfSizeInNetworkOrder = number2BytesInNetworkOrder(
|
||||
plainTextBytes.length);
|
||||
plainTextBytes.length);
|
||||
byte[] appIdBytes = this.appidOrCorpid.getBytes(CHARSET);
|
||||
|
||||
// randomStr + networkBytesOrder + text + appid
|
||||
@@ -198,7 +261,7 @@ public class WxCryptUtil {
|
||||
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
|
||||
SecretKeySpec key_spec = new SecretKeySpec(this.aesKey, "AES");
|
||||
IvParameterSpec iv = new IvParameterSpec(
|
||||
Arrays.copyOfRange(this.aesKey, 0, 16));
|
||||
Arrays.copyOfRange(this.aesKey, 0, 16));
|
||||
cipher.init(Cipher.DECRYPT_MODE, key_spec, iv);
|
||||
|
||||
// 使用BASE64对密文进行解码
|
||||
@@ -221,9 +284,9 @@ public class WxCryptUtil {
|
||||
int xmlLength = bytesNetworkOrder2Number(networkOrder);
|
||||
|
||||
xmlContent = new String(Arrays.copyOfRange(bytes, 20, 20 + xmlLength),
|
||||
CHARSET);
|
||||
CHARSET);
|
||||
from_appid = new String(
|
||||
Arrays.copyOfRange(bytes, 20 + xmlLength, bytes.length), CHARSET);
|
||||
Arrays.copyOfRange(bytes, 20 + xmlLength, bytes.length), CHARSET);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
@@ -237,64 +300,4 @@ public class WxCryptUtil {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 将一个数字转换成生成4个字节的网络字节序bytes数组
|
||||
*
|
||||
* @param number
|
||||
*/
|
||||
private static byte[] number2BytesInNetworkOrder(int number) {
|
||||
byte[] orderBytes = new byte[4];
|
||||
orderBytes[3] = (byte) (number & 0xFF);
|
||||
orderBytes[2] = (byte) (number >> 8 & 0xFF);
|
||||
orderBytes[1] = (byte) (number >> 16 & 0xFF);
|
||||
orderBytes[0] = (byte) (number >> 24 & 0xFF);
|
||||
return orderBytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* 4个字节的网络字节序bytes数组还原成一个数字
|
||||
*
|
||||
* @param bytesInNetworkOrder
|
||||
*/
|
||||
private static int bytesNetworkOrder2Number(byte[] bytesInNetworkOrder) {
|
||||
int sourceNumber = 0;
|
||||
for (int i = 0; i < 4; i++) {
|
||||
sourceNumber <<= 8;
|
||||
sourceNumber |= bytesInNetworkOrder[i] & 0xff;
|
||||
}
|
||||
return sourceNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* 随机生成16位字符串
|
||||
*/
|
||||
private static String genRandomStr() {
|
||||
String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
||||
Random random = new Random();
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for (int i = 0; i < 16; i++) {
|
||||
int number = random.nextInt(base.length());
|
||||
sb.append(base.charAt(number));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成xml消息
|
||||
*
|
||||
* @param encrypt 加密后的消息密文
|
||||
* @param signature 安全签名
|
||||
* @param timestamp 时间戳
|
||||
* @param nonce 随机字符串
|
||||
* @return 生成的xml字符串
|
||||
*/
|
||||
private static String generateXml(String encrypt, String signature,
|
||||
String timestamp, String nonce) {
|
||||
String format = "<xml>\n" + "<Encrypt><![CDATA[%1$s]]></Encrypt>\n"
|
||||
+ "<MsgSignature><![CDATA[%2$s]]></MsgSignature>\n"
|
||||
+ "<TimeStamp>%3$s</TimeStamp>\n" + "<Nonce><![CDATA[%4$s]]></Nonce>\n"
|
||||
+ "</xml>";
|
||||
return String.format(format, encrypt, signature, timestamp, nonce);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -25,7 +25,7 @@ public class FileUtils {
|
||||
}
|
||||
|
||||
tmpFile.deleteOnExit();
|
||||
|
||||
|
||||
try (FileOutputStream fos = new FileOutputStream(tmpFile)) {
|
||||
int read = 0;
|
||||
byte[] bytes = new byte[1024 * 100];
|
||||
@@ -35,7 +35,7 @@ public class FileUtils {
|
||||
|
||||
fos.flush();
|
||||
return tmpFile;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -1,38 +1,10 @@
|
||||
package me.chanjar.weixin.common.util.http;
|
||||
|
||||
import jodd.http.HttpConnectionProvider;
|
||||
import jodd.http.HttpRequest;
|
||||
import jodd.http.HttpResponse;
|
||||
import jodd.http.ProxyInfo;
|
||||
import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.fs.FileUtils;
|
||||
import me.chanjar.weixin.common.util.http.apache.ApacheMediaDownloadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.apache.InputStreamResponseHandler;
|
||||
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
|
||||
import me.chanjar.weixin.common.util.http.jodd.JoddMediaDownloadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkMediaDownloadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkhttpProxyInfo;
|
||||
import okhttp3.*;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Proxy;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* 下载媒体文件请求执行器,请求的参数是String, 返回的结果是File
|
||||
@@ -42,8 +14,14 @@ import java.util.regex.Pattern;
|
||||
*/
|
||||
public abstract class MediaDownloadRequestExecutor<H, P> implements RequestExecutor<File, String> {
|
||||
|
||||
public static RequestExecutor<File, String> create(RequestHttp requestHttp, File tmpDirFile){
|
||||
switch (requestHttp.getRequestType()){
|
||||
protected RequestHttp<H, P> requestHttp;
|
||||
protected File tmpDirFile;
|
||||
public MediaDownloadRequestExecutor(RequestHttp requestHttp, File tmpDirFile) {
|
||||
this.tmpDirFile = tmpDirFile;
|
||||
}
|
||||
|
||||
public static RequestExecutor<File, String> create(RequestHttp requestHttp, File tmpDirFile) {
|
||||
switch (requestHttp.getRequestType()) {
|
||||
case apacheHttp:
|
||||
return new ApacheMediaDownloadRequestExecutor(requestHttp, tmpDirFile);
|
||||
case joddHttp:
|
||||
@@ -55,11 +33,4 @@ public abstract class MediaDownloadRequestExecutor<H, P> implements RequestExecu
|
||||
}
|
||||
}
|
||||
|
||||
protected RequestHttp<H, P> requestHttp;
|
||||
protected File tmpDirFile;
|
||||
|
||||
public MediaDownloadRequestExecutor(RequestHttp requestHttp, File tmpDirFile) {
|
||||
this.tmpDirFile = tmpDirFile;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,34 +1,11 @@
|
||||
package me.chanjar.weixin.common.util.http;
|
||||
|
||||
import jodd.http.HttpConnectionProvider;
|
||||
import jodd.http.HttpRequest;
|
||||
import jodd.http.HttpResponse;
|
||||
import jodd.http.ProxyInfo;
|
||||
import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.apache.ApacheMediaUploadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
|
||||
|
||||
import me.chanjar.weixin.common.util.http.jodd.JoddMediaUploadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkMediaUploadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkhttpProxyInfo;
|
||||
import okhttp3.*;
|
||||
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.entity.mime.HttpMultipartMode;
|
||||
import org.apache.http.entity.mime.MultipartEntityBuilder;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Proxy;
|
||||
|
||||
/**
|
||||
* 上传媒体文件请求执行器,请求的参数是File, 返回的结果是String
|
||||
@@ -36,14 +13,14 @@ import java.net.Proxy;
|
||||
* @author Daniel Qian
|
||||
*/
|
||||
public abstract class MediaUploadRequestExecutor<H, P> implements RequestExecutor<WxMediaUploadResult, File> {
|
||||
protected RequestHttp<H,P> requestHttp;
|
||||
protected RequestHttp<H, P> requestHttp;
|
||||
|
||||
public MediaUploadRequestExecutor(RequestHttp requestHttp){
|
||||
this.requestHttp =requestHttp;
|
||||
public MediaUploadRequestExecutor(RequestHttp requestHttp) {
|
||||
this.requestHttp = requestHttp;
|
||||
}
|
||||
|
||||
public static RequestExecutor<WxMediaUploadResult, File> create(RequestHttp requestHttp){
|
||||
switch (requestHttp.getRequestType()){
|
||||
public static RequestExecutor<WxMediaUploadResult, File> create(RequestHttp requestHttp) {
|
||||
switch (requestHttp.getRequestType()) {
|
||||
case apacheHttp:
|
||||
return new ApacheMediaUploadRequestExecutor(requestHttp);
|
||||
case joddHttp:
|
||||
|
@@ -1,17 +1,9 @@
|
||||
package me.chanjar.weixin.common.util.http;
|
||||
|
||||
import jodd.http.HttpConnectionProvider;
|
||||
import jodd.http.ProxyInfo;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkhttpProxyInfo;
|
||||
import okhttp3.ConnectionPool;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
|
||||
/**
|
||||
* http请求执行器
|
||||
*
|
||||
@@ -41,15 +33,15 @@ public interface RequestExecutor<T, E> {
|
||||
T executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, E data) throws WxErrorException, IOException;
|
||||
|
||||
*//**
|
||||
* jodd-http实现方式
|
||||
* @param provider
|
||||
* @param proxyInfo
|
||||
* @param uri
|
||||
* @param data
|
||||
* @return
|
||||
* @throws WxErrorException
|
||||
* @throws IOException
|
||||
*//*
|
||||
* jodd-http实现方式
|
||||
* @param provider
|
||||
* @param proxyInfo
|
||||
* @param uri
|
||||
* @param data
|
||||
* @return
|
||||
* @throws WxErrorException
|
||||
* @throws IOException
|
||||
*//*
|
||||
T executeJodd(HttpConnectionProvider provider, ProxyInfo proxyInfo, String uri, E data) throws WxErrorException, IOException;
|
||||
|
||||
|
||||
|
@@ -3,22 +3,23 @@ package me.chanjar.weixin.common.util.http;
|
||||
/**
|
||||
* Created by ecoolper on 2017/4/22.
|
||||
*/
|
||||
public interface RequestHttp<H,P> {
|
||||
public interface RequestHttp<H, P> {
|
||||
|
||||
/**
|
||||
* 返回httpClient
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
H getRequestHttpClient();
|
||||
|
||||
/**
|
||||
* 返回httpProxy
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
P getRequestHttpProxy();
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
HttpType getRequestType();
|
||||
|
@@ -1,29 +1,8 @@
|
||||
package me.chanjar.weixin.common.util.http;
|
||||
|
||||
import jodd.http.HttpConnectionProvider;
|
||||
import jodd.http.HttpRequest;
|
||||
import jodd.http.HttpResponse;
|
||||
import jodd.http.ProxyInfo;
|
||||
import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.apache.ApacheSimpleGetRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
|
||||
|
||||
import me.chanjar.weixin.common.util.http.jodd.JoddSimpleGetRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkSimpleGetRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkhttpProxyInfo;
|
||||
import okhttp3.*;
|
||||
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Proxy;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 简单的GET请求执行器,请求的参数是String, 返回的结果也是String
|
||||
@@ -31,15 +10,15 @@ import java.util.concurrent.TimeUnit;
|
||||
* @author Daniel Qian
|
||||
*/
|
||||
public abstract class SimpleGetRequestExecutor<H, P> implements RequestExecutor<String, String> {
|
||||
protected RequestHttp<H,P> requestHttp;
|
||||
protected RequestHttp<H, P> requestHttp;
|
||||
|
||||
public SimpleGetRequestExecutor(RequestHttp requestHttp){
|
||||
this.requestHttp =requestHttp;
|
||||
public SimpleGetRequestExecutor(RequestHttp requestHttp) {
|
||||
this.requestHttp = requestHttp;
|
||||
}
|
||||
|
||||
|
||||
public static RequestExecutor<String, String> create(RequestHttp requestHttp){
|
||||
switch(requestHttp.getRequestType()){
|
||||
public static RequestExecutor<String, String> create(RequestHttp requestHttp) {
|
||||
switch (requestHttp.getRequestType()) {
|
||||
case apacheHttp:
|
||||
return new ApacheSimpleGetRequestExecutor(requestHttp);
|
||||
case joddHttp:
|
||||
|
@@ -1,29 +1,8 @@
|
||||
package me.chanjar.weixin.common.util.http;
|
||||
|
||||
import jodd.http.HttpConnectionProvider;
|
||||
import jodd.http.HttpRequest;
|
||||
import jodd.http.HttpResponse;
|
||||
import jodd.http.ProxyInfo;
|
||||
import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.apache.ApacheSimpleGetRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.apache.ApacheSimplePostRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
|
||||
|
||||
import me.chanjar.weixin.common.util.http.jodd.JoddSimplePostRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkSimplePostRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkhttpProxyInfo;
|
||||
import okhttp3.*;
|
||||
|
||||
import org.apache.http.Consts;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* 用装饰模式实现
|
||||
@@ -32,14 +11,14 @@ import java.io.IOException;
|
||||
* @author Daniel Qian
|
||||
*/
|
||||
public abstract class SimplePostRequestExecutor<H, P> implements RequestExecutor<String, String> {
|
||||
protected RequestHttp<H,P> requestHttp;
|
||||
protected RequestHttp<H, P> requestHttp;
|
||||
|
||||
public SimplePostRequestExecutor(RequestHttp requestHttp){
|
||||
this.requestHttp =requestHttp;
|
||||
public SimplePostRequestExecutor(RequestHttp requestHttp) {
|
||||
this.requestHttp = requestHttp;
|
||||
}
|
||||
|
||||
public static RequestExecutor<String, String> create(RequestHttp requestHttp){
|
||||
switch (requestHttp.getRequestType()){
|
||||
public static RequestExecutor<String, String> create(RequestHttp requestHttp) {
|
||||
switch (requestHttp.getRequestType()) {
|
||||
case apacheHttp:
|
||||
return new ApacheSimplePostRequestExecutor(requestHttp);
|
||||
case joddHttp:
|
||||
|
@@ -1,64 +1,61 @@
|
||||
package me.chanjar.weixin.common.util.http;
|
||||
|
||||
import org.apache.http.conn.DnsResolver;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.http.conn.DnsResolver;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* 微信DNS域名解析器,将微信域名绑定到指定IP
|
||||
* --------------------------------------------
|
||||
* 适用于服务器端调用微信服务器需要开通出口防火墙情况
|
||||
*
|
||||
* <p>
|
||||
* Created by Andy Huo on 17/03/28.
|
||||
*/
|
||||
public class WxDnsResolver implements DnsResolver {
|
||||
|
||||
protected final Logger log = LoggerFactory.getLogger(WxDnsResolver.class);
|
||||
|
||||
private static Map<String, InetAddress[]> MAPPINGS = new HashMap<String, InetAddress[]>();
|
||||
|
||||
private final static String WECHAT_API_URL = "api.weixin.qq.com";
|
||||
|
||||
private String wxApiIp;
|
||||
|
||||
public WxDnsResolver(String ip){
|
||||
|
||||
this.wxApiIp = ip;
|
||||
this.init();
|
||||
}
|
||||
|
||||
private void init(){
|
||||
if(log.isDebugEnabled()){
|
||||
log.debug("init wechat dns config with ip {}", wxApiIp);
|
||||
}
|
||||
try {
|
||||
MAPPINGS.put(WECHAT_API_URL, new InetAddress[]{InetAddress.getByName(wxApiIp)});
|
||||
} catch (UnknownHostException e) {
|
||||
//如果初始化DNS配置失败则使用默认配置,不影响服务的启动
|
||||
log.error("init WxDnsResolver error", e);
|
||||
MAPPINGS = new HashMap<String, InetAddress[]>();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public InetAddress[] resolve(String host) throws UnknownHostException {
|
||||
|
||||
|
||||
return MAPPINGS.containsKey(host) ? MAPPINGS.get(host) : new InetAddress[0];
|
||||
}
|
||||
private final static String WECHAT_API_URL = "api.weixin.qq.com";
|
||||
private static Map<String, InetAddress[]> MAPPINGS = new HashMap<String, InetAddress[]>();
|
||||
protected final Logger log = LoggerFactory.getLogger(WxDnsResolver.class);
|
||||
private String wxApiIp;
|
||||
|
||||
public String getWxApiIp() {
|
||||
return wxApiIp;
|
||||
}
|
||||
public WxDnsResolver(String ip) {
|
||||
|
||||
public void setWxApiIp(String wxApiIp) {
|
||||
this.wxApiIp = wxApiIp;
|
||||
this.init();
|
||||
}
|
||||
this.wxApiIp = ip;
|
||||
this.init();
|
||||
}
|
||||
|
||||
private void init() {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("init wechat dns config with ip {}", wxApiIp);
|
||||
}
|
||||
try {
|
||||
MAPPINGS.put(WECHAT_API_URL, new InetAddress[]{InetAddress.getByName(wxApiIp)});
|
||||
} catch (UnknownHostException e) {
|
||||
//如果初始化DNS配置失败则使用默认配置,不影响服务的启动
|
||||
log.error("init WxDnsResolver error", e);
|
||||
MAPPINGS = new HashMap<String, InetAddress[]>();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public InetAddress[] resolve(String host) throws UnknownHostException {
|
||||
|
||||
|
||||
return MAPPINGS.containsKey(host) ? MAPPINGS.get(host) : new InetAddress[0];
|
||||
}
|
||||
|
||||
public String getWxApiIp() {
|
||||
return wxApiIp;
|
||||
}
|
||||
|
||||
public void setWxApiIp(String wxApiIp) {
|
||||
this.wxApiIp = wxApiIp;
|
||||
this.init();
|
||||
}
|
||||
}
|
||||
|
@@ -5,6 +5,7 @@ import org.apache.http.impl.client.CloseableHttpClient;
|
||||
|
||||
/**
|
||||
* httpclient build interface
|
||||
*
|
||||
* @author kakotor
|
||||
*/
|
||||
public interface ApacheHttpClientBuilder {
|
||||
|
@@ -30,285 +30,277 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
/**
|
||||
* httpclient 连接管理器 自带DNS解析
|
||||
*
|
||||
* <p>
|
||||
* 大部分代码拷贝自:DefaultApacheHttpClientBuilder
|
||||
*
|
||||
* @author Andy.Huo
|
||||
*/
|
||||
@NotThreadSafe
|
||||
public class ApacheHttpDnsClientBuilder implements ApacheHttpClientBuilder {
|
||||
protected final Logger log = LoggerFactory.getLogger(ApacheHttpDnsClientBuilder.class);
|
||||
private final AtomicBoolean prepared = new AtomicBoolean(false);
|
||||
private int connectionRequestTimeout = 3000;
|
||||
private int connectionTimeout = 5000;
|
||||
private int soTimeout = 5000;
|
||||
private int idleConnTimeout = 60000;
|
||||
private int checkWaitTime = 60000;
|
||||
private int maxConnPerHost = 10;
|
||||
private int maxTotalConn = 50;
|
||||
private String userAgent;
|
||||
protected final Logger log = LoggerFactory.getLogger(ApacheHttpDnsClientBuilder.class);
|
||||
private final AtomicBoolean prepared = new AtomicBoolean(false);
|
||||
private int connectionRequestTimeout = 3000;
|
||||
private int connectionTimeout = 5000;
|
||||
private int soTimeout = 5000;
|
||||
private int idleConnTimeout = 60000;
|
||||
private int checkWaitTime = 60000;
|
||||
private int maxConnPerHost = 10;
|
||||
private int maxTotalConn = 50;
|
||||
private String userAgent;
|
||||
|
||||
private DnsResolver dnsResover;
|
||||
private DnsResolver dnsResover;
|
||||
|
||||
private HttpRequestRetryHandler httpRequestRetryHandler = new HttpRequestRetryHandler() {
|
||||
@Override
|
||||
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
private SSLConnectionSocketFactory sslConnectionSocketFactory = SSLConnectionSocketFactory.getSocketFactory();
|
||||
private PlainConnectionSocketFactory plainConnectionSocketFactory = PlainConnectionSocketFactory.getSocketFactory();
|
||||
private String httpProxyHost;
|
||||
private int httpProxyPort;
|
||||
private String httpProxyUsername;
|
||||
private String httpProxyPassword;
|
||||
private HttpRequestRetryHandler httpRequestRetryHandler = new HttpRequestRetryHandler() {
|
||||
@Override
|
||||
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
private SSLConnectionSocketFactory sslConnectionSocketFactory = SSLConnectionSocketFactory.getSocketFactory();
|
||||
private PlainConnectionSocketFactory plainConnectionSocketFactory = PlainConnectionSocketFactory.getSocketFactory();
|
||||
private String httpProxyHost;
|
||||
private int httpProxyPort;
|
||||
private String httpProxyUsername;
|
||||
private String httpProxyPassword;
|
||||
|
||||
/**
|
||||
* 闲置连接监控线程
|
||||
*/
|
||||
private IdleConnectionMonitorThread idleConnectionMonitorThread;
|
||||
private HttpClientBuilder httpClientBuilder;
|
||||
/**
|
||||
* 闲置连接监控线程
|
||||
*/
|
||||
private IdleConnectionMonitorThread idleConnectionMonitorThread;
|
||||
private HttpClientBuilder httpClientBuilder;
|
||||
|
||||
private ApacheHttpDnsClientBuilder() {
|
||||
}
|
||||
private ApacheHttpDnsClientBuilder() {
|
||||
}
|
||||
|
||||
public static ApacheHttpDnsClientBuilder get() {
|
||||
return new ApacheHttpDnsClientBuilder();
|
||||
}
|
||||
public static ApacheHttpDnsClientBuilder get() {
|
||||
return new ApacheHttpDnsClientBuilder();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ApacheHttpClientBuilder httpProxyHost(String httpProxyHost) {
|
||||
this.httpProxyHost = httpProxyHost;
|
||||
return this;
|
||||
}
|
||||
@Override
|
||||
public ApacheHttpClientBuilder httpProxyHost(String httpProxyHost) {
|
||||
this.httpProxyHost = httpProxyHost;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ApacheHttpClientBuilder httpProxyPort(int httpProxyPort) {
|
||||
this.httpProxyPort = httpProxyPort;
|
||||
return this;
|
||||
}
|
||||
@Override
|
||||
public ApacheHttpClientBuilder httpProxyPort(int httpProxyPort) {
|
||||
this.httpProxyPort = httpProxyPort;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ApacheHttpClientBuilder httpProxyUsername(String httpProxyUsername) {
|
||||
this.httpProxyUsername = httpProxyUsername;
|
||||
return this;
|
||||
}
|
||||
@Override
|
||||
public ApacheHttpClientBuilder httpProxyUsername(String httpProxyUsername) {
|
||||
this.httpProxyUsername = httpProxyUsername;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ApacheHttpClientBuilder httpProxyPassword(String httpProxyPassword) {
|
||||
this.httpProxyPassword = httpProxyPassword;
|
||||
return this;
|
||||
}
|
||||
@Override
|
||||
public ApacheHttpClientBuilder httpProxyPassword(String httpProxyPassword) {
|
||||
this.httpProxyPassword = httpProxyPassword;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ApacheHttpClientBuilder sslConnectionSocketFactory(SSLConnectionSocketFactory sslConnectionSocketFactory) {
|
||||
this.sslConnectionSocketFactory = sslConnectionSocketFactory;
|
||||
return this;
|
||||
}
|
||||
@Override
|
||||
public ApacheHttpClientBuilder sslConnectionSocketFactory(SSLConnectionSocketFactory sslConnectionSocketFactory) {
|
||||
this.sslConnectionSocketFactory = sslConnectionSocketFactory;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取链接的超时时间设置,默认3000ms
|
||||
* <p>
|
||||
* 设置为零时不超时,一直等待. 设置为负数是使用系统默认设置(非上述的3000ms的默认值,而是httpclient的默认设置).
|
||||
* </p>
|
||||
*
|
||||
* @param connectionRequestTimeout
|
||||
* 获取链接的超时时间设置(单位毫秒),默认3000ms
|
||||
*/
|
||||
public void setConnectionRequestTimeout(int connectionRequestTimeout) {
|
||||
this.connectionRequestTimeout = connectionRequestTimeout;
|
||||
}
|
||||
/**
|
||||
* 获取链接的超时时间设置,默认3000ms
|
||||
* <p>
|
||||
* 设置为零时不超时,一直等待. 设置为负数是使用系统默认设置(非上述的3000ms的默认值,而是httpclient的默认设置).
|
||||
* </p>
|
||||
*
|
||||
* @param connectionRequestTimeout 获取链接的超时时间设置(单位毫秒),默认3000ms
|
||||
*/
|
||||
public void setConnectionRequestTimeout(int connectionRequestTimeout) {
|
||||
this.connectionRequestTimeout = connectionRequestTimeout;
|
||||
}
|
||||
|
||||
/**
|
||||
* 建立链接的超时时间,默认为5000ms.由于是在链接池获取链接,此设置应该并不起什么作用
|
||||
* <p>
|
||||
* 设置为零时不超时,一直等待. 设置为负数是使用系统默认设置(非上述的5000ms的默认值,而是httpclient的默认设置).
|
||||
* </p>
|
||||
*
|
||||
* @param connectionTimeout
|
||||
* 建立链接的超时时间设置(单位毫秒),默认5000ms
|
||||
*/
|
||||
public void setConnectionTimeout(int connectionTimeout) {
|
||||
this.connectionTimeout = connectionTimeout;
|
||||
}
|
||||
/**
|
||||
* 建立链接的超时时间,默认为5000ms.由于是在链接池获取链接,此设置应该并不起什么作用
|
||||
* <p>
|
||||
* 设置为零时不超时,一直等待. 设置为负数是使用系统默认设置(非上述的5000ms的默认值,而是httpclient的默认设置).
|
||||
* </p>
|
||||
*
|
||||
* @param connectionTimeout 建立链接的超时时间设置(单位毫秒),默认5000ms
|
||||
*/
|
||||
public void setConnectionTimeout(int connectionTimeout) {
|
||||
this.connectionTimeout = connectionTimeout;
|
||||
}
|
||||
|
||||
/**
|
||||
* 默认NIO的socket超时设置,默认5000ms.
|
||||
*
|
||||
* @param soTimeout
|
||||
* 默认NIO的socket超时设置,默认5000ms.
|
||||
* @see java.net.SocketOptions#SO_TIMEOUT
|
||||
*/
|
||||
public void setSoTimeout(int soTimeout) {
|
||||
this.soTimeout = soTimeout;
|
||||
}
|
||||
/**
|
||||
* 默认NIO的socket超时设置,默认5000ms.
|
||||
*
|
||||
* @param soTimeout 默认NIO的socket超时设置,默认5000ms.
|
||||
* @see java.net.SocketOptions#SO_TIMEOUT
|
||||
*/
|
||||
public void setSoTimeout(int soTimeout) {
|
||||
this.soTimeout = soTimeout;
|
||||
}
|
||||
|
||||
/**
|
||||
* 空闲链接的超时时间,默认60000ms.
|
||||
* <p>
|
||||
* 超时的链接将在下一次空闲链接检查是被销毁
|
||||
* </p>
|
||||
*
|
||||
* @param idleConnTimeout
|
||||
* 空闲链接的超时时间,默认60000ms.
|
||||
*/
|
||||
public void setIdleConnTimeout(int idleConnTimeout) {
|
||||
this.idleConnTimeout = idleConnTimeout;
|
||||
}
|
||||
/**
|
||||
* 空闲链接的超时时间,默认60000ms.
|
||||
* <p>
|
||||
* 超时的链接将在下一次空闲链接检查是被销毁
|
||||
* </p>
|
||||
*
|
||||
* @param idleConnTimeout 空闲链接的超时时间,默认60000ms.
|
||||
*/
|
||||
public void setIdleConnTimeout(int idleConnTimeout) {
|
||||
this.idleConnTimeout = idleConnTimeout;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查空间链接的间隔周期,默认60000ms.
|
||||
*
|
||||
* @param checkWaitTime
|
||||
* 检查空间链接的间隔周期,默认60000ms.
|
||||
*/
|
||||
public void setCheckWaitTime(int checkWaitTime) {
|
||||
this.checkWaitTime = checkWaitTime;
|
||||
}
|
||||
/**
|
||||
* 检查空间链接的间隔周期,默认60000ms.
|
||||
*
|
||||
* @param checkWaitTime 检查空间链接的间隔周期,默认60000ms.
|
||||
*/
|
||||
public void setCheckWaitTime(int checkWaitTime) {
|
||||
this.checkWaitTime = checkWaitTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* 每路的最大链接数,默认10
|
||||
*
|
||||
* @param maxConnPerHost
|
||||
* 每路的最大链接数,默认10
|
||||
*/
|
||||
public void setMaxConnPerHost(int maxConnPerHost) {
|
||||
this.maxConnPerHost = maxConnPerHost;
|
||||
}
|
||||
/**
|
||||
* 每路的最大链接数,默认10
|
||||
*
|
||||
* @param maxConnPerHost 每路的最大链接数,默认10
|
||||
*/
|
||||
public void setMaxConnPerHost(int maxConnPerHost) {
|
||||
this.maxConnPerHost = maxConnPerHost;
|
||||
}
|
||||
|
||||
/**
|
||||
* 最大总连接数,默认50
|
||||
*
|
||||
* @param maxTotalConn
|
||||
* 最大总连接数,默认50
|
||||
*/
|
||||
public void setMaxTotalConn(int maxTotalConn) {
|
||||
this.maxTotalConn = maxTotalConn;
|
||||
}
|
||||
/**
|
||||
* 最大总连接数,默认50
|
||||
*
|
||||
* @param maxTotalConn 最大总连接数,默认50
|
||||
*/
|
||||
public void setMaxTotalConn(int maxTotalConn) {
|
||||
this.maxTotalConn = maxTotalConn;
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义httpclient的User Agent
|
||||
*
|
||||
* @param userAgent
|
||||
* User Agent
|
||||
*/
|
||||
public void setUserAgent(String userAgent) {
|
||||
this.userAgent = userAgent;
|
||||
}
|
||||
/**
|
||||
* 自定义httpclient的User Agent
|
||||
*
|
||||
* @param userAgent User Agent
|
||||
*/
|
||||
public void setUserAgent(String userAgent) {
|
||||
this.userAgent = userAgent;
|
||||
}
|
||||
|
||||
public IdleConnectionMonitorThread getIdleConnectionMonitorThread() {
|
||||
return this.idleConnectionMonitorThread;
|
||||
}
|
||||
public IdleConnectionMonitorThread getIdleConnectionMonitorThread() {
|
||||
return this.idleConnectionMonitorThread;
|
||||
}
|
||||
|
||||
private synchronized void prepare() {
|
||||
if (prepared.get()) {
|
||||
return;
|
||||
}
|
||||
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
|
||||
.register("http", this.plainConnectionSocketFactory).register("https", this.sslConnectionSocketFactory)
|
||||
.build();
|
||||
private synchronized void prepare() {
|
||||
if (prepared.get()) {
|
||||
return;
|
||||
}
|
||||
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
|
||||
.register("http", this.plainConnectionSocketFactory).register("https", this.sslConnectionSocketFactory)
|
||||
.build();
|
||||
|
||||
@SuppressWarnings("resource")
|
||||
PoolingHttpClientConnectionManager connectionManager;
|
||||
if(dnsResover != null){
|
||||
if(log.isDebugEnabled()){
|
||||
log.debug("specified dns resolver.");
|
||||
}
|
||||
connectionManager = new PoolingHttpClientConnectionManager(registry, dnsResover);
|
||||
}else{
|
||||
if(log.isDebugEnabled()){
|
||||
log.debug("Not specified dns resolver.");
|
||||
}
|
||||
connectionManager = new PoolingHttpClientConnectionManager(registry);
|
||||
}
|
||||
@SuppressWarnings("resource")
|
||||
PoolingHttpClientConnectionManager connectionManager;
|
||||
if (dnsResover != null) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("specified dns resolver.");
|
||||
}
|
||||
connectionManager = new PoolingHttpClientConnectionManager(registry, dnsResover);
|
||||
} else {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Not specified dns resolver.");
|
||||
}
|
||||
connectionManager = new PoolingHttpClientConnectionManager(registry);
|
||||
}
|
||||
|
||||
connectionManager.setMaxTotal(this.maxTotalConn);
|
||||
connectionManager.setDefaultMaxPerRoute(this.maxConnPerHost);
|
||||
connectionManager
|
||||
.setDefaultSocketConfig(SocketConfig.copy(SocketConfig.DEFAULT).setSoTimeout(this.soTimeout).build());
|
||||
connectionManager.setMaxTotal(this.maxTotalConn);
|
||||
connectionManager.setDefaultMaxPerRoute(this.maxConnPerHost);
|
||||
connectionManager
|
||||
.setDefaultSocketConfig(SocketConfig.copy(SocketConfig.DEFAULT).setSoTimeout(this.soTimeout).build());
|
||||
|
||||
this.idleConnectionMonitorThread = new IdleConnectionMonitorThread(connectionManager, this.idleConnTimeout,
|
||||
this.checkWaitTime);
|
||||
this.idleConnectionMonitorThread.setDaemon(true);
|
||||
this.idleConnectionMonitorThread.start();
|
||||
this.idleConnectionMonitorThread = new IdleConnectionMonitorThread(connectionManager, this.idleConnTimeout,
|
||||
this.checkWaitTime);
|
||||
this.idleConnectionMonitorThread.setDaemon(true);
|
||||
this.idleConnectionMonitorThread.start();
|
||||
|
||||
this.httpClientBuilder = HttpClients.custom().setConnectionManager(connectionManager)
|
||||
.setConnectionManagerShared(true)
|
||||
.setDefaultRequestConfig(RequestConfig.custom().setSocketTimeout(this.soTimeout)
|
||||
.setConnectTimeout(this.connectionTimeout)
|
||||
.setConnectionRequestTimeout(this.connectionRequestTimeout).build())
|
||||
.setRetryHandler(this.httpRequestRetryHandler);
|
||||
this.httpClientBuilder = HttpClients.custom().setConnectionManager(connectionManager)
|
||||
.setConnectionManagerShared(true)
|
||||
.setDefaultRequestConfig(RequestConfig.custom().setSocketTimeout(this.soTimeout)
|
||||
.setConnectTimeout(this.connectionTimeout)
|
||||
.setConnectionRequestTimeout(this.connectionRequestTimeout).build())
|
||||
.setRetryHandler(this.httpRequestRetryHandler);
|
||||
|
||||
if (StringUtils.isNotBlank(this.httpProxyHost) && StringUtils.isNotBlank(this.httpProxyUsername)) {
|
||||
// 使用代理服务器 需要用户认证的代理服务器
|
||||
CredentialsProvider provider = new BasicCredentialsProvider();
|
||||
provider.setCredentials(new AuthScope(this.httpProxyHost, this.httpProxyPort),
|
||||
new UsernamePasswordCredentials(this.httpProxyUsername, this.httpProxyPassword));
|
||||
this.httpClientBuilder.setDefaultCredentialsProvider(provider);
|
||||
}
|
||||
if (StringUtils.isNotBlank(this.httpProxyHost) && StringUtils.isNotBlank(this.httpProxyUsername)) {
|
||||
// 使用代理服务器 需要用户认证的代理服务器
|
||||
CredentialsProvider provider = new BasicCredentialsProvider();
|
||||
provider.setCredentials(new AuthScope(this.httpProxyHost, this.httpProxyPort),
|
||||
new UsernamePasswordCredentials(this.httpProxyUsername, this.httpProxyPassword));
|
||||
this.httpClientBuilder.setDefaultCredentialsProvider(provider);
|
||||
}
|
||||
|
||||
if (StringUtils.isNotBlank(this.userAgent)) {
|
||||
this.httpClientBuilder.setUserAgent(this.userAgent);
|
||||
}
|
||||
prepared.set(true);
|
||||
}
|
||||
if (StringUtils.isNotBlank(this.userAgent)) {
|
||||
this.httpClientBuilder.setUserAgent(this.userAgent);
|
||||
}
|
||||
prepared.set(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CloseableHttpClient build() {
|
||||
if (!prepared.get()) {
|
||||
prepare();
|
||||
}
|
||||
return this.httpClientBuilder.build();
|
||||
}
|
||||
@Override
|
||||
public CloseableHttpClient build() {
|
||||
if (!prepared.get()) {
|
||||
prepare();
|
||||
}
|
||||
return this.httpClientBuilder.build();
|
||||
}
|
||||
|
||||
public static class IdleConnectionMonitorThread extends Thread {
|
||||
private final HttpClientConnectionManager connMgr;
|
||||
private final int idleConnTimeout;
|
||||
private final int checkWaitTime;
|
||||
private volatile boolean shutdown;
|
||||
public DnsResolver getDnsResover() {
|
||||
return dnsResover;
|
||||
}
|
||||
|
||||
public IdleConnectionMonitorThread(HttpClientConnectionManager connMgr, int idleConnTimeout,
|
||||
int checkWaitTime) {
|
||||
super("IdleConnectionMonitorThread");
|
||||
this.connMgr = connMgr;
|
||||
this.idleConnTimeout = idleConnTimeout;
|
||||
this.checkWaitTime = checkWaitTime;
|
||||
}
|
||||
public void setDnsResover(DnsResolver dnsResover) {
|
||||
this.dnsResover = dnsResover;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
while (!this.shutdown) {
|
||||
synchronized (this) {
|
||||
wait(this.checkWaitTime);
|
||||
this.connMgr.closeExpiredConnections();
|
||||
this.connMgr.closeIdleConnections(this.idleConnTimeout, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
}
|
||||
} catch (InterruptedException ignore) {
|
||||
}
|
||||
}
|
||||
public static class IdleConnectionMonitorThread extends Thread {
|
||||
private final HttpClientConnectionManager connMgr;
|
||||
private final int idleConnTimeout;
|
||||
private final int checkWaitTime;
|
||||
private volatile boolean shutdown;
|
||||
|
||||
public void trigger() {
|
||||
synchronized (this) {
|
||||
notifyAll();
|
||||
}
|
||||
}
|
||||
public IdleConnectionMonitorThread(HttpClientConnectionManager connMgr, int idleConnTimeout,
|
||||
int checkWaitTime) {
|
||||
super("IdleConnectionMonitorThread");
|
||||
this.connMgr = connMgr;
|
||||
this.idleConnTimeout = idleConnTimeout;
|
||||
this.checkWaitTime = checkWaitTime;
|
||||
}
|
||||
|
||||
public void shutdown() {
|
||||
this.shutdown = true;
|
||||
synchronized (this) {
|
||||
notifyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
while (!this.shutdown) {
|
||||
synchronized (this) {
|
||||
wait(this.checkWaitTime);
|
||||
this.connMgr.closeExpiredConnections();
|
||||
this.connMgr.closeIdleConnections(this.idleConnTimeout, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
}
|
||||
} catch (InterruptedException ignore) {
|
||||
}
|
||||
}
|
||||
|
||||
public DnsResolver getDnsResover() {
|
||||
return dnsResover;
|
||||
}
|
||||
public void trigger() {
|
||||
synchronized (this) {
|
||||
notifyAll();
|
||||
}
|
||||
}
|
||||
|
||||
public void setDnsResover(DnsResolver dnsResover) {
|
||||
this.dnsResover = dnsResover;
|
||||
}
|
||||
public void shutdown() {
|
||||
this.shutdown = true;
|
||||
synchronized (this) {
|
||||
notifyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,11 +1,10 @@
|
||||
package me.chanjar.weixin.common.util.http.apache;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.fs.FileUtils;
|
||||
import me.chanjar.weixin.common.util.http.MediaDownloadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.HttpHost;
|
||||
@@ -15,13 +14,11 @@ import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
|
||||
import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.fs.FileUtils;
|
||||
import me.chanjar.weixin.common.util.http.MediaDownloadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Created by ecoolper on 2017/5/5.
|
||||
|
@@ -1,8 +1,10 @@
|
||||
package me.chanjar.weixin.common.util.http.apache;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
@@ -13,12 +15,8 @@ import org.apache.http.entity.mime.HttpMultipartMode;
|
||||
import org.apache.http.entity.mime.MultipartEntityBuilder;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
|
||||
import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Created by ecoolper on 2017/5/5.
|
||||
|
@@ -1,18 +1,16 @@
|
||||
package me.chanjar.weixin.common.util.http.apache;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
|
||||
import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Created by ecoolper on 2017/5/4.
|
||||
|
@@ -1,7 +1,9 @@
|
||||
package me.chanjar.weixin.common.util.http.apache;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
|
||||
import org.apache.http.Consts;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
@@ -10,11 +12,7 @@ import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
|
||||
import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Created by ecoolper on 2017/5/4.
|
||||
|
@@ -188,7 +188,7 @@ public class DefaultApacheHttpClientBuilder implements ApacheHttpClientBuilder {
|
||||
}
|
||||
|
||||
private synchronized void prepare() {
|
||||
if(prepared.get()){
|
||||
if (prepared.get()) {
|
||||
return;
|
||||
}
|
||||
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
|
||||
@@ -242,7 +242,7 @@ public class DefaultApacheHttpClientBuilder implements ApacheHttpClientBuilder {
|
||||
|
||||
@Override
|
||||
public CloseableHttpClient build() {
|
||||
if(!prepared.get()){
|
||||
if (!prepared.get()) {
|
||||
prepare();
|
||||
}
|
||||
return this.httpClientBuilder.build();
|
||||
|
@@ -1,14 +1,5 @@
|
||||
package me.chanjar.weixin.common.util.http.jodd;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import jodd.http.HttpConnectionProvider;
|
||||
import jodd.http.HttpRequest;
|
||||
import jodd.http.HttpResponse;
|
||||
@@ -17,8 +8,15 @@ import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.fs.FileUtils;
|
||||
import me.chanjar.weixin.common.util.http.MediaDownloadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Created by ecoolper on 2017/5/5.
|
||||
|
@@ -1,8 +1,5 @@
|
||||
package me.chanjar.weixin.common.util.http.jodd;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import jodd.http.HttpConnectionProvider;
|
||||
import jodd.http.HttpRequest;
|
||||
import jodd.http.HttpResponse;
|
||||
@@ -11,9 +8,11 @@ import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Created by ecoolper on 2017/5/5.
|
||||
*/
|
||||
|
@@ -1,23 +1,15 @@
|
||||
package me.chanjar.weixin.common.util.http.jodd;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
|
||||
import jodd.http.HttpConnectionProvider;
|
||||
import jodd.http.HttpRequest;
|
||||
import jodd.http.HttpResponse;
|
||||
import jodd.http.ProxyInfo;
|
||||
import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Created by ecoolper on 2017/5/4.
|
||||
|
@@ -1,17 +1,16 @@
|
||||
package me.chanjar.weixin.common.util.http.jodd;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import jodd.http.HttpConnectionProvider;
|
||||
import jodd.http.HttpRequest;
|
||||
import jodd.http.HttpResponse;
|
||||
import jodd.http.ProxyInfo;
|
||||
import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Created by ecoolper on 2017/5/4.
|
||||
*/
|
||||
@@ -24,7 +23,7 @@ public class JoddSimplePostRequestExecutor extends SimplePostRequestExecutor<Htt
|
||||
@Override
|
||||
public String execute(String uri, String postEntity) throws WxErrorException, IOException {
|
||||
HttpConnectionProvider provider = requestHttp.getRequestHttpClient();
|
||||
ProxyInfo proxyInfo = requestHttp.getRequestHttpProxy();
|
||||
ProxyInfo proxyInfo = requestHttp.getRequestHttpProxy();
|
||||
|
||||
HttpRequest request = HttpRequest.post(uri);
|
||||
if (proxyInfo != null) {
|
||||
|
@@ -1,5 +1,13 @@
|
||||
package me.chanjar.weixin.common.util.http.okhttp;
|
||||
|
||||
import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.fs.FileUtils;
|
||||
import me.chanjar.weixin.common.util.http.MediaDownloadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import okhttp3.*;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
@@ -7,17 +15,6 @@ import java.io.InputStream;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.fs.FileUtils;
|
||||
import me.chanjar.weixin.common.util.http.MediaDownloadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import okhttp3.*;
|
||||
|
||||
/**
|
||||
* Created by ecoolper on 2017/5/5.
|
||||
*/
|
||||
|
@@ -1,16 +1,15 @@
|
||||
package me.chanjar.weixin.common.util.http.okhttp;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import okhttp3.*;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Created by ecoolper on 2017/5/5.
|
||||
*/
|
||||
|
@@ -1,18 +1,13 @@
|
||||
package me.chanjar.weixin.common.util.http.okhttp;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import jodd.http.HttpConnectionProvider;
|
||||
import jodd.http.HttpRequest;
|
||||
import jodd.http.HttpResponse;
|
||||
import jodd.http.ProxyInfo;
|
||||
import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
|
||||
import okhttp3.*;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Created by ecoolper on 2017/5/4.
|
||||
*/
|
||||
@@ -47,7 +42,7 @@ public class OkSimpleGetRequestExecutor extends SimpleGetRequestExecutor<Connect
|
||||
}
|
||||
});
|
||||
//得到httpClient
|
||||
OkHttpClient client =clientBuilder.build();
|
||||
OkHttpClient client = clientBuilder.build();
|
||||
|
||||
Request request = new Request.Builder().url(uri).build();
|
||||
|
||||
|
@@ -1,26 +1,25 @@
|
||||
package me.chanjar.weixin.common.util.http.okhttp;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
|
||||
import okhttp3.*;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Created by ecoolper on 2017/5/4.
|
||||
*/
|
||||
public class OkSimplePostRequestExecutor extends SimplePostRequestExecutor<ConnectionPool,OkhttpProxyInfo>{
|
||||
public class OkSimplePostRequestExecutor extends SimplePostRequestExecutor<ConnectionPool, OkhttpProxyInfo> {
|
||||
|
||||
public OkSimplePostRequestExecutor(RequestHttp requestHttp) {
|
||||
super(requestHttp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String execute(String uri, String postEntity) throws WxErrorException, IOException {
|
||||
ConnectionPool pool = requestHttp.getRequestHttpClient();
|
||||
public String execute(String uri, String postEntity) throws WxErrorException, IOException {
|
||||
ConnectionPool pool = requestHttp.getRequestHttpClient();
|
||||
final OkhttpProxyInfo proxyInfo = requestHttp.getRequestHttpProxy();
|
||||
|
||||
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().connectionPool(pool);
|
||||
|
@@ -8,19 +8,11 @@ import java.net.Proxy;
|
||||
* Proxy information.
|
||||
*/
|
||||
public class OkhttpProxyInfo {
|
||||
/**
|
||||
* Proxy types.
|
||||
*/
|
||||
public enum ProxyType {
|
||||
NONE, HTTP, SOCKS4, SOCKS5
|
||||
}
|
||||
|
||||
private final String proxyAddress;
|
||||
private final int proxyPort;
|
||||
private final String proxyUsername;
|
||||
private final String proxyPassword;
|
||||
private final ProxyType proxyType;
|
||||
|
||||
public OkhttpProxyInfo(ProxyType proxyType, String proxyHost, int proxyPort, String proxyUser, String proxyPassword) {
|
||||
this.proxyType = proxyType;
|
||||
this.proxyAddress = proxyHost;
|
||||
@@ -29,8 +21,6 @@ public class OkhttpProxyInfo {
|
||||
this.proxyPassword = proxyPassword;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- factory
|
||||
|
||||
/**
|
||||
* Creates directProxy.
|
||||
*/
|
||||
@@ -38,6 +28,8 @@ public class OkhttpProxyInfo {
|
||||
return new OkhttpProxyInfo(ProxyType.NONE, null, 0, null, null);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- factory
|
||||
|
||||
/**
|
||||
* Creates SOCKS4 proxy.
|
||||
*/
|
||||
@@ -59,8 +51,6 @@ public class OkhttpProxyInfo {
|
||||
return new OkhttpProxyInfo(ProxyType.HTTP, proxyAddress, proxyPort, proxyUser, proxyPassword);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- getter
|
||||
|
||||
/**
|
||||
* Returns proxy type.
|
||||
*/
|
||||
@@ -68,6 +58,8 @@ public class OkhttpProxyInfo {
|
||||
return proxyType;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- getter
|
||||
|
||||
/**
|
||||
* Returns proxy address.
|
||||
*/
|
||||
@@ -99,6 +91,7 @@ public class OkhttpProxyInfo {
|
||||
|
||||
/**
|
||||
* 返回 java.net.Proxy
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Proxy getProxy() {
|
||||
@@ -114,4 +107,11 @@ public class OkhttpProxyInfo {
|
||||
}
|
||||
return proxy;
|
||||
}
|
||||
|
||||
/**
|
||||
* Proxy types.
|
||||
*/
|
||||
public enum ProxyType {
|
||||
NONE, HTTP, SOCKS4, SOCKS5
|
||||
}
|
||||
}
|
||||
|
@@ -2,7 +2,6 @@ package me.chanjar.weixin.common.util.json;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
|
||||
import me.chanjar.weixin.common.bean.WxAccessToken;
|
||||
import me.chanjar.weixin.common.bean.menu.WxMenu;
|
||||
import me.chanjar.weixin.common.bean.result.WxError;
|
||||
|
@@ -47,7 +47,7 @@ import java.util.*;
|
||||
public class StringManager {
|
||||
|
||||
private static final Map<String, Map<Locale, StringManager>> managers =
|
||||
new Hashtable<>();
|
||||
new Hashtable<>();
|
||||
private static int LOCALE_CACHE_SIZE = 10;
|
||||
/**
|
||||
* The ResourceBundle for this StringManager.
|
||||
@@ -103,7 +103,7 @@ public class StringManager {
|
||||
* @param packageName The package name
|
||||
*/
|
||||
public static final synchronized StringManager getManager(
|
||||
String packageName) {
|
||||
String packageName) {
|
||||
return getManager(packageName, Locale.getDefault());
|
||||
}
|
||||
|
||||
@@ -116,7 +116,7 @@ public class StringManager {
|
||||
* @param locale The Locale
|
||||
*/
|
||||
public static final synchronized StringManager getManager(
|
||||
String packageName, Locale locale) {
|
||||
String packageName, Locale locale) {
|
||||
|
||||
Map<Locale, StringManager> map = managers.get(packageName);
|
||||
if (map == null) {
|
||||
@@ -133,7 +133,7 @@ public class StringManager {
|
||||
|
||||
@Override
|
||||
protected boolean removeEldestEntry(
|
||||
Map.Entry<Locale, StringManager> eldest) {
|
||||
Map.Entry<Locale, StringManager> eldest) {
|
||||
return size() > (LOCALE_CACHE_SIZE - 1);
|
||||
}
|
||||
};
|
||||
|
@@ -1,7 +1,5 @@
|
||||
package me.chanjar.weixin.common.util.xml;
|
||||
|
||||
import java.io.Writer;
|
||||
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
import com.thoughtworks.xstream.core.util.QuickWriter;
|
||||
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
|
||||
@@ -10,6 +8,8 @@ import com.thoughtworks.xstream.io.xml.XppDriver;
|
||||
import com.thoughtworks.xstream.security.NullPermission;
|
||||
import com.thoughtworks.xstream.security.PrimitiveTypePermission;
|
||||
|
||||
import java.io.Writer;
|
||||
|
||||
public class XStreamInitializer {
|
||||
|
||||
public static XStream getInstance() {
|
||||
|
Reference in New Issue
Block a user