From 5e9f60c5d7b9a50b5e34239611935e6325051257 Mon Sep 17 00:00:00 2001 From: 1 <1@qq> Date: Fri, 5 Apr 2019 09:22:25 +0800 Subject: [PATCH 1/7] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=8F=96=E5=A0=86?= =?UTF-8?q?=E6=A0=88=E8=AE=B0=E5=BD=95=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../design/mainframe/errorinfo/ErrorInfoLogAppender.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/designer-realize/src/main/java/com/fr/design/mainframe/errorinfo/ErrorInfoLogAppender.java b/designer-realize/src/main/java/com/fr/design/mainframe/errorinfo/ErrorInfoLogAppender.java index f0852abfb..bcdea3e5a 100644 --- a/designer-realize/src/main/java/com/fr/design/mainframe/errorinfo/ErrorInfoLogAppender.java +++ b/designer-realize/src/main/java/com/fr/design/mainframe/errorinfo/ErrorInfoLogAppender.java @@ -82,12 +82,10 @@ public class ErrorInfoLogAppender extends AppenderSkeleton { private String readStackTrace(LoggingEvent event) { String[] s = event.getThrowableStrRep(); StringBuilder sb = new StringBuilder(); - if(s != null){ - int len = s.length; + if (s != null) { + int len = Math.min(s.length, ERROR_STACK_TRACE); for (int i = 0; i < len; i++) { - if(i < ERROR_STACK_TRACE){ - sb.append(s[i]).append("\n"); - } + sb.append(s[i]).append("\n"); } } return sb.toString(); From 633c0c67dfee381365d522d22e0f1399c4aea2be Mon Sep 17 00:00:00 2001 From: 1 <1@qq> Date: Fri, 5 Apr 2019 09:28:31 +0800 Subject: [PATCH 2/7] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E8=BF=9E=E6=8E=A5?= =?UTF-8?q?=E5=9F=8B=E7=82=B9=E5=8F=8A=E5=9B=9E=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mainframe/InformationCollector.java | 262 +----------------- .../SendDataToCloudProvider.java | 30 ++ .../impl/AbstractSendDataToCloud.java | 247 +++++++++++++++++ .../impl/FocusPointMessageUploader.java | 86 ++++++ .../utils/MessageCollectUtils.java | 69 +++++ 5 files changed, 439 insertions(+), 255 deletions(-) create mode 100644 designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/SendDataToCloudProvider.java create mode 100644 designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/impl/AbstractSendDataToCloud.java create mode 100644 designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/impl/FocusPointMessageUploader.java create mode 100644 designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/utils/MessageCollectUtils.java diff --git a/designer-realize/src/main/java/com/fr/design/mainframe/InformationCollector.java b/designer-realize/src/main/java/com/fr/design/mainframe/InformationCollector.java index df9b1b556..9d04cc185 100644 --- a/designer-realize/src/main/java/com/fr/design/mainframe/InformationCollector.java +++ b/designer-realize/src/main/java/com/fr/design/mainframe/InformationCollector.java @@ -7,6 +7,7 @@ import com.fr.base.FRContext; import com.fr.config.MarketConfig; import com.fr.design.DesignerEnvManager; import com.fr.design.mainframe.errorinfo.ErrorInfoUploader; +import com.fr.design.mainframe.messagecollect.impl.FocusPointMessageUploader; import com.fr.design.mainframe.templateinfo.TemplateInfoCollector; import com.fr.general.CloudCenter; import com.fr.general.ComparatorUtils; @@ -15,20 +16,13 @@ import com.fr.general.DesUtils; import com.fr.general.GeneralUtils; import com.fr.general.IOUtils; import com.fr.general.http.HttpToolbox; -import com.fr.intelli.record.FocusPoint; -import com.fr.intelli.record.MetricRegistry; import com.fr.json.JSONArray; -import com.fr.json.JSONException; import com.fr.json.JSONObject; import com.fr.log.FineLoggerFactory; import com.fr.stable.EncodeConstants; import com.fr.stable.ProductConstants; import com.fr.stable.StableUtils; import com.fr.stable.StringUtils; -import com.fr.stable.query.QueryFactory; -import com.fr.stable.query.condition.QueryCondition; -import com.fr.stable.query.data.DataList; -import com.fr.stable.query.restriction.RestrictionFactory; import com.fr.stable.xml.XMLPrintWriter; import com.fr.stable.xml.XMLReadable; import com.fr.stable.xml.XMLTools; @@ -53,8 +47,6 @@ import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; -import java.util.Map; - /** * @author neil * @@ -64,7 +56,7 @@ public class InformationCollector implements XMLReadable, XMLWriter { // 24小时上传一次 private static final long DELTA = 24 * 3600 * 1000L; - private static final long SEND_DELAY = 300 * 1000L; + private static final long SEND_DELAY = 30 * 1000L; private static final String FILE_NAME = "fr.info"; private static final String XML_START_STOP_LIST = "StartStopList"; private static final String XML_START_STOP = "StartStop"; @@ -78,24 +70,6 @@ public class InformationCollector implements XMLReadable, XMLWriter { private static final String XML_KEY = "ActiveKey"; private static final String XML_OS = "OS"; - public static final String TABLE_NAME = "fr_functionrecord"; - public static final String FUNC_COLUMNNAME = "func"; - public static final String COLUMN_TIME = "time"; - public static final String TABLE_FUNCTION_RECORD = "function.record"; - private static final String ATTR_ID = "id"; - private static final String ATTR_TEXT = "text"; - private static final String ATTR_SOURCE = "source"; - private static final String ATTR_TIME = "time"; - private static final String ATTR_TIMES = "times"; - private static final String ATTR_TITLE = "title"; - private static final String ATTR_USER_NAME = "username"; - private static final String ATTR_UUID = "uuid"; - private static final String ATTR_ITEMS = "items"; - private static final String ATTR_FUNCTION_ARRAY = "functionArray"; - private static final int MAX_EACH_REQUEST_RECORD_COUNT = 200; - private static final int PAGE_SIZE = 200; - private long totalCount = -1; - private static InformationCollector collector; //启动时间与关闭时间列表 @@ -204,141 +178,9 @@ public class InformationCollector implements XMLReadable, XMLWriter { } } - private void sendFunctionsInfo(){ - Date current = new Date(); - long lastTime = getLastTimeMillis(); - long currentTime = current.getTime(); - if (currentTime - lastTime <= DELTA) { - return; - } - FineLoggerFactory.getLogger().info("Start sent function records to the cloud center..."); - queryAndSendOnePageFunctionContent(currentTime, lastTime, 0); - long page = (totalCount/PAGE_SIZE) + 1; - for(int i=1; i focusPoints = MetricRegistry.getMetric().find(FocusPoint.class,condition); - //第一次查询获取总记录数 - if(page == 0){ - totalCount = focusPoints.getTotalCount(); - } - sendThisPageFunctionContent(focusPoints); - } catch (Exception e) { - FineLoggerFactory.getLogger().error(e.getMessage(), e); - } - } - - private void sendThisPageFunctionContent(DataList focusPoints) { - String url = CloudCenter.getInstance().acquireUrlByKind(TABLE_FUNCTION_RECORD); - try { - JSONObject jsonObject = dealWithSendFunctionContent(focusPoints); - sendFunctionRecord(url, jsonObject); - } catch (JSONException e) { - FineLoggerFactory.getLogger().error(e.getMessage(), e); - } - } - - private JSONObject dealWithSendFunctionContent(DataList focusPoints) throws JSONException { - JSONObject jsonObject = new JSONObject(); - Map map = new HashMap<>(); - if(!focusPoints.isEmpty()){ - DesignerEnvManager envManager = DesignerEnvManager.getEnvManager(); - String bbsUserName = MarketConfig.getInstance().getBbsUsername(); - String uuid = envManager.getUUID(); - jsonObject.put(ATTR_UUID, uuid); - jsonObject.put(ATTR_USER_NAME, bbsUserName); - for(FocusPoint focusPoint : focusPoints.getList()) { - FunctionRecord functionRecord = getOneRecord(focusPoint); - if (map.containsKey(focusPoint.getId())) { - int times = ((FunctionRecord)map.get(focusPoint.getId())).getTimes() + 1; - functionRecord.setTimes(times); - map.put(focusPoint.getId(), functionRecord); - } else { - map.put(focusPoint.getId(), functionRecord); - } - } - jsonObject.put(ATTR_ITEMS, mapToJSONArray(map)); - } - return jsonObject; - } - - private JSONArray mapToJSONArray(Map map) throws JSONException { - JSONArray jsonArray = new JSONArray(); - for(String keys : map.keySet()){ - FunctionRecord functionRecord = (FunctionRecord)map.get(keys); - JSONObject jo = new JSONObject(); - jo.put(ATTR_ID, functionRecord.getId()); - jo.put(ATTR_TEXT, functionRecord.getText()); - jo.put(ATTR_SOURCE, functionRecord.getSource()); - jo.put(ATTR_TIME, functionRecord.getTime()); - jo.put(ATTR_TITLE, functionRecord.getTitle()); - jo.put(ATTR_TIMES, functionRecord.getTimes()); - jsonArray.put(jo); - } - return jsonArray; - } - - private void sendFunctionRecord(String url, JSONObject record) { - boolean success = false; - try { - HashMap para = new HashMap<>(); - para.put("token", SiteCenterToken.generateToken()); - para.put("content", record); - String res = HttpToolbox.post(url, para); - success = ComparatorUtils.equals(new JSONObject(res).get("status"), "success"); - if (success) { - this.lastTime = dateToString(); - } else { - FineLoggerFactory.getLogger().error("Error occured when sent function records to the cloud center."); - } - } catch (Exception e) { - FineLoggerFactory.getLogger().error(e.getMessage(), e); - } - } - - private FunctionRecord getOneRecord(FocusPoint focusPoint) { - FunctionRecord functionRecord = new FunctionRecord(); - functionRecord.setId(focusPoint.getId() == null?StringUtils.EMPTY : focusPoint.getId()); - functionRecord.setText(focusPoint.getText() == null?StringUtils.EMPTY : focusPoint.getText()); - functionRecord.setSource(focusPoint.getSource()); - functionRecord.setTime(focusPoint.getTime().getTime()); - functionRecord.setTitle(focusPoint.getTitle() == null?StringUtils.EMPTY : focusPoint.getTitle()); - functionRecord.setUsername(MarketConfig.getInstance().getBbsUsername() == null?StringUtils.EMPTY : MarketConfig.getInstance().getBbsUsername()); - functionRecord.setUuid(DesignerEnvManager.getEnvManager().getUUID() == null?StringUtils.EMPTY : DesignerEnvManager.getEnvManager().getUUID()); - return functionRecord; - } - - /** - * 收集开始使用时间,发送信息 - */ + /** + * 收集开始使用时间,发送信息 + */ public void collectStartTime(){ this.current.setStartDate(dateToString()); @@ -360,8 +202,8 @@ public class InformationCollector implements XMLReadable, XMLWriter { } catch (InterruptedException e) { FineLoggerFactory.getLogger().error(e.getMessage(), e); } - sendUserInfo(); - sendFunctionsInfo(); + sendUserInfo(); + FocusPointMessageUploader.getInstance().sendToCloudCenter(); TemplateInfoCollector.getInstance().sendTemplateInfo(); ErrorInfoUploader.getInstance().sendErrorInfo(); } @@ -534,94 +376,4 @@ public class InformationCollector implements XMLReadable, XMLWriter { } } - - private class FunctionRecord implements Comparable{ - private String id; - private String text; - private int source; - private long time; - private int times = 1; - private String title; - private String username; - private String uuid; - - public FunctionRecord(){ - - } - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public int getTimes() { - return times; - } - - public void setTimes(int times) { - this.times = times; - } - - public String getText() { - return text; - } - - public void setText(String text) { - this.text = text; - } - - public int getSource() { - return source; - } - - public void setSource(int source) { - this.source = source; - } - - public long getTime() { - return time; - } - - public void setTime(long time) { - this.time = time; - } - - public String getTitle() { - return title; - } - - public void setTitle(String title) { - this.title = title; - } - - public String getUsername() { - return username; - } - - public void setUsername(String username) { - this.username = username; - } - - public String getUuid() { - return uuid; - } - - public void setUuid(String uuid) { - this.uuid = uuid; - } - - @Override - public int compareTo(Object o) { - FunctionRecord functionRecord = (FunctionRecord) o; - if(this.getId().equals((functionRecord.getId())) && this.getText().equals(functionRecord.getText()) - && this.getSource() == functionRecord.getSource() && this.getTime() == functionRecord.getTime() - && this.getTitle().equals(functionRecord.getTitle()) && this.getUsername().equals(functionRecord.getUsername()) - && this.getUuid().equals(functionRecord.getUuid())){ - return 0; - } - return 1; - } - } } \ No newline at end of file diff --git a/designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/SendDataToCloudProvider.java b/designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/SendDataToCloudProvider.java new file mode 100644 index 000000000..187b72f92 --- /dev/null +++ b/designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/SendDataToCloudProvider.java @@ -0,0 +1,30 @@ +package com.fr.design.mainframe.messagecollect; + +import com.fr.stable.query.data.DataList; + +/** + * @author alex sung + * @date 2019/3/22 + */ +public interface SendDataToCloudProvider { + + /** + * 获取要回传的数据 + * @param currentTime 当前时间 + * @param lastTime 上次回传时间 + * @param tClass 埋点对象类型 + * @throws Exception 取数过程中可能的异常 + */ + void getData(long currentTime, long lastTime, Class tClass) throws Exception; + + /** + * @param points 从swift获取的埋点数据 + * @throws Exception 解析或存储临时文件时可能的异常 + */ + void dealWithData(DataList points) throws Exception; + + /** + * 回传Zip到云中心 + */ + void sendToCloudCenter(); +} diff --git a/designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/impl/AbstractSendDataToCloud.java b/designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/impl/AbstractSendDataToCloud.java new file mode 100644 index 000000000..c611fc060 --- /dev/null +++ b/designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/impl/AbstractSendDataToCloud.java @@ -0,0 +1,247 @@ +package com.fr.design.mainframe.messagecollect.impl; + +import com.fr.design.mainframe.messagecollect.SendDataToCloudProvider; +import com.fr.general.CloudCenter; +import com.fr.general.IOUtils; +import com.fr.general.http.HttpRequestType; +import com.fr.general.http.HttpToolbox; +import com.fr.intelli.record.MetricRegistry; +import com.fr.json.JSONException; +import com.fr.json.JSONObject; +import com.fr.log.FineLoggerFactory; +import com.fr.stable.CommonUtils; +import com.fr.stable.EncodeConstants; +import com.fr.stable.ProductConstants; +import com.fr.stable.StableUtils; +import com.fr.stable.query.QueryFactory; +import com.fr.stable.query.condition.QueryCondition; +import com.fr.stable.query.data.DataList; +import com.fr.stable.query.restriction.RestrictionFactory; +import com.fr.stable.xml.XMLTools; +import com.fr.stable.xml.XMLable; +import com.fr.third.org.apache.http.entity.mime.MultipartEntityBuilder; +import com.fr.third.org.apache.http.entity.mime.content.FileBody; + +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.Charset; +import java.util.HashMap; +import java.util.Map; +import java.util.zip.ZipEntry; + +/** + * @author alex sung + * @date 2019/3/22 + */ +public abstract class AbstractSendDataToCloud implements SendDataToCloudProvider, XMLable { + + 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 FILE_NAME = "messagecollect.info"; + private static final String COLUMN_TIME = "time"; + + protected String lastTime; + private static final int PAGE_SIZE = 200; + private long totalCount = -1; + protected String fileName; + protected String pathName; + protected String folderName; + + public String getFolderName() { + return folderName; + } + + public void setFolderName(String folderName) { + this.folderName = folderName; + } + + public String getFileName() { + return fileName; + } + + public void setFileName(String fileName) { + this.fileName = fileName; + } + + public String getPathName() { + return pathName; + } + + public void setPathName(String pathName) { + this.pathName = pathName; + } + + public String getLastTime() { + return lastTime; + } + + public void setLastTime(String lastTime) { + this.lastTime = lastTime; + } + + public void saveLastTime() { + try { + FileOutputStream out = new FileOutputStream(getLastTimeFile()); + XMLTools.writeOutputStreamXML(this, out); + } catch (Exception ex) { + FineLoggerFactory.getLogger().error(ex.getMessage()); + } + } + + public static File getLastTimeFile() { + return new File(StableUtils.pathJoin(ProductConstants.getEnvHome(), FILE_NAME)); + } + + @Override + public void getData(long currentTime, long lastTime, Class tClass) { + queryAndSendOnePageFunctionContent(currentTime, lastTime, 0, tClass); + long page = (totalCount / PAGE_SIZE) + 1; + for (int i = 1; i < page; i++) { + queryAndSendOnePageFunctionContent(currentTime, lastTime, i, tClass); + } + } + + @Override + public Object clone() throws CloneNotSupportedException { + return super.clone(); + } + + private void queryAndSendOnePageFunctionContent(long current, long last, int page, Class tClass) { + QueryCondition condition = QueryFactory.create() + .skip(page * PAGE_SIZE) + .count(PAGE_SIZE) + .addSort(COLUMN_TIME, true) + .addRestriction(RestrictionFactory.lte(COLUMN_TIME, current)) + .addRestriction(RestrictionFactory.gte(COLUMN_TIME, last)); + try { + DataList points = MetricRegistry.getMetric().find(tClass, condition); + //第一次查询获取总记录数 + if (page == 0) { + totalCount = points.getTotalCount(); + } + dealWithData(points); + + } catch (Exception e) { + FineLoggerFactory.getLogger().error(e.getMessage(), e); + } + } + + @Override + public void dealWithData(DataList tDataList) throws Exception { + generateThisPageFile(tDataList); + } + + private void generateThisPageFile(DataList points) { + File file = null; + try { + JSONObject jsonObject = dealWithSendFunctionContent(points); + //生成json文件 + generateFile(jsonObject); + } catch (Exception e) { + FineLoggerFactory.getLogger().error(e.getMessage(), e); + } + } + + public abstract JSONObject dealWithSendFunctionContent(DataList focusPoints); + + /** + * 生成zip并发送zip文件 + * @param pathName zip文件路径 + */ + protected void sendZipFile(String pathName) { + + File file = null; + try { + file = generateZipFile(pathName); + if (file != null) { + uploadFile(file, file.getName()); + } + } catch (Exception e) { + FineLoggerFactory.getLogger().error(e.getMessage(), e); + return; + } + deleteFileAndZipFile(file, pathName); + } + + private File generateZipFile(String pathName) { + File zipFile = null; + try { + File file = new File(pathName); + zipFile = new File(pathName + ".zip"); + InputStream input = null; + java.util.zip.ZipOutputStream zipOut = null; + zipOut = new java.util.zip.ZipOutputStream(new FileOutputStream(zipFile)); + int temp = 0; + if (file.isDirectory()) { + File lists[] = file.listFiles(); + for (int i = 0; i < lists.length; i++) { + input = new FileInputStream(lists[i]); + zipOut.putNextEntry(new ZipEntry(file.getName() + + File.separator + lists[i].getName())); + while ((temp = input.read()) != -1) { + zipOut.write(temp); + } + input.close(); + } + } + zipOut.close(); + } catch (Exception e) { + FineLoggerFactory.getLogger().error(e.getMessage(), e); + } + return zipFile; + } + + private void generateFile(JSONObject jsonObject) { + try { + String content = jsonObject.toString(); + File file = new File(pathName + ".json"); + StableUtils.makesureFileExist(file); + FileOutputStream out = new FileOutputStream(file); + InputStream in = new ByteArrayInputStream(content.getBytes(EncodeConstants.ENCODING_UTF_8)); + IOUtils.copyBinaryTo(in, out); + out.close(); + } catch (Exception e) { + FineLoggerFactory.getLogger().error(e.getMessage(), e); + } + } + + private static void uploadFile(File file, String keyFileName) throws IOException { + String url = generateSignedUploadUrl(keyFileName); + MultipartEntityBuilder builder = MultipartEntityBuilder.create() + .addPart("file", new FileBody(file)); + Map headers = new HashMap(); + headers.put("Content-Type", "application/zip"); + HttpToolbox.upload(url, builder, Charset.forName("utf-8"), headers, HttpRequestType.PUT); + } + + + + private void deleteFileAndZipFile(File zipFile, String pathName) { + File file = new File(StableUtils.pathJoin(ProductConstants.getEnvHome(), pathName)); + CommonUtils.deleteFile(file); + CommonUtils.deleteFile(zipFile); + } + + 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); + try { + JSONObject data = new JSONObject(responseText); + if ("success".equals(data.optString("status"))) { + return data.optString("url"); + } + } catch (JSONException e) { + FineLoggerFactory.getLogger().error("Illegal response text."); + } + 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 new file mode 100644 index 000000000..15dc8f971 --- /dev/null +++ b/designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/impl/FocusPointMessageUploader.java @@ -0,0 +1,86 @@ +package com.fr.design.mainframe.messagecollect.impl; + +import com.fr.config.MarketConfig; +import com.fr.design.DesignerEnvManager; +import com.fr.design.mainframe.messagecollect.utils.MessageCollectUtils; +import com.fr.intelli.record.FocusPoint; +import com.fr.json.JSONObject; +import com.fr.log.FineLoggerFactory; +import com.fr.stable.ProductConstants; +import com.fr.stable.StableUtils; +import com.fr.stable.query.data.DataList; +import com.fr.stable.xml.XMLPrintWriter; +import com.fr.stable.xml.XMLableReader; + +import java.util.Date; +import java.util.UUID; + +/** + * @author alex sung + * @date 2019/3/22 + */ +public class FocusPointMessageUploader extends AbstractSendDataToCloud { + + private static final String TAG = "FocusPointMessageTag"; + private static final String SEPARATOR = "_"; + private static final String FOCUS_POINT_VERSION = "FocusPoint1003_"; + private static volatile FocusPointMessageUploader instance; + + public static FocusPointMessageUploader getInstance() { + if (instance == null) { + synchronized (FocusPointMessageUploader.class) { + if (instance == null) { + instance = new FocusPointMessageUploader(); + } + } + } + return instance; + } + + @Override + public JSONObject dealWithSendFunctionContent(DataList focusPoints) { + return new JSONObject(); + } + + @Override + public void sendToCloudCenter() { + MessageCollectUtils.readXMLFile(instance, getLastTimeFile()); + long currentTime = new Date().getTime(); + long lastTIme = MessageCollectUtils.getLastTimeMillis(lastTime); + try { + generatePath(); + getData(currentTime, lastTIme, FocusPoint.class); + } catch (Exception e) { + FineLoggerFactory.getLogger().error(e.getMessage()); + } + sendZipFile(getFolderName()); + saveLastTime(); + } + + @Override + public void readXML(XMLableReader reader) { + if (reader.isAttr()) { + this.setLastTime(reader.getAttrAsString("focusPointLastTime", null)); + } + } + + @Override + public void writeXML(XMLPrintWriter writer) { + writer.startTAG(TAG); + writer.attr("focusPointLastTime", lastTime); + writer.end(); + } + + private void generatePath(){ + DesignerEnvManager envManager = DesignerEnvManager.getEnvManager(); + String bbsUserName = MarketConfig.getInstance().getBbsUsername(); + String uuid = envManager.getUUID(); + //文件夹名称的格式是: "FocusPoint1003_" + uuid_bbsUserName_randomUuid,均以下划线分隔 + StringBuilder sb = new StringBuilder(); + sb.append(FOCUS_POINT_VERSION).append(uuid).append(SEPARATOR).append(bbsUserName).append(SEPARATOR).append(UUID.randomUUID()); + + setFileName(String.valueOf(UUID.randomUUID())); + setPathName(StableUtils.pathJoin(ProductConstants.getEnvHome(), sb.toString(), getFileName())); + setFolderName(StableUtils.pathJoin(ProductConstants.getEnvHome(), sb.toString())); + } +} diff --git a/designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/utils/MessageCollectUtils.java b/designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/utils/MessageCollectUtils.java new file mode 100644 index 000000000..721e42741 --- /dev/null +++ b/designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/utils/MessageCollectUtils.java @@ -0,0 +1,69 @@ +package com.fr.design.mainframe.messagecollect.utils; + +import com.fr.base.FRContext; +import com.fr.general.DateUtils; +import com.fr.general.IOUtils; +import com.fr.log.FineLoggerFactory; +import com.fr.stable.EncodeConstants; +import com.fr.stable.StringUtils; +import com.fr.stable.xml.XMLReadable; +import com.fr.stable.xml.XMLableReader; +import com.fr.third.javax.xml.stream.XMLStreamException; + +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.UnsupportedEncodingException; +import java.text.DateFormat; +import java.util.Date; + +/** + * @author alex sung + * @date 2019/3/26 + */ +public class MessageCollectUtils { + + public static String getFileContent(File xmlFile) throws FileNotFoundException, UnsupportedEncodingException { + InputStream is = new FileInputStream(xmlFile); + return IOUtils.inputStream2String(is); + } + + public static void readXMLFile(XMLReadable xmlReadable, File xmlFile) { + if (xmlFile == null || !xmlFile.exists()) { + return; + } + String charset = EncodeConstants.ENCODING_UTF_8; + try { + String fileContent = MessageCollectUtils.getFileContent(xmlFile); + InputStream xmlInputStream = new ByteArrayInputStream(fileContent.getBytes(charset)); + InputStreamReader inputStreamReader = new InputStreamReader(xmlInputStream, charset); + XMLableReader xmlReader = XMLableReader.createXMLableReader(inputStreamReader); + if (xmlReader != null) { + xmlReader.readXMLObject(xmlReadable); + } + xmlInputStream.close(); + } catch (IOException | XMLStreamException e) { + FineLoggerFactory.getLogger().error(e.getMessage(), e); + } + } + + public static String newDateToString() { + DateFormat df = FRContext.getDefaultValues().getDateTimeFormat(); + return df.format(new Date()); + } + + public static long getLastTimeMillis(String lastTime) { + if (StringUtils.isEmpty(lastTime)) { + return 0; + } + try { + return DateUtils.string2Date(lastTime, true).getTime(); + } catch (Exception e) { + return -1; + } + } +} From 9312b4ea4c27cabcc9a5e09641c6eea673df4542 Mon Sep 17 00:00:00 2001 From: 1 <1@qq> Date: Fri, 5 Apr 2019 09:32:03 +0800 Subject: [PATCH 3/7] fix --- .../main/java/com/fr/design/mainframe/InformationCollector.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/designer-realize/src/main/java/com/fr/design/mainframe/InformationCollector.java b/designer-realize/src/main/java/com/fr/design/mainframe/InformationCollector.java index 9d04cc185..7a3966c17 100644 --- a/designer-realize/src/main/java/com/fr/design/mainframe/InformationCollector.java +++ b/designer-realize/src/main/java/com/fr/design/mainframe/InformationCollector.java @@ -56,7 +56,7 @@ public class InformationCollector implements XMLReadable, XMLWriter { // 24小时上传一次 private static final long DELTA = 24 * 3600 * 1000L; - private static final long SEND_DELAY = 30 * 1000L; + private static final long SEND_DELAY = 300 * 1000L; private static final String FILE_NAME = "fr.info"; private static final String XML_START_STOP_LIST = "StartStopList"; private static final String XML_START_STOP = "StartStop"; From ee16d597d5df9794afedcd433ec2c1b07d6d7167 Mon Sep 17 00:00:00 2001 From: 1 <1@qq> Date: Mon, 8 Apr 2019 20:13:04 +0800 Subject: [PATCH 4/7] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E8=B4=A8=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SendDataToCloudProvider.java | 2 +- .../messagecollect/entity/FileEntity.java | 50 +++++++++++++++++ .../impl/AbstractSendDataToCloud.java | 53 +++++++------------ .../impl/FocusPointMessageUploader.java | 25 +++++---- .../utils/MessageCollectUtils.java | 31 ++++++----- 5 files changed, 101 insertions(+), 60 deletions(-) create mode 100644 designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/entity/FileEntity.java diff --git a/designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/SendDataToCloudProvider.java b/designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/SendDataToCloudProvider.java index 187b72f92..a994064a6 100644 --- a/designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/SendDataToCloudProvider.java +++ b/designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/SendDataToCloudProvider.java @@ -15,7 +15,7 @@ public interface SendDataToCloudProvider { * @param tClass 埋点对象类型 * @throws Exception 取数过程中可能的异常 */ - void getData(long currentTime, long lastTime, Class tClass) throws Exception; + void queryData(long currentTime, long lastTime, Class tClass) throws Exception; /** * @param points 从swift获取的埋点数据 diff --git a/designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/entity/FileEntity.java b/designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/entity/FileEntity.java new file mode 100644 index 000000000..63c40f458 --- /dev/null +++ b/designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/entity/FileEntity.java @@ -0,0 +1,50 @@ +package com.fr.design.mainframe.messagecollect.entity; + +/** + * @author alex sung + * @date 2019/4/8 + */ +public class FileEntity { + /** + * 文件名 + */ + private String fileName; + /** + * 文件的完整路径 + */ + private String pathName; + /** + * 文件夹路径 + */ + private String folderName; + + public FileEntity(String fileName, String pathName, String folderName) { + this.fileName = fileName; + this.pathName = pathName; + this.folderName = folderName; + } + + public String getFileName() { + return fileName; + } + + public void setFileName(String fileName) { + this.fileName = fileName; + } + + public String getPathName() { + return pathName; + } + + public void setPathName(String pathName) { + this.pathName = pathName; + } + + public String getFolderName() { + return folderName; + } + + public void setFolderName(String folderName) { + this.folderName = folderName; + } +} diff --git a/designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/impl/AbstractSendDataToCloud.java b/designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/impl/AbstractSendDataToCloud.java index c611fc060..f19ecd5b4 100644 --- a/designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/impl/AbstractSendDataToCloud.java +++ b/designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/impl/AbstractSendDataToCloud.java @@ -1,6 +1,8 @@ package com.fr.design.mainframe.messagecollect.impl; import com.fr.design.mainframe.messagecollect.SendDataToCloudProvider; +import com.fr.design.mainframe.messagecollect.entity.FileEntity; +import com.fr.design.mainframe.messagecollect.utils.MessageCollectUtils; import com.fr.general.CloudCenter; import com.fr.general.IOUtils; import com.fr.general.http.HttpRequestType; @@ -13,6 +15,7 @@ import com.fr.stable.CommonUtils; import com.fr.stable.EncodeConstants; import com.fr.stable.ProductConstants; import com.fr.stable.StableUtils; +import com.fr.stable.StringUtils; import com.fr.stable.query.QueryFactory; import com.fr.stable.query.condition.QueryCondition; import com.fr.stable.query.data.DataList; @@ -49,32 +52,14 @@ public abstract class AbstractSendDataToCloud implements SendDataToCloudProvider protected String lastTime; private static final int PAGE_SIZE = 200; private long totalCount = -1; - protected String fileName; - protected String pathName; - protected String folderName; + private FileEntity fileEntity; - public String getFolderName() { - return folderName; + public FileEntity getFileEntity() { + return fileEntity; } - public void setFolderName(String folderName) { - this.folderName = folderName; - } - - public String getFileName() { - return fileName; - } - - public void setFileName(String fileName) { - this.fileName = fileName; - } - - public String getPathName() { - return pathName; - } - - public void setPathName(String pathName) { - this.pathName = pathName; + public void setFileEntity(FileEntity fileEntity) { + this.fileEntity = fileEntity; } public String getLastTime() { @@ -99,7 +84,7 @@ public abstract class AbstractSendDataToCloud implements SendDataToCloudProvider } @Override - public void getData(long currentTime, long lastTime, Class tClass) { + public void queryData(long currentTime, long lastTime, Class tClass) { queryAndSendOnePageFunctionContent(currentTime, lastTime, 0, tClass); long page = (totalCount / PAGE_SIZE) + 1; for (int i = 1; i < page; i++) { @@ -200,7 +185,7 @@ public abstract class AbstractSendDataToCloud implements SendDataToCloudProvider private void generateFile(JSONObject jsonObject) { try { String content = jsonObject.toString(); - File file = new File(pathName + ".json"); + File file = new File(getFileEntity().getPathName() + ".json"); StableUtils.makesureFileExist(file); FileOutputStream out = new FileOutputStream(file); InputStream in = new ByteArrayInputStream(content.getBytes(EncodeConstants.ENCODING_UTF_8)); @@ -213,18 +198,20 @@ public abstract class AbstractSendDataToCloud implements SendDataToCloudProvider private static void uploadFile(File file, String keyFileName) throws IOException { String url = generateSignedUploadUrl(keyFileName); - MultipartEntityBuilder builder = MultipartEntityBuilder.create() - .addPart("file", new FileBody(file)); - Map headers = new HashMap(); - headers.put("Content-Type", "application/zip"); - HttpToolbox.upload(url, builder, Charset.forName("utf-8"), headers, HttpRequestType.PUT); + if(StringUtils.isEmpty(url)){ + FineLoggerFactory.getLogger().error("url is null."); + }else { + MultipartEntityBuilder builder = MultipartEntityBuilder.create() + .addPart("file", new FileBody(file)); + Map headers = new HashMap(); + headers.put("Content-Type", "application/zip"); + HttpToolbox.upload(url, builder, Charset.forName("utf-8"), headers, HttpRequestType.PUT); + } } - - private void deleteFileAndZipFile(File zipFile, String pathName) { File file = new File(StableUtils.pathJoin(ProductConstants.getEnvHome(), pathName)); - CommonUtils.deleteFile(file); + MessageCollectUtils.deleteDir(file); CommonUtils.deleteFile(zipFile); } 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 15dc8f971..4b89aeacb 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 @@ -2,6 +2,7 @@ package com.fr.design.mainframe.messagecollect.impl; import com.fr.config.MarketConfig; import com.fr.design.DesignerEnvManager; +import com.fr.design.mainframe.messagecollect.entity.FileEntity; import com.fr.design.mainframe.messagecollect.utils.MessageCollectUtils; import com.fr.intelli.record.FocusPoint; import com.fr.json.JSONObject; @@ -23,7 +24,7 @@ public class FocusPointMessageUploader extends AbstractSendDataToCloud { private static final String TAG = "FocusPointMessageTag"; private static final String SEPARATOR = "_"; - private static final String FOCUS_POINT_VERSION = "FocusPoint1003_"; + private static final String FOCUS_POINT = "FocusPoint"; private static volatile FocusPointMessageUploader instance; public static FocusPointMessageUploader getInstance() { @@ -49,11 +50,11 @@ public class FocusPointMessageUploader extends AbstractSendDataToCloud { long lastTIme = MessageCollectUtils.getLastTimeMillis(lastTime); try { generatePath(); - getData(currentTime, lastTIme, FocusPoint.class); + queryData(currentTime, lastTIme, FocusPoint.class); } catch (Exception e) { FineLoggerFactory.getLogger().error(e.getMessage()); } - sendZipFile(getFolderName()); + sendZipFile(getFileEntity().getFolderName()); saveLastTime(); } @@ -71,16 +72,20 @@ public class FocusPointMessageUploader extends AbstractSendDataToCloud { writer.end(); } - private void generatePath(){ + private void generatePath() { DesignerEnvManager envManager = DesignerEnvManager.getEnvManager(); String bbsUserName = MarketConfig.getInstance().getBbsUsername(); String uuid = envManager.getUUID(); - //文件夹名称的格式是: "FocusPoint1003_" + uuid_bbsUserName_randomUuid,均以下划线分隔 + //文件夹名称的格式是: "FocusPoint" + 大版本号 + 小版本号 + uuid + bbsUserName + randomUuid,均以下划线分隔 StringBuilder sb = new StringBuilder(); - sb.append(FOCUS_POINT_VERSION).append(uuid).append(SEPARATOR).append(bbsUserName).append(SEPARATOR).append(UUID.randomUUID()); - - setFileName(String.valueOf(UUID.randomUUID())); - setPathName(StableUtils.pathJoin(ProductConstants.getEnvHome(), sb.toString(), getFileName())); - setFolderName(StableUtils.pathJoin(ProductConstants.getEnvHome(), sb.toString())); + 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 fileName = String.valueOf(UUID.randomUUID()); + String pathName = StableUtils.pathJoin(ProductConstants.getEnvHome(), sb.toString(), fileName); + String folderName = StableUtils.pathJoin(ProductConstants.getEnvHome(), sb.toString()); + setFileEntity(new FileEntity(fileName, pathName, folderName)); } } diff --git a/designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/utils/MessageCollectUtils.java b/designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/utils/MessageCollectUtils.java index 721e42741..17ce547ce 100644 --- a/designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/utils/MessageCollectUtils.java +++ b/designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/utils/MessageCollectUtils.java @@ -1,6 +1,5 @@ package com.fr.design.mainframe.messagecollect.utils; -import com.fr.base.FRContext; import com.fr.general.DateUtils; import com.fr.general.IOUtils; import com.fr.log.FineLoggerFactory; @@ -13,13 +12,9 @@ import com.fr.third.javax.xml.stream.XMLStreamException; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; -import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import java.io.UnsupportedEncodingException; -import java.text.DateFormat; -import java.util.Date; /** * @author alex sung @@ -27,18 +22,14 @@ import java.util.Date; */ public class MessageCollectUtils { - public static String getFileContent(File xmlFile) throws FileNotFoundException, UnsupportedEncodingException { - InputStream is = new FileInputStream(xmlFile); - return IOUtils.inputStream2String(is); - } - public static void readXMLFile(XMLReadable xmlReadable, File xmlFile) { if (xmlFile == null || !xmlFile.exists()) { return; } String charset = EncodeConstants.ENCODING_UTF_8; try { - String fileContent = MessageCollectUtils.getFileContent(xmlFile); + InputStream is = new FileInputStream(xmlFile); + String fileContent = IOUtils.inputStream2String(is); InputStream xmlInputStream = new ByteArrayInputStream(fileContent.getBytes(charset)); InputStreamReader inputStreamReader = new InputStreamReader(xmlInputStream, charset); XMLableReader xmlReader = XMLableReader.createXMLableReader(inputStreamReader); @@ -51,11 +42,6 @@ public class MessageCollectUtils { } } - public static String newDateToString() { - DateFormat df = FRContext.getDefaultValues().getDateTimeFormat(); - return df.format(new Date()); - } - public static long getLastTimeMillis(String lastTime) { if (StringUtils.isEmpty(lastTime)) { return 0; @@ -66,4 +52,17 @@ public class MessageCollectUtils { return -1; } } + + public static boolean deleteDir(File dir) { + if (dir.isDirectory()) { + String[] children = dir.list(); + for (int i = 0; i < children.length; i++) { + boolean success = deleteDir(new File(dir, children[i])); + if (!success) { + return false; + } + } + } + return dir.delete(); + } } From 15cadcebadd41d6bbbefde6784d31d9d2370f815 Mon Sep 17 00:00:00 2001 From: 1 <1@qq> Date: Wed, 10 Apr 2019 17:03:07 +0800 Subject: [PATCH 5/7] =?UTF-8?q?=E4=BD=BF=E7=94=A8websocket=E5=A4=84?= =?UTF-8?q?=E7=90=86=E5=89=8D=E7=AB=AF=E5=8F=91=E6=9D=A5=E7=9A=84=E5=9F=8B?= =?UTF-8?q?=E7=82=B9=E6=B6=88=E6=81=AF=EF=BC=9B=20=E6=A0=B9=E6=8D=AE?= =?UTF-8?q?=E4=BA=A7=E5=93=81=E6=96=B0=E8=A7=84=E5=AE=9A=E7=9A=84=E5=9F=8B?= =?UTF-8?q?=E7=82=B9=E6=A0=87=E8=AF=86=E5=91=BD=E5=90=8D=EF=BC=9B=20?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E6=93=8D=E4=BD=9C=E6=94=BE=E5=9C=A8FileEntit?= =?UTF-8?q?yBuilder=E9=87=8C=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SendDataToCloudProvider.java | 30 ---- .../impl/AbstractSendDataToCloud.java | 131 ++---------------- .../impl/FocusPointMessageUploader.java | 26 +++- .../utils/MessageCollectUtils.java | 17 +-- 4 files changed, 41 insertions(+), 163 deletions(-) delete mode 100644 designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/SendDataToCloudProvider.java diff --git a/designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/SendDataToCloudProvider.java b/designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/SendDataToCloudProvider.java deleted file mode 100644 index a994064a6..000000000 --- a/designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/SendDataToCloudProvider.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.fr.design.mainframe.messagecollect; - -import com.fr.stable.query.data.DataList; - -/** - * @author alex sung - * @date 2019/3/22 - */ -public interface SendDataToCloudProvider { - - /** - * 获取要回传的数据 - * @param currentTime 当前时间 - * @param lastTime 上次回传时间 - * @param tClass 埋点对象类型 - * @throws Exception 取数过程中可能的异常 - */ - void queryData(long currentTime, long lastTime, Class tClass) throws Exception; - - /** - * @param points 从swift获取的埋点数据 - * @throws Exception 解析或存储临时文件时可能的异常 - */ - void dealWithData(DataList points) throws Exception; - - /** - * 回传Zip到云中心 - */ - void sendToCloudCenter(); -} diff --git a/designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/impl/AbstractSendDataToCloud.java b/designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/impl/AbstractSendDataToCloud.java index f19ecd5b4..b6d18235e 100644 --- a/designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/impl/AbstractSendDataToCloud.java +++ b/designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/impl/AbstractSendDataToCloud.java @@ -1,65 +1,41 @@ package com.fr.design.mainframe.messagecollect.impl; -import com.fr.design.mainframe.messagecollect.SendDataToCloudProvider; -import com.fr.design.mainframe.messagecollect.entity.FileEntity; +import com.fr.design.mainframe.messagecollect.entity.FileEntityBuilder; import com.fr.design.mainframe.messagecollect.utils.MessageCollectUtils; -import com.fr.general.CloudCenter; -import com.fr.general.IOUtils; -import com.fr.general.http.HttpRequestType; -import com.fr.general.http.HttpToolbox; import com.fr.intelli.record.MetricRegistry; -import com.fr.json.JSONException; -import com.fr.json.JSONObject; +import com.fr.json.JSONArray; import com.fr.log.FineLoggerFactory; -import com.fr.stable.CommonUtils; -import com.fr.stable.EncodeConstants; import com.fr.stable.ProductConstants; import com.fr.stable.StableUtils; -import com.fr.stable.StringUtils; import com.fr.stable.query.QueryFactory; import com.fr.stable.query.condition.QueryCondition; import com.fr.stable.query.data.DataList; import com.fr.stable.query.restriction.RestrictionFactory; import com.fr.stable.xml.XMLTools; import com.fr.stable.xml.XMLable; -import com.fr.third.org.apache.http.entity.mime.MultipartEntityBuilder; -import com.fr.third.org.apache.http.entity.mime.content.FileBody; -import java.io.ByteArrayInputStream; import java.io.File; -import java.io.FileInputStream; import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.nio.charset.Charset; -import java.util.HashMap; -import java.util.Map; -import java.util.zip.ZipEntry; /** * @author alex sung * @date 2019/3/22 */ -public abstract class AbstractSendDataToCloud implements SendDataToCloudProvider, XMLable { - - 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"; +public abstract class AbstractSendDataToCloud implements XMLable { private static final String FILE_NAME = "messagecollect.info"; private static final String COLUMN_TIME = "time"; protected String lastTime; private static final int PAGE_SIZE = 200; private long totalCount = -1; - private FileEntity fileEntity; + private FileEntityBuilder fileEntityBuilder; - public FileEntity getFileEntity() { - return fileEntity; + public FileEntityBuilder getFileEntityBuilder() { + return fileEntityBuilder; } - public void setFileEntity(FileEntity fileEntity) { - this.fileEntity = fileEntity; + public void setFileEntityBuilder(FileEntityBuilder fileEntityBuilder) { + this.fileEntityBuilder = fileEntityBuilder; } public String getLastTime() { @@ -71,6 +47,7 @@ public abstract class AbstractSendDataToCloud implements SendDataToCloudProvider } public void saveLastTime() { + setLastTime(MessageCollectUtils.dateToString()); try { FileOutputStream out = new FileOutputStream(getLastTimeFile()); XMLTools.writeOutputStreamXML(this, out); @@ -83,7 +60,6 @@ public abstract class AbstractSendDataToCloud implements SendDataToCloudProvider return new File(StableUtils.pathJoin(ProductConstants.getEnvHome(), FILE_NAME)); } - @Override public void queryData(long currentTime, long lastTime, Class tClass) { queryAndSendOnePageFunctionContent(currentTime, lastTime, 0, tClass); long page = (totalCount / PAGE_SIZE) + 1; @@ -117,7 +93,6 @@ public abstract class AbstractSendDataToCloud implements SendDataToCloudProvider } } - @Override public void dealWithData(DataList tDataList) throws Exception { generateThisPageFile(tDataList); } @@ -125,15 +100,15 @@ public abstract class AbstractSendDataToCloud implements SendDataToCloudProvider private void generateThisPageFile(DataList points) { File file = null; try { - JSONObject jsonObject = dealWithSendFunctionContent(points); + JSONArray jsonArray = dealWithSendFunctionContent(points); //生成json文件 - generateFile(jsonObject); + fileEntityBuilder.generateFile(jsonArray, getFileEntityBuilder().getPathName()); } catch (Exception e) { FineLoggerFactory.getLogger().error(e.getMessage(), e); } } - public abstract JSONObject dealWithSendFunctionContent(DataList focusPoints); + public abstract JSONArray dealWithSendFunctionContent(DataList focusPoints); /** * 生成zip并发送zip文件 @@ -143,92 +118,16 @@ public abstract class AbstractSendDataToCloud implements SendDataToCloudProvider File file = null; try { - file = generateZipFile(pathName); + file = fileEntityBuilder.generateZipFile(pathName); if (file != null) { - uploadFile(file, file.getName()); + fileEntityBuilder.uploadFile(file, file.getName()); } } catch (Exception e) { FineLoggerFactory.getLogger().error(e.getMessage(), e); return; } - deleteFileAndZipFile(file, pathName); - } - - private File generateZipFile(String pathName) { - File zipFile = null; - try { - File file = new File(pathName); - zipFile = new File(pathName + ".zip"); - InputStream input = null; - java.util.zip.ZipOutputStream zipOut = null; - zipOut = new java.util.zip.ZipOutputStream(new FileOutputStream(zipFile)); - int temp = 0; - if (file.isDirectory()) { - File lists[] = file.listFiles(); - for (int i = 0; i < lists.length; i++) { - input = new FileInputStream(lists[i]); - zipOut.putNextEntry(new ZipEntry(file.getName() - + File.separator + lists[i].getName())); - while ((temp = input.read()) != -1) { - zipOut.write(temp); - } - input.close(); - } - } - zipOut.close(); - } catch (Exception e) { - FineLoggerFactory.getLogger().error(e.getMessage(), e); - } - return zipFile; - } - - private void generateFile(JSONObject jsonObject) { - try { - String content = jsonObject.toString(); - File file = new File(getFileEntity().getPathName() + ".json"); - StableUtils.makesureFileExist(file); - FileOutputStream out = new FileOutputStream(file); - InputStream in = new ByteArrayInputStream(content.getBytes(EncodeConstants.ENCODING_UTF_8)); - IOUtils.copyBinaryTo(in, out); - out.close(); - } catch (Exception e) { - FineLoggerFactory.getLogger().error(e.getMessage(), e); - } - } - - private static void uploadFile(File file, String keyFileName) throws IOException { - String url = generateSignedUploadUrl(keyFileName); - if(StringUtils.isEmpty(url)){ - FineLoggerFactory.getLogger().error("url is null."); - }else { - MultipartEntityBuilder builder = MultipartEntityBuilder.create() - .addPart("file", new FileBody(file)); - Map headers = new HashMap(); - headers.put("Content-Type", "application/zip"); - HttpToolbox.upload(url, builder, Charset.forName("utf-8"), headers, HttpRequestType.PUT); - } + fileEntityBuilder.deleteFileAndZipFile(file, pathName); } - private void deleteFileAndZipFile(File zipFile, String pathName) { - File file = new File(StableUtils.pathJoin(ProductConstants.getEnvHome(), pathName)); - MessageCollectUtils.deleteDir(file); - CommonUtils.deleteFile(zipFile); - } - 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); - try { - JSONObject data = new JSONObject(responseText); - if ("success".equals(data.optString("status"))) { - return data.optString("url"); - } - } catch (JSONException e) { - FineLoggerFactory.getLogger().error("Illegal response text."); - } - 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 4b89aeacb..668e7fa41 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 @@ -2,9 +2,10 @@ package com.fr.design.mainframe.messagecollect.impl; import com.fr.config.MarketConfig; import com.fr.design.DesignerEnvManager; -import com.fr.design.mainframe.messagecollect.entity.FileEntity; +import com.fr.design.mainframe.messagecollect.entity.FileEntityBuilder; import com.fr.design.mainframe.messagecollect.utils.MessageCollectUtils; import com.fr.intelli.record.FocusPoint; +import com.fr.json.JSONArray; import com.fr.json.JSONObject; import com.fr.log.FineLoggerFactory; import com.fr.stable.ProductConstants; @@ -39,11 +40,24 @@ public class FocusPointMessageUploader extends AbstractSendDataToCloud { } @Override - public JSONObject dealWithSendFunctionContent(DataList focusPoints) { - return new JSONObject(); + public JSONArray dealWithSendFunctionContent(DataList focusPoints) { + JSONArray ja = new JSONArray(); + for(T t:focusPoints.getList()){ + FocusPoint focusPoint = (FocusPoint)t; + JSONObject jo = new JSONObject(); + jo.put("id",focusPoint.getId()); + jo.put("text",focusPoint.getId()); + jo.put("source",focusPoint.getId()); + jo.put("time",focusPoint.getId()); + jo.put("username",focusPoint.getId()); + jo.put("ip",focusPoint.getId()); + jo.put("title",focusPoint.getId()); + jo.put("body",focusPoint.getId()); + ja.put(jo); + } + return ja; } - @Override public void sendToCloudCenter() { MessageCollectUtils.readXMLFile(instance, getLastTimeFile()); long currentTime = new Date().getTime(); @@ -54,7 +68,7 @@ public class FocusPointMessageUploader extends AbstractSendDataToCloud { } catch (Exception e) { FineLoggerFactory.getLogger().error(e.getMessage()); } - sendZipFile(getFileEntity().getFolderName()); + sendZipFile(getFileEntityBuilder().getFolderName()); saveLastTime(); } @@ -86,6 +100,6 @@ public class FocusPointMessageUploader extends AbstractSendDataToCloud { String fileName = String.valueOf(UUID.randomUUID()); String pathName = StableUtils.pathJoin(ProductConstants.getEnvHome(), sb.toString(), fileName); String folderName = StableUtils.pathJoin(ProductConstants.getEnvHome(), sb.toString()); - setFileEntity(new FileEntity(fileName, pathName, folderName)); + setFileEntityBuilder(new FileEntityBuilder(fileName, pathName, folderName)); } } diff --git a/designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/utils/MessageCollectUtils.java b/designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/utils/MessageCollectUtils.java index 17ce547ce..738942208 100644 --- a/designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/utils/MessageCollectUtils.java +++ b/designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/utils/MessageCollectUtils.java @@ -1,5 +1,6 @@ package com.fr.design.mainframe.messagecollect.utils; +import com.fr.base.FRContext; import com.fr.general.DateUtils; import com.fr.general.IOUtils; import com.fr.log.FineLoggerFactory; @@ -15,6 +16,8 @@ import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.text.DateFormat; +import java.util.Date; /** * @author alex sung @@ -53,16 +56,8 @@ public class MessageCollectUtils { } } - public static boolean deleteDir(File dir) { - if (dir.isDirectory()) { - String[] children = dir.list(); - for (int i = 0; i < children.length; i++) { - boolean success = deleteDir(new File(dir, children[i])); - if (!success) { - return false; - } - } - } - return dir.delete(); + public static String dateToString(){ + DateFormat df = FRContext.getDefaultValues().getDateTimeFormat(); + return df.format(new Date()); } } From f7a1036a9ce9fdfd147362ef3ba2ede896f749ab Mon Sep 17 00:00:00 2001 From: 1 <1@qq> Date: Wed, 10 Apr 2019 17:28:31 +0800 Subject: [PATCH 6/7] =?UTF-8?q?=E6=94=B9=E4=B8=8B=E5=91=BD=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../messagecollect/entity/FileEntity.java | 50 ------ .../entity/FileEntityBuilder.java | 166 ++++++++++++++++++ 2 files changed, 166 insertions(+), 50 deletions(-) delete mode 100644 designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/entity/FileEntity.java create mode 100644 designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/entity/FileEntityBuilder.java diff --git a/designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/entity/FileEntity.java b/designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/entity/FileEntity.java deleted file mode 100644 index 63c40f458..000000000 --- a/designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/entity/FileEntity.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.fr.design.mainframe.messagecollect.entity; - -/** - * @author alex sung - * @date 2019/4/8 - */ -public class FileEntity { - /** - * 文件名 - */ - private String fileName; - /** - * 文件的完整路径 - */ - private String pathName; - /** - * 文件夹路径 - */ - private String folderName; - - public FileEntity(String fileName, String pathName, String folderName) { - this.fileName = fileName; - this.pathName = pathName; - this.folderName = folderName; - } - - public String getFileName() { - return fileName; - } - - public void setFileName(String fileName) { - this.fileName = fileName; - } - - public String getPathName() { - return pathName; - } - - public void setPathName(String pathName) { - this.pathName = pathName; - } - - public String getFolderName() { - return folderName; - } - - public void setFolderName(String folderName) { - this.folderName = folderName; - } -} 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 new file mode 100644 index 000000000..85d8e40ce --- /dev/null +++ b/designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/entity/FileEntityBuilder.java @@ -0,0 +1,166 @@ +package com.fr.design.mainframe.messagecollect.entity; + +import com.fr.general.CloudCenter; +import com.fr.general.IOUtils; +import com.fr.general.http.HttpRequestType; +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.EncodeConstants; +import com.fr.stable.ProductConstants; +import com.fr.stable.StableUtils; +import com.fr.stable.StringUtils; +import com.fr.third.org.apache.http.entity.mime.MultipartEntityBuilder; +import com.fr.third.org.apache.http.entity.mime.content.FileBody; + +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.Charset; +import java.util.HashMap; +import java.util.Map; +import java.util.zip.ZipEntry; + +/** + * @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 String fileName; + /** + * 文件的完整路径 + */ + private String pathName; + /** + * 文件夹路径 + */ + private String folderName; + + public FileEntityBuilder(String fileName, String pathName, String folderName) { + this.fileName = fileName; + this.pathName = pathName; + this.folderName = folderName; + } + + public String getFileName() { + return fileName; + } + + public void setFileName(String fileName) { + this.fileName = fileName; + } + + public String getPathName() { + return pathName; + } + + public void setPathName(String pathName) { + this.pathName = pathName; + } + + public String getFolderName() { + return folderName; + } + + public void setFolderName(String folderName) { + this.folderName = folderName; + } + + public File generateZipFile(String pathName) { + File zipFile = null; + try { + File file = new File(pathName); + zipFile = new File(pathName + ".zip"); + InputStream input = null; + java.util.zip.ZipOutputStream zipOut = null; + zipOut = new java.util.zip.ZipOutputStream(new FileOutputStream(zipFile)); + int temp = 0; + if (file.isDirectory()) { + File lists[] = file.listFiles(); + for (int i = 0; i < lists.length; i++) { + input = new FileInputStream(lists[i]); + zipOut.putNextEntry(new ZipEntry(file.getName() + + File.separator + lists[i].getName())); + while ((temp = input.read()) != -1) { + zipOut.write(temp); + } + input.close(); + } + } + zipOut.close(); + } catch (Exception e) { + FineLoggerFactory.getLogger().error(e.getMessage(), e); + } + return zipFile; + } + + public void generateFile(JSONArray jsonArray, String pathName) { + try { + String content = jsonArray.toString(); + File file = new File(pathName + ".json"); + StableUtils.makesureFileExist(file); + FileOutputStream out = new FileOutputStream(file); + InputStream in = new ByteArrayInputStream(content.getBytes(EncodeConstants.ENCODING_UTF_8)); + IOUtils.copyBinaryTo(in, out); + out.close(); + } catch (Exception e) { + FineLoggerFactory.getLogger().error(e.getMessage(), e); + } + } + + public void deleteFileAndZipFile(File zipFile, String pathName) { + File file = new File(StableUtils.pathJoin(ProductConstants.getEnvHome(), pathName)); + CommonUtils.deleteFile(file); + CommonUtils.deleteFile(zipFile); + } + + /** + * 上传文件到云中心 + * @param file 待上传文件 + * @param keyFileName 目标文件 + * @throws IOException + */ + public static void uploadFile(File file, String keyFileName) throws IOException { + String url = generateSignedUploadUrl("FocusPoint/"+keyFileName); + if(StringUtils.isEmpty(url)){ + FineLoggerFactory.getLogger().error("url is null."); + }else { + MultipartEntityBuilder builder = MultipartEntityBuilder.create() + .addPart("file", new FileBody(file)); + Map headers = new HashMap(); + headers.put("Content-Type", "application/zip"); + HttpToolbox.upload(url, builder, Charset.forName("utf-8"), headers, HttpRequestType.PUT); + } + } + + 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); + try { + JSONObject data = new JSONObject(responseText); + if ("success".equals(data.optString("status"))) { + return data.optString("url"); + } + } catch (JSONException e) { + FineLoggerFactory.getLogger().error("Illegal response text."); + } + return null; + } +} From 57283ecd9904109f2e31aaf0643d25050ecf132a Mon Sep 17 00:00:00 2001 From: 1 <1@qq> Date: Wed, 10 Apr 2019 20:32:18 +0800 Subject: [PATCH 7/7] =?UTF-8?q?=E7=94=A8IOUtils.zip=E5=8E=8B=E7=BC=A9?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../entity/FileEntityBuilder.java | 23 +++---------------- 1 file changed, 3 insertions(+), 20 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 85d8e40ce..e9eec71b8 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 @@ -18,14 +18,12 @@ import com.fr.third.org.apache.http.entity.mime.content.FileBody; import java.io.ByteArrayInputStream; import java.io.File; -import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.nio.charset.Charset; import java.util.HashMap; import java.util.Map; -import java.util.zip.ZipEntry; /** * @author alex sung @@ -83,24 +81,9 @@ public class FileEntityBuilder { public File generateZipFile(String pathName) { File zipFile = null; try { - File file = new File(pathName); zipFile = new File(pathName + ".zip"); - InputStream input = null; - java.util.zip.ZipOutputStream zipOut = null; - zipOut = new java.util.zip.ZipOutputStream(new FileOutputStream(zipFile)); - int temp = 0; - if (file.isDirectory()) { - File lists[] = file.listFiles(); - for (int i = 0; i < lists.length; i++) { - input = new FileInputStream(lists[i]); - zipOut.putNextEntry(new ZipEntry(file.getName() - + File.separator + lists[i].getName())); - while ((temp = input.read()) != -1) { - zipOut.write(temp); - } - input.close(); - } - } + java.util.zip.ZipOutputStream zipOut = new java.util.zip.ZipOutputStream(new FileOutputStream(zipFile)); + IOUtils.zip(zipOut, new File(pathName)); zipOut.close(); } catch (Exception e) { FineLoggerFactory.getLogger().error(e.getMessage(), e); @@ -159,7 +142,7 @@ public class FileEntityBuilder { return data.optString("url"); } } catch (JSONException e) { - FineLoggerFactory.getLogger().error("Illegal response text."); + FineLoggerFactory.getLogger().error("Illegal response text."+e, e.getMessage()); } return null; }