@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
// Get the path info, minus the leading slash.
String lspUrl = req.getPathInfo().substring(1);
LivingStory livingStory = livingStoryDataService.retrieveByUrlName(lspUrl, true);
if (livingStory == null) {
resp.sendRedirect("/");
return;
}
Long livingStoryId = livingStory.getId();
Date lastVisitTime = null;
boolean subscribedToEmails = false;
FilterSpec defaultView = null;
String loggedInUser = userLoginService.getUserId();
if (loggedInUser != null) {
lastVisitTime = userDataService.getLastVisitTimeForStory(loggedInUser, livingStoryId);
subscribedToEmails = userDataService.isUserSubscribedToEmails(loggedInUser, livingStoryId);
defaultView = userDataService.getDefaultStoryView(loggedInUser);
}
if (lastVisitTime == null) {
// No last visit time found (either user was not logged in,
// or user logged in for the first time)
// Try to get the cookie with the last visit time
String cookieName = Constants.getCookieName(lspUrl);
Cookie[] cookies = req.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
if (cookieName.equals(cookie.getName())) {
try {
lastVisitTime = new Date(Long.valueOf(cookie.getValue()));
} catch (NumberFormatException e) {
}
}
}
}
}
// If the last visit time is not available in the default list of summary revisions,
// we need to query the datastore for the full list of revisions. This is slow,
// but is not expected to happen often.
if (!livingStory.dateWithinAvailableRevisions(lastVisitTime)) {
// TODO: decide if this should be an async call from the client.
livingStory = livingStoryDataService.retrieveById(livingStoryId, false);
}
ExternalServiceKeyChain keyChain = new ExternalServiceKeyChain(getServletContext());