Search in sources :

Example 16 with WxErrorException

use of me.chanjar.weixin.common.exception.WxErrorException 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 17 with WxErrorException

use of me.chanjar.weixin.common.exception.WxErrorException 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 18 with WxErrorException

use of me.chanjar.weixin.common.exception.WxErrorException 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)

Example 19 with WxErrorException

use of me.chanjar.weixin.common.exception.WxErrorException in project weixin-java-tools by chanjarster.

the class WxCpServiceImpl method executeInternal.

protected synchronized <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException {
    if (uri.indexOf("access_token=") != -1) {
        throw new IllegalArgumentException("uri参数中不允许有access_token: " + uri);
    }
    String accessToken = getAccessToken(false);
    String uriWithAccessToken = uri;
    uriWithAccessToken += uri.indexOf('?') == -1 ? "?access_token=" + accessToken : "&access_token=" + accessToken;
    try {
        return executor.execute(getHttpclient(), httpProxy, uriWithAccessToken, data);
    } catch (WxErrorException e) {
        WxError error = e.getError();
        /*
       * 发生以下情况时尝试刷新access_token
       * 40001 获取access_token时AppSecret错误,或者access_token无效
       * 42001 access_token超时
       */
        if (error.getErrorCode() == 42001 || error.getErrorCode() == 40001) {
            // 强制设置wxCpConfigStorage它的access token过期了,这样在下一次请求里就会刷新access token
            wxCpConfigStorage.expireAccessToken();
            return execute(executor, uri, data);
        }
        if (error.getErrorCode() != 0) {
            throw new WxErrorException(error);
        }
        return null;
    } catch (ClientProtocolException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : WxError(me.chanjar.weixin.common.bean.result.WxError) IOException(java.io.IOException) WxErrorException(me.chanjar.weixin.common.exception.WxErrorException) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Example 20 with WxErrorException

use of me.chanjar.weixin.common.exception.WxErrorException in project weixin-java-tools by chanjarster.

the class WxMpOAuth2Servlet method service.

@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html;charset=utf-8");
    response.setStatus(HttpServletResponse.SC_OK);
    String code = request.getParameter("code");
    try {
        response.getWriter().println("<h1>code</h1>");
        response.getWriter().println(code);
        WxMpOAuth2AccessToken wxMpOAuth2AccessToken = wxMpService.oauth2getAccessToken(code);
        response.getWriter().println("<h1>access token</h1>");
        response.getWriter().println(wxMpOAuth2AccessToken.toString());
        WxMpUser wxMpUser = wxMpService.oauth2getUserInfo(wxMpOAuth2AccessToken, null);
        response.getWriter().println("<h1>user info</h1>");
        response.getWriter().println(wxMpUser.toString());
        wxMpOAuth2AccessToken = wxMpService.oauth2refreshAccessToken(wxMpOAuth2AccessToken.getRefreshToken());
        response.getWriter().println("<h1>after refresh</h1>");
        response.getWriter().println(wxMpOAuth2AccessToken.toString());
    } catch (WxErrorException e) {
        e.printStackTrace();
    }
    response.getWriter().flush();
    response.getWriter().close();
}
Also used : WxMpOAuth2AccessToken(me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken) WxMpUser(me.chanjar.weixin.mp.bean.result.WxMpUser) WxErrorException(me.chanjar.weixin.common.exception.WxErrorException)

Aggregations

WxErrorException (me.chanjar.weixin.common.exception.WxErrorException)28 WxError (me.chanjar.weixin.common.bean.result.WxError)19 RequestConfig (org.apache.http.client.config.RequestConfig)13 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)13 HashMap (java.util.HashMap)8 HttpPost (org.apache.http.client.methods.HttpPost)8 IOException (java.io.IOException)6 StringEntity (org.apache.http.entity.StringEntity)6 HttpGet (org.apache.http.client.methods.HttpGet)5 ClientProtocolException (org.apache.http.client.ClientProtocolException)4 JsonObject (com.google.gson.JsonObject)3 InputStream (java.io.InputStream)3 File (java.io.File)2 ExecutorService (java.util.concurrent.ExecutorService)2 WxAccessToken (me.chanjar.weixin.common.bean.WxAccessToken)2 RequestExecutor (me.chanjar.weixin.common.util.http.RequestExecutor)2 Header (org.apache.http.Header)2 BasicResponseHandler (org.apache.http.impl.client.BasicResponseHandler)2 DataProvider (org.testng.annotations.DataProvider)2 Test (org.testng.annotations.Test)2