You are looking at documentation for an older release. Not what you want? See the current release documentation.
The source code of this tutorial can be found at eXo Samples Repository.
The project is a data injector that runs as a Java standalone application and does the followings:
- create 5 users - connect everyone together - create 2 spaces and add everyone - login as user0 and post a message - everyone else likes and comments
The project consists of the following components:
A Java "main" class in which the injecting scenario is written.
A connector that basically takes care of all the work with the API. It provides basic CRUD functions and some other methods that fit the requirement in particular.
Some classes that assist the connector: ServiceInfo
provides the URIs of the API,
HttpUtils
provides Http GET/POST/PUT/DELETE methods.
HttpUtils uses HttpURLConnection
that is not described in this tutorial.
Read Calendar Rest API tutorial if necessary.
The data models that are POJO classes for utilizing Gson.
Login/Logout
See the login() method in the main class:
public static void login(String username, String password) {
final String username_ = username;
final String password_ = password;
// logout first
sun.net.www.protocol.http.AuthCacheValue.setAuthCache(new sun.net.www.protocol.http.AuthCacheImpl());
Authenticator.setDefault(new Authenticator() {
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username_, password_.toCharArray());
}
});
}
The code is explained in Authentication section.
Effective JSON
Read Calendar Rest API tutorial for utilizing Gson. However, unlike Calendar API, with current Social API you generally could not re-use a JSON object between methods. For Create/Update, you need to create "effective" JSON objects that are described in Social data models as JSON entities.
In Java, you should always create a new object, set the values for only "effective" fields before serializing and sending it. Here is an example of updating User - do not use the current user object, neither blind copy all its JSON fields:
User current_user;
current_user = connector.getUser(username);
User to_be_updated_user = new User();
to_be_updated_user.setFullname("A new name");
// format new object to JSON and PUT
String json = (new Gson()).toJson(to_be_updated_user);
connector.updateUser(username, json);
DateTime format
When using parameters after
and before
of
GET /v1/social/users/{username}/activities
,
you should adjust the local DateTime to UTC and format it to yyyy-MM-dd HH:mm:ss
.
Here is the code sample to request activities of the local "today":
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.clear(Calendar.MINUTE);
cal.clear(Calendar.SECOND);
cal.clear(Calendar.MILLISECOND);
Date start_of_day = cal.getTime();
Date end_of_day = new Date(cal.getTimeInMillis() + 1000*60*60*24 -1);
// format dates in "yyyy-MM-dd HH:mm:ss" in UTC timezone
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
String after = df.format(start_of_day);
String before = df.format(end_of_day);
These query params contain white spaces, so they need to be URL-encoded:
String url = BASE_URL + "/v1/social/users/" + username + "?returnSize=true&offset=" + offset
+ "&after=" + URLEncoder.encode(after, "UTF-8") + "&before=" + URLEncoder.encode(before, "UTF-8");