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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -4,7 +4,7 @@ import com.foxinmy.weixin4j.mp.type.CardType;
/** /**
* 卡券 * 卡券
* *
* @className CardCoupon * @className CardCoupon
* @author jinyu(foxinmy@gmail.com) * @author jinyu(foxinmy@gmail.com)
* @date 2016年4月4日 * @date 2016年4月4日
@ -14,8 +14,29 @@ import com.foxinmy.weixin4j.mp.type.CardType;
public interface CardCoupon { public interface CardCoupon {
/** /**
* 卡券类型 * 卡券类型
* *
* @return * @return
*/ */
public CardType getCardType(); 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.toJSONString());
obj = response.getAsJson(); obj = response.getAsJson();
obj.put("items", obj.remove("itemlist"));
MediaRecord mediaRecord = JSON.toJavaObject(obj, MediaRecord.class); MediaRecord mediaRecord = JSON.toJavaObject(obj, MediaRecord.class);
mediaRecord.setMediaType(mediaType); mediaRecord.setMediaType(mediaType);
mediaRecord.setPageable(pageable); mediaRecord.setPageable(pageable);