This commit is contained in:
jinyu 2016-06-11 11:31:37 +08:00
parent 57daea99a8
commit 31a66335a8
11 changed files with 126 additions and 179 deletions

View File

@ -16,82 +16,121 @@ import javax.net.ssl.SSLContext;
*/
public final class HttpParams {
private final boolean allowUserInteraction;
private final int connectTimeout;
private final int socketTimeout;
private final int readTimeout;
private final int chunkSize;
private final boolean followRedirects;
private boolean allowUserInteraction;
private int connectTimeout;
private int socketTimeout;
private int readTimeout;
private int chunkSize;
private boolean followRedirects;
/**
* 代理对象
*/
private final Proxy proxy;
private Proxy proxy;
/**
* SSL对象
*/
private final SSLContext sslContext;
private SSLContext sslContext;
/**
* hostname对象
*/
private final HostnameVerifier hostnameVerifier;
private HostnameVerifier hostnameVerifier;
HttpParams(boolean allowUserInteraction, int connectTimeout,
int socketTimeout, int readTimeout, int chunkSize,
boolean followRedirects, Proxy proxy, SSLContext sslContext,
HostnameVerifier hostnameVerifier) {
this.allowUserInteraction = allowUserInteraction;
public HttpParams() {
this(5000, 5000, 5000);
}
public HttpParams(int connectTimeout, int socketTimeout, int readTimeout) {
this.allowUserInteraction = true;
this.connectTimeout = connectTimeout;
this.socketTimeout = socketTimeout;
this.readTimeout = readTimeout;
this.chunkSize = chunkSize;
this.followRedirects = followRedirects;
this.proxy = proxy;
this.sslContext = sslContext;
this.hostnameVerifier = hostnameVerifier;
this.chunkSize = 4096;
this.followRedirects = false;
}
public boolean isAllowUserInteraction() {
return allowUserInteraction;
}
public HttpParams setAllowUserInteraction(boolean allowUserInteraction) {
this.allowUserInteraction = allowUserInteraction;
return this;
}
public int getConnectTimeout() {
return connectTimeout;
}
public HttpParams setConnectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
return this;
}
public int getSocketTimeout() {
return socketTimeout;
}
public HttpParams setSocketTimeout(int socketTimeout) {
this.socketTimeout = socketTimeout;
return this;
}
public int getReadTimeout() {
return readTimeout;
}
public HttpParams setReadTimeout(int readTimeout) {
this.readTimeout = readTimeout;
return this;
}
public int getChunkSize() {
return chunkSize;
}
public HttpParams setChunkSize(int chunkSize) {
this.chunkSize = chunkSize;
return this;
}
public boolean isFollowRedirects() {
return followRedirects;
}
public HttpParams setFollowRedirects(boolean followRedirects) {
this.followRedirects = followRedirects;
return this;
}
public Proxy getProxy() {
return proxy;
}
public HttpParams setProxy(Proxy proxy) {
this.proxy = proxy;
return this;
}
public SSLContext getSSLContext() {
return sslContext;
}
public HttpParams setSSLContext(SSLContext sslContext) {
this.sslContext = sslContext;
return this;
}
public HostnameVerifier getHostnameVerifier() {
return hostnameVerifier;
}
public static HttpParams.Builder custom() {
return new Builder();
public HttpParams setHostnameVerifier(HostnameVerifier hostnameVerifier) {
this.hostnameVerifier = hostnameVerifier;
return this;
}
public static HttpParams.Builder copy(final HttpParams params) {
return new Builder()
public static HttpParams copy(final HttpParams params) {
return new HttpParams()
.setAllowUserInteraction(params.isAllowUserInteraction())
.setConnectTimeout(params.getConnectTimeout())
.setSocketTimeout(params.getSocketTimeout())
@ -100,120 +139,13 @@ public final class HttpParams {
.setFollowRedirects(params.isFollowRedirects());
}
public static class Builder {
private boolean allowUserInteraction;
private int connectTimeout;
private int socketTimeout;
private int readTimeout;
private int chunkSize;
private boolean followRedirects;
/**
* 代理对象
*/
private Proxy proxy;
/**
* SSL对象
*/
private SSLContext sslContext;
/**
* hostname对象
*/
private HostnameVerifier hostnameVerifier;
Builder() {
this.allowUserInteraction = true;
this.connectTimeout = 5000;
this.socketTimeout = 5000;
this.readTimeout = 5000;
this.chunkSize = 4096;
this.followRedirects = false;
}
public boolean isAllowUserInteraction() {
return allowUserInteraction;
}
public Builder setAllowUserInteraction(boolean allowUserInteraction) {
this.allowUserInteraction = allowUserInteraction;
return this;
}
public int getConnectTimeout() {
return connectTimeout;
}
public Builder setConnectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
return this;
}
public int getSocketTimeout() {
return socketTimeout;
}
public Builder setSocketTimeout(int socketTimeout) {
this.socketTimeout = socketTimeout;
return this;
}
public int getReadTimeout() {
return readTimeout;
}
public Builder setReadTimeout(int readTimeout) {
this.readTimeout = readTimeout;
return this;
}
public int getChunkSize() {
return chunkSize;
}
public Builder setChunkSize(int chunkSize) {
this.chunkSize = chunkSize;
return this;
}
public boolean isFollowRedirects() {
return followRedirects;
}
public Builder setFollowRedirects(boolean followRedirects) {
this.followRedirects = followRedirects;
return this;
}
public Proxy getProxy() {
return proxy;
}
public Builder setProxy(Proxy proxy) {
this.proxy = proxy;
return this;
}
public SSLContext getSslContext() {
return sslContext;
}
public Builder setSslContext(SSLContext sslContext) {
this.sslContext = sslContext;
return this;
}
public HostnameVerifier getHostnameVerifier() {
return hostnameVerifier;
}
public Builder setHostnameVerifier(HostnameVerifier hostnameVerifier) {
this.hostnameVerifier = hostnameVerifier;
return this;
}
public HttpParams build() {
return new HttpParams(allowUserInteraction, connectTimeout,
socketTimeout, readTimeout, chunkSize, followRedirects,
proxy, sslContext, hostnameVerifier);
}
@Override
public String toString() {
return "HttpParams [allowUserInteraction=" + allowUserInteraction
+ ", connectTimeout=" + connectTimeout + ", socketTimeout="
+ socketTimeout + ", readTimeout=" + readTimeout
+ ", chunkSize=" + chunkSize + ", followRedirects="
+ followRedirects + ", proxy=" + proxy + ", sslContext="
+ sslContext + ", hostnameVerifier=" + hostnameVerifier + "]";
}
}

View File

@ -117,8 +117,7 @@ public class SimpleHttpClient extends AbstractHttpClient implements HttpClient {
}
logger.debug("request >> " + request.getMethod() + " "
+ request.getURI().toString());
for (Entry<String, List<String>> header : headers
.entrySet()) {
for (Entry<String, List<String>> header : headers.entrySet()) {
if (HttpHeaders.COOKIE.equalsIgnoreCase(header.getKey())) {
connection.setRequestProperty(header.getKey(),
StringUtil.join(header.getValue(), ';'));
@ -170,8 +169,8 @@ public class SimpleHttpClient extends AbstractHttpClient implements HttpClient {
response = new SimpleHttpResponse(connection, content);
logger.debug("response << " + response.getProtocol()
+ response.getStatus().toString());
for (Entry<String, List<String>> header : response
.getHeaders().entrySet()) {
for (Entry<String, List<String>> header : response.getHeaders()
.entrySet()) {
logger.debug("headers << " + header.getKey() + ":"
+ StringUtil.join(header.getValue(), ';'));
}

View File

@ -38,11 +38,11 @@ public class WeixinRequestExecutor {
protected final InternalLogger logger = InternalLoggerFactory
.getInstance(getClass());
private final HttpClient httpClient;
private final HttpParams httpParams;
protected final HttpClient httpClient;
protected final HttpParams httpParams;
public WeixinRequestExecutor() {
this(HttpParams.custom().build());
this(new HttpParams());
}
public WeixinRequestExecutor(HttpParams httpParams) {

View File

@ -7,7 +7,6 @@ import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.http.HttpParams;
import com.foxinmy.weixin4j.http.HttpRequest;
import com.foxinmy.weixin4j.model.Consts;
@ -51,14 +50,7 @@ public class WeixinSSLRequestExecutor extends WeixinRequestExecutor {
@Override
protected WeixinResponse doRequest(HttpRequest request)
throws WeixinException {
HttpParams params = null;
if (request.getParams() != null) {
params = HttpParams.copy(request.getParams())
.setSslContext(sslContext).build();
} else {
params = HttpParams.custom().setSslContext(sslContext).build();
}
request.setParams(params);
httpParams.setSSLContext(sslContext);
return super.doRequest(request);
}
}

View File

@ -8,7 +8,7 @@ import com.foxinmy.weixin4j.type.MediaType;
/**
* 媒体素材记录
*
*
* @className MediaRecord
* @author jinyu(foxinmy@gmail.com)
* @date 2015年3月22日
@ -37,7 +37,7 @@ public class MediaRecord implements Serializable {
/**
* 媒体信息
*/
@JSONField(name = "itemlist")
@JSONField(name = "items")
private List<MediaItem> items;
/**
* 分页信息

View File

@ -23,11 +23,8 @@ public class HttpClientTest {
static HttpRequest request = new HttpRequest(HttpMethod.GET,
"http://www.iteye.com/");
static {
HttpParams params = HttpParams
.custom()
.setProxy(
new Proxy(Type.HTTP, new InetSocketAddress(
"117.136.234.9", 80))).build();
HttpParams params = new HttpParams().setProxy(new Proxy(Type.HTTP,
new InetSocketAddress("117.136.234.9", 80)));
// request.setParams(params);
}

View File

@ -1,8 +1,8 @@
package com.foxinmy.weixin4j.example.server;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.handler.DebugMessageHandler;
import com.foxinmy.weixin4j.startup.WeixinServerBootstrap;
/**
* 微信消息服务:单独作为一个服务jar包启动
@ -38,10 +38,15 @@ public class Weixin4jServerStartupWithoutThread {
* @param args
* @throws WeixinException
*/
@SuppressWarnings("resource")
public static void main(String[] args) throws WeixinException {
new WeixinServerBootstrap(aesToken)
.handlerPackagesToScan(handlerPackage)
.addHandler(DebugMessageHandler.global).openAlwaysResponse()
.startup(port);
// 单独服务启动
//new WeixinServerBootstrap(aesToken)
//.handlerPackagesToScan(handlerPackage)
//.addHandler(DebugMessageHandler.global).openAlwaysResponse()
//.startup(port);
// spring容器启动
new ClassPathXmlApplicationContext(
new String[] { "classpath:/spring-bean.xml" });
}
}

View File

@ -9,7 +9,7 @@
<!-- 微信接口代理start -->
<bean id="weixinProxy" class="com.foxinmy.weixin4j.mp.WeixinProxy">
<constructor-arg>
<bean class="com.foxinmy.weixin4j.util.Weixin4jSettings">
<bean class="com.foxinmy.weixin4j.setting.Weixin4jSettings">
<!-- 公众号信息:不声明则默认使用weixin4j.properties配置的weixin4j.account字段 -->
<constructor-arg>
<bean class="com.foxinmy.weixin4j.model.WeixinAccount">
@ -39,7 +39,7 @@
</property -->
<!-- http参数:http请求时的参数,比如代理、超时等配置信息,暂时还未实现 -->
<property name="httpParams">
<bean class="com.foxinmy.weixin4j.http.HttpParams" />
<bean class="com.foxinmy.weixin4j.http.HttpParams"/>
</property>
<!-- 临时目录:weixin4j调用某些接口时需要用到的临时目录,不声明则获取顺序为:weixin4j.properties#weixin4j.tmpdir->`java.io.tmp` -->
<property name="tmpdir" value="/tmp/weixin4j" />
@ -51,7 +51,7 @@
<!-- 微信支付接口代理start -->
<bean id="weixinPayProxy" class="com.foxinmy.weixin4j.payment.WeixinPayProxy">
<constructor-arg>
<bean class="com.foxinmy.weixin4j.util.Weixin4jSettings">
<bean class="com.foxinmy.weixin4j.setting.Weixin4jSettings">
<!-- 商户信息:不声明则默认使用weixin4j.properties配置的weixin4j.account字段 -->
<constructor-arg>
<bean class="com.foxinmy.weixin4j.model.WeixinPayAccount">
@ -64,7 +64,7 @@
<property name="certificateFile" value="/path/to/certificate/file" />
<!-- http参数:http请求时的参数,比如代理、超时等配置信息,暂时还未实现 -->
<property name="httpParams">
<bean class="com.foxinmy.weixin4j.http.HttpParams" />
<bean class="com.foxinmy.weixin4j.http.HttpParams"/>
</property>
<!-- 临时目录:weixin4j调用某些接口时需要用到的临时目录,不声明则获取顺序为:weixin4j.properties#weixin4j.tmpdir->`java.io.tmp` -->
<property name="tmpdir" value="/tmp/weixin4j" />
@ -75,7 +75,7 @@
<!-- 微信消息服务start -->
<bean
class="com.foxinmy.weixin4j.example.server.Weixin4jServerStartupWithThread">
class="com.foxinmy.weixin4j.example.server.Weixin4jServerStartupWithThread" init-method="start" destroy-method="stop">
<!-- 端口号 微信暂时只支持80端口 所以需要自己把微信被动消息请求转发(nginx等)到这个端口上来 -->
<constructor-arg index="0" value="30000" />
<!-- token信息 -->
@ -85,8 +85,8 @@
<constructor-arg index="0" value="weixin4j" />
</bean>
<!-- 加密模式 -->
<!-- bean class="com.foxinmy.weixin4j.util.AesToken"> <constructor-arg
index="0" value="公众号的应用ID(appid/corpid)" /> <constructor-arg index="1" value="开发者Token"
<!-- bean class="com.foxinmy.weixin4j.util.AesToken"> <constructor-arg
index="0" value="公众号的应用ID(appid/corpid)" /> <constructor-arg index="1" value="开发者Token"
/> <constructor-arg index="2" value="解密的EncodingAESKey" /> </bean -->
</constructor-arg>
<!-- 处理微信消息的全限包名 -->

View File

@ -505,9 +505,9 @@ public class MediaApi extends MpApi {
}
});
} else {
mediaRecord = response
.getAsObject(new TypeReference<MediaRecord>() {
});
obj = response.getAsJson();
obj.put("items", obj.remove("itemlist"));
mediaRecord = JSON.toJavaObject(obj, MediaRecord.class);
}
mediaRecord.setMediaType(mediaType);
mediaRecord.setPageable(pageable);

View File

@ -4,7 +4,7 @@ import com.foxinmy.weixin4j.mp.type.CardType;
/**
* 卡券
*
*
* @className CardCoupon
* @author jinyu(foxinmy@gmail.com)
* @date 2016年4月4日
@ -14,8 +14,29 @@ import com.foxinmy.weixin4j.mp.type.CardType;
public interface CardCoupon {
/**
* 卡券类型
*
*
* @return
*/
public CardType getCardType();
/**
* 卡券基本信息
*
* @return
*/
public CouponBaseInfo getBaseInfo();
/**
* 卡券高级信息
*
* @return
*/
public CouponAdvancedInfo getAdvancedInfo();
/**
* 卡券详细信息
*
* @return
*/
public CouponDetailInfo getDetailInfo();
}

View File

@ -417,6 +417,7 @@ public class MediaApi extends QyApi {
obj.toJSONString());
obj = response.getAsJson();
obj.put("items", obj.remove("itemlist"));
MediaRecord mediaRecord = JSON.toJavaObject(obj, MediaRecord.class);
mediaRecord.setMediaType(mediaType);
mediaRecord.setPageable(pageable);