/
WsSampleGetGrouperPrivilegesListRestLite_list_xml.txt
305 lines (252 loc) · 11.6 KB
/
WsSampleGetGrouperPrivilegesListRestLite_list_xml.txt
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
Grouper web service sample of service: getGrouperPrivileges, WsSampleGetGrouperPrivilegesListRestLite, manually written lite/rest, type: _list, format: xml, for version: 2.5.0
#########################################
##
## HTTP request sample (could be formatted for view by
## indenting or changing dates or other data)
##
#########################################
POST /grouper-ws/servicesRest/v2_5_000/grouperPrivileges HTTP/1.1
Connection: close
Authorization: Basic xxxxxxxxxxxxxxxxx==
User-Agent: Jakarta Commons-HttpClient/3.1
Host: localhost:8092
Content-Length: 234
Content-Type: application/xml; charset=UTF-8
<WsRestGetGrouperPrivilegesLiteRequest>
<subjectId>test.subject.9</subjectId>
<groupName>aStem:aGroup</groupName>
<privilegeType>access</privilegeType>
<actAsSubjectId>GrouperSystem</actAsSubjectId>
</WsRestGetGrouperPrivilegesLiteRequest>
#########################################
##
## HTTP response sample (could be formatted for view by
## indenting or changing dates or other data)
##
#########################################
HTTP/1.1 200
Set-Cookie: JSESSIONID=53340B5025894AE885E88E5EEA97181B;path=/grouper-ws/;HttpOnly
X-Grouper-resultCode: SUCCESS
X-Grouper-success: T
X-Grouper-resultCode2: NONE
Content-Type: application/xml;charset=UTF-8
Transfer-Encoding: chunked
Date: Tue, 31 Mar 2020 20:35:26 GMT
Connection: close
Server: Apache TomEE
3f7
<WsGetGrouperPrivilegesLiteResult>
<resultMetadata>
<resultCode>SUCCESS</resultCode>
<success>T</success>
</resultMetadata>
<privilegeResults>
<WsGrouperPrivilegeResult>
<allowed>T</allowed>
<ownerSubject>
<resultCode>SUCCESS</resultCode>
<success>T</success>
<id>test.subject.9</id>
<name>my name is test.subject.9</name>
<sourceId>jdbc</sourceId>
</ownerSubject>
<privilegeName>admin</privilegeName>
<privilegeType>access</privilegeType>
<revokable>T</revokable>
<wsGroup>
<extension>aGroup</extension>
<typeOfGroup>group</typeOfGroup>
<displayExtension>a group</displayExtension>
<description>a group description</description>
<displayName>a stem:a group</displayName>
<name>aStem:aGroup</name>
<uuid>32ca90db41b04a1a9611a214a20bac42</uuid>
<idIndex>10009</idIndex>
<enabled>T</enabled>
</wsGroup>
<wsSubject>
<resultCode>SUCCESS</resultCode>
<success>T</success>
<id>test.subject.9</id>
<name>my name is test.subject.9</name>
<sourceId>jdbc</sourceId>
</wsSubject>
</WsGrouperPrivilegeResult>
</privilegeResults>
<responseMetadata>
<resultWarnings>
</
7c
resultWarnings>
<millis>19</millis>
<serverVersion>2.5.0</serverVersion>
</responseMetadata>
</WsGetGrouperPrivilegesLiteResult>
0
#########################################
##
## Java source code (note, any programming language / objects
## can use used to generate the above request/response. Nothing
## is Java specific. Also, if you are using Java, the client libraries
## are available
##
#########################################
/*******************************************************************************
* Copyright 2012 Internet2
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package edu.internet2.middleware.grouper.ws.samples.rest.grouperPrivileges;
import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.DefaultHttpParams;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.lang.StringUtils;
import edu.internet2.middleware.grouper.Group;
import edu.internet2.middleware.grouper.GroupFinder;
import edu.internet2.middleware.grouper.GrouperSession;
import edu.internet2.middleware.grouper.SubjectFinder;
import edu.internet2.middleware.grouper.util.GrouperUtil;
import edu.internet2.middleware.grouper.ws.coresoap.WsGetGrouperPrivilegesLiteResult;
import edu.internet2.middleware.grouper.ws.coresoap.WsGrouperPrivilegeResult;
import edu.internet2.middleware.grouper.ws.rest.group.WsRestGetGrouperPrivilegesLiteRequest;
import edu.internet2.middleware.grouper.ws.samples.types.WsSampleRest;
import edu.internet2.middleware.grouper.ws.samples.types.WsSampleRestType;
import edu.internet2.middleware.grouper.ws.util.RestClientSettings;
/**
* @author mchyzer
*/
public class WsSampleGetGrouperPrivilegesListRestLite implements WsSampleRest {
/**
* get grouper privileges lite web service with REST
* @param wsSampleRestType is the type of rest (xml, xhtml, etc)
*/
@SuppressWarnings("deprecation")
public static void getGrouperPrivilegesListLite(WsSampleRestType wsSampleRestType) {
try {
HttpClient httpClient = new HttpClient();
DefaultHttpParams.getDefaultParams().setParameter(
HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(0, false));
//URL e.g. http://localhost:8093/grouper-ws/servicesRest/v1_3_000/...
//NOTE: aStem:aGroup urlencoded substitutes %3A for a colon
PostMethod method = new PostMethod(
RestClientSettings.URL + "/" + RestClientSettings.VERSION
+ "/grouperPrivileges");
httpClient.getParams().setAuthenticationPreemptive(true);
Credentials defaultcreds = new UsernamePasswordCredentials(RestClientSettings.USER,
RestClientSettings.PASS);
//no keep alive so response if easier to indent for tests
method.setRequestHeader("Connection", "close");
//e.g. localhost and 8093
String host = RestClientSettings.HOST;
int port = RestClientSettings.PORT;
httpClient.getState()
.setCredentials(new AuthScope(host, port), defaultcreds);
//Make the body of the request, in this case with beans and marshaling, but you can make
//your request document in whatever language or way you want
WsRestGetGrouperPrivilegesLiteRequest wsRestGetGrouperPrivilegesLiteRequest
= new WsRestGetGrouperPrivilegesLiteRequest();
// set the act as id
wsRestGetGrouperPrivilegesLiteRequest.setActAsSubjectId("GrouperSystem");
wsRestGetGrouperPrivilegesLiteRequest.setSubjectId("test.subject.9");
wsRestGetGrouperPrivilegesLiteRequest.setGroupName("aStem:aGroup");
wsRestGetGrouperPrivilegesLiteRequest.setPrivilegeType("access");
//get the xml / json / xhtml / paramString
String requestDocument = wsSampleRestType.getWsLiteRequestContentType().writeString(wsRestGetGrouperPrivilegesLiteRequest);
//make sure right content type is in request (e.g. application/xhtml+xml
String contentType = wsSampleRestType.getWsLiteRequestContentType().getContentType();
method.setRequestEntity(new StringRequestEntity(requestDocument, contentType, "UTF-8"));
httpClient.executeMethod(method);
//make sure a request came back
Header successHeader = method.getResponseHeader("X-Grouper-success");
String successString = successHeader == null ? null : successHeader.getValue();
if (StringUtils.isBlank(successString)) {
throw new RuntimeException("Web service did not even respond!");
}
boolean success = "T".equals(successString);
String resultCode = method.getResponseHeader("X-Grouper-resultCode").getValue();
String response = RestClientSettings.responseBodyAsString(method);
//convert to object (from xhtml, xml, json, etc)
WsGetGrouperPrivilegesLiteResult wsGetGrouperPrivilegesLiteResult = (WsGetGrouperPrivilegesLiteResult)wsSampleRestType
.getWsLiteResponseContentType().parseString(response);
String resultMessage = wsGetGrouperPrivilegesLiteResult.getResultMetadata().getResultMessage();
// see if request worked or not
if (!success) {
throw new RuntimeException("Bad response from web service: resultCode: " + resultCode
+ ", " + resultMessage);
}
System.out.println("Server version: " + wsGetGrouperPrivilegesLiteResult.getResponseMetadata().getServerVersion()
+ ", result code: " + resultCode
+ ", result message: " + resultMessage );
System.out.println(wsGetGrouperPrivilegesLiteResult);
boolean foundAdmin = false;
//lets make sure admin is in there
for (WsGrouperPrivilegeResult wsGrouperPrivilegeResult : GrouperUtil.nonNull(wsGetGrouperPrivilegesLiteResult.getPrivilegeResults(), WsGrouperPrivilegeResult.class)) {
if (StringUtils.equals("admin", wsGrouperPrivilegeResult.getPrivilegeName())
&& StringUtils.equals("T", wsGrouperPrivilegeResult.getAllowed())) {
foundAdmin = true;
}
}
GrouperSession grouperSession = GrouperSession.startRootSession();
boolean shouldHaveAdmin = false;
try {
Group group = GroupFinder.findByName(grouperSession, "aStem:aGroup");
shouldHaveAdmin = group.hasAdmin(SubjectFinder.findById("test.subject.9"));
} finally {
GrouperSession.stopQuietly(grouperSession);
}
if (shouldHaveAdmin != foundAdmin) {
throw new RuntimeException("admin priv found (" + foundAdmin
+ ") is not the same as it should be: " + shouldHaveAdmin);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @param args
*/
@SuppressWarnings("unchecked")
public static void main(String[] args) {
RestClientSettings.resetData();
getGrouperPrivilegesListLite(WsSampleRestType.xhtml);
}
/**
* @see edu.internet2.middleware.grouper.ws.samples.types.WsSampleRest#executeSample(edu.internet2.middleware.grouper.ws.samples.types.WsSampleRestType)
*/
public void executeSample(WsSampleRestType wsSampleRestType) {
getGrouperPrivilegesListLite(wsSampleRestType);
}
/**
* @see edu.internet2.middleware.grouper.ws.samples.types.WsSampleRest#validType(edu.internet2.middleware.grouper.ws.samples.types.WsSampleRestType)
*/
public boolean validType(WsSampleRestType wsSampleRestType) {
return true;
}
}
#########################################
##
## Stdout
##
#########################################
Server version: 2.5.0, result code: SUCCESS, result message: null
WsGetGrouperPrivilegesLiteResult[
resultMetadata=WsResultMeta[resultCode=SUCCESS,success=T,httpStatusCode=0],
privilegeResults={subject: jdbc - test.subject.9, ownerSubject: jdbc - test.subject.9, admin, group: aStem:aGroup, },
responseMetadata=WsResponseMeta[resultWarnings=,millis=19,millisStart=0,serverVersion=2.5.0]]