use of com.google.appengine.api.users.User in project community by GoogleCloudPlatform.
the class SendMessageServlet method doPost.
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
// Parse POST request body received in the format:
// [{"message": "my-message", "socket_id": "1232.24", "channel": "presence-my-channel"}]
String body = CharStreams.readLines(request.getReader()).toString();
String json = body.replaceFirst("^\\[", "").replaceFirst("\\]$", "");
Map<String, String> data = gson.fromJson(json, typeReference.getType());
String message = data.get("message");
String socketId = data.get("socket_id");
String channelId = data.get("channel_id");
User user = UserServiceFactory.getUserService().getCurrentUser();
// User email prefix as display name for current currently authenticated user
String displayName = user.getNickname().replaceFirst("@.*", "");
// Create a message including the user email prefix to display in the chat window
String taggedMessage = "<strong><" + displayName + "></strong> " + message;
Map<String, String> messageData = new HashMap<>();
messageData.put("message", taggedMessage);
// Send a message over the Pusher channel (maximum size of a message is 10KB)
Result result = PusherService.getDefaultInstance().trigger(channelId, // name of event
"new_message", messageData, // (Optional) Use client socket_id to exclude the sender from receiving the message
socketId);
// result.getStatus() == SUCCESS indicates successful transmission
messageData.put("status", result.getStatus().name());
response.getWriter().println(gson.toJson(messageData));
}
use of com.google.appengine.api.users.User in project pac4j by pac4j.
the class GaeUserServiceClient method clientInit.
@Override
protected void clientInit() {
service = UserServiceFactory.getUserService();
CommonHelper.assertNotNull("service", this.service);
defaultRedirectActionBuilder(ctx -> {
final String destinationUrl = computeFinalCallbackUrl(ctx);
final String loginUrl = authDomain == null ? service.createLoginURL(destinationUrl) : service.createLoginURL(destinationUrl, authDomain);
return RedirectAction.redirect(loginUrl);
});
defaultCredentialsExtractor(ctx -> {
final GaeUserCredentials credentials = new GaeUserCredentials();
credentials.setUser(service.getCurrentUser());
return credentials;
});
defaultAuthenticator((credentials, ctx) -> {
final User user = credentials.getUser();
if (user != null) {
final GaeUserServiceProfile profile = PROFILE_DEFINITION.newProfile();
profile.setId(user.getEmail());
PROFILE_DEFINITION.convertAndAdd(profile, PROFILE_ATTRIBUTE, CommonProfileDefinition.EMAIL, user.getEmail());
PROFILE_DEFINITION.convertAndAdd(profile, PROFILE_ATTRIBUTE, CommonProfileDefinition.DISPLAY_NAME, user.getNickname());
if (service.isUserAdmin()) {
profile.addRole(GaeUserServiceProfile.PAC4J_GAE_GLOBAL_ADMIN_ROLE);
}
credentials.setUserProfile(profile);
}
});
}
use of com.google.appengine.api.users.User in project appengine-guestbook-java by googlearchive.
the class GuestbookServlet method doGet.
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
if (req.getParameter("testing") == null) {
resp.setContentType("text/plain");
resp.getWriter().println("Hello, this is a testing servlet. \n\n");
Properties p = System.getProperties();
p.list(resp.getWriter());
} else {
UserService userService = UserServiceFactory.getUserService();
User currentUser = userService.getCurrentUser();
if (currentUser != null) {
resp.setContentType("text/plain");
resp.getWriter().println("Hello, " + currentUser.getNickname());
} else {
resp.sendRedirect(userService.createLoginURL(req.getRequestURI()));
}
}
}
use of com.google.appengine.api.users.User in project appengine-guestbook-java by googlearchive.
the class SignGuestbookServlet method doPost.
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
UserService userService = UserServiceFactory.getUserService();
User user = userService.getCurrentUser();
String guestbookName = req.getParameter("guestbookName");
Key guestbookKey = KeyFactory.createKey("Guestbook", guestbookName);
String content = req.getParameter("content");
Date date = new Date();
Entity greeting = new Entity("Greeting", guestbookKey);
greeting.setProperty("user", user);
greeting.setProperty("date", date);
greeting.setProperty("content", content);
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
datastore.put(greeting);
resp.sendRedirect("/guestbook.jsp?guestbookName=" + guestbookName);
}
use of com.google.appengine.api.users.User in project appengine-guestbook-java by googlearchive.
the class GuestbookServletTest method testDoGet.
@Test
public void testDoGet() throws IOException {
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
StringWriter stringWriter = new StringWriter();
when(response.getWriter()).thenReturn(new PrintWriter(stringWriter));
guestbookServlet.doGet(request, response);
User currentUser = UserServiceFactory.getUserService().getCurrentUser();
assertEquals(true, stringWriter.toString().startsWith("Hello"));
}
Aggregations