From b6f567dab66065b2a89db0ffcfb0d1427147fb6b Mon Sep 17 00:00:00 2001 From: richie Date: Tue, 25 Dec 2018 10:10:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A0JIRA=E4=BB=BB=E5=8A=A1=20=E5=8E=BB?= =?UTF-8?q?=E6=8E=89httpclient=E7=9A=84=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../errorinfo/ErrorInfoUploader.java | 82 ++++++++----------- 1 file changed, 36 insertions(+), 46 deletions(-) diff --git a/designer-realize/src/main/java/com/fr/design/mainframe/errorinfo/ErrorInfoUploader.java b/designer-realize/src/main/java/com/fr/design/mainframe/errorinfo/ErrorInfoUploader.java index 4d59644262..42298b8123 100644 --- a/designer-realize/src/main/java/com/fr/design/mainframe/errorinfo/ErrorInfoUploader.java +++ b/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.design.mainframe.SiteCenterToken; import com.fr.general.CloudCenter; -import com.fr.general.ComparatorUtils; +import com.fr.general.CommonIOUtils; import com.fr.general.GeneralContext; import com.fr.general.IOUtils; -import com.fr.general.http.HttpClient; -import com.fr.json.JSONException; +import com.fr.general.http.HttpResponseType; +import com.fr.general.http.HttpToolbox; import com.fr.json.JSONObject; import com.fr.license.function.VT4FR; import com.fr.log.FineLoggerFactory; import com.fr.log.LogHandler; +import com.fr.stable.ArrayUtils; import com.fr.stable.EnvChangedListener; import com.fr.stable.ProductConstants; import com.fr.stable.StableUtils; @@ -93,9 +94,7 @@ public class ErrorInfoUploader { try { String downloadURL = CloudCenter.getInstance().acquireUrlByKind("solution.download", "http://cloud.fanruan.com/api/solution"); downloadURL = String.format("%s?token=%s", downloadURL, SiteCenterToken.generateToken()); - HttpClient hc = new HttpClient(downloadURL); - hc.asGet(); - InputStream in = hc.getResponseStream(); + InputStream in = HttpToolbox.post(downloadURL, new HashMap(), HttpResponseType.STREAM); StableUtils.makesureFileExist(localCacheZip); FileOutputStream out = new FileOutputStream(localCacheZip); 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"); checkURL = String.format("%s?token=%s", checkURL, SiteCenterToken.generateToken()); - HttpClient client = new HttpClient(checkURL); - client.asGet(); - if (client.isServerAlive()) { - try { - JSONObject res = new JSONObject(client.getResponseText()); - // 简单粗暴, 直接判断文件大小. - return res.optLong("version") != localCacheZip.length(); - } catch (JSONException ignore) { - } + try { + JSONObject res = new JSONObject(HttpToolbox.get(checkURL)); + // 简单粗暴, 直接判断文件大小. + return res.optLong("version") != localCacheZip.length(); + } catch (Exception ignore) { } return false; } @@ -140,27 +135,29 @@ public class ErrorInfoUploader { } File[] files = folder.listFiles(); - if (files.length > MAX_ERROR_SIZE) { - StableUtils.deleteFile(folder); + if (ArrayUtils.getLength(files) > MAX_ERROR_SIZE) { + CommonIOUtils.deleteFile(folder); return; } try { - for (File file : files) { - String filePath = file.getPath(); - String suffix = filePath.substring(filePath.lastIndexOf(".")); - - if (suffix.endsWith(SUFFIX)) { - Thread.sleep(1000L); - String content = IOUtils.inputStream2String(new FileInputStream(file)); - if (content.length() > MAX_ERROR_SIZE) { - file.delete(); - continue; - } - - String url = CloudCenter.getInstance().acquireUrlByKind("design.error"); - if (sendErroInfo(url, content)) { - file.delete(); + if (ArrayUtils.isNotEmpty(files)) { + for (File file : files) { + String filePath = file.getPath(); + String suffix = filePath.substring(filePath.lastIndexOf(".")); + + if (suffix.endsWith(SUFFIX)) { + Thread.sleep(1000L); + String content = IOUtils.inputStream2String(new FileInputStream(file)); + if (content.length() > MAX_ERROR_SIZE) { + CommonIOUtils.deleteFile(file); + continue; + } + + String url = CloudCenter.getInstance().acquireUrlByKind("design.error"); + if (sendErrorInfo(url, content)) { + CommonIOUtils.deleteFile(file); + } } } } @@ -169,25 +166,18 @@ public class ErrorInfoUploader { } } - private boolean sendErroInfo(String url, String content) { - HashMap para = new HashMap<>(); + private boolean sendErrorInfo(String url, String content) { + HashMap para = new HashMap<>(); para.put("token", SiteCenterToken.generateToken()); 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 { - success = ComparatorUtils.equals(new JSONObject(res).get("status"), "success"); - } catch (Exception ex) { - success = true; + String responseText = HttpToolbox.post(url, para); + return "success".equals(new JSONObject(responseText).get("status")); + } catch (Exception ignore) { + } - return success; + return false; }