From 411c9cd424f64509a5625430f6c8eb9c8008cc47 Mon Sep 17 00:00:00 2001 From: "alex.sung" Date: Wed, 8 May 2019 16:16:10 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=BE=E8=AE=A1=E5=99=A8=E5=9B=9E=E4=BC=A0zi?= =?UTF-8?q?p=E5=88=B0=E4=BA=91=E4=B8=AD=E5=BF=83=EF=BC=8C=E7=94=9F?= =?UTF-8?q?=E6=88=90=E5=9B=9E=E8=B0=83=E6=B6=88=E6=81=AF=E5=88=B0kafka?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../entity/FileEntityBuilder.java | 88 ++++++++----------- .../impl/FocusPointMessageUploader.java | 8 +- 2 files changed, 37 insertions(+), 59 deletions(-) diff --git a/designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/entity/FileEntityBuilder.java b/designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/entity/FileEntityBuilder.java index 2555a2632..99ba2c5f7 100644 --- a/designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/entity/FileEntityBuilder.java +++ b/designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/entity/FileEntityBuilder.java @@ -1,24 +1,20 @@ package com.fr.design.mainframe.messagecollect.entity; +import com.fr.config.MarketConfig; +import com.fr.design.DesignerEnvManager; import com.fr.general.CloudCenter; +import com.fr.general.CloudClient; import com.fr.general.IOUtils; import com.fr.general.http.HttpToolbox; import com.fr.json.JSONArray; -import com.fr.json.JSONException; import com.fr.json.JSONObject; import com.fr.log.FineLoggerFactory; import com.fr.stable.CommonUtils; +import com.fr.stable.CoreConstants; import com.fr.stable.EncodeConstants; import com.fr.stable.StableUtils; import com.fr.stable.StringUtils; import com.fr.third.jodd.datetime.JDateTime; -import com.fr.third.org.apache.http.HttpEntity; -import com.fr.third.org.apache.http.HttpResponse; -import com.fr.third.org.apache.http.client.HttpClient; -import com.fr.third.org.apache.http.client.methods.HttpPut; -import com.fr.third.org.apache.http.entity.FileEntity; -import com.fr.third.org.apache.http.impl.client.DefaultHttpClient; -import com.fr.third.org.apache.http.util.EntityUtils; import java.io.ByteArrayInputStream; import java.io.File; @@ -26,22 +22,20 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; -import java.util.Map; import java.util.UUID; -import static com.fr.third.org.apache.http.HttpStatus.SC_OK; - /** * @author alex sung * @date 2019/4/8 */ public class FileEntityBuilder { - private static final String INTELLI_OPERATION_URL = "intelli.operation.url"; - private static final String OPERATION_URL = "https://cloud.fanruan.com/config/protect/operation"; - private static final String ATTR_SIGNATURE = "signature"; - private static final String ATTR_KEY = "key"; private static final String FOCUS_POINT_FILE_ROOT_PATH = "FocusPoint"; + private static final String FOCUS_POINT_FILE_UPLOAD_TOPIC = "__fine_intelli_treasure_upload__"; + private static final String FILE_FROM = "design"; + private static final String FOCUS_POINT_FILE_UPLOAD_TYPE = "FocusPoint"; + private static final String FOCUS_POINT_FILE_UPLOAD_URL = CloudCenter.getInstance().acquireUrlByKind("design.feedback"); + private static final String FOCUS_POINT_URL_KEY = "focuspoint"; /** * 文件夹路径 @@ -61,6 +55,9 @@ public class FileEntityBuilder { } public File generateZipFile(String pathName) { + if (pathName == null) { + return null; + } File zipFile = null; try { zipFile = new File(pathName + ".zip"); @@ -100,53 +97,40 @@ public class FileEntityBuilder { /** * 上传文件到云中心 - * @param file 待上传文件 + * + * @param file 待上传文件 * @param keyFileName 目标文件 * @throws IOException */ public static void uploadFile(File file, String keyFileName) throws IOException { + CloudClient client = CloudClient.getInstance(); String today = new JDateTime().toString("YYYY-MM-DD"); - HttpClient httpclient = new DefaultHttpClient(); - try { - String signedUrl = generateSignedUploadUrl(FOCUS_POINT_FILE_ROOT_PATH + File.separator + today + File.separator +keyFileName); - if(StringUtils.isEmpty(signedUrl)){ - FineLoggerFactory.getLogger().error("signedUrl is null."); - return; - } - HttpPut httpPost = new HttpPut(signedUrl); - httpPost.addHeader("Content-Type","application/octet-stream"); - FileEntity fileEntity = new FileEntity(file); - httpPost.setEntity(fileEntity); - HttpResponse response = httpclient.execute(httpPost); + String filePath = FOCUS_POINT_FILE_ROOT_PATH + CoreConstants.SEPARATOR + today + CoreConstants.SEPARATOR + keyFileName; + String bbsUserName = MarketConfig.getInstance().getBbsUsername(); + String uuid = DesignerEnvManager.getEnvManager().getUUID(); + String name = bbsUserName == null ? uuid : bbsUserName; - int statusCode = response.getStatusLine().getStatusCode(); - if (statusCode == SC_OK) { - HttpEntity resEntity = response.getEntity(); - EntityUtils.consume(resEntity); - } else { - HttpEntity entity = response.getEntity(); - String result = EntityUtils.toString(entity, "utf-8"); - FineLoggerFactory.getLogger().info("upload file result:" + result); - } - } catch (Exception e) { - FineLoggerFactory.getLogger().error(e.getMessage(), e); - } + client.uploadFile(file, filePath, name, FILE_FROM); + addMessageQueue(filePath, bbsUserName, uuid); } - private static String generateSignedUploadUrl(String fileKeyName) throws IOException { - String url = CloudCenter.getInstance().acquireUrlByKind(INTELLI_OPERATION_URL, OPERATION_URL); - Map parameters = new HashMap(); - parameters.put(ATTR_KEY, fileKeyName); - parameters.put(ATTR_SIGNATURE, String.valueOf(CommonUtils.signature())); - String responseText = HttpToolbox.get(url, parameters); + private static void addMessageQueue(String filePath, String userName, String uuid) { + JSONObject uploadInfo = new JSONObject(FOCUS_POINT_FILE_UPLOAD_URL); + String focusPointUrl = uploadInfo.optString(FOCUS_POINT_URL_KEY); try { - JSONObject data = new JSONObject(responseText); - if ("success".equals(data.optString("status"))) { - return data.optString("url"); + HashMap params = new HashMap<>(); + params.put("topic", FOCUS_POINT_FILE_UPLOAD_TOPIC); + params.put("username", userName); + params.put("uuid", uuid); + params.put("filepath", filePath); + params.put("timestamp", String.valueOf(System.currentTimeMillis())); + params.put("signature", String.valueOf(CommonUtils.signature())); + params.put("type", FOCUS_POINT_FILE_UPLOAD_TYPE); + if(StringUtils.isNotEmpty(focusPointUrl)){ + HttpToolbox.post(focusPointUrl, params); } - } catch (JSONException e) { - FineLoggerFactory.getLogger().error("Illegal response text."+e, e.getMessage()); + } catch (Exception e) { + FineLoggerFactory.getLogger().error(e.getMessage(), e); } - return null; } } diff --git a/designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/impl/FocusPointMessageUploader.java b/designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/impl/FocusPointMessageUploader.java index 33adf8e2d..1175c7c6e 100644 --- a/designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/impl/FocusPointMessageUploader.java +++ b/designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/impl/FocusPointMessageUploader.java @@ -1,7 +1,5 @@ package com.fr.design.mainframe.messagecollect.impl; -import com.fr.config.MarketConfig; -import com.fr.design.DesignerEnvManager; import com.fr.design.mainframe.messagecollect.entity.FileEntityBuilder; import com.fr.design.mainframe.messagecollect.utils.MessageCollectUtils; import com.fr.intelli.record.FocusPoint; @@ -91,15 +89,11 @@ public class FocusPointMessageUploader extends AbstractSendDataToCloud { } private void generatePath() { - DesignerEnvManager envManager = DesignerEnvManager.getEnvManager(); - String bbsUserName = MarketConfig.getInstance().getBbsUsername(); - String uuid = envManager.getUUID(); - //文件夹名称的格式是: "FocusPoint" + 大版本号 + 小版本号 + uuid + bbsUserName + randomUuid,均以下划线分隔 + //文件夹名称的格式是: "FocusPoint" + 大版本号 + 小版本号 + randomUuid,均以下划线分隔 StringBuilder sb = new StringBuilder(); sb.append(FOCUS_POINT).append(SEPARATOR). append(ProductConstants.MAIN_VERSION).append(SEPARATOR). append(ProductConstants.MINOR_VERSION).append(SEPARATOR). - append(uuid).append(SEPARATOR).append(bbsUserName).append(SEPARATOR). append(UUID.randomUUID()); String folderName = StableUtils.pathJoin(ProductConstants.getEnvHome(), sb.toString()); setFileEntityBuilder(new FileEntityBuilder(folderName));