Search in sources :

Example 1 with BaseVO

use of com.xnx3.BaseVO in project xnx3 by xnx3.

the class AliyunSMSUtil method send.

/**
 * 发送短信,如
 * 	<pre>sms.send("网市场","SMS_40000000","{\"code\":\"123456\"}","18711111111");</pre>
 * @param signName 控制台创建的签名名称(状态必须是验证通过)
 * 				<br/>&nbsp;&nbsp;&nbsp;&nbsp; https://sms.console.aliyun.com/?spm=#/sms/Sign
 * @param templateCode 控制台创建的模板CODE(状态必须是验证通过)
 * 				<br/>&nbsp;&nbsp;&nbsp;&nbsp; https://sms.console.aliyun.com/?spm=#/sms/Template
 * @param paramString 短信模板中的变量;数字需要转换为字符串;个人用户每个变量长度必须小于15个字符。 例如:短信模板为:“接受短信验证码${no}”,此参数传递{“no”:”123456”},用户将接收到[短信签名]接受短信验证码123456,传入的字符串为JSON格式
 * @param phone 接收短信的手机号
 * @return {@link BaseVO}
 * 			<ul>
 * 				<li>若成功,返回 {@link BaseVO#SUCCESS},此时可以用 {@link BaseVO#getInfo()} 拿到其requestId</li>
 * 				<li>若失败,返回 {@link BaseVO#FAILURE},此时可以用 {@link BaseVO#getInfo()} 拿到其错误原因(catch的抛出的异常名字)</li>
 * 			</ul>
 */
public BaseVO send(String signName, String templateCode, String templateParamString, String phone) {
    BaseVO vo = new BaseVO();
    // 可自助调整超时时间
    System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
    System.setProperty("sun.net.client.defaultReadTimeout", "10000");
    // 初始化acsClient,暂不支持region化
    IClientProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
    try {
        DefaultProfile.addEndpoint(regionId, regionId, product, domain);
    } catch (ClientException e1) {
        e1.printStackTrace();
        vo.setBaseVO(BaseVO.FAILURE, e1.getMessage());
        return vo;
    }
    IAcsClient acsClient = new DefaultAcsClient(profile);
    // 组装请求对象-具体描述见控制台-文档部分内容
    SendSmsRequest request = new SendSmsRequest();
    // 必填:待发送手机号
    request.setPhoneNumbers(phone);
    // 必填:短信签名-可在短信控制台中找到
    request.setSignName(signName);
    // 必填:短信模板-可在短信控制台中找到
    request.setTemplateCode(templateCode);
    // 可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
    request.setTemplateParam(templateParamString);
    // hint 此处可能会抛出异常,注意catch
    try {
        SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
        vo.setInfo(sendSmsResponse.getRequestId());
    } catch (ServerException e) {
        vo.setBaseVO(BaseVO.FAILURE, e.getMessage());
        e.printStackTrace();
    } catch (ClientException e) {
        vo.setBaseVO(BaseVO.FAILURE, e.getMessage());
        e.printStackTrace();
    }
    return vo;
}
Also used : ServerException(com.aliyuncs.exceptions.ServerException) DefaultAcsClient(com.aliyuncs.DefaultAcsClient) BaseVO(com.xnx3.BaseVO) SendSmsRequest(com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest) IAcsClient(com.aliyuncs.IAcsClient) ClientException(com.aliyuncs.exceptions.ClientException) IClientProfile(com.aliyuncs.profile.IClientProfile) SendSmsResponse(com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse)

Example 2 with BaseVO

use of com.xnx3.BaseVO in project xnx3 by xnx3.

the class MNSUtil method createSubScription.

/**
 * 创建订阅
 * @param topicName 要在哪个主题下创建订阅,这个是主题的名字
 * @param subMeta 要创建的订阅的对象
 * @return
 */
public BaseVO createSubScription(String topicName, SubscriptionMeta subMeta) {
    BaseVO vo = new BaseVO();
    CloudTopic topic = getMNSClient().getTopicRef(topicName);
    try {
        // SubscriptionMeta subMeta = new SubscriptionMeta();
        // subMeta.setSubscriptionName("TestSub");
        // subMeta.setEndpoint(HttpEndpoint.GenEndpointLocal());
        // subMeta.setNotifyContentFormat(SubscriptionMeta.NotifyContentFormat.XML);
        // //subMeta.setFilterTag("filterTag"); //设置订阅的filterTag
        String subUrl = topic.subscribe(subMeta);
        vo.setBaseVO(BaseVO.SUCCESS, subUrl);
    } catch (Exception e) {
        vo.setBaseVO(BaseVO.FAILURE, e.getMessage());
        System.out.println("subscribe/unsubribe error");
        e.printStackTrace();
    }
    return vo;
}
Also used : CloudTopic(com.aliyun.mns.client.CloudTopic) BaseVO(com.xnx3.BaseVO) ServiceException(com.aliyun.mns.common.ServiceException) ClientException(com.aliyun.mns.common.ClientException)

Example 3 with BaseVO

use of com.xnx3.BaseVO in project xnx3 by xnx3.

the class HuaweiSMSUtil method send.

/**
 * 发送短信
 * @param phone 要发送的手机号。可以传入 +8618788888888 ,也可以不带+86,接口里面会自动加上
 * @param templateParas 模板变量。
 * 	选填,使用无变量模板时请赋空值 String templateParas = "";
 * 单变量模板示例:模板内容为"您的验证码是${1}"时,templateParas可填写为"[\"369751\"]"
 * 双变量模板示例:模板内容为"您有${1}件快递请到${2}领取"时,templateParas可填写为"[\"3\",\"人民公园正门\"]"
 * 模板中的每个变量都必须赋值,且取值不能为空
 * 查看更多模板和变量规范:产品介绍>模板和变量规范
 * @param templateId 模板ID
 * @return {@link BaseVO} result = 1 则是发送成功, =0则是失败,用getInfo() 获取失败原因
 */
public BaseVO send(String phone, String templateId, String templateParas) {
    if (phone.indexOf("+") != 0) {
        phone = "+86" + phone;
    }
    // 选填,短信状态报告接收地址,推荐使用域名,为空或者不填表示不接收状态报告
    String statusCallBack = "";
    // 请求Body,不携带签名名称时,signature请填null
    String body = buildRequestBody(sender, phone, templateId, templateParas, statusCallBack, signature);
    if (null == body || body.isEmpty()) {
        return BaseVO.failure("body is null.");
    }
    // 请求Headers中的X-WSSE参数值
    String wsseHeader = buildWsseHeader(appKey, appSecret);
    if (null == wsseHeader || wsseHeader.isEmpty()) {
        return BaseVO.failure("wsse header is null.");
    }
    // 如果JDK版本低于1.8,可使用如下代码
    // 为防止因HTTPS证书认证失败造成API调用失败,需要先忽略证书信任问题
    // CloseableHttpClient client = HttpClients.custom()
    // .setSSLContext(new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
    // @Override
    // public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
    // return true;
    // }
    // }).build()).setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE).build();
    // 如果JDK版本是1.8,可使用如下代码
    // 为防止因HTTPS证书认证失败造成API调用失败,需要先忽略证书信任问题
    CloseableHttpClient client = null;
    try {
        client = HttpClients.custom().setSSLContext(new SSLContextBuilder().loadTrustMaterial(null, (x509CertChain, authType) -> true).build()).setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE).build();
    } catch (KeyManagementException e) {
        e.printStackTrace();
        return BaseVO.failure(e.getMessage());
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return BaseVO.failure(e.getMessage());
    } catch (KeyStoreException e) {
        e.printStackTrace();
        return BaseVO.failure(e.getMessage());
    }
    HttpResponse response;
    try {
        response = client.execute(// 请求方法POST
        RequestBuilder.create("POST").setUri(url).addHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded").addHeader(HttpHeaders.AUTHORIZATION, AUTH_HEADER_VALUE).addHeader("X-WSSE", wsseHeader).setEntity(new StringEntity(body)).build());
        String responseContent = EntityUtils.toString(response.getEntity());
        if (responseContent != null) {
            JSONObject json = JSONObject.fromObject(responseContent);
            if (json.get("code") != null && json.getString("code").equals("000000")) {
                return BaseVO.success("success");
            }
        }
        return BaseVO.failure(responseContent);
    } catch (ClientProtocolException e) {
        e.printStackTrace();
        return BaseVO.failure(e.getMessage());
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        return BaseVO.failure(e.getMessage());
    } catch (IOException e) {
        e.printStackTrace();
        return BaseVO.failure(e.getMessage());
    }
}
Also used : ClientProtocolException(org.apache.http.client.ClientProtocolException) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) Date(java.util.Date) SimpleDateFormat(java.text.SimpleDateFormat) Hex(org.apache.commons.codec.binary.Hex) KeyStoreException(java.security.KeyStoreException) EntityUtils(org.apache.http.util.EntityUtils) ArrayList(java.util.ArrayList) Charset(java.nio.charset.Charset) RequestBuilder(org.apache.http.client.methods.RequestBuilder) NoopHostnameVerifier(org.apache.http.conn.ssl.NoopHostnameVerifier) HttpHeaders(org.apache.http.HttpHeaders) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) StringEntity(org.apache.http.entity.StringEntity) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) UUID(java.util.UUID) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder) BaseVO(com.xnx3.BaseVO) Base64(java.util.Base64) List(java.util.List) URLEncodedUtils(org.apache.http.client.utils.URLEncodedUtils) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) HttpResponse(org.apache.http.HttpResponse) JSONObject(net.sf.json.JSONObject) DigestUtils(org.apache.commons.codec.digest.DigestUtils) NameValuePair(org.apache.http.NameValuePair) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HttpClients(org.apache.http.impl.client.HttpClients) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpResponse(org.apache.http.HttpResponse) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) ClientProtocolException(org.apache.http.client.ClientProtocolException) StringEntity(org.apache.http.entity.StringEntity) JSONObject(net.sf.json.JSONObject) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder)

Aggregations

BaseVO (com.xnx3.BaseVO)3 CloudTopic (com.aliyun.mns.client.CloudTopic)1 ClientException (com.aliyun.mns.common.ClientException)1 ServiceException (com.aliyun.mns.common.ServiceException)1 DefaultAcsClient (com.aliyuncs.DefaultAcsClient)1 IAcsClient (com.aliyuncs.IAcsClient)1 SendSmsRequest (com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest)1 SendSmsResponse (com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse)1 ClientException (com.aliyuncs.exceptions.ClientException)1 ServerException (com.aliyuncs.exceptions.ServerException)1 IClientProfile (com.aliyuncs.profile.IClientProfile)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Charset (java.nio.charset.Charset)1 KeyManagementException (java.security.KeyManagementException)1 KeyStoreException (java.security.KeyStoreException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Base64 (java.util.Base64)1