Search in sources :

Example 11 with JSONObject

use of cn.hutool.json.JSONObject in project smartapp-openapi-java by baidu.

the class ContentSecurityServiceImpl method checkPath.

@Override
public String checkPath(String accessToken, String path, String[] type) throws SmartAppErrorException, OpenAPIErrorException {
    Map<String, Object> params = getRequestMapper(accessToken);
    JSONObject obj = JSONUtil.createObj();
    obj.putOnce("path", path);
    obj.putOnce("type", type);
    String body = JSONUtil.toJsonStr(obj.toString());
    String response = null;
    response = SmartAppHttpUtil.sendHttpPost(CHECK_PATH, params, body);
    System.out.println(response);
    SmartAppResult<String> result = JSONUtil.toBean(response, new TypeReference<SmartAppResult<String>>() {
    }.getType(), true);
    BaiduAssert.error(result, response);
    return result.getData();
}
Also used : JSONObject(cn.hutool.json.JSONObject) JSONObject(cn.hutool.json.JSONObject) TypeReference(cn.hutool.core.lang.TypeReference)

Example 12 with JSONObject

use of cn.hutool.json.JSONObject in project smartapp-openapi-java by baidu.

the class ContentSecurityServiceImpl method checkText.

@Override
public CommonCheckResult checkText(String accessToken, String content, String[] type) throws SmartAppErrorException, OpenAPIErrorException {
    Map<String, Object> params = getRequestMapper(accessToken);
    JSONObject obj = JSONUtil.createObj();
    obj.putOnce("conent", content);
    obj.putOnce("type", type);
    String body = JSONUtil.toJsonStr(obj.toString());
    String response = null;
    response = SmartAppHttpUtil.sendHttpPost(CHECK_TEXT, params, body);
    SmartAppResult<CommonCheckResult> result = JSONUtil.toBean(response, new TypeReference<SmartAppResult<CommonCheckResult>>() {
    }.getType(), true);
    BaiduAssert.error(result, response, true);
    return result.getData();
}
Also used : JSONObject(cn.hutool.json.JSONObject) JSONObject(cn.hutool.json.JSONObject) TypeReference(cn.hutool.core.lang.TypeReference) CommonCheckResult(com.baidu.mapp.developer.bean.contentsecurity.CommonCheckResult)

Example 13 with JSONObject

use of cn.hutool.json.JSONObject in project thinglinks by mqttsnet.

the class ScheduledJob method appInfoListTask.

/**
 * 30秒后执行,每隔5分钟执行, 单位:ms。
 * 获取监控进程
 */
@Scheduled(initialDelay = 28 * 1000L, fixedRate = 300 * 1000)
public void appInfoListTask() {
    JSONObject jsonObject = new JSONObject();
    LogInfo logInfo = new LogInfo();
    Timestamp t = DateUtils.getNowTime();
    logInfo.setHostname(bindIp + ":Agent获取进程列表错误");
    logInfo.setCreateTime(t);
    try {
        JSONObject paramsJson = new JSONObject();
        paramsJson.put("hostname", bindIp);
        List<AppInfo> list = appInfoMapper.selectByParams((Map<String, Object>) new HashMap<>().put("hostname", bindIp));
        if (list != null && list.size() > 0) {
            appInfoList.clear();
            appInfoList = list;
        }
    } catch (Exception e) {
        e.printStackTrace();
        logInfo.setInfoContent(e.toString());
    } finally {
        if (!StringUtils.isEmpty(logInfo.getInfoContent())) {
            jsonObject.put("logInfo", logInfo);
        }
        collectionProducer.senJsonObject(mqConfig.getSystemTopic(), jsonObject.toString());
    }
}
Also used : JSONObject(cn.hutool.json.JSONObject) JSONObject(cn.hutool.json.JSONObject) Timestamp(java.sql.Timestamp) Scheduled(org.springframework.scheduling.annotation.Scheduled)

Example 14 with JSONObject

use of cn.hutool.json.JSONObject in project HOJ by HimitZH.

the class CodeForcesJudge method result.

@Override
public RemoteJudgeRes result() {
    String resJson = getSubmissionResult(getRemoteJudgeDTO().getUsername(), 1000).body();
    JSONObject jsonObject = JSONUtil.parseObj(resJson);
    RemoteJudgeRes remoteJudgeRes = RemoteJudgeRes.builder().status(Constants.Judge.STATUS_JUDGING.getStatus()).build();
    JSONArray results = (JSONArray) jsonObject.get("result");
    for (Object tmp : results) {
        JSONObject result = (JSONObject) tmp;
        long runId = Long.parseLong(result.get("id").toString());
        if (runId == getRemoteJudgeDTO().getSubmitId()) {
            String verdict = (String) result.get("verdict");
            Constants.Judge resultStatus = statusMap.get(verdict);
            if (resultStatus == Constants.Judge.STATUS_JUDGING) {
                return RemoteJudgeRes.builder().status(resultStatus.getStatus()).build();
            } else if (resultStatus == null) {
                return RemoteJudgeRes.builder().status(Constants.Judge.STATUS_PENDING.getStatus()).build();
            }
            remoteJudgeRes.setTime((Integer) result.get("timeConsumedMillis"));
            remoteJudgeRes.setMemory((int) result.get("memoryConsumedBytes") / 1024);
            if (resultStatus == Constants.Judge.STATUS_COMPILE_ERROR) {
                String csrfToken = getCsrfToken(HOST);
                HttpRequest httpRequest = HttpUtil.createPost(HOST + CE_INFO_URL).timeout(30000);
                httpRequest.form(MapUtil.builder(new HashMap<String, Object>()).put("csrf_token", csrfToken).put("submissionId", getRemoteJudgeDTO().getSubmitId().toString()).map());
                HttpResponse response = httpRequest.execute();
                if (response.getStatus() == 200) {
                    JSONObject CEInfoJson = JSONUtil.parseObj(response.body());
                    String CEInfo = CEInfoJson.getStr("checkerStdoutAndStderr#1");
                    remoteJudgeRes.setErrorInfo(CEInfo);
                } else {
                    // 非200则说明cf没有提供编译失败的详情
                    remoteJudgeRes.setErrorInfo("Oops! Because Codeforces does not provide compilation details, it is unable to provide the reason for compilation failure!");
                }
            }
            remoteJudgeRes.setStatus(resultStatus.getStatus());
            return remoteJudgeRes;
        }
    }
    return remoteJudgeRes;
}
Also used : RemoteJudgeRes(top.hcode.hoj.remoteJudge.entity.RemoteJudgeRes) HttpRequest(cn.hutool.http.HttpRequest) JSONObject(cn.hutool.json.JSONObject) JSONArray(cn.hutool.json.JSONArray) Constants(top.hcode.hoj.util.Constants) HttpResponse(cn.hutool.http.HttpResponse) JSONObject(cn.hutool.json.JSONObject)

Example 15 with JSONObject

use of cn.hutool.json.JSONObject in project HOJ by HimitZH.

the class SandboxRun method interactTestCase.

/**
 * @param args                   cmd的命令参数 评测运行的命令
 * @param envs                   测评的环境变量
 * @param userExeName            用户程序的名字
 * @param userFileId             用户程序在编译后返回的id,主要是对应内存中已编译后的文件
 * @param userFileSrc            用户程序文件的绝对路径,如果userFileId存在则为null
 * @param userMaxTime            用户程序的最大测评时间 ms
 * @param userMaxStack           用户程序的最大测评栈空间 mb
 * @param testCaseInputPath      题目数据的输入文件路径
 * @param testCaseInputFileName  题目数据的输入文件名字
 * @param testCaseOutputFilePath 题目数据的输出文件路径
 * @param testCaseOutputFileName 题目数据的输出文件名字
 * @param userOutputFileName     用户程序的输出文件名字
 * @param interactArgs           交互程序运行的cmd命令参数
 * @param interactEnvs           交互程序运行的环境变量
 * @param interactExeSrc         交互程序的exe文件路径
 * @param interactExeName        交互程序的exe文件名字
 * @MethodName interactTestCase
 * @Description 交互评测
 * @Return JSONArray
 * @Since 2022/1/3
 */
public static JSONArray interactTestCase(List<String> args, List<String> envs, String userExeName, String userFileId, String userFileSrc, Long userMaxTime, Long userMaxMemory, Integer userMaxStack, String testCaseInputPath, String testCaseInputFileName, String testCaseOutputFilePath, String testCaseOutputFileName, String userOutputFileName, List<String> interactArgs, List<String> interactEnvs, String interactExeSrc, String interactExeName) throws SystemError {
    /**
     *  注意:用户源代码需要先编译,若是通过编译需要先将文件存入内存,再利用管道判题,同时特殊判题程序必须已编译且存在(否则判题失败,系统错误)!
     */
    JSONObject pipeInputCmd = new JSONObject();
    pipeInputCmd.set("args", args);
    pipeInputCmd.set("env", envs);
    JSONArray files = new JSONArray();
    JSONObject stderr = new JSONObject();
    stderr.set("name", "stderr");
    stderr.set("max", 1024 * 1024 * STDIO_SIZE_MB);
    files.put(new JSONObject());
    files.put(new JSONObject());
    files.put(stderr);
    String inTmp = files.toString().replace("{}", "null");
    pipeInputCmd.set("files", JSONUtil.parseArray(inTmp, false));
    // ms-->ns
    pipeInputCmd.set("cpuLimit", userMaxTime * 1000 * 1000L);
    pipeInputCmd.set("clockLimit", userMaxTime * 1000 * 1000L * 3);
    // byte
    pipeInputCmd.set("memoryLimit", (userMaxMemory + 100) * 1024 * 1024L);
    pipeInputCmd.set("procLimit", maxProcessNumber);
    pipeInputCmd.set("stackLimit", userMaxStack * 1024 * 1024L);
    JSONObject exeFile = new JSONObject();
    if (!StringUtils.isEmpty(userFileId)) {
        exeFile.set("fileId", userFileId);
    } else {
        exeFile.set("src", userFileSrc);
    }
    JSONObject copyIn = new JSONObject();
    copyIn.set(userExeName, exeFile);
    pipeInputCmd.set("copyIn", copyIn);
    pipeInputCmd.set("copyOut", new JSONArray());
    // 管道输出,用户程序输出数据经过特殊判题程序后,得到的最终输出结果。
    JSONObject pipeOutputCmd = new JSONObject();
    pipeOutputCmd.set("args", interactArgs);
    pipeOutputCmd.set("env", interactEnvs);
    JSONArray outFiles = new JSONArray();
    JSONObject outStderr = new JSONObject();
    outStderr.set("name", "stderr");
    outStderr.set("max", 1024 * 1024 * STDIO_SIZE_MB);
    outFiles.put(new JSONObject());
    outFiles.put(new JSONObject());
    outFiles.put(outStderr);
    String outTmp = outFiles.toString().replace("{}", "null");
    pipeOutputCmd.set("files", JSONUtil.parseArray(outTmp, false));
    // ms-->ns
    pipeOutputCmd.set("cpuLimit", userMaxTime * 1000 * 1000L * 2);
    pipeOutputCmd.set("clockLimit", userMaxTime * 1000 * 1000L * 3 * 2);
    // byte
    pipeOutputCmd.set("memoryLimit", (userMaxMemory + 100) * 1024 * 1024L * 2);
    pipeOutputCmd.set("procLimit", maxProcessNumber);
    pipeOutputCmd.set("stackLimit", STACK_LIMIT_MB * 1024 * 1024L);
    JSONObject spjExeFile = new JSONObject();
    spjExeFile.set("src", interactExeSrc);
    JSONObject stdInputFileSrc = new JSONObject();
    stdInputFileSrc.set("src", testCaseInputPath);
    JSONObject stdOutFileSrc = new JSONObject();
    stdOutFileSrc.set("src", testCaseOutputFilePath);
    JSONObject interactiveCopyIn = new JSONObject();
    interactiveCopyIn.set(interactExeName, spjExeFile);
    interactiveCopyIn.set(testCaseInputFileName, stdInputFileSrc);
    interactiveCopyIn.set(testCaseOutputFileName, stdOutFileSrc);
    pipeOutputCmd.set("copyIn", interactiveCopyIn);
    pipeOutputCmd.set("copyOut", new JSONArray().put(userOutputFileName));
    JSONArray cmdList = new JSONArray();
    cmdList.put(pipeInputCmd);
    cmdList.put(pipeOutputCmd);
    JSONObject param = new JSONObject();
    // 添加cmd指令
    param.set("cmd", cmdList);
    // 添加管道映射
    JSONArray pipeMapping = new JSONArray();
    // 用户程序
    JSONObject user = new JSONObject();
    JSONObject userIn = new JSONObject();
    userIn.set("index", 0);
    userIn.set("fd", 1);
    JSONObject userOut = new JSONObject();
    userOut.set("index", 1);
    userOut.set("fd", 0);
    user.set("in", userIn);
    user.set("out", userOut);
    user.set("max", STDIO_SIZE_MB * 1024 * 1024);
    user.set("proxy", true);
    user.set("name", "stdout");
    // 评测程序
    JSONObject judge = new JSONObject();
    JSONObject judgeIn = new JSONObject();
    judgeIn.set("index", 1);
    judgeIn.set("fd", 1);
    JSONObject judgeOut = new JSONObject();
    judgeOut.set("index", 0);
    judgeOut.set("fd", 0);
    judge.set("in", judgeIn);
    judge.set("out", judgeOut);
    judge.set("max", STDIO_SIZE_MB * 1024 * 1024);
    judge.set("proxy", true);
    judge.set("name", "stdout");
    // 添加到管道映射列表
    pipeMapping.add(user);
    pipeMapping.add(judge);
    param.set("pipeMapping", pipeMapping);
    // 调用判题安全沙箱
    JSONArray result = instance.run("/run", param);
    JSONObject userRes = (JSONObject) result.get(0);
    JSONObject interactiveRes = (JSONObject) result.get(1);
    userRes.set("status", RESULT_MAP_STATUS.get(userRes.getStr("status")));
    interactiveRes.set("status", RESULT_MAP_STATUS.get(interactiveRes.getStr("status")));
    return result;
}
Also used : JSONObject(cn.hutool.json.JSONObject) JSONArray(cn.hutool.json.JSONArray)

Aggregations

JSONObject (cn.hutool.json.JSONObject)103 JSONArray (cn.hutool.json.JSONArray)27 Test (org.junit.Test)10 FileWriter (cn.hutool.core.io.file.FileWriter)7 FileReader (cn.hutool.core.io.file.FileReader)6 JWT (cn.hutool.jwt.JWT)6 IOException (java.io.IOException)6 HashMap (java.util.HashMap)6 RequiresAuthentication (org.apache.shiro.authz.annotation.RequiresAuthentication)6 RequiresRoles (org.apache.shiro.authz.annotation.RequiresRoles)6 SystemError (top.hcode.hoj.common.exception.SystemError)6 GuildedException (vip.floatationdevice.guilded4j.exception.GuildedException)6 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)5 Transactional (org.springframework.transaction.annotation.Transactional)5 Constants (top.hcode.hoj.util.Constants)5 File (java.io.File)4 HttpSession (javax.servlet.http.HttpSession)4 Scheduled (org.springframework.scheduling.annotation.Scheduled)4 UserRolesVo (top.hcode.hoj.pojo.vo.UserRolesVo)4 JSONConfig (cn.hutool.json.JSONConfig)3