1
|
package eu.dnetlib.parthenos.catalogue;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
|
5
|
import com.fasterxml.jackson.databind.JsonNode;
|
6
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
7
|
import eu.dnetlib.parthenos.jrr.ParthenosRegistryResource;
|
8
|
|
9
|
/**
|
10
|
* Created by Alessia Bardi on 08/03/2018.
|
11
|
*
|
12
|
* @author Alessia Bardi
|
13
|
* TODO: reimplement for new RESTful API
|
14
|
*/
|
15
|
public class CatalogueAPIResponse {
|
16
|
|
17
|
private JsonNode response;
|
18
|
|
19
|
public void setResponseBody(final String responseBody) throws IOException {
|
20
|
ObjectMapper mapper = new ObjectMapper();
|
21
|
response = mapper.readTree(responseBody);
|
22
|
}
|
23
|
|
24
|
public boolean isSuccess(){
|
25
|
return response.get("success").asBoolean();
|
26
|
}
|
27
|
|
28
|
|
29
|
public ParthenosRegistryResource getParthenosRegistryResource() throws IOException {
|
30
|
if(!hasParthenosRegistryResource()) return null;
|
31
|
else{
|
32
|
JsonNode result = response.get("result");
|
33
|
return parseResult(result);
|
34
|
}
|
35
|
}
|
36
|
|
37
|
public boolean hasParthenosRegistryResource(){
|
38
|
JsonNode result = response.path("result");
|
39
|
return !result.isMissingNode();
|
40
|
}
|
41
|
|
42
|
|
43
|
public String getErrorMessage(){
|
44
|
if(isSuccess()) return "";
|
45
|
//TODO: tell d4science guys that error messages sometimes are in error.name, sometimes in message
|
46
|
JsonNode message = response.path("message");
|
47
|
if(!message.isMissingNode()) return message.toString();
|
48
|
else{
|
49
|
JsonNode error = response.get("error");
|
50
|
if(!error.isMissingNode()){
|
51
|
return error.toString();
|
52
|
}
|
53
|
}
|
54
|
return "Error message not available in error or message fields";
|
55
|
}
|
56
|
|
57
|
protected ParthenosRegistryResource parseResult(JsonNode result) throws IOException {
|
58
|
String uuid = result.get("id").asText();
|
59
|
String type = null;
|
60
|
JsonNode extras = result.get("extras");
|
61
|
for(JsonNode jn : extras){
|
62
|
String key = jn.get("key").asText();
|
63
|
if(key.equals("system:type")){
|
64
|
type = jn.get("value").asText();
|
65
|
}
|
66
|
}
|
67
|
return new ParthenosRegistryResource().setJson(result.asText()).setType(type).setUuid(uuid);
|
68
|
}
|
69
|
|
70
|
}
|