|
@@ -1,16 +1,33 @@
|
|
|
package cn.iselab.mooctest.site.service.impl;
|
|
|
|
|
|
+import cn.iselab.mooctest.site.dao.ReportTaskDao;
|
|
|
+import cn.iselab.mooctest.site.domainObject.ReportTask;
|
|
|
+import cn.iselab.mooctest.site.domainObject.ReportTaskStatus;
|
|
|
+import cn.iselab.mooctest.site.factory.ReportTaskFactory;
|
|
|
+import cn.iselab.mooctest.site.repository.IReportTaskRepo;
|
|
|
import cn.iselab.mooctest.site.service.AutoReportProductService;
|
|
|
+import cn.iselab.mooctest.site.web.data.vulData.VulReportDataVO;
|
|
|
+import com.google.gson.Gson;
|
|
|
+import net.lingala.zip4j.exception.ZipException;
|
|
|
+import net.lingala.zip4j.model.ZipParameters;
|
|
|
+import net.lingala.zip4j.util.Zip4jConstants;
|
|
|
import org.apache.commons.io.FileUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.scheduling.annotation.Async;
|
|
|
+import org.springframework.scheduling.annotation.AsyncResult;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import java.io.*;
|
|
|
+import java.util.concurrent.Future;
|
|
|
|
|
|
@Service
|
|
|
public class AutoReportProductServiceImpl implements AutoReportProductService {
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private IReportTaskRepo iReportTaskRepo;
|
|
|
+
|
|
|
private static final Logger log = LoggerFactory.getLogger(Process.class.getName());
|
|
|
|
|
|
@Value("${order.autoReport.service.path}")
|
|
@@ -21,6 +38,82 @@ public class AutoReportProductServiceImpl implements AutoReportProductService {
|
|
|
private String BUILDER_COPYPATH;
|
|
|
@Value("${static.resource.autoReport.url}")
|
|
|
private String AUTOREPORT_URL;
|
|
|
+ @Value("${order.vulReport.json.upload.path}")
|
|
|
+ private String REPORT_JSON_UPLOAD_PATH;
|
|
|
+ @Value("${host.ip}")
|
|
|
+ private String HOST_IP;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int getReportTaskStatus(Long orderId, Long ownerId) {
|
|
|
+ ReportTask reportTask = iReportTaskRepo.getByTaskId(orderId, ownerId);
|
|
|
+ if(reportTask == null){
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ return reportTask.getStatus();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ReportTask getReportTask(Long orderId, Long ownerId) {
|
|
|
+ return iReportTaskRepo.getByTaskId(orderId, ownerId);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Async
|
|
|
+ @Override
|
|
|
+ public Future<String> generateReport(Long orderId, Long ownerId, VulReportDataVO reportData) throws InterruptedException {
|
|
|
+ log.info("开始做任务" + orderId);
|
|
|
+ log.info("当前线程:" + Thread.currentThread().getName() + "=-=====queryUserMsgCount");
|
|
|
+ //存入数据库
|
|
|
+ ReportTask reportTask = ReportTaskFactory.createReportTask();
|
|
|
+ reportTask.setTaskId(orderId);
|
|
|
+ reportTask.setOwnerId(ownerId);
|
|
|
+ reportTask.setStatus(ReportTaskStatus.GENERATING);
|
|
|
+ Long reportTaskId = iReportTaskRepo.saveReportTask(reportTask);
|
|
|
+
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
+
|
|
|
+ String builderPath = copyReportBuilderToPath(orderId, "static-scan-report");
|
|
|
+ writeDataToJsonFile(new Gson().toJson(reportData), builderPath + REPORT_JSON_UPLOAD_PATH);
|
|
|
+ boolean bale = runBuilderToProductReport("npm run build", builderPath + "/static-scan-report/");
|
|
|
+ if (bale) {
|
|
|
+ reportTransferToPath(orderId, ownerId, builderPath + "/static-scan-report/");
|
|
|
+ }
|
|
|
+ String taskUrl = "http://" + HOST_IP + "/reportFile/" + ownerId + "/" + orderId + "/vulReport/dist/vulResults.html";
|
|
|
+
|
|
|
+
|
|
|
+ //存入数据库
|
|
|
+ reportTask.setId(reportTaskId);
|
|
|
+ reportTask.setTaskUrl(taskUrl);
|
|
|
+ reportTask.setStatus(ReportTaskStatus.COMPLETED);
|
|
|
+ iReportTaskRepo.saveReportTask(reportTask);
|
|
|
+
|
|
|
+ long end = System.currentTimeMillis();
|
|
|
+ log.info("完成任务"+orderId+",耗时:" + (end - start) + "毫秒");
|
|
|
+
|
|
|
+ return new AsyncResult<>(taskUrl);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void reportTransferToPath(Long orderId, Long ownerId, String path) {
|
|
|
+ String autoReportPath = REPORT_UPLOAD_PATH + ownerId + "/" + orderId + "/vulReport";
|
|
|
+ String downloadPath = autoReportPath + "/report.zip";
|
|
|
+ File uploadFile = new File(autoReportPath);
|
|
|
+ File downloadFile = new File(downloadPath);
|
|
|
+ boolean sign = uploadFile.mkdirs();
|
|
|
+ log.info("------------mkdirs------------", sign);
|
|
|
+ try {
|
|
|
+ log.info("开始拷贝文件");
|
|
|
+ FileUtils.copyDirectoryToDirectory(new File(path + "dist"), new File(autoReportPath));
|
|
|
+ net.lingala.zip4j.core.ZipFile zipFile = new net.lingala.zip4j.core.ZipFile(downloadFile);
|
|
|
+ ZipParameters parameters = new ZipParameters();
|
|
|
+ parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
|
|
|
+ parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
|
|
|
+ zipFile.addFolder(new File(autoReportPath + "/dist"), parameters);
|
|
|
+ log.info("----------------拷贝结束-------------------");
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.info("----------copy error{}---------", e);
|
|
|
+ } catch (ZipException e) {
|
|
|
+ log.info("----------copy error{}---------", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
@Override
|
|
|
public void writeDataToJsonFile(String jsonData, String path) {
|
|
@@ -55,10 +148,10 @@ public class AutoReportProductServiceImpl implements AutoReportProductService {
|
|
|
try {
|
|
|
while ((line = in.readLine())!=null || (errline = err.readLine())!=null){
|
|
|
if(line!=null){
|
|
|
- log.info(line);
|
|
|
+// log.info(line);
|
|
|
}
|
|
|
if(errline!=null) {
|
|
|
- log.info(errline);
|
|
|
+// log.info(errline);
|
|
|
}
|
|
|
}
|
|
|
}catch (IOException e){
|
|
@@ -76,7 +169,8 @@ public class AutoReportProductServiceImpl implements AutoReportProductService {
|
|
|
|
|
|
int status = process.waitFor();
|
|
|
if (status == 0) {
|
|
|
- System.out.println(status);
|
|
|
+ log.info(String.valueOf(status));
|
|
|
+// System.out.println(status);
|
|
|
return true;
|
|
|
}
|
|
|
return false;
|