Search in sources :

Example 11 with WxError

use of me.chanjar.weixin.common.bean.result.WxError in project weixin-java-tools by chanjarster.

the class WxErrorTest method testFromBadJson1.

public void testFromBadJson1() {
    String json = "{ \"errcode\": 40003, \"errmsg\": \"invalid openid\", \"media_id\": \"12323423dsfafsf232f\" }";
    WxError wxError = WxError.fromJson(json);
    Assert.assertTrue(wxError.getErrorCode() == 40003);
    Assert.assertEquals(wxError.getErrorMsg(), "invalid openid");
}
Also used : WxError(me.chanjar.weixin.common.bean.result.WxError)

Example 12 with WxError

use of me.chanjar.weixin.common.bean.result.WxError in project weixin-java-tools by chanjarster.

the class MediaUploadRequestExecutor method execute.

@Override
public WxMediaUploadResult execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, File file) throws WxErrorException, ClientProtocolException, IOException {
    HttpPost httpPost = new HttpPost(uri);
    if (httpProxy != null) {
        RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
        httpPost.setConfig(config);
    }
    if (file != null) {
        HttpEntity entity = MultipartEntityBuilder.create().addBinaryBody("media", file).setMode(HttpMultipartMode.RFC6532).build();
        httpPost.setEntity(entity);
        httpPost.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString());
    }
    try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
        String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
        WxError error = WxError.fromJson(responseContent);
        if (error.getErrorCode() != 0) {
            throw new WxErrorException(error);
        }
        return WxMediaUploadResult.fromJson(responseContent);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) RequestConfig(org.apache.http.client.config.RequestConfig) WxError(me.chanjar.weixin.common.bean.result.WxError) HttpEntity(org.apache.http.HttpEntity) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) WxErrorException(me.chanjar.weixin.common.exception.WxErrorException)

Example 13 with WxError

use of me.chanjar.weixin.common.bean.result.WxError in project weixin-java-tools by chanjarster.

the class SimplePostRequestExecutor method execute.

@Override
public String execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String postEntity) throws WxErrorException, ClientProtocolException, IOException {
    HttpPost httpPost = new HttpPost(uri);
    if (httpProxy != null) {
        RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
        httpPost.setConfig(config);
    }
    if (postEntity != null) {
        StringEntity entity = new StringEntity(postEntity, Consts.UTF_8);
        httpPost.setEntity(entity);
    }
    try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
        String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
        WxError error = WxError.fromJson(responseContent);
        if (error.getErrorCode() != 0) {
            throw new WxErrorException(error);
        }
        return responseContent;
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) RequestConfig(org.apache.http.client.config.RequestConfig) StringEntity(org.apache.http.entity.StringEntity) WxError(me.chanjar.weixin.common.bean.result.WxError) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) WxErrorException(me.chanjar.weixin.common.exception.WxErrorException)

Example 14 with WxError

use of me.chanjar.weixin.common.bean.result.WxError in project weixin-java-tools by chanjarster.

the class WxCpBusyRetryTest method getService.

@DataProvider(name = "getService")
public Object[][] getService() {
    WxCpService service = new WxCpServiceImpl() {

        @Override
        protected <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException {
            WxError error = new WxError();
            error.setErrorCode(-1);
            throw new WxErrorException(error);
        }
    };
    service.setMaxRetryTimes(3);
    service.setRetrySleepMillis(500);
    return new Object[][] { new Object[] { service } };
}
Also used : WxError(me.chanjar.weixin.common.bean.result.WxError) RequestExecutor(me.chanjar.weixin.common.util.http.RequestExecutor) WxErrorException(me.chanjar.weixin.common.exception.WxErrorException) DataProvider(org.testng.annotations.DataProvider)

Example 15 with WxError

use of me.chanjar.weixin.common.bean.result.WxError in project weixin-java-tools by chanjarster.

the class WxCpServiceImpl method getAccessToken.

public String getAccessToken(boolean forceRefresh) throws WxErrorException {
    if (forceRefresh) {
        wxCpConfigStorage.expireAccessToken();
    }
    if (wxCpConfigStorage.isAccessTokenExpired()) {
        synchronized (globalAccessTokenRefreshLock) {
            if (wxCpConfigStorage.isAccessTokenExpired()) {
                String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?" + "&corpid=" + wxCpConfigStorage.getCorpId() + "&corpsecret=" + wxCpConfigStorage.getCorpSecret();
                try {
                    HttpGet httpGet = new HttpGet(url);
                    if (httpProxy != null) {
                        RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
                        httpGet.setConfig(config);
                    }
                    CloseableHttpClient httpclient = getHttpclient();
                    String resultContent = null;
                    try (CloseableHttpResponse response = httpclient.execute(httpGet)) {
                        resultContent = new BasicResponseHandler().handleResponse(response);
                    }
                    WxError error = WxError.fromJson(resultContent);
                    if (error.getErrorCode() != 0) {
                        throw new WxErrorException(error);
                    }
                    WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
                    wxCpConfigStorage.updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
                } catch (ClientProtocolException e) {
                    throw new RuntimeException(e);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
    return wxCpConfigStorage.getAccessToken();
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) WxError(me.chanjar.weixin.common.bean.result.WxError) WxAccessToken(me.chanjar.weixin.common.bean.WxAccessToken) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler) IOException(java.io.IOException) WxErrorException(me.chanjar.weixin.common.exception.WxErrorException) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Aggregations

WxError (me.chanjar.weixin.common.bean.result.WxError)23 WxErrorException (me.chanjar.weixin.common.exception.WxErrorException)19 RequestConfig (org.apache.http.client.config.RequestConfig)11 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)11 HttpPost (org.apache.http.client.methods.HttpPost)8 HashMap (java.util.HashMap)6 StringEntity (org.apache.http.entity.StringEntity)6 IOException (java.io.IOException)5 ClientProtocolException (org.apache.http.client.ClientProtocolException)4 HttpGet (org.apache.http.client.methods.HttpGet)3 JsonObject (com.google.gson.JsonObject)2 WxAccessToken (me.chanjar.weixin.common.bean.WxAccessToken)2 RequestExecutor (me.chanjar.weixin.common.util.http.RequestExecutor)2 BasicResponseHandler (org.apache.http.impl.client.BasicResponseHandler)2 DataProvider (org.testng.annotations.DataProvider)2 XStream (com.thoughtworks.xstream.XStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 TreeMap (java.util.TreeMap)1 WxRedpackResult (me.chanjar.weixin.mp.bean.result.WxRedpackResult)1