Browse Source

无JIRA任务 去掉httpclient的使用

bugfix/10.0
richie 6 years ago
parent
commit
b6f567dab6
  1. 52
      designer-realize/src/main/java/com/fr/design/mainframe/errorinfo/ErrorInfoUploader.java

52
designer-realize/src/main/java/com/fr/design/mainframe/errorinfo/ErrorInfoUploader.java

@ -3,15 +3,16 @@ package com.fr.design.mainframe.errorinfo;
import com.fr.base.FRContext; import com.fr.base.FRContext;
import com.fr.design.mainframe.SiteCenterToken; import com.fr.design.mainframe.SiteCenterToken;
import com.fr.general.CloudCenter; import com.fr.general.CloudCenter;
import com.fr.general.ComparatorUtils; import com.fr.general.CommonIOUtils;
import com.fr.general.GeneralContext; import com.fr.general.GeneralContext;
import com.fr.general.IOUtils; import com.fr.general.IOUtils;
import com.fr.general.http.HttpClient; import com.fr.general.http.HttpResponseType;
import com.fr.json.JSONException; import com.fr.general.http.HttpToolbox;
import com.fr.json.JSONObject; import com.fr.json.JSONObject;
import com.fr.license.function.VT4FR; import com.fr.license.function.VT4FR;
import com.fr.log.FineLoggerFactory; import com.fr.log.FineLoggerFactory;
import com.fr.log.LogHandler; import com.fr.log.LogHandler;
import com.fr.stable.ArrayUtils;
import com.fr.stable.EnvChangedListener; import com.fr.stable.EnvChangedListener;
import com.fr.stable.ProductConstants; import com.fr.stable.ProductConstants;
import com.fr.stable.StableUtils; import com.fr.stable.StableUtils;
@ -93,9 +94,7 @@ public class ErrorInfoUploader {
try { try {
String downloadURL = CloudCenter.getInstance().acquireUrlByKind("solution.download", "http://cloud.fanruan.com/api/solution"); String downloadURL = CloudCenter.getInstance().acquireUrlByKind("solution.download", "http://cloud.fanruan.com/api/solution");
downloadURL = String.format("%s?token=%s", downloadURL, SiteCenterToken.generateToken()); downloadURL = String.format("%s?token=%s", downloadURL, SiteCenterToken.generateToken());
HttpClient hc = new HttpClient(downloadURL); InputStream in = HttpToolbox.post(downloadURL, new HashMap<String, Object>(), HttpResponseType.STREAM);
hc.asGet();
InputStream in = hc.getResponseStream();
StableUtils.makesureFileExist(localCacheZip); StableUtils.makesureFileExist(localCacheZip);
FileOutputStream out = new FileOutputStream(localCacheZip); FileOutputStream out = new FileOutputStream(localCacheZip);
IOUtils.copyBinaryTo(in, out); IOUtils.copyBinaryTo(in, out);
@ -114,15 +113,11 @@ public class ErrorInfoUploader {
// 判断本地文件大小. // 判断本地文件大小.
String checkURL = CloudCenter.getInstance().acquireUrlByKind("solution.check", "http://cloud.fanruan.com/api/solution/cache/check"); String checkURL = CloudCenter.getInstance().acquireUrlByKind("solution.check", "http://cloud.fanruan.com/api/solution/cache/check");
checkURL = String.format("%s?token=%s", checkURL, SiteCenterToken.generateToken()); checkURL = String.format("%s?token=%s", checkURL, SiteCenterToken.generateToken());
HttpClient client = new HttpClient(checkURL);
client.asGet();
if (client.isServerAlive()) {
try { try {
JSONObject res = new JSONObject(client.getResponseText()); JSONObject res = new JSONObject(HttpToolbox.get(checkURL));
// 简单粗暴, 直接判断文件大小. // 简单粗暴, 直接判断文件大小.
return res.optLong("version") != localCacheZip.length(); return res.optLong("version") != localCacheZip.length();
} catch (JSONException ignore) { } catch (Exception ignore) {
}
} }
return false; return false;
} }
@ -140,12 +135,13 @@ public class ErrorInfoUploader {
} }
File[] files = folder.listFiles(); File[] files = folder.listFiles();
if (files.length > MAX_ERROR_SIZE) { if (ArrayUtils.getLength(files) > MAX_ERROR_SIZE) {
StableUtils.deleteFile(folder); CommonIOUtils.deleteFile(folder);
return; return;
} }
try { try {
if (ArrayUtils.isNotEmpty(files)) {
for (File file : files) { for (File file : files) {
String filePath = file.getPath(); String filePath = file.getPath();
String suffix = filePath.substring(filePath.lastIndexOf(".")); String suffix = filePath.substring(filePath.lastIndexOf("."));
@ -154,13 +150,14 @@ public class ErrorInfoUploader {
Thread.sleep(1000L); Thread.sleep(1000L);
String content = IOUtils.inputStream2String(new FileInputStream(file)); String content = IOUtils.inputStream2String(new FileInputStream(file));
if (content.length() > MAX_ERROR_SIZE) { if (content.length() > MAX_ERROR_SIZE) {
file.delete(); CommonIOUtils.deleteFile(file);
continue; continue;
} }
String url = CloudCenter.getInstance().acquireUrlByKind("design.error"); String url = CloudCenter.getInstance().acquireUrlByKind("design.error");
if (sendErroInfo(url, content)) { if (sendErrorInfo(url, content)) {
file.delete(); CommonIOUtils.deleteFile(file);
}
} }
} }
} }
@ -169,25 +166,18 @@ public class ErrorInfoUploader {
} }
} }
private boolean sendErroInfo(String url, String content) { private boolean sendErrorInfo(String url, String content) {
HashMap<String, String> para = new HashMap<>(); HashMap<String, Object> para = new HashMap<>();
para.put("token", SiteCenterToken.generateToken()); para.put("token", SiteCenterToken.generateToken());
para.put("content", content); para.put("content", content);
HttpClient httpClient = new HttpClient(url, para, true);
httpClient.asGet();
if (!httpClient.isServerAlive()) {
return false;
}
String res = httpClient.getResponseText();
boolean success;
try { try {
success = ComparatorUtils.equals(new JSONObject(res).get("status"), "success"); String responseText = HttpToolbox.post(url, para);
} catch (Exception ex) { return "success".equals(new JSONObject(responseText).get("status"));
success = true; } catch (Exception ignore) {
} }
return success; return false;
} }

Loading…
Cancel
Save