public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String sessionToken = null;
// Initialize a client to talk to Google Data API services.
DocsService client = new DocsService("google-feedfetcher-v1");
// If a user is currently signed in to the application, attempt to retrieve
// a previously stored session token associated with that account from App
// Engine's datastore.
if (userService.isUserLoggedIn()) {
User user = userService.getCurrentUser();
sessionToken = TokenStore.getToken(user.getEmail());
}
// If sessionToken is null, check for a single-use token in the query
// string; if found, exchange this token for a session token using the
// client library.
if (sessionToken == null) {
try {
// Find the AuthSub token and upgrade it to a session token.
String authToken = AuthSubUtil.getTokenFromReply(
request.getQueryString());
// Upgrade the single-use token to a multi-use session token.
sessionToken = AuthSubUtil.exchangeForSessionToken(authToken, null);
} catch (AuthenticationException e) {
//...
} catch (GeneralSecurityException e) {
//...
} catch (NullPointerException e) {
// Ignore
}
}
if (sessionToken != null) {
// If there is a current user, store the token in the datastore and
// associate it with the current user's email address.
if (userService.isUserLoggedIn()) {
User user = userService.getCurrentUser();
TokenStore.addToken(user.getEmail(), sessionToken);
}
// Set the session token as a field of the Service object. Since a new
// Service object is created with each get call, we don't need to
// worry about the anonymous token being used by other users.
client.setAuthSubToken(sessionToken);
// Fetch and write feed data to response
String feedUrl = request.getParameter("feedUrl");
if (feedUrl == null) {
feedUrl = "https://docs.google.com/feeds/default/private/full";