In my previous tutorial I tried to explain the registration and and authorization of a OneDrive app. So you should have a basic understanding of how the process flow works. In this second tutorial I will try to explain how to make calls to the OneDrive API via Java. The examples are based on the OneDriveAPI which I wrote and made available on GitHub.
Talking RESTful with the Jersey and Jackson framework
To be able to talk with any RESTful service I used the Jersey framework. This is an (I quote) “open source, production quality, framework for developing RESTful Web Services in Java that provides support for JAX-RS APIs and serves as a JAX-RS (JSR 311 & JSR 339) Reference Implementation.“. In other words this framework makes it easy to program against RESTful services (Jersey-client) or to make RESTful services (Jersey-server). The Jackson framework is used for mapping (or converting) JSON responses to Java beans and vice versa.
Basically all RESTful Java calls to the OneDrive RESTful API look in Jersey like the following:
import com.sun.jersey.api.*; import com.fasterxml.jackson.databind.ObjectMapper; (...) MultivaluedMap<String, String> queryParams; Client jerseyClient; jerseyClient = Client.Create(); WebResource webResource = client.resource("https://apis.live.net/v5.0/"); queryParams.add("access_token","somelongstringwithcharactersandnumbers"); ClientResponse clientResponse = webResource.queryParams(queryParams).accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); MyBean myBean = objectMapper.readValue(clientResponse.getEntity(String.class).toString(), MyBean.class);
Where the queryParams are the parameters added to the end of the URL. The Client is our RESTful Jersey client and the resource defines which RESTful URL we are calling. The final step is using the WebResource object to make the real call. We specify the media type which we are accepting from the server via the accept method and after that we specify we are doing a GET call.
When the WebResource has been executed it will return the response in the ClientResponse object. Quite often the result will be returned in JSON format and we can use the Jackson library to map the JSON response from the RESTful service to a Java bean. This is done using the ObjectMapper object which converts the client response entity (a JSON string) to a custom bean (MyBean).
Basically that’s how the Jersey library works. I would suggest to look into the OneDrive API source code to see how all different calls (like GET/POST/PUT/DELETE) work.
The OneDrive API
I put together some basic RESTful calls for the OneDrive RESTful service into a Maven based project called OneDrive API for Java. The following basic functionalities are implemented:
- list folders and/or folder contents
- create/delete/update folder
- change file properties
- upload/download files
- get owner/user
- get shared edit/read link from a file
The project contains a readme which explains in three steps how to get started with your first application. The project is open source so please feel free to contribute, fork or give feedback for improvement.