完成java的执行

This commit is contained in:
p71924506 2022-07-19 19:34:18 +08:00
parent 1143acfe44
commit 60e5382f69
10 changed files with 320 additions and 96 deletions

106
pom.xml
View File

@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.1</version>
<version>2.7.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.osredm</groupId>
@ -14,9 +14,7 @@
<name>osredm-code-scan</name>
<description>osredm-code-scan</description>
<properties>
<java.version>17</java.version>
<repackage.classifier/>
<spring-native.version>0.12.1</spring-native.version>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
@ -118,6 +116,7 @@
<version>2.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
@ -130,85 +129,33 @@
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
<classifier>${repackage.classifier}</classifier>
<image>
<builder>paketobuildpacks/builder:tiny</builder>
<env>
<BP_NATIVE_IMAGE>true</BP_NATIVE_IMAGE>
</env>
</image>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.experimental</groupId>
<artifactId>spring-aot-maven-plugin</artifactId>
<version>${spring-native.version}</version>
<executions>
<execution>
<id>test-generate</id>
<goals>
<goal>test-generate</goal>
</goals>
</execution>
<execution>
<id>generate</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/release</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/release</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
<profiles>
<profile>
<id>native</id>
<properties>
<repackage.classifier>exec</repackage.classifier>
<native-buildtools.version>0.9.13</native-buildtools.version>
</properties>
<dependencies>
<!-- xml转json -->
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<scope>test</scope>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>2.1.3</version>
</dependency>
<!-- apache 工具类 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.79</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
@ -231,9 +178,8 @@
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>

View File

@ -24,14 +24,14 @@ public class SwaggerConfig {
public Docket docket(Environment environment) {
// 设置要显示的Swagger环境
Profiles profiles = Profiles.of("dev", "test","lwh","win","desk","lwhWin");
// 通过environment.acceptsProfiles判断是否在自己设定的环境当中
boolean flag = environment.acceptsProfiles(profiles);
// Profiles profiles = Profiles.of("dev", "test","lwh","win","desk","lwhWin");
// // 通过environment.acceptsProfiles判断是否在自己设定的环境当中
// boolean flag = environment.acceptsProfiles(profiles);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable(flag) // 是否启动
.enable(true) // 是否启动
.groupName("developer")
.select()
//RequestHandlerSelectors 配置要扫描接口的方式
@ -40,7 +40,7 @@ public class SwaggerConfig {
//none() 都不扫描
// withClassAnnotation()扫描类上的注解 需要注解的class
//withMethodAnnotation 扫描方法上的注解
.apis(RequestHandlerSelectors.basePackage("com.osredm.osredmcompbackend.controller"))
.apis(RequestHandlerSelectors.basePackage("com.osredm.codescan.controller"))
// 过滤 什么 路径
// .paths(PathSelectors.ant("/user"))
.build();

View File

@ -0,0 +1,26 @@
package com.osredm.codescan.controller;
import com.osredm.codescan.entity.NicadInfo;
import com.osredm.codescan.service.NicadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.HashMap;
@RestController
@RequestMapping("/nicad")
public class NicadController {
@Autowired
NicadService nicadService;
@PostMapping("/check")
public HashMap<String,Object>Check(@RequestBody NicadInfo nicadInfo) {
return nicadService.check(nicadInfo);
}
}

View File

@ -30,7 +30,8 @@ public class UploadController {
boolean result = FileUploadUtil.uploadFileWithName(file, storeConfig.getFilePath(), fileName);
if (result) {
map.put("status",SUCCESS);
map.put("url","/api/files/"+ fileName);
// 由于后期接口是脚本运行 所以不需要映射 直接返回文件名称即可
map.put("fileName",fileName);
}else {
map.put("status",FAIL);
}

View File

@ -0,0 +1,13 @@
package com.osredm.codescan.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class NicadInfo {
private String fileName;
private String language;
}

View File

@ -0,0 +1,11 @@
package com.osredm.codescan.service;
import com.osredm.codescan.entity.NicadInfo;
import java.io.IOException;
import java.util.HashMap;
public interface NicadService {
HashMap<String,Object>check(NicadInfo nicadInfo);
}

View File

@ -0,0 +1,110 @@
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;
}
}
}

View File

@ -120,7 +120,7 @@ public class FileUploadUtil {
String line = "";
String line1;
while ((line1 = br.readLine()) != null) {
line = line + line1 + '\n' ;
line = line + line1 ;
}
return line;
} catch (IOException e) {

View File

@ -0,0 +1,89 @@
package com.osredm.codescan.utils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.dom4j.*;
import java.util.List;
import java.util.Map;
public class XmlJsonUtils {
/**
* xml转json
* @param xmlStr xml字符串
* @return
*/
public static String xml2Json(String xmlStr) {
try {
Document doc = DocumentHelper.parseText(xmlStr);
JSONObject json = new JSONObject();
dom4j2Json(doc.getRootElement(), json);
return JSONObject.toJSONString(json);
} catch (DocumentException e) {
System.out.println(e);
return null;
}
}
/**
* xml转json
*
* @param element
* @param json
*/
private static void dom4j2Json(Element element, JSONObject json) {
//如果是属性
for (Object o : element.attributes()) {
Attribute attr = (Attribute) o;
if (!StringUtils.isBlank(attr.getValue())) {
json.put("@" + attr.getName(), attr.getValue());
}
}
List<Element> chdEl = element.elements();
if (chdEl.isEmpty() && !StringUtils.isBlank(element.getText())) {//如果没有子元素,只有一个值
json.put(element.getName(), element.getText());
}
for (Element e : chdEl) {//有子元素
if (!e.elements().isEmpty()) {//子元素也有子元素
JSONObject chdjson = new JSONObject();
dom4j2Json(e, chdjson);
Object o = json.get(e.getName());
if (o != null) {
JSONArray jsona = null;
if (o instanceof JSONObject) {//如果此元素已存在,则转为jsonArray
JSONObject jsono = (JSONObject) o;
json.remove(e.getName());
jsona = new JSONArray();
jsona.add(jsono);
jsona.add(chdjson);
}
if (o instanceof JSONArray) {
jsona = (JSONArray) o;
jsona.add(chdjson);
}
json.put(e.getName(), jsona);
} else {
if (!chdjson.isEmpty()) {
json.put(e.getName(), chdjson);
}
}
} else {//子元素没有子元素
for (Object o : element.attributes()) {
Attribute attr = (Attribute) o;
if (!StringUtils.isBlank(attr.getValue())) {
json.put("@" + attr.getName(), attr.getValue());
}
}
if (!e.getText().isEmpty()) {
json.put(e.getName(), e.getText());
}
}
}
}
}

View File

@ -1 +1,29 @@
server.port=8011
#MyBatis
mybatis_config_file=mybatis-config.xml
mapper_path=/mapper/**.xml
entity_package=com.osredm.osredmcompbackend.entity,com.osredm.osredmcompbackend.vo
#MySQL
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://114.116.228.69:8006/osredm_comp?useUnicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true&allowMultiQueries=true
spring.datasource.username=root
spring.datasource.password=HskyOsredm@163.com
#swagger
spring.mvc.pathmatch.matching-strategy=ant_path_matcher
localPath=file:/root/code-scan/nicad/user-code/
file.filePath=/root/code-scan/nicad/user-code/
fileUploadPath=/root/code-scan/nicad/user-code/
domain=http://localhost:8011/
spring.servlet.multipart.max-file-size = 1000MB
spring.servlet.multipart.max-request-size=10000MB