Browse Source

设计器回传zip到云中心,生成回调消息到kafka

bugfix/10.0
alex.sung 6 years ago
parent
commit
411c9cd424
  1. 86
      designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/entity/FileEntityBuilder.java
  2. 8
      designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/impl/FocusPointMessageUploader.java

86
designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/entity/FileEntityBuilder.java

@ -1,24 +1,20 @@
package com.fr.design.mainframe.messagecollect.entity; 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.CloudCenter;
import com.fr.general.CloudClient;
import com.fr.general.IOUtils; import com.fr.general.IOUtils;
import com.fr.general.http.HttpToolbox; import com.fr.general.http.HttpToolbox;
import com.fr.json.JSONArray; import com.fr.json.JSONArray;
import com.fr.json.JSONException;
import com.fr.json.JSONObject; import com.fr.json.JSONObject;
import com.fr.log.FineLoggerFactory; import com.fr.log.FineLoggerFactory;
import com.fr.stable.CommonUtils; import com.fr.stable.CommonUtils;
import com.fr.stable.CoreConstants;
import com.fr.stable.EncodeConstants; import com.fr.stable.EncodeConstants;
import com.fr.stable.StableUtils; import com.fr.stable.StableUtils;
import com.fr.stable.StringUtils; import com.fr.stable.StringUtils;
import com.fr.third.jodd.datetime.JDateTime; 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.ByteArrayInputStream;
import java.io.File; import java.io.File;
@ -26,22 +22,20 @@ import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map;
import java.util.UUID; import java.util.UUID;
import static com.fr.third.org.apache.http.HttpStatus.SC_OK;
/** /**
* @author alex sung * @author alex sung
* @date 2019/4/8 * @date 2019/4/8
*/ */
public class FileEntityBuilder { 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_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) { public File generateZipFile(String pathName) {
if (pathName == null) {
return null;
}
File zipFile = null; File zipFile = null;
try { try {
zipFile = new File(pathName + ".zip"); zipFile = new File(pathName + ".zip");
@ -100,53 +97,40 @@ public class FileEntityBuilder {
/** /**
* 上传文件到云中心 * 上传文件到云中心
*
* @param file 待上传文件 * @param file 待上传文件
* @param keyFileName 目标文件 * @param keyFileName 目标文件
* @throws IOException * @throws IOException
*/ */
public static void uploadFile(File file, String 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"); String today = new JDateTime().toString("YYYY-MM-DD");
HttpClient httpclient = new DefaultHttpClient(); String filePath = FOCUS_POINT_FILE_ROOT_PATH + CoreConstants.SEPARATOR + today + CoreConstants.SEPARATOR + keyFileName;
try { String bbsUserName = MarketConfig.getInstance().getBbsUsername();
String signedUrl = generateSignedUploadUrl(FOCUS_POINT_FILE_ROOT_PATH + File.separator + today + File.separator +keyFileName); String uuid = DesignerEnvManager.getEnvManager().getUUID();
if(StringUtils.isEmpty(signedUrl)){ String name = bbsUserName == null ? uuid : bbsUserName;
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);
int statusCode = response.getStatusLine().getStatusCode(); client.uploadFile(file, filePath, name, FILE_FROM);
if (statusCode == SC_OK) { addMessageQueue(filePath, bbsUserName, uuid);
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);
}
} }
private static String generateSignedUploadUrl(String fileKeyName) throws IOException { private static void addMessageQueue(String filePath, String userName, String uuid) {
String url = CloudCenter.getInstance().acquireUrlByKind(INTELLI_OPERATION_URL, OPERATION_URL); JSONObject uploadInfo = new JSONObject(FOCUS_POINT_FILE_UPLOAD_URL);
Map<String, String> parameters = new HashMap<String, String>(); String focusPointUrl = uploadInfo.optString(FOCUS_POINT_URL_KEY);
parameters.put(ATTR_KEY, fileKeyName);
parameters.put(ATTR_SIGNATURE, String.valueOf(CommonUtils.signature()));
String responseText = HttpToolbox.get(url, parameters);
try { try {
JSONObject data = new JSONObject(responseText); HashMap<String, Object> params = new HashMap<>();
if ("success".equals(data.optString("status"))) { params.put("topic", FOCUS_POINT_FILE_UPLOAD_TOPIC);
return data.optString("url"); 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) { } catch (Exception e) {
FineLoggerFactory.getLogger().error("Illegal response text."+e, e.getMessage()); FineLoggerFactory.getLogger().error(e.getMessage(), e);
} }
return null;
} }
} }

8
designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/impl/FocusPointMessageUploader.java

@ -1,7 +1,5 @@
package com.fr.design.mainframe.messagecollect.impl; 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.entity.FileEntityBuilder;
import com.fr.design.mainframe.messagecollect.utils.MessageCollectUtils; import com.fr.design.mainframe.messagecollect.utils.MessageCollectUtils;
import com.fr.intelli.record.FocusPoint; import com.fr.intelli.record.FocusPoint;
@ -91,15 +89,11 @@ public class FocusPointMessageUploader extends AbstractSendDataToCloud {
} }
private void generatePath() { private void generatePath() {
DesignerEnvManager envManager = DesignerEnvManager.getEnvManager(); //文件夹名称的格式是: "FocusPoint" + 大版本号 + 小版本号 + randomUuid,均以下划线分隔
String bbsUserName = MarketConfig.getInstance().getBbsUsername();
String uuid = envManager.getUUID();
//文件夹名称的格式是: "FocusPoint" + 大版本号 + 小版本号 + uuid + bbsUserName + randomUuid,均以下划线分隔
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append(FOCUS_POINT).append(SEPARATOR). sb.append(FOCUS_POINT).append(SEPARATOR).
append(ProductConstants.MAIN_VERSION).append(SEPARATOR). append(ProductConstants.MAIN_VERSION).append(SEPARATOR).
append(ProductConstants.MINOR_VERSION).append(SEPARATOR). append(ProductConstants.MINOR_VERSION).append(SEPARATOR).
append(uuid).append(SEPARATOR).append(bbsUserName).append(SEPARATOR).
append(UUID.randomUUID()); append(UUID.randomUUID());
String folderName = StableUtils.pathJoin(ProductConstants.getEnvHome(), sb.toString()); String folderName = StableUtils.pathJoin(ProductConstants.getEnvHome(), sb.toString());
setFileEntityBuilder(new FileEntityBuilder(folderName)); setFileEntityBuilder(new FileEntityBuilder(folderName));

Loading…
Cancel
Save