|
@@ -1,248 +0,0 @@
|
|
-package cn.com.qmth.mps.util;
|
|
|
|
-
|
|
|
|
-import java.io.BufferedReader;
|
|
|
|
-import java.io.IOException;
|
|
|
|
-import java.io.InputStream;
|
|
|
|
-import java.io.InputStreamReader;
|
|
|
|
-import java.io.OutputStream;
|
|
|
|
-import java.net.HttpURLConnection;
|
|
|
|
-import java.net.URL;
|
|
|
|
-import java.security.cert.CertificateException;
|
|
|
|
-import java.security.cert.X509Certificate;
|
|
|
|
-import java.util.ArrayList;
|
|
|
|
-import java.util.HashMap;
|
|
|
|
-import java.util.List;
|
|
|
|
-import java.util.Map;
|
|
|
|
-
|
|
|
|
-import javax.net.ssl.HttpsURLConnection;
|
|
|
|
-import javax.net.ssl.SSLContext;
|
|
|
|
-import javax.net.ssl.SSLSocketFactory;
|
|
|
|
-import javax.net.ssl.TrustManager;
|
|
|
|
-import javax.net.ssl.X509TrustManager;
|
|
|
|
-
|
|
|
|
-import com.alibaba.fastjson.JSONArray;
|
|
|
|
-import com.alibaba.fastjson.JSONObject;
|
|
|
|
-import com.qmth.boot.core.exception.StatusException;
|
|
|
|
-import com.qmth.boot.core.solar.model.AppLicense;
|
|
|
|
-import com.qmth.boot.tools.signature.SignatureEntity;
|
|
|
|
-import com.qmth.boot.tools.signature.SignatureType;
|
|
|
|
-
|
|
|
|
-import cn.com.qmth.mps.entity.SchoolEntity;
|
|
|
|
-
|
|
|
|
-public class SolarHttpUtil {
|
|
|
|
-
|
|
|
|
- /** 默认的编码格式 */
|
|
|
|
- private static final String DEFAULT_CHARSET = "UTF-8";
|
|
|
|
-
|
|
|
|
- private static final String CONTENT_TYPE = "Content-Type";
|
|
|
|
-
|
|
|
|
- private static final String APPLICATION_JSON = "application/x-www-form-urlencoded;charset=utf-8";
|
|
|
|
-
|
|
|
|
- private static final String METHOD_POST = "POST";
|
|
|
|
-
|
|
|
|
- private static final String AUTH = "Authorization";
|
|
|
|
-
|
|
|
|
- private String host = null;
|
|
|
|
-
|
|
|
|
- private String uri = null;
|
|
|
|
-
|
|
|
|
- private String accessKey = null;
|
|
|
|
-
|
|
|
|
- private String accessSecret = null;
|
|
|
|
-
|
|
|
|
- public SolarHttpUtil(String accessKey, String accessSecret, String host, String uri) {
|
|
|
|
- this.accessKey = accessKey;
|
|
|
|
- this.accessSecret = accessSecret;
|
|
|
|
- this.uri = uri;
|
|
|
|
- this.host = host;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- *
|
|
|
|
- * @param params headers参数
|
|
|
|
- * @param datas requestParams参数
|
|
|
|
- * @return
|
|
|
|
- * @throws Exception
|
|
|
|
- */
|
|
|
|
- public String httpAction(Map<String, String> params, Map<String, Object> datas) {
|
|
|
|
- String result = null;
|
|
|
|
- HttpsURLConnection conn = null;
|
|
|
|
- OutputStream os = null;
|
|
|
|
- InputStream is = null;
|
|
|
|
-
|
|
|
|
- try {
|
|
|
|
-
|
|
|
|
- // 获取链接
|
|
|
|
- URL url = new URL(host + uri);
|
|
|
|
- conn = (HttpsURLConnection) url.openConnection();
|
|
|
|
-
|
|
|
|
- conn.setRequestMethod(METHOD_POST);
|
|
|
|
- conn.setRequestProperty(CONTENT_TYPE, APPLICATION_JSON);
|
|
|
|
- // 设置鉴权
|
|
|
|
- long time = System.currentTimeMillis();
|
|
|
|
- String signature = SignatureEntity.build(SignatureType.SECRET, METHOD_POST, uri, time, accessKey,
|
|
|
|
- accessSecret);
|
|
|
|
- conn.setRequestProperty(AUTH, signature);
|
|
|
|
- conn.setRequestProperty("time", String.valueOf(time));
|
|
|
|
- // ssl
|
|
|
|
- SSLContext context = SSLContext.getInstance("SSL", "SunJSSE");
|
|
|
|
- TrustManager[] tm = new TrustManager[] { new X509TrustManager() {
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
- public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
- public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
- public X509Certificate[] getAcceptedIssuers() {
|
|
|
|
- return null;
|
|
|
|
- }
|
|
|
|
- } };
|
|
|
|
- // 初始化
|
|
|
|
- context.init(null, tm, new java.security.SecureRandom());
|
|
|
|
- // 获取SSLSocketFactory对象
|
|
|
|
- SSLSocketFactory ssf = context.getSocketFactory();
|
|
|
|
- conn.setSSLSocketFactory(ssf);
|
|
|
|
-
|
|
|
|
- conn.setUseCaches(false);
|
|
|
|
- conn.setDoOutput(true);
|
|
|
|
-
|
|
|
|
- // 设置额外的参数
|
|
|
|
- if (params != null && !params.isEmpty()) {
|
|
|
|
-
|
|
|
|
- for (Map.Entry<String, String> param : params.entrySet()) {
|
|
|
|
- conn.setRequestProperty(param.getKey(), param.getValue());
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- // 创建链接
|
|
|
|
- conn.connect();
|
|
|
|
- // 设置请求参数
|
|
|
|
- if (datas != null) {
|
|
|
|
- StringBuilder sb = new StringBuilder();
|
|
|
|
- for (Map.Entry<String, Object> data : datas.entrySet()) {
|
|
|
|
- sb.append(data.getKey()).append("=").append(data.getValue()).append("&");
|
|
|
|
- }
|
|
|
|
- os = conn.getOutputStream();
|
|
|
|
- os.write(sb.toString().getBytes());
|
|
|
|
- os.flush();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- result = getResult(conn);
|
|
|
|
- } catch (IOException e) {
|
|
|
|
- try {
|
|
|
|
- JSONObject js = JSONObject.parseObject(e.getMessage());
|
|
|
|
- if (js.getString("code").startsWith("401")) {
|
|
|
|
- throw new StatusException("无效的授权");
|
|
|
|
- } else {
|
|
|
|
- throw new StatusException("授权服务器访问失败", e);
|
|
|
|
- }
|
|
|
|
- } catch (StatusException e1) {
|
|
|
|
- throw e1;
|
|
|
|
- } catch (Exception e2) {
|
|
|
|
- throw new StatusException("授权服务器访问失败", e);
|
|
|
|
- }
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- throw new StatusException("授权服务器访问失败", e);
|
|
|
|
- } finally {
|
|
|
|
- try {
|
|
|
|
- if (os != null) {
|
|
|
|
- os.close();
|
|
|
|
- os = null;
|
|
|
|
- }
|
|
|
|
- if (is != null) {
|
|
|
|
- is.close();
|
|
|
|
- is = null;
|
|
|
|
- }
|
|
|
|
- } catch (IOException e) {
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if (conn != null) {
|
|
|
|
- conn.disconnect();
|
|
|
|
- conn = null;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return result;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 获得连接请求的返回数据
|
|
|
|
- *
|
|
|
|
- * @param conn
|
|
|
|
- *
|
|
|
|
- * @return 字符串
|
|
|
|
- */
|
|
|
|
- private String getResult(HttpURLConnection conn) throws IOException {
|
|
|
|
-
|
|
|
|
- StringBuilder text = new StringBuilder();
|
|
|
|
-
|
|
|
|
- InputStream is = null;
|
|
|
|
- InputStreamReader sr = null;
|
|
|
|
- BufferedReader br = null;
|
|
|
|
-
|
|
|
|
- int code = conn.getResponseCode();
|
|
|
|
-
|
|
|
|
- try {
|
|
|
|
- is = code >= 400 ? conn.getErrorStream() : conn.getInputStream();
|
|
|
|
-
|
|
|
|
- sr = new InputStreamReader(is, DEFAULT_CHARSET);
|
|
|
|
- br = new BufferedReader(sr);
|
|
|
|
-
|
|
|
|
- char[] chars = new char[4096];
|
|
|
|
- int length = 0;
|
|
|
|
-
|
|
|
|
- while ((length = br.read(chars)) != -1) {
|
|
|
|
- text.append(chars, 0, length);
|
|
|
|
- }
|
|
|
|
- } finally {
|
|
|
|
- if (br != null) {
|
|
|
|
- br.close();
|
|
|
|
- br = null;
|
|
|
|
- }
|
|
|
|
- if (sr != null) {
|
|
|
|
- sr.close();
|
|
|
|
- sr = null;
|
|
|
|
- }
|
|
|
|
- if (is != null) {
|
|
|
|
- is.close();
|
|
|
|
- is = null;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- if (code >= 400) {
|
|
|
|
- throw new IOException(text.toString());
|
|
|
|
- }
|
|
|
|
- return text.toString();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public AppLicense getAppInfo() {
|
|
|
|
- String str = this.httpAction(null, null);
|
|
|
|
- AppLicense appInfo = JSONObject.parseObject(str, AppLicense.class);
|
|
|
|
- return appInfo;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public List<SchoolEntity> getOrgs() {
|
|
|
|
- List<SchoolEntity> orgs = new ArrayList<SchoolEntity>();
|
|
|
|
- int pageNumber = 1;
|
|
|
|
- int pageSize = 100;
|
|
|
|
- for (;;) {
|
|
|
|
- Map<String, Object> datas = new HashMap<String, Object>();
|
|
|
|
- datas.put("pageNumber", pageNumber);
|
|
|
|
- datas.put("pageSize", pageSize);
|
|
|
|
- String str = this.httpAction(null, datas);
|
|
|
|
- JSONArray orgArray = JSONArray.parseArray(str);
|
|
|
|
- for (int i = 0; i < orgArray.size(); i++) {
|
|
|
|
- SchoolEntity school = JSONObject.parseObject(orgArray.getString(i), SchoolEntity.class);
|
|
|
|
- orgs.add(school);
|
|
|
|
- }
|
|
|
|
- if (orgArray.size() != pageSize) {
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
- pageNumber++;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return orgs;
|
|
|
|
- }
|
|
|
|
-}
|
|
|