Search in sources :

Example 21 with WxErrorException

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

the class WxMpMessageRouterRule method service.

/**
   * 处理微信推送过来的消息
   *
   * @param wxMessage
   * @return true 代表继续执行别的router,false 代表停止执行别的router
   */
protected WxMpXmlOutMessage service(WxMpXmlMessage wxMessage, WxMpService wxMpService, WxSessionManager sessionManager, WxErrorExceptionHandler exceptionHandler) {
    try {
        Map<String, Object> context = new HashMap<String, Object>();
        // 如果拦截器不通过
        for (WxMpMessageInterceptor interceptor : this.interceptors) {
            if (!interceptor.intercept(wxMessage, context, wxMpService, sessionManager)) {
                return null;
            }
        }
        // 交给handler处理
        WxMpXmlOutMessage res = null;
        for (WxMpMessageHandler handler : this.handlers) {
            // 返回最后handler的结果
            res = handler.handle(wxMessage, context, wxMpService, sessionManager);
        }
        return res;
    } catch (WxErrorException e) {
        exceptionHandler.handle(e);
    }
    return null;
}
Also used : HashMap(java.util.HashMap) WxMpXmlOutMessage(me.chanjar.weixin.mp.bean.WxMpXmlOutMessage) WxErrorException(me.chanjar.weixin.common.exception.WxErrorException)

Example 22 with WxErrorException

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

the class WxMpServiceImpl method materialNewsUpdate.

public boolean materialNewsUpdate(WxMpMaterialArticleUpdate wxMpMaterialArticleUpdate) throws WxErrorException {
    String url = "https://api.weixin.qq.com/cgi-bin/material/update_news";
    String responseText = post(url, wxMpMaterialArticleUpdate.toJson());
    WxError wxError = WxError.fromJson(responseText);
    if (wxError.getErrorCode() == 0) {
        return true;
    } else {
        throw new WxErrorException(wxError);
    }
}
Also used : WxError(me.chanjar.weixin.common.bean.result.WxError) WxErrorException(me.chanjar.weixin.common.exception.WxErrorException)

Example 23 with WxErrorException

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

the class WxMpServiceImpl method materialFileBatchGet.

public WxMpMaterialFileBatchGetResult materialFileBatchGet(String type, int offset, int count) throws WxErrorException {
    String url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material";
    Map<String, Object> params = new HashMap<>();
    params.put("type", type);
    params.put("offset", offset);
    params.put("count", count);
    String responseText = post(url, WxGsonBuilder.create().toJson(params));
    WxError wxError = WxError.fromJson(responseText);
    if (wxError.getErrorCode() == 0) {
        return WxMpGsonBuilder.create().fromJson(responseText, WxMpMaterialFileBatchGetResult.class);
    } else {
        throw new WxErrorException(wxError);
    }
}
Also used : WxError(me.chanjar.weixin.common.bean.result.WxError) HashMap(java.util.HashMap) JsonObject(com.google.gson.JsonObject) WxErrorException(me.chanjar.weixin.common.exception.WxErrorException)

Example 24 with WxErrorException

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

the class WxMpServiceImpl method getAccessToken.

public String getAccessToken(boolean forceRefresh) throws WxErrorException {
    if (forceRefresh) {
        wxMpConfigStorage.expireAccessToken();
    }
    if (wxMpConfigStorage.isAccessTokenExpired()) {
        synchronized (globalAccessTokenRefreshLock) {
            if (wxMpConfigStorage.isAccessTokenExpired()) {
                String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential" + "&appid=" + wxMpConfigStorage.getAppId() + "&secret=" + wxMpConfigStorage.getSecret();
                try {
                    HttpGet httpGet = new HttpGet(url);
                    if (httpProxy != null) {
                        RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
                        httpGet.setConfig(config);
                    }
                    CloseableHttpResponse response = getHttpclient().execute(httpGet);
                    String resultContent = new BasicResponseHandler().handleResponse(response);
                    WxError error = WxError.fromJson(resultContent);
                    if (error.getErrorCode() != 0) {
                        throw new WxErrorException(error);
                    }
                    WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
                    wxMpConfigStorage.updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
                } catch (ClientProtocolException e) {
                    throw new RuntimeException(e);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
    return wxMpConfigStorage.getAccessToken();
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) 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 25 with WxErrorException

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

the class MaterialUploadRequestExecutor method execute.

public WxMpMaterialUploadResult execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, WxMpMaterial material) throws WxErrorException, ClientProtocolException, IOException {
    HttpPost httpPost = new HttpPost(uri);
    if (httpProxy != null) {
        RequestConfig response = RequestConfig.custom().setProxy(httpProxy).build();
        httpPost.setConfig(response);
    }
    if (material != null) {
        File file = material.getFile();
        if (file == null || !file.exists()) {
            throw new FileNotFoundException();
        }
        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
        MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
        multipartEntityBuilder.addPart("media", new InputStreamBody(bufferedInputStream, material.getName())).setMode(HttpMultipartMode.RFC6532);
        Map<String, String> form = material.getForm();
        if (material.getForm() != null) {
            multipartEntityBuilder.addTextBody("description", WxGsonBuilder.create().toJson(form));
        }
        httpPost.setEntity(multipartEntityBuilder.build());
        httpPost.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString());
    }
    CloseableHttpResponse response = httpclient.execute(httpPost);
    String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
    WxError error = WxError.fromJson(responseContent);
    if (error.getErrorCode() != 0) {
        throw new WxErrorException(error);
    } else {
        return WxMpMaterialUploadResult.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) MultipartEntityBuilder(org.apache.http.entity.mime.MultipartEntityBuilder) WxErrorException(me.chanjar.weixin.common.exception.WxErrorException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) InputStreamBody(org.apache.http.entity.mime.content.InputStreamBody)

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