119 lines
4.0 KiB
Java
119 lines
4.0 KiB
Java
/**
|
||
* 项目名称: 系统名称
|
||
* 包名: com.message
|
||
* 文件名: SendMessage.java
|
||
* 版本信息: 1.0.0
|
||
* 创建日期: 2013-12-2-下午12:29:53
|
||
* Copyright:2013XX公司-版权所有
|
||
**/
|
||
|
||
package com.tenwa.lease.util;
|
||
|
||
import java.net.Inet4Address;
|
||
import java.net.InetAddress;
|
||
import java.net.NetworkInterface;
|
||
import java.net.SocketException;
|
||
import java.util.ArrayList;
|
||
import java.util.Enumeration;
|
||
import java.util.HashMap;
|
||
import java.util.List;
|
||
import java.util.Map;
|
||
|
||
import jbo.com.tenwa.lease.comm.LB_MESSAGE_CONFIG;
|
||
|
||
import org.apache.commons.httpclient.HttpClient;
|
||
import org.apache.commons.httpclient.HttpStatus;
|
||
import org.apache.commons.httpclient.NameValuePair;
|
||
import org.apache.commons.httpclient.methods.PostMethod;
|
||
|
||
import com.amarsoft.are.jbo.BizObject;
|
||
import com.amarsoft.are.jbo.JBOFactory;
|
||
|
||
public class SendMessageUtil{
|
||
/**
|
||
* @method sendMessageByHttpClient 发送短信到指定号码
|
||
* @param receiverMobilePhone 接收方手机号码
|
||
* @param msg 消息内容
|
||
* @return
|
||
* @throws Exception
|
||
* @returnType String
|
||
* @exception
|
||
* @since 1.0.0
|
||
**/
|
||
public static String sendMessageByHttpClient(String receiverMobilePhone, String msg) throws Exception {
|
||
List<BizObject> messageConfigList = JBOFactory.getBizObjectManager(LB_MESSAGE_CONFIG.CLASS_NAME).createQuery("1=1").getResultList(false);
|
||
BizObject messageConfig = messageConfigList.get(0);
|
||
String allowSendIps = messageConfig.getAttribute("ALLOW_SEND_IPS").getString();
|
||
if("N".equals(messageConfig.getAttribute("IS_SEND").getString()) || !getServerIsAllowSendMsg(allowSendIps)){
|
||
System.out.println("短信设置为不发送短信或本机IP未在[允许发送短信的主机IP]之中!");
|
||
return "当前系统IP不在允许发送的IP地址之中";
|
||
}
|
||
|
||
String sendParamsUrl = messageConfig.getAttribute("SEND_PARAMS_URL_").getString();
|
||
String[] sendParamsUrlArr = sendParamsUrl.split("&");
|
||
|
||
Map<String,String> model = new HashMap<String, String>();
|
||
for(String str : sendParamsUrlArr){
|
||
model.put(str.split("=")[0], str.split("=")[1]);
|
||
}
|
||
|
||
HttpClient httpClient = new HttpClient();
|
||
String sendMessageUrl = messageConfig.getAttribute("SEND_MESSAGE_URL_").getString();
|
||
String url = sendMessageUrl;
|
||
String uid = model.get("uid");
|
||
String auth = MD5Util.getMD5EncodedPassword(messageConfig.getAttribute("SENDER_").getString() + messageConfig.getAttribute("SENDER_PASSWORD_").getString());
|
||
String content = java.net.URLEncoder.encode(msg, "gbk");
|
||
PostMethod postMethod = new PostMethod(url);
|
||
|
||
NameValuePair[] data = { new NameValuePair("uid", uid),
|
||
new NameValuePair("auth", auth),
|
||
new NameValuePair("mobile", receiverMobilePhone),
|
||
new NameValuePair("expid", "0"),
|
||
new NameValuePair("msg", content) };
|
||
postMethod.setRequestBody(data);
|
||
|
||
int statusCode = httpClient.executeMethod(postMethod);
|
||
String result = "";
|
||
if (statusCode == HttpStatus.SC_OK) {
|
||
result = postMethod.getResponseBodyAsString();
|
||
}else{
|
||
result = statusCode + "";
|
||
}
|
||
postMethod.releaseConnection();
|
||
return result;
|
||
}
|
||
|
||
public static List<String> getServerIpList() throws SocketException{
|
||
List<String> hostAddrList = new ArrayList<String>();
|
||
Enumeration allNetInterfaces = NetworkInterface.getNetworkInterfaces();
|
||
InetAddress ip = null;
|
||
while (allNetInterfaces.hasMoreElements()) {
|
||
NetworkInterface netInterface = (NetworkInterface)allNetInterfaces.nextElement();
|
||
Enumeration addresses = netInterface.getInetAddresses();
|
||
while (addresses.hasMoreElements()) {
|
||
ip = (InetAddress)addresses.nextElement();
|
||
if (ip != null && ip instanceof Inet4Address) {
|
||
hostAddrList.add(ip.getHostAddress());
|
||
System.out.println("本机的IP = " + ip.getHostAddress());
|
||
}
|
||
}
|
||
}
|
||
return hostAddrList;
|
||
}
|
||
|
||
public static boolean getServerIsAllowSendMsg(String allowIps) throws SocketException {
|
||
boolean flag = false;
|
||
List<String> hostAddrList = getServerIpList();
|
||
if(hostAddrList.size() > 0){
|
||
for(String ipStr : hostAddrList){
|
||
if(allowIps.indexOf(ipStr) != -1){
|
||
flag = true;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
return flag;
|
||
}
|
||
|
||
}
|