String userId = getUserId();
// Get flow from the data store
PersistenceManager manager = pmf.getPersistenceManager();
try {
ThreeLeggedFlow flow = null;
try {
flow = manager.getObjectById(flowType, userId);
} catch (JDOObjectNotFoundException e) {
LOG.severe("Unable to locate flow by user: " + userId);
resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
resp.getWriter().print("Unable to find flow for user: " + userId);
return;
}
flow.setHttpTransport(getHttpTransport());
flow.setJsonFactory(getJsonFactory());
// Complete the flow object with the token we got in our query parameters
Credential c = flow.complete(completionCode);
manager.makePersistent(c);
manager.deletePersistent(flow);
resp.sendRedirect(redirectUrl);
} finally {
manager.close();