Explorar el Código

fix: HttpService sendPost
Project AddMember

mjh hace 3 años
padre
commit
e7d2db38c6

+ 1 - 1
seecoder-portal-server/src/main/java/cn/seecoder/seecoderportalserver/service/HttpService.java

@@ -9,7 +9,7 @@ import java.util.List;
 import java.util.Map;
 
 public interface HttpService {
-    String sendPost(String uri, JSONObject requestBody, String token) throws Exception;
+    String sendPost(String uri, JSONObject requestBody, List<NameValuePair> params, String token) throws Exception;
 
     String sendPostFile(String uri, MultipartFile file, JSONObject requestBody, String token) throws Exception;
 

+ 14 - 8
seecoder-portal-server/src/main/java/cn/seecoder/seecoderportalserver/serviceImpl/HttpServiceImpl.java

@@ -47,12 +47,18 @@ public class HttpServiceImpl implements HttpService {
     private final static Logger log = LoggerFactory.getLogger(HttpService.class);
 
     @Override
-    public String sendPost(String uri, JSONObject requestBody, String token) throws Exception {
-        StringEntity requestEntity = new StringEntity(JSON.toJSONString(requestBody), StandardCharsets.UTF_8);
-        HttpPost httpPost = new HttpPost(uri);
+    public String sendPost(String uri, JSONObject requestBody, List<NameValuePair> params, String token) throws Exception {
+        String completedUri;
+        if (params != null && !params.isEmpty()) {
+            String requestParam = EntityUtils.toString(new UrlEncodedFormEntity(params, Consts.UTF_8));
+            completedUri = uri + "?" + requestParam;
+        } else {
+            completedUri = uri;
+        }
+        HttpPost httpPost = new HttpPost(completedUri);
         httpPost.setHeader("Authorization", token);
         httpPost.setHeader("Content-Type", "application/json;chartset=UTF-8");
-        httpPost.setEntity(requestEntity);
+        httpPost.setEntity(new StringEntity(JSON.toJSONString(requestBody), StandardCharsets.UTF_8));
         httpPost.setConfig(RequestConfig.DEFAULT);
         CloseableHttpClient client = HttpClients.createDefault();
         CloseableHttpResponse response = client.execute(httpPost);
@@ -67,7 +73,7 @@ public class HttpServiceImpl implements HttpService {
         CloseableHttpClient client = HttpClients.createDefault();
 
         HttpPost httpPost = new HttpPost(uri); //创建http请求
-        if(token != null) {
+        if (token != null) {
             httpPost.setHeader("Authorization", token); //设置请求头
         }
 //        httpPost.setConfig(RequestConfig.DEFAULT);
@@ -126,7 +132,7 @@ public class HttpServiceImpl implements HttpService {
         CloseableHttpClient httpClient = HttpClients.createDefault();
         HttpDelete httpDelete = new HttpDelete(uri);
         httpDelete.setConfig(RequestConfig.DEFAULT);
-        if(token != null) {
+        if (token != null) {
             httpDelete.setHeader("Authorization", token);
         }
         CloseableHttpResponse response = httpClient.execute(httpDelete);
@@ -141,7 +147,7 @@ public class HttpServiceImpl implements HttpService {
     public String sendPut(String uri, JSONObject requestBody, String token) throws Exception {
         CloseableHttpClient httpClient = HttpClients.createDefault();
         HttpPut httpPut = new HttpPut(uri);
-        if(token != null) {
+        if (token != null) {
             httpPut.setHeader("Authorization", token);
         }
         httpPut.setHeader("Content-Type", "application/json;chartset=UTF-8");
@@ -158,7 +164,7 @@ public class HttpServiceImpl implements HttpService {
     public String sendPutFile(String uri, MultipartFile file, String path, String token) throws Exception {
         CloseableHttpClient httpClient = HttpClients.createDefault();
         HttpPut httpPut = new HttpPut(uri);
-        if(token != null) {
+        if (token != null) {
             httpPut.setHeader("Authorization", token);
         }
         String filename = file.getOriginalFilename();

+ 12 - 13
seecoder-portal-server/src/main/java/cn/seecoder/seecoderportalserver/serviceImpl/ProjectServiceImpl.java

@@ -147,14 +147,15 @@ public class ProjectServiceImpl implements ProjectService {
         if (!courseService.checkSelection(SecurityUtils.getCurrentUser().getId(), courseProject.getCourse().getId())) {
             throw CustomErrorException.unauthorizedUser();
         }
-        JSONObject devcloudResult = devcloudService.forkProject(projectId, name);
-        log.info("devcloudService.forkProject: {}", devcloudResult);
-        CourseProject forkCourseProject = new CourseProject();
-        Integer newProjectId = devcloudResult.getInteger("projectId");
-        forkCourseProject.setProjectId(newProjectId);
-        forkCourseProject.setCourse(courseProject.getCourse());
-        forkCourseProject.setPublic(false);
-        courseProjectRepository.save(forkCourseProject);
+//        JSONObject devcloudResult = devcloudService.forkProject(projectId, name);
+//        log.info("devcloudService.forkProject: {}", devcloudResult);
+//        CourseProject forkCourseProject = new CourseProject();
+//        Integer newProjectId = devcloudResult.getInteger("projectId");
+        Integer newProjectId = 6520;
+//        forkCourseProject.setProjectId(newProjectId);
+//        forkCourseProject.setCourse(courseProject.getCourse());
+//        forkCourseProject.setPublic(false);
+//        courseProjectRepository.save(forkCourseProject);
         // 若当前用户已组队,则将队友添加到当前项目当中
         List<UserVO> members;
         try {
@@ -162,11 +163,9 @@ public class ProjectServiceImpl implements ProjectService {
         } catch (BadSqlGrammarException e) {
             members = new ArrayList<>();
         }
-        if (members.size() != 0) {
-            for (UserVO member : members) {
-                if (!member.getPhone().equals(SecurityUtils.getCurrentUser().getPhone())) {
-                    this.addProjectMember(newProjectId, member.getPhone());
-                }
+        for (UserVO member : members) {
+            if (!member.getPhone().equals(SecurityUtils.getCurrentUser().getPhone())) {
+                this.addProjectMember(newProjectId, member.getPhone());
             }
         }
         return findProjectById(newProjectId);

+ 46 - 32
seecoder-portal-server/src/main/java/cn/seecoder/seecoderportalserver/serviceImpl/subsystem/CoderService.java

@@ -32,7 +32,7 @@ public class CoderService implements SubsystemService {
         requestBody.put("price", courseVO.getPrice().toString());
         String result = null;
         try {
-            result = httpService.sendPost(uri, requestBody, token);
+            result = httpService.sendPost(uri, requestBody, null, token);
         } catch (Exception e) {
             e.printStackTrace();
         }
@@ -43,15 +43,15 @@ public class CoderService implements SubsystemService {
     public void deleteCourse(CourseVO courseVO) {
     }
 
-    public JSONObject getAllCourse(int page, int limit){
+    public JSONObject getAllCourse(int page, int limit) {
         String token = httpService.getToken();
         List<NameValuePair> params = new ArrayList<>();
         params.add(new BasicNameValuePair("page", String.valueOf(page)));
         params.add(new BasicNameValuePair("limit", String.valueOf(limit)));
         String result = null;
         try {
-            result = httpService.sendGet(coderUrlPrefix+"/course", params, token);
-        } catch (Exception e){
+            result = httpService.sendGet(coderUrlPrefix + "/course", params, token);
+        } catch (Exception e) {
             e.printStackTrace();
         }
         return JSON.parseObject(result);
@@ -140,7 +140,7 @@ public class CoderService implements SubsystemService {
         requestBody.put("code", code);
         String result = null;
         try {
-            result = httpService.sendPost(uri, requestBody, token);
+            result = httpService.sendPost(uri, requestBody, null, token);
         } catch (Exception e) {
             e.printStackTrace();
         }
@@ -149,10 +149,11 @@ public class CoderService implements SubsystemService {
 
     /**
      * 获取Coder题目
-     * @param page 页号
-     * @param limit 页容量
+     *
+     * @param page    页号
+     * @param limit   页容量
      * @param typeStr 题目类型(固定格式的字符串)
-     * @param name 搜索内容
+     * @param name    搜索内容
      * @return Json格式数据
      */
     public JSONObject retrievePageProblem(int page, int limit, String typeStr, String name) {
@@ -164,8 +165,8 @@ public class CoderService implements SubsystemService {
         params.add(new BasicNameValuePair("name", String.valueOf(name)));
         String result = null;
         try {
-            result = httpService.sendGet(coderUrlPrefix+"/problem", params, token);
-        }catch (Exception e){
+            result = httpService.sendGet(coderUrlPrefix + "/problem", params, token);
+        } catch (Exception e) {
             e.printStackTrace();
         }
         return JSON.parseObject(result);
@@ -173,16 +174,17 @@ public class CoderService implements SubsystemService {
 
     /**
      * 创建Coder考试
+     *
      * @param examDTO 考试对象
      * @return 返回Json格式数据,前端自行处理。TODO 各个子系统返回的数据,后端进行处理,统一返回格式化数据
      */
-    public JSONObject createCoderExam(JSONObject examDTO){
+    public JSONObject createCoderExam(JSONObject examDTO) {
         String token = httpService.getToken();
         JSONObject requestBody = new JSONObject(examDTO);
         String result = null;
         try {
-            result = httpService.sendPost(coderUrlPrefix+"/exam", requestBody, token);
-        } catch (Exception e){
+            result = httpService.sendPost(coderUrlPrefix + "/exam", requestBody, null, token);
+        } catch (Exception e) {
             e.printStackTrace();
         }
         return JSON.parseObject(result);
@@ -190,6 +192,7 @@ public class CoderService implements SubsystemService {
 
     /**
      * 删除Coder课程
+     *
      * @param examId
      * @return
      */
@@ -197,7 +200,7 @@ public class CoderService implements SubsystemService {
         String token = httpService.getToken();
         String result = null;
         try {
-            result = httpService.sendDelete(coderUrlPrefix+"/exam/"+examId, null, token);
+            result = httpService.sendDelete(coderUrlPrefix + "/exam/" + examId, null, token);
         } catch (Exception e) {
             e.printStackTrace();
         }
@@ -206,6 +209,7 @@ public class CoderService implements SubsystemService {
 
     /**
      * 更新Coder课程
+     *
      * @param examId
      * @param examDTO
      * @return
@@ -214,7 +218,7 @@ public class CoderService implements SubsystemService {
         String token = httpService.getToken();
         String result = null;
         try {
-            result = httpService.sendPut(coderUrlPrefix+"/exam/"+examId, examDTO, token);
+            result = httpService.sendPut(coderUrlPrefix + "/exam/" + examId, examDTO, token);
         } catch (Exception e) {
             e.printStackTrace();
         }
@@ -223,6 +227,7 @@ public class CoderService implements SubsystemService {
 
     /**
      * 加入Coder考试
+     *
      * @param examId
      * @return
      */
@@ -230,7 +235,7 @@ public class CoderService implements SubsystemService {
         String token = httpService.getToken();
         String result = null;
         try {
-            result = httpService.sendPost(coderUrlPrefix+"/exam/"+examId+"/join", null, token);
+            result = httpService.sendPost(coderUrlPrefix + "/exam/" + examId + "/join", null, null, token);
         } catch (Exception e) {
             e.printStackTrace();
         }
@@ -240,6 +245,7 @@ public class CoderService implements SubsystemService {
 
     /**
      * 根据id获得Coder的考试信息
+     *
      * @param examId
      * @return
      */
@@ -247,7 +253,7 @@ public class CoderService implements SubsystemService {
         String token = httpService.getToken();
         String result = null;
         try {
-            result = httpService.sendGet(coderUrlPrefix+"/exam/"+examId,null, token);
+            result = httpService.sendGet(coderUrlPrefix + "/exam/" + examId, null, token);
         } catch (Exception e) {
             e.printStackTrace();
         }
@@ -257,6 +263,7 @@ public class CoderService implements SubsystemService {
 
     /**
      * 根据id获取Coder代码题
+     *
      * @param problemId
      * @return
      */
@@ -264,7 +271,7 @@ public class CoderService implements SubsystemService {
         String token = httpService.getToken();
         String result = null;
         try {
-            result = httpService.sendGet(coderUrlPrefix+"/problem/"+problemId,null, token);
+            result = httpService.sendGet(coderUrlPrefix + "/problem/" + problemId, null, token);
         } catch (Exception e) {
             e.printStackTrace();
         }
@@ -274,6 +281,7 @@ public class CoderService implements SubsystemService {
 
     /**
      * TODO 创建Coder代码题,文件上传
+     *
      * @param problemDTO
      * @param file
      * @return
@@ -281,14 +289,13 @@ public class CoderService implements SubsystemService {
     public JSONObject createCoderProblem(JSONObject problemDTO, MultipartFile file) {
         String token = httpService.getToken();
         String result = null;
-        try{
-            result = httpService.sendPostFile(coderUrlPrefix+"/problem", file, problemDTO, token);
+        try {
+            result = httpService.sendPostFile(coderUrlPrefix + "/problem", file, problemDTO, token);
 //            result = httpService.sendPostFile("http://localhost:8081/api/problem", file, problemDTO, token);
 //            HttpPost httpPost = new HttpPost("http://localhost:8081/api/problem");
 
 
-
-        }catch (Exception e) {
+        } catch (Exception e) {
             e.printStackTrace();
         }
         return JSON.parseObject(result);
@@ -297,6 +304,7 @@ public class CoderService implements SubsystemService {
 
     /**
      * 根据id删除代码题
+     *
      * @param problemId
      * @return
      */
@@ -304,7 +312,7 @@ public class CoderService implements SubsystemService {
         String token = httpService.getToken();
         String result = null;
         try {
-            result = httpService.sendDelete(coderUrlPrefix+"/problem/"+problemId, null, token);
+            result = httpService.sendDelete(coderUrlPrefix + "/problem/" + problemId, null, token);
         } catch (Exception e) {
             e.printStackTrace();
         }
@@ -314,6 +322,7 @@ public class CoderService implements SubsystemService {
 
     /**
      * 更新Coder代码题
+     *
      * @param problemId
      * @param path
      * @param file
@@ -323,7 +332,7 @@ public class CoderService implements SubsystemService {
         String token = httpService.getToken();
         String result = null;
         try {
-            result = httpService.sendPutFile(coderUrlPrefix+"/problem/"+problemId, file, path, token);
+            result = httpService.sendPutFile(coderUrlPrefix + "/problem/" + problemId, file, path, token);
         } catch (Exception e) {
             e.printStackTrace();
         }
@@ -333,6 +342,7 @@ public class CoderService implements SubsystemService {
 
     /**
      * 获取代码题的具体文件信息
+     *
      * @param problemId
      * @param filename
      * @return
@@ -343,7 +353,7 @@ public class CoderService implements SubsystemService {
         params.add(new BasicNameValuePair("name", String.valueOf(filename)));
         String result = null;
         try {
-            result = httpService.sendGet(coderUrlPrefix+"/problem/"+problemId+"/file", params,token);
+            result = httpService.sendGet(coderUrlPrefix + "/problem/" + problemId + "/file", params, token);
         } catch (Exception e) {
             e.printStackTrace();
         }
@@ -353,6 +363,7 @@ public class CoderService implements SubsystemService {
 
     /**
      * 根据id获取考试结果
+     *
      * @param examId
      * @return
      */
@@ -360,8 +371,8 @@ public class CoderService implements SubsystemService {
         String token = httpService.getToken();
         String result = null;
         try {
-            result = httpService.sendGet(coderUrlPrefix+"/exam/"+examId+"/result", null, token);
-        }catch (Exception e) {
+            result = httpService.sendGet(coderUrlPrefix + "/exam/" + examId + "/result", null, token);
+        } catch (Exception e) {
             e.printStackTrace();
         }
         return JSON.parseObject(result);
@@ -369,6 +380,7 @@ public class CoderService implements SubsystemService {
 
     /**
      * 根据id获取某个结果的详细信息
+     *
      * @param resultId
      * @return
      */
@@ -376,7 +388,7 @@ public class CoderService implements SubsystemService {
         String token = httpService.getToken();
         String result = null;
         try {
-            result = httpService.sendGet(coderUrlPrefix+"/result/"+resultId, null, token);
+            result = httpService.sendGet(coderUrlPrefix + "/result/" + resultId, null, token);
         } catch (Exception e) {
             e.printStackTrace();
         }
@@ -385,6 +397,7 @@ public class CoderService implements SubsystemService {
 
     /**
      * 根据id获取某次提交的具体信息
+     *
      * @param commitId
      * @return
      */
@@ -392,7 +405,7 @@ public class CoderService implements SubsystemService {
         String token = httpService.getToken();
         String result = null;
         try {
-            result = httpService.sendGet(coderUrlPrefix+"/commit/"+commitId, null, token);
+            result = httpService.sendGet(coderUrlPrefix + "/commit/" + commitId, null, token);
         } catch (Exception e) {
             e.printStackTrace();
         }
@@ -401,6 +414,7 @@ public class CoderService implements SubsystemService {
 
     /**
      * 根据id获取某次记录的具体信息
+     *
      * @param recordId
      * @return
      */
@@ -408,7 +422,7 @@ public class CoderService implements SubsystemService {
         String token = httpService.getToken();
         String result = null;
         try {
-            result = httpService.sendGet(coderUrlPrefix+"/record/"+recordId, null, token);
+            result = httpService.sendGet(coderUrlPrefix + "/record/" + recordId, null, token);
         } catch (Exception e) {
             e.printStackTrace();
         }
@@ -420,7 +434,7 @@ public class CoderService implements SubsystemService {
         String token = httpService.getToken();
         String result = null;
         try {
-            result = httpService.sendGet(coderUrlPrefix+"/exam/"+examId+"/showConsole", null, token);
+            result = httpService.sendGet(coderUrlPrefix + "/exam/" + examId + "/showConsole", null, token);
         } catch (Exception e) {
             e.printStackTrace();
         }
@@ -434,7 +448,7 @@ public class CoderService implements SubsystemService {
 //        JSONObject requestBody = new JSONObject();
 //        requestBody.put("showConsole", showConsole);
         try {
-            result = httpService.sendPost(coderUrlPrefix+"/exam/"+examId+"/showConsole?showConsole="+showConsole, null, token);
+            result = httpService.sendPost(coderUrlPrefix + "/exam/" + examId + "/showConsole?showConsole=" + showConsole, null, null, token);
         } catch (Exception e) {
             e.printStackTrace();
         }

+ 7 - 4
seecoder-portal-server/src/main/java/cn/seecoder/seecoderportalserver/serviceImpl/subsystem/DevcloudServiceImpl.java

@@ -100,6 +100,7 @@ public class DevcloudServiceImpl implements DevcloudService {
                     httpService.sendPost(
                             devcloudUri + "/project/create",
                             project,
+                            null,
                             httpService.getToken()
                     )
             );
@@ -124,6 +125,7 @@ public class DevcloudServiceImpl implements DevcloudService {
                     httpService.sendPost(
                             devcloudUri + "/fork",
                             requestBody,
+                            null,
                             httpService.getToken()
                     )
             );
@@ -139,14 +141,15 @@ public class DevcloudServiceImpl implements DevcloudService {
     @Override
     public JSONObject addMember(Integer projectId, String phone) {
         JSONObject result;
-        JSONObject requestBody = new JSONObject();
-        requestBody.put("projectId", String.valueOf(projectId));
-        requestBody.put("phone", phone);
+        List<NameValuePair> params = new ArrayList<>();
+        params.add(new BasicNameValuePair("projectId", String.valueOf(projectId)));
+        params.add(new BasicNameValuePair("phone", phone));
         try {
             result = JSONObject.parseObject(
                     httpService.sendPost(
                             devcloudUri + "/project/members",
-                            requestBody,
+                            null,
+                            params,
                             httpService.getToken()
                     )
             );

+ 4 - 4
seecoder-portal-server/src/main/java/cn/seecoder/seecoderportalserver/serviceImpl/subsystem/EvalService.java

@@ -30,7 +30,7 @@ public class EvalService implements SubsystemService {
         requestBody.put("isPublic", true);
         String result = null;
         try {
-            result = httpService.sendPost(uri, requestBody, token);
+            result = httpService.sendPost(uri, requestBody, null, token);
         } catch (Exception e) {
             e.printStackTrace();
         }
@@ -48,7 +48,7 @@ public class EvalService implements SubsystemService {
     }
 
     @Override
-    public JSONObject findTeacherExamsByCourseId(int courseId){
+    public JSONObject findTeacherExamsByCourseId(int courseId) {
         String uri = "https://eval-server.seec.seecoder.cn/api/exam/teacher/course/" + courseId;
         String token = httpService.getToken();
         List<NameValuePair> params = new ArrayList<>();
@@ -62,7 +62,7 @@ public class EvalService implements SubsystemService {
     }
 
     @Override
-    public JSONObject findStudentExamsByCourseId(int courseId){
+    public JSONObject findStudentExamsByCourseId(int courseId) {
         String uri = "https://eval-server.seec.seecoder.cn/api/exam/course/" + courseId;
         String token = httpService.getToken();
         List<NameValuePair> params = new ArrayList<>();
@@ -98,7 +98,7 @@ public class EvalService implements SubsystemService {
     }
 
     @Override
-    public JSONObject findRecommendExamsByCourseId(int courseId){
+    public JSONObject findRecommendExamsByCourseId(int courseId) {
         String uri = "https://eval-server.seec.seecoder.cn/api/exam/recommend/" + courseId;
         String result = null;
         try {