diff --git a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/HttpParams.java b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/HttpParams.java index e8a03daf..f2054038 100644 --- a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/HttpParams.java +++ b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/HttpParams.java @@ -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 + "]"; } } diff --git a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/SimpleHttpClient.java b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/SimpleHttpClient.java index 3a8dcb7a..203d2b18 100644 --- a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/SimpleHttpClient.java +++ b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/SimpleHttpClient.java @@ -117,8 +117,7 @@ public class SimpleHttpClient extends AbstractHttpClient implements HttpClient { } logger.debug("request >> " + request.getMethod() + " " + request.getURI().toString()); - for (Entry> header : headers - .entrySet()) { + for (Entry> 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> header : response - .getHeaders().entrySet()) { + for (Entry> header : response.getHeaders() + .entrySet()) { logger.debug("headers << " + header.getKey() + ":" + StringUtil.join(header.getValue(), ';')); } diff --git a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/weixin/WeixinRequestExecutor.java b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/weixin/WeixinRequestExecutor.java index e1bed0b8..3ed5762d 100644 --- a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/weixin/WeixinRequestExecutor.java +++ b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/weixin/WeixinRequestExecutor.java @@ -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) { diff --git a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/weixin/WeixinSSLRequestExecutor.java b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/weixin/WeixinSSLRequestExecutor.java index dc7a4b0f..aa088380 100644 --- a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/weixin/WeixinSSLRequestExecutor.java +++ b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/weixin/WeixinSSLRequestExecutor.java @@ -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); } } diff --git a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/model/MediaRecord.java b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/model/MediaRecord.java index ac334c21..67cdfb0f 100644 --- a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/model/MediaRecord.java +++ b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/model/MediaRecord.java @@ -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 items; /** * 分页信息 diff --git a/weixin4j-base/src/test/java/com/foxinmy/weixin4j/base/test/HttpClientTest.java b/weixin4j-base/src/test/java/com/foxinmy/weixin4j/base/test/HttpClientTest.java index e3820ead..aee8a211 100644 --- a/weixin4j-base/src/test/java/com/foxinmy/weixin4j/base/test/HttpClientTest.java +++ b/weixin4j-base/src/test/java/com/foxinmy/weixin4j/base/test/HttpClientTest.java @@ -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); } diff --git a/weixin4j-example/src/main/java/com/foxinmy/weixin4j/example/server/Weixin4jServerStartupWithoutThread.java b/weixin4j-example/src/main/java/com/foxinmy/weixin4j/example/server/Weixin4jServerStartupWithoutThread.java index 131f9efa..87a2aa1f 100644 --- a/weixin4j-example/src/main/java/com/foxinmy/weixin4j/example/server/Weixin4jServerStartupWithoutThread.java +++ b/weixin4j-example/src/main/java/com/foxinmy/weixin4j/example/server/Weixin4jServerStartupWithoutThread.java @@ -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" }); } } diff --git a/weixin4j-example/src/main/resources/spring-bean.xml b/weixin4j-example/src/main/resources/spring-bean.xml index 15ec6293..7da1e329 100644 --- a/weixin4j-example/src/main/resources/spring-bean.xml +++ b/weixin4j-example/src/main/resources/spring-bean.xml @@ -9,7 +9,7 @@ - + @@ -39,7 +39,7 @@ - + @@ -51,7 +51,7 @@ - + @@ -64,7 +64,7 @@ - + @@ -75,7 +75,7 @@ + class="com.foxinmy.weixin4j.example.server.Weixin4jServerStartupWithThread" init-method="start" destroy-method="stop"> @@ -85,8 +85,8 @@ - diff --git a/weixin4j-mp/src/main/java/com/foxinmy/weixin4j/mp/api/MediaApi.java b/weixin4j-mp/src/main/java/com/foxinmy/weixin4j/mp/api/MediaApi.java index 2c32412f..2d08a1a6 100644 --- a/weixin4j-mp/src/main/java/com/foxinmy/weixin4j/mp/api/MediaApi.java +++ b/weixin4j-mp/src/main/java/com/foxinmy/weixin4j/mp/api/MediaApi.java @@ -505,9 +505,9 @@ public class MediaApi extends MpApi { } }); } else { - mediaRecord = response - .getAsObject(new TypeReference() { - }); + obj = response.getAsJson(); + obj.put("items", obj.remove("itemlist")); + mediaRecord = JSON.toJavaObject(obj, MediaRecord.class); } mediaRecord.setMediaType(mediaType); mediaRecord.setPageable(pageable); diff --git a/weixin4j-mp/src/main/java/com/foxinmy/weixin4j/mp/card/CardCoupon.java b/weixin4j-mp/src/main/java/com/foxinmy/weixin4j/mp/card/CardCoupon.java index 3a2c3987..fd0e1c84 100644 --- a/weixin4j-mp/src/main/java/com/foxinmy/weixin4j/mp/card/CardCoupon.java +++ b/weixin4j-mp/src/main/java/com/foxinmy/weixin4j/mp/card/CardCoupon.java @@ -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(); } diff --git a/weixin4j-qy/src/main/java/com/foxinmy/weixin4j/qy/api/MediaApi.java b/weixin4j-qy/src/main/java/com/foxinmy/weixin4j/qy/api/MediaApi.java index fe677808..0023a6bc 100644 --- a/weixin4j-qy/src/main/java/com/foxinmy/weixin4j/qy/api/MediaApi.java +++ b/weixin4j-qy/src/main/java/com/foxinmy/weixin4j/qy/api/MediaApi.java @@ -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);