use of com.google.appengine.api.users.User in project java-docs-samples by GoogleCloudPlatform.
the class ChatServlet method doGet.
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
final UserService userService = UserServiceFactory.getUserService();
User currentUser = userService.getCurrentUser();
String room = req.getParameter("room");
// Show login link if user is not logged in.
if (currentUser == null) {
String loginUrl = userService.createLoginURL(getUriWithChatRoom(req, room));
resp.getWriter().println("<p>Please <a href=\"" + loginUrl + "\">sign in</a>.</p>");
return;
}
// user is already logged in
if (room != null) {
req.setAttribute("room", room);
}
getServletContext().getRequestDispatcher("/WEB-INF/view/chat.jsp").forward(req, resp);
}
use of com.google.appengine.api.users.User in project java-docs-samples by GoogleCloudPlatform.
the class Greeting method create.
static Greeting create(Entity entity) {
User user = (User) entity.getProperty("user");
Instant date = new Instant((Date) entity.getProperty("date"));
String content = (String) entity.getProperty("content");
return new AutoValue_Greeting(user, date, content);
}
use of com.google.appengine.api.users.User in project appengine-angular-guestbook-java by googlearchive.
the class Greeting method fromEntity.
public static Greeting fromEntity(Entity greetingEntity) {
String author;
User user = (User) greetingEntity.getProperty("user");
if (user == null) {
author = "an anonymous user";
} else {
author = user.getEmail();
}
return new Greeting((String) greetingEntity.getProperty("content"), (Date) greetingEntity.getProperty("date"), author);
}
use of com.google.appengine.api.users.User in project appengine-guestbook-java by googlearchive.
the class SignGuestbookServletTest method testDoPost.
@Test
public void testDoPost() throws IOException, EntityNotFoundException {
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
String guestbookName = "TestGuestbook";
String testContent = "Test Content";
when(request.getParameter("guestbookName")).thenReturn(guestbookName);
when(request.getParameter("content")).thenReturn(testContent);
Date priorToRequest = new Date();
signGuestbookServlet.doPost(request, response);
Date afterRequest = new Date();
verify(response).sendRedirect("/guestbook.jsp?guestbookName=TestGuestbook");
User currentUser = UserServiceFactory.getUserService().getCurrentUser();
Entity greeting = DatastoreServiceFactory.getDatastoreService().prepare(new Query()).asSingleEntity();
assertEquals(guestbookName, greeting.getKey().getParent().getName());
assertEquals(testContent, greeting.getProperty("content"));
assertEquals(currentUser, greeting.getProperty("user"));
Date date = (Date) greeting.getProperty("date");
assertTrue("The date in the entity [" + date + "] is prior to the request being performed", priorToRequest.before(date) || priorToRequest.equals(date));
assertTrue("The date in the entity [" + date + "] is after to the request completed", afterRequest.after(date) || afterRequest.equals(date));
}
Aggregations