/
ApiController.groovy
186 lines (163 loc) · 5.23 KB
/
ApiController.groovy
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import org.hyperic.hq.hqu.rendit.BaseController
import groovy.xml.StreamingMarkupBuilder
import org.hyperic.hq.hqapi1.ErrorCode
class ApiController extends BaseController {
// Statistics object
private static _methodStats = [:]
private static final STATS_LOCK = new Object()
/**
* Get the ResponseStatus Success XML.
*/
protected Closure getSuccessXML() {
{ doc ->
Status("Success")
}
}
/**
* Get the ResponseStatus Failure XML.
*/
protected Closure getFailureXML(ErrorCode code) {
return getFailureXML(code, code.getReasonText())
}
/**
* Get the ResponseStatus Failure XML with the specified reason text.
*/
protected Closure getFailureXML(ErrorCode code, String reason) {
{ doc ->
Status("Failure")
Error() {
ErrorCode(code.getErrorCode())
ReasonText(reason)
}
}
}
/**
* Get an Agent by id, address or port.
*/
protected getAgent(id, address, port) {
if (id) {
return agentHelper.getAgent(id)
} else if (address && port) {
return agentHelper.getAgent(address, port)
}
return null
}
/**
* Checks view permission for the passed in resource.
* @throws PermissionException if permission is not granted, otherwise
* the passed in Resource is returned.
*/
protected checkViewPermission(resource) {
if (resource.isPlatform()) {
return resource.toPlatform().checkPerms(operation: 'view', user:user)
} else if (resource.isServer()) {
return resource.toServer().checkPerms(operation: 'view', user:user)
} else if (resource.isService()) {
return resource.toService().checkPerms(operation: 'view', user:user)
} else {
log.error("Unhandled resource type " + resource.prototype)
return null
}
}
/**
* Get the resource based on the given id. If the resource is not found,
* null is returned.
*/
protected getResource(id) {
if (id == null) {
return null
}
def resource = resourceHelper.findById(id)
if (!resource) {
return null
} else {
//XXX: ResourceHelper needs some work here..
try {
resource.name // Check the object really exists
resource.entityId // Check the object is an appdef object
} catch (Throwable t) {
return null
}
// ResourceHelper does not check permissions
return checkViewPermission(resource)
}
}
/**
* Get a User by id or name
* @return The user by the given id. If the passed in id is null then
* the user by the given name is returned. If no user could be found
* for either the id or name, null is returned.
*/
protected getUser(Integer id, String name) {
if (id != null) {
return userHelper.getUser(id)
} else {
return userHelper.findUser(name)
}
}
/**
* Get a Role by id or name
*
* @return The role by the given id. If the passed in id is null then
* the Role by the given name is returned. If no role could be found
* for either the id or name, null is returned.
*/
protected getRole(Integer id, String name) {
if (id != null) {
return roleHelper.getRoleById(id)
} else {
return roleHelper.findRoleByName(name)
}
}
/**
* Get a group by id or name
*
* @return The group with the given id or name. If the passed in id is null,
* then the group with the given name is returned. If no group could be found
* for either the id or name, null is returned.
*/
protected getGroup(Integer id, String name) {
if (id != null) {
return resourceHelper.findGroup(id)
} else {
return resourceHelper.findGroupByName(name)
}
}
/**
* Get POST data from the client.
*/
protected getPostData() {
// Check for multipart/form-data
if (invokeArgs.request.contentType.contains("multipart")) {
return getUpload('postdata')
} else {
return invokeArgs.request.inputStream.getText()
}
}
def dispatchRequest() {
long start = System.currentTimeMillis()
super.dispatchRequest()
long total = System.currentTimeMillis() - start;
synchronized(STATS_LOCK) {
def method = controllerName + "." + action
def stats = _methodStats[method]
if (!stats) {
stats = [calls: 0, maxTime: 0, minTime: Integer.MAX_VALUE,
totalTime: 0]
}
stats.calls++
stats.totalTime += total
if (stats.maxTime < total) {
stats.maxTime = total
}
if (stats.minTime > total) {
stats.minTime = total
}
_methodStats[method] = stats
}
}
def index(params) {
render(locals:[plugin: getPlugin(),
stats: _methodStats])
}
}