liuxiaolong
2019-05-09 0d1d88cdb668e75ea8609417ac18ae19947e9525
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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;
    }
}