|
|
@@ -1,6 +1,8 @@
|
|
|
package com.nju.edu.gitlab;
|
|
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
+import com.fasterxml.jackson.core.type.TypeReference;
|
|
|
+import com.fasterxml.jackson.databind.JavaType;
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.nju.edu.gitlab.dto.*;
|
|
|
@@ -48,21 +50,23 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi{
|
|
|
.build();
|
|
|
}
|
|
|
|
|
|
- private <T> T processResponse(ObjectMapper objectMapper, Response response, Class<T> clazz) throws SeecoderGitlabException {
|
|
|
+ private <T> T processResponse(ObjectMapper objectMapper, Response response, TypeReference<T> typeReference) throws SeecoderGitlabException {
|
|
|
try {
|
|
|
validateResponseStatus(response);
|
|
|
String body = response.body().string();
|
|
|
JsonNode node = objectMapper.readTree(body);
|
|
|
- Integer code = Integer.parseInt(node.get("code").asText());
|
|
|
- String msg = node.get("msg").asText();
|
|
|
- if (code != 0) {
|
|
|
- throw new SeecoderGitlabException(BIZ_ERROR_CODE, msg);
|
|
|
- }
|
|
|
- if (clazz == null) {
|
|
|
+ //todo 注释的代码和ResourceResponse字段对应不上,我先改成能用的形式,后续在修改
|
|
|
+ // author: wph
|
|
|
+// Integer code = Integer.parseInt(node.get("code").asText());
|
|
|
+// String msg = node.get("msg").asText();
|
|
|
+// if (code != 0) {
|
|
|
+// throw new SeecoderGitlabException(BIZ_ERROR_CODE, msg);
|
|
|
+// }
|
|
|
+ if (typeReference == null) {
|
|
|
return null;
|
|
|
}
|
|
|
- if (node.get("result")!=null)
|
|
|
- return objectMapper.treeToValue(node.get("result"), clazz);
|
|
|
+ if (node.get("res")!=null)
|
|
|
+ return objectMapper.readValue(node.get("res").traverse(), typeReference);
|
|
|
else
|
|
|
throw SeecoderGitlabException.NOT_FOUND_ERROR;
|
|
|
} catch (IOException e) {
|
|
|
@@ -71,6 +75,7 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi{
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
private void validateResponseStatus(Response response) throws SeecoderGitlabException {
|
|
|
if (response.code() == 403) {
|
|
|
throw SeecoderGitlabException.UNAHTORIZATION_ERROR;
|
|
|
@@ -99,7 +104,7 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi{
|
|
|
Request request = builder.build();
|
|
|
final Call call = client.newCall(request);
|
|
|
Response response = call.execute();
|
|
|
- GitlabUserVO gitlabUserVO = processResponse(objectMapper, response, GitlabUserVO.class);
|
|
|
+ GitlabUserVO gitlabUserVO = processResponse(objectMapper, response, new TypeReference<GitlabUserVO>() {});
|
|
|
return gitlabUserVO;
|
|
|
} catch (JsonProcessingException e) {
|
|
|
logger.error("JSON writeValueAsString error", e);
|
|
|
@@ -126,7 +131,7 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi{
|
|
|
Request request = builder.build();
|
|
|
final Call call = client.newCall(request);
|
|
|
Response response = call.execute();
|
|
|
- return processResponse(objectMapper, response, boolean.class);
|
|
|
+ return processResponse(objectMapper, response, new TypeReference<Boolean>() {});
|
|
|
} catch (JsonProcessingException e) {
|
|
|
logger.error("JSON writeValueAsString error", e);
|
|
|
throw SeecoderGitlabException.JSON_ERROR;
|
|
|
@@ -148,7 +153,8 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi{
|
|
|
Request request = builder.build();
|
|
|
final Call call = client.newCall(request);
|
|
|
Response response = call.execute();
|
|
|
- boolean isDeleted = processResponse(objectMapper, response, boolean.class);
|
|
|
+ boolean isDeleted = processResponse(objectMapper, response, new TypeReference<Boolean>() {
|
|
|
+ });
|
|
|
return isDeleted;
|
|
|
} catch (JsonProcessingException e) {
|
|
|
logger.error("JSON writeValueAsString error", e);
|
|
|
@@ -171,7 +177,8 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi{
|
|
|
Request request = builder.build();
|
|
|
final Call call = client.newCall(request);
|
|
|
Response response = call.execute();
|
|
|
- GitlabGroup gitlabGroup = processResponse(objectMapper, response, GitlabGroup.class);
|
|
|
+ GitlabGroup gitlabGroup = processResponse(objectMapper, response, new TypeReference<GitlabGroup>() {
|
|
|
+ });
|
|
|
return gitlabGroup;
|
|
|
} catch (JsonProcessingException e) {
|
|
|
logger.error("JSON writeValueAsString error", e);
|
|
|
@@ -200,7 +207,8 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi{
|
|
|
Request request = builder.build();
|
|
|
final Call call = client.newCall(request);
|
|
|
Response response = call.execute();
|
|
|
- boolean isAdded = processResponse(objectMapper, response, boolean.class);
|
|
|
+ boolean isAdded = processResponse(objectMapper, response, new TypeReference<Boolean>() {
|
|
|
+ });
|
|
|
return isAdded;
|
|
|
} catch (JsonProcessingException e) {
|
|
|
logger.error("JSON writeValueAsString error", e);
|
|
|
@@ -228,7 +236,8 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi{
|
|
|
Request request = builder.build();
|
|
|
final Call call = client.newCall(request);
|
|
|
Response response = call.execute();
|
|
|
- return processResponse(objectMapper, response, ProjectVO.class);
|
|
|
+ return processResponse(objectMapper, response, new TypeReference<ProjectVO>() {
|
|
|
+ });
|
|
|
} catch (JsonProcessingException e) {
|
|
|
logger.error("JSON writeValueAsString error", e);
|
|
|
throw SeecoderGitlabException.JSON_ERROR;
|
|
|
@@ -254,7 +263,8 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi{
|
|
|
Request request = builder.build();
|
|
|
final Call call = client.newCall(request);
|
|
|
Response response = call.execute();
|
|
|
- projectId = processResponse(objectMapper, response, Integer.class);
|
|
|
+ projectId = processResponse(objectMapper, response, new TypeReference<Integer>() {
|
|
|
+ });
|
|
|
return projectId;
|
|
|
} catch (JsonProcessingException e) {
|
|
|
logger.error("JSON writeValueAsString error", e);
|
|
|
@@ -281,7 +291,8 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi{
|
|
|
Request request = builder.build();
|
|
|
final Call call = client.newCall(request);
|
|
|
Response response = call.execute();
|
|
|
- projectId = processResponse(objectMapper, response, Integer.class);
|
|
|
+ projectId = processResponse(objectMapper, response, new TypeReference<Integer>() {
|
|
|
+ });
|
|
|
return projectId;
|
|
|
} catch (JsonProcessingException e) {
|
|
|
logger.error("JSON writeValueAsString error", e);
|
|
|
@@ -305,7 +316,8 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi{
|
|
|
final Call call = client.newCall(request);
|
|
|
Response response = call.execute();
|
|
|
Set<DiffVO> diffVOSet= new HashSet<>();
|
|
|
- diffVOSet=processResponse(objectMapper, response, diffVOSet.getClass());
|
|
|
+ diffVOSet=processResponse(objectMapper, response, new TypeReference<Set<DiffVO>>() {
|
|
|
+ });
|
|
|
return diffVOSet;
|
|
|
} catch (JsonProcessingException e) {
|
|
|
logger.error("JSON writeValueAsString error", e);
|
|
|
@@ -335,7 +347,8 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi{
|
|
|
Request request = builder.build();
|
|
|
final Call call = client.newCall(request);
|
|
|
Response response = call.execute();
|
|
|
- return processResponse(objectMapper, response, boolean.class);
|
|
|
+ return processResponse(objectMapper, response, new TypeReference<Boolean>() {
|
|
|
+ });
|
|
|
} catch (JsonProcessingException e) {
|
|
|
logger.error("JSON writeValueAsString error", e);
|
|
|
throw SeecoderGitlabException.JSON_ERROR;
|
|
|
@@ -357,7 +370,8 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi{
|
|
|
Request request = builder.build();
|
|
|
final Call call = client.newCall(request);
|
|
|
Response response = call.execute();
|
|
|
- return processResponse(objectMapper, response, boolean.class);
|
|
|
+ return processResponse(objectMapper, response, new TypeReference<Boolean>() {
|
|
|
+ });
|
|
|
} catch (JsonProcessingException e) {
|
|
|
logger.error("JSON writeValueAsString error", e);
|
|
|
throw SeecoderGitlabException.JSON_ERROR;
|
|
|
@@ -383,7 +397,8 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi{
|
|
|
Request request = builder.build();
|
|
|
final Call call = client.newCall(request);
|
|
|
Response response = call.execute();
|
|
|
- return processResponse(objectMapper, response, String.class);
|
|
|
+ return processResponse(objectMapper, response, new TypeReference<String>() {
|
|
|
+ });
|
|
|
} catch (JsonProcessingException e) {
|
|
|
logger.error("JSON writeValueAsString error", e);
|
|
|
throw SeecoderGitlabException.JSON_ERROR;
|
|
|
@@ -409,7 +424,8 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi{
|
|
|
Request request = builder.build();
|
|
|
final Call call = client.newCall(request);
|
|
|
Response response = call.execute();
|
|
|
- return processResponse(objectMapper, response, String.class);
|
|
|
+ return processResponse(objectMapper, response, new TypeReference<String>() {
|
|
|
+ });
|
|
|
} catch (JsonProcessingException e) {
|
|
|
logger.error("JSON writeValueAsString error", e);
|
|
|
throw SeecoderGitlabException.JSON_ERROR;
|
|
|
@@ -443,7 +459,8 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi{
|
|
|
Request request = builder.build();
|
|
|
final Call call = client.newCall(request);
|
|
|
Response response = call.execute();
|
|
|
- return processResponse(objectMapper, response, CommitVO.class);
|
|
|
+ return processResponse(objectMapper, response, new TypeReference<CommitVO>() {
|
|
|
+ });
|
|
|
} catch (JsonProcessingException e) {
|
|
|
logger.error("JSON writeValueAsString error", e);
|
|
|
throw SeecoderGitlabException.JSON_ERROR;
|
|
|
@@ -466,7 +483,8 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi{
|
|
|
final Call call = client.newCall(request);
|
|
|
Response response = call.execute();
|
|
|
List<CommitVO> commitVOList=new ArrayList<>();
|
|
|
- commitVOList=processResponse(objectMapper, response, commitVOList.getClass());
|
|
|
+ commitVOList=processResponse(objectMapper, response, new TypeReference<List<CommitVO>>() {
|
|
|
+ });
|
|
|
return commitVOList;
|
|
|
} catch (JsonProcessingException e) {
|
|
|
logger.error("JSON writeValueAsString error", e);
|
|
|
@@ -490,7 +508,8 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi{
|
|
|
final Call call = client.newCall(request);
|
|
|
Response response = call.execute();
|
|
|
List<BranchVO> branchVOList=new ArrayList<>();
|
|
|
- branchVOList=processResponse(objectMapper, response, branchVOList.getClass());
|
|
|
+ branchVOList=processResponse(objectMapper, response, new TypeReference<List<BranchVO>>() {
|
|
|
+ });
|
|
|
return branchVOList;
|
|
|
} catch (JsonProcessingException e) {
|
|
|
logger.error("JSON writeValueAsString error", e);
|
|
|
@@ -519,7 +538,8 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi{
|
|
|
Request request = builder.build();
|
|
|
final Call call = client.newCall(request);
|
|
|
Response response = call.execute();
|
|
|
- boolean isAdded = processResponse(objectMapper, response, boolean.class);
|
|
|
+ boolean isAdded = processResponse(objectMapper, response, new TypeReference<Boolean>() {
|
|
|
+ });
|
|
|
return isAdded;
|
|
|
} catch (JsonProcessingException e) {
|
|
|
logger.error("JSON writeValueAsString error", e);
|
|
|
@@ -543,7 +563,7 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi{
|
|
|
final Call call = client.newCall(request);
|
|
|
Response response = call.execute();
|
|
|
List<ProjectVO> projectVOList=new ArrayList<>();
|
|
|
- projectVOList=processResponse(objectMapper, response, projectVOList.getClass());
|
|
|
+ projectVOList=processResponse(objectMapper, response, new TypeReference<List<ProjectVO>>() {});
|
|
|
return projectVOList;
|
|
|
} catch (JsonProcessingException e) {
|
|
|
logger.error("JSON writeValueAsString error", e);
|
|
|
@@ -567,7 +587,8 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi{
|
|
|
final Call call = client.newCall(request);
|
|
|
Response response = call.execute();
|
|
|
List<Integer> userList=new ArrayList<>();
|
|
|
- userList=processResponse(objectMapper, response, userList.getClass());
|
|
|
+ userList=processResponse(objectMapper, response, new TypeReference<List<Integer>>() {
|
|
|
+ });
|
|
|
return userList;
|
|
|
} catch (JsonProcessingException e) {
|
|
|
logger.error("JSON writeValueAsString error", e);
|