GitHub and JAR download
Setup
To use RosterServer, you need to download the OneRoster JAR file and add a reference to the JAR file in your project. To do this in IntelliJ, click File -> Project Structure -> Libraries -> Click the plus sign -> Select the JAR File.
Usage
First, import com.classlink.roster.OneRoster.
import com.classlink.roster.OneRoster;
Next, create a new instance of the OneRoster object, which takes in two strings,
the client_id and client_secret.
OneRoster oneRoster = new OneRoster("XXXXXXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXXXXXX");
You are now able to make requests to a specified URL. This is done using the method makeRosterRequest(url), which takes in the URL and returns a OneRosterResponse object, which contains the status code and response.
OneRoster oneRoster = new OneRoster("XXXXXXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXXXXXX");
OneRosterResponse response = oneRoster.makeRosterRequest("https://example.com/users");
The response is made up of statusCode, which contains the status code as an int, and response, which contains the JSON response as a string.
System.out.println(response.getStatuscode()); // The status code
System.out.println(response.getResponse()); // The JSON response
Example
Example
Print all students' names
import com.google.gson.Gson;
import java.util.List;
import java.util.Map;
// Import OneRoster
import com.classlink.roster.OneRoster;
public class Example {
private static List<Map<String, String>> getUsers(String url) {
// Create new OneRoster object with the client_id and client_secret
OneRoster oneRoster = new OneRoster("XXXXXXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXXXXXX");
// Make the request to the given url
OneRosterResponse response = rr.makeRosterRequest(url);
Gson gson = new Gson();
// If the status code is 200, return Map of users, otherwise print message
if (response.getStatusCode() == 200) {
Map<String, List<Map<String, String>>> users = gson.fromJson(response.getResponse(), Map.class);
return users.get("users");
}
switch (response.getStatusCode()) {
case 401:
System.out.println("Unauthorized Request\n" + response.getResponse());
break;
case 404:
System.out.println("Not Found\n" + response.getResponse());
break;
case 500:
System.out.println("Server Error\n" + response.getResponse());
break;
default:
System.out.println("Something Went Wrong, status code " + response.getStatusCode() +
"\n" + response.getResponse());
break;
}
return null;
}
public static void main(String[] args) {
List<Map<String, String>> users = getUsers(
"https://example.com/users?fields=givenName%2CfamilyName&filter=role%3D'student'");
if (users != null) {
for (Map<String, String> user : users) {
System.out.println(user.get("givenName") + " " + user.get("familyName"));
}
}
}
}