package com.basic.easemob;
|
|
import java.io.InputStream;
|
import java.net.MalformedURLException;
|
import java.net.URL;
|
import java.util.ArrayList;
|
import java.util.List;
|
import java.util.Properties;
|
import java.util.regex.Matcher;
|
import java.util.regex.Pattern;
|
|
import org.apache.http.HttpEntity;
|
import org.apache.http.HttpResponse;
|
import org.apache.http.HttpStatus;
|
import org.apache.http.NameValuePair;
|
import org.apache.http.client.HttpClient;
|
import org.apache.http.client.methods.HttpPost;
|
import org.apache.http.entity.StringEntity;
|
import org.apache.http.message.BasicNameValuePair;
|
import org.apache.http.util.EntityUtils;
|
import org.apache.log4j.Logger;
|
import org.json.JSONObject;
|
|
/**
|
* 环信IM配置
|
*
|
* @company 北京贝思科技术有限公司
|
* @author liuyajun, 8384503@qq.com
|
* @date 2016年1月26日
|
* @time 上午9:57:10
|
*/
|
public class IMConfig {
|
private static Logger log = Logger.getLogger(IMConfig.class);
|
|
public static String ENCODING = "UTF-8";
|
|
public static String API_HTTP_SCHEMA = "https";
|
|
public static String API_SERVER_HOST = "";
|
|
public static String APPKEY = "";
|
|
public static String APP_URL_ROOT = "";
|
|
public static String APP_CLIENT_ID = "";
|
|
public static String APP_CLIENT_SECRET = "";
|
|
static {
|
try {
|
Properties p = new Properties();
|
|
InputStream inputStream = IMConfig.class.getResourceAsStream(
|
"config.properties");
|
|
p.load(inputStream);
|
|
API_SERVER_HOST = p.getProperty("API_SERVER_HOST");
|
APPKEY = p.getProperty("APPKEY");
|
APP_CLIENT_ID = p.getProperty("APP_CLIENT_ID");
|
APP_CLIENT_SECRET = p.getProperty("APP_CLIENT_SECRET");
|
|
APP_URL_ROOT = APPKEY.replace("#", "/");
|
|
} catch (Throwable e1) {
|
e1.printStackTrace();
|
}
|
}
|
|
/**
|
* 前缀已经包含本公司的本app
|
* @param path 需以/开头
|
* @return
|
* @throws MalformedURLException
|
*/
|
public static URL getURL(String path) {
|
URL url;
|
try {
|
url = new URL(IMConfig.API_HTTP_SCHEMA, IMConfig.API_SERVER_HOST,
|
"/" + APP_URL_ROOT + path);
|
} catch (MalformedURLException e) {
|
throw new RuntimeException(e);
|
}
|
|
return url;
|
}
|
|
/**
|
* Check illegal String
|
*
|
* @param regex
|
* @param str
|
* @return
|
*/
|
public static boolean match(String regex, String str) {
|
Pattern pattern = Pattern.compile(regex);
|
Matcher matcher = pattern.matcher(str);
|
|
return matcher.lookingAt();
|
}
|
|
private static String tokenAcessToken = null;
|
private static Long tokenExpiredAt = null;
|
|
/**
|
* 获取token
|
* @return
|
*/
|
public static String getToken(){
|
//如果当前没有,如已超时
|
if (null == tokenAcessToken || tokenAcessToken.trim().length()==0
|
|| System.currentTimeMillis() > tokenExpiredAt) {
|
|
HttpClient client = HttpUtil.getClient(true);
|
|
try {
|
URL tokenUrl = getURL("/token");
|
|
JSONObject data = new JSONObject();
|
data.put("grant_type", "client_credentials");
|
data.put("client_id", APP_CLIENT_ID);
|
data.put("client_secret", APP_CLIENT_SECRET);
|
|
List<NameValuePair> headers = new ArrayList<NameValuePair>();
|
headers.add(new BasicNameValuePair("Content-Type", "application/json"));
|
|
HttpPost httpPost = new HttpPost();
|
httpPost.setURI(tokenUrl.toURI());
|
|
for (NameValuePair nameValuePair : headers) {
|
httpPost.addHeader(nameValuePair.getName(), nameValuePair.getValue());
|
}
|
httpPost.setEntity(new StringEntity(data.toString(), ENCODING));
|
|
HttpResponse tokenResponse = client.execute(httpPost);
|
HttpEntity entity = tokenResponse.getEntity();
|
|
String results = EntityUtils.toString(entity, ENCODING);
|
|
log.info("获取token: " + results);
|
|
tokenAcessToken = "";
|
if (tokenResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
|
JSONObject json = new JSONObject(results);
|
tokenAcessToken = json.getString("access_token");
|
tokenExpiredAt = System.currentTimeMillis() +
|
json.getInt("expires_in") * 1000;
|
}
|
|
} catch (Throwable e) {
|
throw new RuntimeException(e);
|
}finally{
|
|
}
|
}
|
|
return tokenAcessToken;
|
}
|
}
|