grouper-users - RE: [grouper-users] Grouper-WS and PHP
Subject: Grouper Users - Open Discussion List
List archive
- From: Chris Hyzer <>
- To: mohan shamachar <>, "" <>
- Subject: RE: [grouper-users] Grouper-WS and PHP
- Date: Wed, 23 Jan 2013 06:42:46 +0000
- Accept-language: en-US
Was there an error? Check the logs and if you have a tcp/ip monitor (e.g. in
eclipse, or webscarab) and see the input and output (I changed it by
formatting it).
I ran this:
INPUT:
POST /grouper-ws/servicesRest/json/v2_1_002/subjects HTTP/1.1
Connection: close
Authorization: Basic R3Jvdabc123==
User-Agent: Jakarta Commons-HttpClient/3.0
Host: localhost:8090
Content-Length: 228
Content-Type: text/xml; charset=UTF-8
<WsRestGetGroupsRequest>
<subjectLookups>
<WsSubjectLookup>
<subjectId>GrouperSystem</subjectId>
</WsSubjectLookup>
</subjectLookups>
<subjectAttributeNames>
<string>description</string>
</subjectAttributeNames>
</WsRestGetGroupsRequest>
RESPONSE:
HTTP/1.1 201 Created
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=2AD05BDB875859F6C6118030077B09E3; Path=/grouper-ws
X-Grouper-resultCode: SUCCESS
X-Grouper-success: T
X-Grouper-resultCode2: NONE
Content-Type: text/x-json;charset=UTF-8
Content-Length: 1682
Date: Wed, 23 Jan 2013 06:36:00 GMT
Connection: close
{
"WsGetGroupsResults": {
"responseMetadata": {
"millis": "12583",
"serverVersion": "2.1.3"
},
"resultMetadata": {
"resultCode": "SUCCESS",
"resultMessage": "Success for: clientVersion: 2.1.2, subjectLookups:
Array size: 1: [0]: WsSubjectLookup[subjectId=GrouperSystem]\n\nmemberFilter:
All, includeGroupDetail: false, actAsSubject: null\n, params: null\n
fieldName1: null\n, scope: null, wsStemLookup: null\n, stemScope: null,
enabled: null, pageSize: null, pageNumber: null, sortString: null, ascending:
null\n, pointInTimeFrom: null, pointInTimeTo: null",
"success": "T"
},
"results": [
{
"resultMetadata": {
"resultCode": "SUCCESS",
"success": "T"
},
"wsGroups": [
{
"displayExtension": "sysadmingroup",
"displayName": "etc:sysadmingroup",
"extension": "sysadmingroup",
"name": "etc:sysadmingroup",
"typeOfGroup": "group",
"uuid": "83f1f5fe428e45e895540e231342c9b1"
},
{
"description": "users allowed to log in to the UI",
"displayExtension": "uiUsers",
"displayName": "etc:uiUsers",
"extension": "uiUsers",
"name": "etc:uiUsers",
"typeOfGroup": "group",
"uuid": "98a20f42f3ce4e8d8a48f91c4f71289c"
},
{
"displayExtension": "webServiceActAsGroup",
"displayName": "etc:webServiceActAsGroup",
"extension": "webServiceActAsGroup",
"name": "etc:webServiceActAsGroup",
"typeOfGroup": "group",
"uuid": "b08c4a980d3740f7acb991ef60fb8a98"
},
{
"displayExtension": "webServiceClientUsers",
"displayName": "etc:webServiceClientUsers",
"extension": "webServiceClientUsers",
"name": "etc:webServiceClientUsers",
"typeOfGroup": "group",
"uuid": "bbefa82f6c914350afa454d7fd802682"
}
],
"wsSubject": {
"attributeValues": [
"GrouperSysAdmin"
],
"id": "GrouperSystem",
"name": "GrouperSysAdmin",
"resultCode": "SUCCESS",
"sourceId": "g:isa",
"success": "T"
}
}
],
"subjectAttributeNames": [
"description"
]
}
}
Here was the java I used:
/*******************************************************************************
* 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.group;
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.ws.coresoap.WsGetGroupsResults;
import edu.internet2.middleware.grouper.ws.coresoap.WsSubjectLookup;
import edu.internet2.middleware.grouper.ws.rest.WsRestResultProblem;
import
edu.internet2.middleware.grouper.ws.rest.contentType.WsRestResponseContentType;
import edu.internet2.middleware.grouper.ws.rest.group.WsRestGetGroupsRequest;
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 WsSampleGetGroupsRest2 implements WsSampleRest {
/**
* get groups lite web service with REST
* @param wsSampleRestType is the type of rest (xml, xhtml, etc)
*/
@SuppressWarnings("deprecation")
public static void getGroups(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 + "/json/" + RestClientSettings.VERSION
+ "/subjects");
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
httpClient.getState()
.setCredentials(new AuthScope(RestClientSettings.HOST,
RestClientSettings.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
WsRestGetGroupsRequest getGroups = new WsRestGetGroupsRequest();
getGroups.setSubjectAttributeNames(new String[]{"description"});
// seeif two subjects are in the group
WsSubjectLookup[] subjectLookups = new WsSubjectLookup[1];
subjectLookups[0] = new WsSubjectLookup("GrouperSystem", null, null);
getGroups.setSubjectLookups(subjectLookups);
//get the xml / json / xhtml / paramString
String requestDocument =
wsSampleRestType.getWsLiteRequestContentType().writeString(getGroups);
//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);
Object result = WsRestResponseContentType.json.parseString(response);
//see if problem
if (result instanceof WsRestResultProblem) {
throw new
RuntimeException(((WsRestResultProblem)result).getResultMetadata().getResultMessage());
}
//convert to object (from xhtml, xml, json, etc)
WsGetGroupsResults wsGetGroupsResults = (WsGetGroupsResults)result;
String resultMessage =
wsGetGroupsResults.getResultMetadata().getResultMessage();
// see if request worked or not
if (!success) {
throw new RuntimeException("Bad response from web service:
successString: " + successString + ", resultCode: " + resultCode
+ ", " + resultMessage);
}
System.out.println("Server version: " +
wsGetGroupsResults.getResponseMetadata().getServerVersion()
+ ", result code: " + resultCode
+ ", result message: " + resultMessage );
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @param args
*/
@SuppressWarnings("unchecked")
public static void main(String[] args) {
getGroups(WsSampleRestType.xml);
}
/**
* @see
edu.internet2.middleware.grouper.ws.samples.types.WsSampleRest#executeSample(edu.internet2.middleware.grouper.ws.samples.types.WsSampleRestType)
*/
public void executeSample(WsSampleRestType wsSampleRestType) {
getGroups(wsSampleRestType);
}
/**
* @see
edu.internet2.middleware.grouper.ws.samples.types.WsSampleRest#validType(edu.internet2.middleware.grouper.ws.samples.types.WsSampleRestType)
*/
public boolean validType(WsSampleRestType wsSampleRestType) {
//dont allow http params
return !WsSampleRestType.http_json.equals(wsSampleRestType);
}
}
ps. not sure why the http status code is 201... hmmm :)
________________________________________
From:
[]
on behalf of mohan shamachar
[]
Sent: Tuesday, January 22, 2013 11:27 AM
To:
Subject: Re: [grouper-users] Grouper-WS and PHP
Hello,
In order to work around the AJAX/Callback data parsing issue, I tried using
PHP to make the grouper-ws calls and return the data back to the browser.
Now, the data I get back is in XHTML form, even though I specify JSON in my
url.
what do I need to do to make this call return JSON. Thank you.
best,
mohan
Here's the php code:
function grouper_rest_request() {
$url = 'http://' . $this->user . ':' . $this->password . '@' .
$this->host . ':8080/grouper-ws/servicesRest/json/v2_1_000/subjects';
$subjectId = 'testuser';
$request = '<WsRestGetGroupsRequest>
<subjectLookups>
<WsSubjectLookup>
<subjectId>'.$subjectId.'</subjectId>
</WsSubjectLookup>
</subjectLookups>
</WsRestGetGroupsRequest>';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml;
charset=UTF-8'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 2);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
$response = curl_exec($ch);
$error = curl_error($ch);
$info = curl_getinfo($ch);
if ($response !== FALSE) {
if ( ($error != '') or ($info['http_code'] >= 300) ) $response
= FALSE;
}
if ($response == FALSE) {
throw new Exception('Could not execute grouper webservice
request: [error: ' . $error . '] [http code: ' . $info['http_code'] . ']');
}
curl_close($ch);
return $response;
}
- [grouper-users] AJAX, JSON, Callback, mohan shamachar, 01/21/2013
- RE: [grouper-users] AJAX, JSON, Callback, Chris Hyzer, 01/22/2013
- Re: [grouper-users] Grouper-WS and PHP, mohan shamachar, 01/22/2013
- RE: [grouper-users] Grouper-WS and PHP, Chris Hyzer, 01/23/2013
- Re: RE: [grouper-users] Grouper-WS and PHP, mohan shamachar, 01/25/2013
- RE: [grouper-users] Grouper-WS and PHP, Chris Hyzer, 01/23/2013
Archive powered by MHonArc 2.6.16.