forked from jesperfj/force-rest-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResourceRepresentation.java
80 lines (69 loc) · 2.15 KB
/
ResourceRepresentation.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package com.force.api;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.force.api.http.HttpResponse;
/**
* A representation of a resource in the Force.com REST API. The format of the representation
* depends on what you passed to Resource when you requested it.
*
* @author jjoergensen
*
*/
public class ResourceRepresentation {
// For some reason this was made public a long time ago. So now it needs to stay this way
public final ObjectMapper jsonMapper;
HttpResponse response;
public ResourceRepresentation(HttpResponse value) {
jsonMapper = new ObjectMapper();
response = value;
}
public ResourceRepresentation(HttpResponse value, ObjectMapper objectMapper) {
jsonMapper = objectMapper;
response = value;
}
public <T> T as(Class<T> clazz) {
try {
return (T) jsonMapper.readValue(response.getStream(), clazz);
} catch (JsonParseException e) {
throw new ResourceException(e);
} catch (JsonMappingException e) {
throw new ResourceException(e);
} catch (IOException e) {
throw new ResourceException(e);
}
}
public Map<?,?> asMap() {
try {
return jsonMapper.readValue(response.getStream(), Map.class);
} catch (JsonParseException e) {
throw new ResourceException(e);
} catch (JsonMappingException e) {
throw new ResourceException(e);
} catch (IOException e) {
throw new ResourceException(e);
}
}
public List<?> asList() {
try {
return jsonMapper.readValue(response.getStream(), List.class);
} catch (JsonParseException e) {
throw new ResourceException(e);
} catch (JsonMappingException e) {
throw new ResourceException(e);
} catch (IOException e) {
throw new ResourceException(e);
}
}
/**
*
* @return the HTTP response code of the underlying request if it was between 200 and 299. Any code outside of that
* range will result in an ApiException being thrown before a ResourceRepresentation is instantiated.
*/
public int getResponseCode() {
return response.getResponseCode();
}
}