CodeScanBack/src/main/java/com/osredm/codescan/service/impl/NicadServiceImpl.java

111 lines
4.6 KiB
Java
Raw Normal View History

2022-07-19 19:34:18 +08:00
package com.osredm.codescan.service.impl;
import com.osredm.codescan.config.StoreConfig;
import com.osredm.codescan.entity.NicadInfo;
import com.osredm.codescan.service.NicadService;
import com.osredm.codescan.utils.FileUploadUtil;
import com.osredm.codescan.utils.XmlJsonUtils;
import org.springframework.stereotype.Service;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashMap;
import static com.osredm.codescan.utils.Constants.FAIL;
import static com.osredm.codescan.utils.Constants.SUCCESS;
@Service
public class NicadServiceImpl implements NicadService {
private final StoreConfig storeConfig;
public NicadServiceImpl(StoreConfig storeConfig) {
this.storeConfig = storeConfig;
}
@Override
public HashMap<String, Object> check(NicadInfo nicadInfo){
HashMap<String, Object> map = new HashMap<>();
String language = nicadInfo.getLanguage();
switch (language) {
case "Java":
String filePath = storeConfig.getFilePath() + nicadInfo.getFileName();
String time = String.valueOf(System.currentTimeMillis());
String flag = storeConfig.getFilePath() + time;
// 解压文件
String[] arguments = new String[] {"unzip" , filePath, "-d", flag};
Process proc;
try {
proc = Runtime.getRuntime().exec(arguments);// 执行解压文件
//用输入输出流来截取结果
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
// InputStreamReader(proc.getInputStream());
String line = null;
String line1 = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
line1 = line;
}
in.close();
int re = proc.waitFor(); //waitFor是用来显示脚本是否运行成功1表示失败0表示成功还有其他的表示其他错误
if (re == 0) {
//成功
Process proc1;
String[] arguments1 = new String[] {"nicad6" ,"functions","java", flag, "default-report"};
proc1 = Runtime.getRuntime().exec(arguments1);// 执行解压文件
//用输入输出流来截取结果
BufferedReader in1 = new BufferedReader(new InputStreamReader(proc1.getInputStream()));
String line2 = null;
String line22 = null;
while ((line2 = in1.readLine()) != null) {
System.out.println(line2);
line22 = line2;
}
in1.close();
int re1 = proc1.waitFor(); //waitFor是用来显示脚本是否运行成功1表示失败0表示成功还有其他的表示其他错误
if (re1 == 0) {
//成功 读取文件
String xml = FileUploadUtil.readFile(storeConfig.getFilePath() + time+"_functions-blind-clones/"+time+"_functions-blind-clones-0.30-classes-withsource.xml");
String s = XmlJsonUtils.xml2Json(xml);
map.put("status",SUCCESS);
map.put("xml",xml);
map.put("json",s);
return map;
}else {
//失败
map.put("status",FAIL);
return map;
}
}else {
//失败
map.put("status",FAIL);
return map;
}
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
case "C":
case "C#":
case "Ruby":
case "JavaScript":
default:
map.put("status",FAIL);
map.put("msg","您选择的语言暂不支持检测!或遇到预期以外的错误");
return map;
}
}
}