@@ -6,7 +6,7 @@ import chanjarster.weixin.bean.WxXmlMessage; | |||||
/** | /** | ||||
* 微信消息拦截器,可以用来做一些验证 | |||||
* 微信消息拦截器,可以用来做验证 | |||||
* @author qianjia | * @author qianjia | ||||
* | * | ||||
*/ | */ | ||||
@@ -75,5 +75,5 @@ public interface WxService { | |||||
*/ | */ | ||||
public WxMenu getMenu() throws WxErrorException; | public WxMenu getMenu() throws WxErrorException; | ||||
public void setWxConfigProvider(WxConfigStorage wxConfigProvider); | |||||
public void setWxConfigStorage(WxConfigStorage wxConfigProvider); | |||||
} | } |
@@ -35,11 +35,11 @@ public class WxServiceImpl implements WxService { | |||||
protected static final CloseableHttpClient httpclient = HttpClients.createDefault(); | protected static final CloseableHttpClient httpclient = HttpClients.createDefault(); | ||||
protected WxConfigStorage wxConfigProvider; | |||||
protected WxConfigStorage wxConfigStorage; | |||||
public boolean checkSignature(String timestamp, String nonce, String signature) { | public boolean checkSignature(String timestamp, String nonce, String signature) { | ||||
try { | try { | ||||
String token = wxConfigProvider.getToken(); | |||||
String token = wxConfigStorage.getToken(); | |||||
MessageDigest sha1 = MessageDigest.getInstance("SHA1"); | MessageDigest sha1 = MessageDigest.getInstance("SHA1"); | ||||
String[] arr = new String[] { token, timestamp, nonce }; | String[] arr = new String[] { token, timestamp, nonce }; | ||||
Arrays.sort(arr); | Arrays.sort(arr); | ||||
@@ -70,8 +70,8 @@ public class WxServiceImpl implements WxService { | |||||
if (!GLOBAL_ACCESS_TOKEN_REFRESH_FLAG.getAndSet(true)) { | if (!GLOBAL_ACCESS_TOKEN_REFRESH_FLAG.getAndSet(true)) { | ||||
try { | try { | ||||
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential" | String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential" | ||||
+ "&appid=" + wxConfigProvider.getAppId() | |||||
+ "&secret=" + wxConfigProvider.getSecret() | |||||
+ "&appid=" + wxConfigStorage.getAppId() | |||||
+ "&secret=" + wxConfigStorage.getSecret() | |||||
; | ; | ||||
try { | try { | ||||
HttpGet httpGet = new HttpGet(url); | HttpGet httpGet = new HttpGet(url); | ||||
@@ -82,7 +82,7 @@ public class WxServiceImpl implements WxService { | |||||
throw new WxErrorException(error); | throw new WxErrorException(error); | ||||
} | } | ||||
WxAccessToken accessToken = WxAccessToken.fromJson(resultContent); | WxAccessToken accessToken = WxAccessToken.fromJson(resultContent); | ||||
wxConfigProvider.updateAccessToken(accessToken.getAccess_token(), accessToken.getExpires_in()); | |||||
wxConfigStorage.updateAccessToken(accessToken.getAccess_token(), accessToken.getExpires_in()); | |||||
} catch (ClientProtocolException e) { | } catch (ClientProtocolException e) { | ||||
throw new RuntimeException(e); | throw new RuntimeException(e); | ||||
} catch (IOException e) { | } catch (IOException e) { | ||||
@@ -147,10 +147,10 @@ public class WxServiceImpl implements WxService { | |||||
* @throws WxErrorException | * @throws WxErrorException | ||||
*/ | */ | ||||
protected String execute(String method, String uri, String data) throws WxErrorException { | protected String execute(String method, String uri, String data) throws WxErrorException { | ||||
if (StringUtils.isBlank(wxConfigProvider.getAccessToken())) { | |||||
if (StringUtils.isBlank(wxConfigStorage.getAccessToken())) { | |||||
refreshAccessToken(); | refreshAccessToken(); | ||||
} | } | ||||
String accessToken = wxConfigProvider.getAccessToken(); | |||||
String accessToken = wxConfigStorage.getAccessToken(); | |||||
String uriWithAccessToken = uri; | String uriWithAccessToken = uri; | ||||
uriWithAccessToken += uri.indexOf('?') == -1 ? "?access_token=" + accessToken : "&access_token=" + accessToken; | uriWithAccessToken += uri.indexOf('?') == -1 ? "?access_token=" + accessToken : "&access_token=" + accessToken; | ||||
@@ -196,8 +196,8 @@ public class WxServiceImpl implements WxService { | |||||
} | } | ||||
} | } | ||||
public void setWxConfigProvider(WxConfigStorage wxConfigProvider) { | |||||
this.wxConfigProvider = wxConfigProvider; | |||||
public void setWxConfigStorage(WxConfigStorage wxConfigProvider) { | |||||
this.wxConfigStorage = wxConfigProvider; | |||||
} | } | ||||
} | } |
@@ -2,7 +2,6 @@ package chanjarster.weixin.bean; | |||||
import java.io.InputStream; | import java.io.InputStream; | ||||
import javax.management.RuntimeErrorException; | |||||
import javax.xml.bind.JAXBException; | import javax.xml.bind.JAXBException; | ||||
import javax.xml.bind.annotation.XmlAccessType; | import javax.xml.bind.annotation.XmlAccessType; | ||||
import javax.xml.bind.annotation.XmlAccessorType; | import javax.xml.bind.annotation.XmlAccessorType; | ||||
@@ -28,16 +28,16 @@ public class WxServiceTest { | |||||
InputStream is1 = ClassLoader.getSystemResourceAsStream("test-config.xml"); | InputStream is1 = ClassLoader.getSystemResourceAsStream("test-config.xml"); | ||||
WxXmlConfigStorage config1 = XmlTransformer.fromXml(WxXmlConfigStorage.class, is1); | WxXmlConfigStorage config1 = XmlTransformer.fromXml(WxXmlConfigStorage.class, is1); | ||||
this.wxService = new WxServiceImpl(); | this.wxService = new WxServiceImpl(); | ||||
this.wxService.setWxConfigProvider(config1); | |||||
this.wxService.setWxConfigStorage(config1); | |||||
} | } | ||||
@Test | @Test | ||||
public void testRefreshAccessToken() throws WxErrorException { | public void testRefreshAccessToken() throws WxErrorException { | ||||
WxConfigStorage configProvider = wxService.wxConfigProvider; | |||||
String before = configProvider.getAccessToken(); | |||||
WxConfigStorage configStorage = wxService.wxConfigStorage; | |||||
String before = configStorage.getAccessToken(); | |||||
wxService.refreshAccessToken(); | wxService.refreshAccessToken(); | ||||
String after = configProvider.getAccessToken(); | |||||
String after = configStorage.getAccessToken(); | |||||
Assert.assertNotEquals(before, after); | Assert.assertNotEquals(before, after); | ||||
Assert.assertTrue(StringUtils.isNotBlank(after)); | Assert.assertTrue(StringUtils.isNotBlank(after)); | ||||
@@ -45,7 +45,7 @@ public class WxServiceTest { | |||||
@Test(dependsOnMethods = "testRefreshAccessToken") | @Test(dependsOnMethods = "testRefreshAccessToken") | ||||
public void sendCustomMessage() throws WxErrorException { | public void sendCustomMessage() throws WxErrorException { | ||||
WxXmlConfigStorage configProvider = (WxXmlConfigStorage) wxService.wxConfigProvider; | |||||
WxXmlConfigStorage configProvider = (WxXmlConfigStorage) wxService.wxConfigStorage; | |||||
WxCustomMessage message = new WxCustomMessage(); | WxCustomMessage message = new WxCustomMessage(); | ||||
message.setMsgtype(WxConsts.MSG_TEXT); | message.setMsgtype(WxConsts.MSG_TEXT); | ||||
message.setTouser(configProvider.getOpenId()); | message.setTouser(configProvider.getOpenId()); | ||||
@@ -4,7 +4,6 @@ import org.testng.Assert; | |||||
import org.testng.annotations.Test; | import org.testng.annotations.Test; | ||||
import chanjarster.weixin.api.WxConsts; | import chanjarster.weixin.api.WxConsts; | ||||
import chanjarster.weixin.bean.WxCustomMessage; | |||||
import chanjarster.weixin.bean.WxCustomMessage.WxArticle; | import chanjarster.weixin.bean.WxCustomMessage.WxArticle; | ||||
@Test | @Test | ||||
@@ -3,8 +3,6 @@ package chanjarster.weixin.bean; | |||||
import org.testng.Assert; | import org.testng.Assert; | ||||
import org.testng.annotations.Test; | import org.testng.annotations.Test; | ||||
import chanjarster.weixin.bean.WxError; | |||||
@Test | @Test | ||||
public class WxErrorTest { | public class WxErrorTest { | ||||
@@ -4,7 +4,6 @@ import org.testng.Assert; | |||||
import org.testng.annotations.Test; | import org.testng.annotations.Test; | ||||
import chanjarster.weixin.api.WxConsts; | import chanjarster.weixin.api.WxConsts; | ||||
import chanjarster.weixin.bean.WxXmlMessage; | |||||
@Test | @Test | ||||
public class WxXmlMessageTest { | public class WxXmlMessageTest { | ||||