Search in sources :

Example 11 with User

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>&lt;" + displayName + "&gt;</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));
}
Also used : User(com.google.appengine.api.users.User) HashMap(java.util.HashMap) Result(com.pusher.rest.data.Result)

Example 12 with User

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);
        }
    });
}
Also used : GaeUserServiceProfile(org.pac4j.gae.profile.GaeUserServiceProfile) User(com.google.appengine.api.users.User) GaeUserCredentials(org.pac4j.gae.credentials.GaeUserCredentials)

Example 13 with User

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()));
        }
    }
}
Also used : User(com.google.appengine.api.users.User) UserService(com.google.appengine.api.users.UserService) Properties(java.util.Properties)

Example 14 with User

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);
}
Also used : Entity(com.google.appengine.api.datastore.Entity) User(com.google.appengine.api.users.User) UserService(com.google.appengine.api.users.UserService) DatastoreService(com.google.appengine.api.datastore.DatastoreService) Key(com.google.appengine.api.datastore.Key) Date(java.util.Date)

Example 15 with User

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"));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) User(com.google.appengine.api.users.User) StringWriter(java.io.StringWriter) HttpServletResponse(javax.servlet.http.HttpServletResponse) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Aggregations

User (com.google.appengine.api.users.User)24 UserService (com.google.appengine.api.users.UserService)8 HashMap (java.util.HashMap)4 Date (java.util.Date)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 Entity (com.google.appengine.api.datastore.Entity)2 Pusher (com.pusher.rest.Pusher)2 PresenceUser (com.pusher.rest.data.PresenceUser)2 Result (com.pusher.rest.data.Result)2 PrintWriter (java.io.PrintWriter)2 Test (org.junit.Test)2 GaeUser (samples.gae.users.GaeUser)2 DatastoreService (com.google.appengine.api.datastore.DatastoreService)1 Key (com.google.appengine.api.datastore.Key)1 Query (com.google.appengine.api.datastore.Query)1 OAuthRequestException (com.google.appengine.api.oauth.OAuthRequestException)1 OAuthService (com.google.appengine.api.oauth.OAuthService)1 Document (com.google.appengine.api.search.Document)1 StringWriter (java.io.StringWriter)1