Search in sources :

Example 1 with ParaValuePair

use of com.creditease.uav.httpasync.api.ParaValuePair in project uavstack by uavorg.

the class SMSAction method run.

@Override
public boolean run(NotificationEvent notifyEvent) {
    // 模板关键字
    Map<String, String> keywords = new HashMap<String, String>();
    String title = notifyEvent.getTitle();
    long timeFlag = notifyEvent.getTime();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String sd = sdf.format(new Date(timeFlag));
    keywords.put("custName", sd);
    keywords.put("comName", title);
    /**
     * 若notifyEvent中包含目的地址,则用该地址;否则使用默认地址
     */
    String sms = notifyEvent.getArg(cName);
    if (StringHelper.isEmpty(sms)) {
        if (log.isTraceEnable()) {
            log.warn(this, "Send SMS FAIL as no any phone numbers.");
        }
        return false;
    }
    String[] phoneNums = sms.split(",");
    for (String phone : phoneNums) {
        List<ParaValuePair> nvps = new ArrayList<ParaValuePair>();
        // 接口版本
        nvps.add(new ParaValuePair("version", "3.0"));
        // 批次号
        nvps.add(new ParaValuePair("batchId", Long.toString(System.currentTimeMillis())));
        // 组织机构号
        nvps.add(new ParaValuePair("orgNo", "2265"));
        // 模板号
        nvps.add(new ParaValuePair("typeNo", "7209"));
        // 关键字替换
        nvps.add(new ParaValuePair("keywords", JSON.toJSONString(keywords)));
        // 手机号
        nvps.add(new ParaValuePair("mobile", phone));
        if (log.isDebugEnable()) {
            log.debug(this, "Send SMS START: phone=" + phone);
        }
        final String phoneNum = phone;
        client.doAsyncHttpPost(baseUrl + "send", nvps, "utf-8", new HttpClientCallback() {

            @Override
            public void completed(HttpClientCallbackResult result) {
                String results = result.getReplyDataAsString();
                JSONObject jo = JSON.parseObject(results);
                // 错误码
                String code = jo.getString("code");
                // 错误信息
                String desc = jo.getString("desc");
                if (log.isDebugEnable()) {
                    log.debug(this, "Send SMS END: phone=" + phoneNum + ",code=" + code + ",desc=" + desc);
                }
            }

            @Override
            public void failed(HttpClientCallbackResult result) {
                String results = result.getReplyDataAsString();
                log.err(this, "Send SMS FAIL: phone=" + phoneNum + ", result=" + results);
            }
        });
    }
    return true;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Date(java.sql.Date) HttpClientCallback(com.creditease.uav.httpasync.HttpClientCallback) ParaValuePair(com.creditease.uav.httpasync.api.ParaValuePair) JSONObject(com.alibaba.fastjson.JSONObject) HttpClientCallbackResult(com.creditease.uav.httpasync.HttpClientCallbackResult) SimpleDateFormat(java.text.SimpleDateFormat)

Example 2 with ParaValuePair

use of com.creditease.uav.httpasync.api.ParaValuePair in project uavstack by uavorg.

the class HttpAsyncClient method buildPostMethod.

private HttpUriRequest buildPostMethod(String url, List<ParaValuePair> nvps, String encoding) {
    HttpUriRequest httpMethod;
    httpMethod = new HttpPost(url);
    if (null == nvps) {
        return null;
    }
    List<BasicNameValuePair> bnvps = new ArrayList<>();
    for (ParaValuePair pvp : nvps) {
        BasicNameValuePair bnvp = new BasicNameValuePair(pvp.getName(), pvp.getValue());
        bnvps.add(bnvp);
    }
    try {
        ((HttpPost) httpMethod).setEntity(new UrlEncodedFormEntity(bnvps, encoding));
    } catch (UnsupportedEncodingException e) {
        // ignore
        httpMethod.abort();
    } catch (Exception e) {
        httpMethod.abort();
    }
    return httpMethod;
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpPost(org.apache.http.client.methods.HttpPost) ParaValuePair(com.creditease.uav.httpasync.api.ParaValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) IOException(java.io.IOException) IOReactorException(org.apache.http.nio.reactor.IOReactorException) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

ParaValuePair (com.creditease.uav.httpasync.api.ParaValuePair)2 ArrayList (java.util.ArrayList)2 JSONObject (com.alibaba.fastjson.JSONObject)1 HttpClientCallback (com.creditease.uav.httpasync.HttpClientCallback)1 HttpClientCallbackResult (com.creditease.uav.httpasync.HttpClientCallbackResult)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)1 Date (java.sql.Date)1 SimpleDateFormat (java.text.SimpleDateFormat)1 HashMap (java.util.HashMap)1 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)1 HttpPost (org.apache.http.client.methods.HttpPost)1 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)1 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)1 IOReactorException (org.apache.http.nio.reactor.IOReactorException)1