Search in sources :

Example 1 with PresenceUser

use of com.pusher.rest.data.PresenceUser in project java-docs-samples by GoogleCloudPlatform.

the class AuthorizeServlet method doPost.

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    // Instantiate a pusher connection
    Pusher pusher = PusherService.getDefaultInstance();
    // Get current logged in user credentials
    User user = UserServiceFactory.getUserService().getCurrentUser();
    // redirect to homepage if user is not authorized
    if (user == null) {
        response.sendRedirect("/");
        return;
    }
    String currentUserId = user.getUserId();
    String displayName = user.getNickname().replaceFirst("@.*", "");
    String query = CharStreams.toString(request.getReader());
    // socket_id, channel_name parameters are automatically set in the POST body of the request
    // eg.socket_id=1232.12&channel_name=presence-my-channel
    Map<String, String> data = splitQuery(query);
    String socketId = data.get("socket_id");
    String channelId = data.get("channel_name");
    // Presence channels (presence-*) require user identification for authentication
    Map<String, String> userInfo = new HashMap<>();
    userInfo.put("displayName", displayName);
    // Inject custom authentication code for your application here to allow /deny current request
    String auth = pusher.authenticate(socketId, channelId, new PresenceUser(currentUserId, userInfo));
    // if successful, returns authorization in the format
    // {
    // "auth":"49e26cb8e9dde3dfc009:a8cf1d3deefbb1bdc6a9d1547640d49d94b4b512320e2597c257a740edd17",
    // "channel_data":"{\"user_id\":\"23423435252\",\"user_info\":{\"displayName\":\"John Doe\"}}"
    // }
    response.getWriter().append(auth);
}
Also used : User(com.google.appengine.api.users.User) PresenceUser(com.pusher.rest.data.PresenceUser) HashMap(java.util.HashMap) PresenceUser(com.pusher.rest.data.PresenceUser) Pusher(com.pusher.rest.Pusher)

Example 2 with PresenceUser

use of com.pusher.rest.data.PresenceUser in project community by GoogleCloudPlatform.

the class AuthorizeServlet method doPost.

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    // Instantiate a pusher connection
    Pusher pusher = PusherService.getDefaultInstance();
    // Get current logged in user credentials
    User user = UserServiceFactory.getUserService().getCurrentUser();
    // redirect to homepage if user is not authorized
    if (user == null) {
        response.sendRedirect("/");
        return;
    }
    String currentUserId = user.getUserId();
    String displayName = user.getNickname().replaceFirst("@.*", "");
    String query = CharStreams.toString(request.getReader());
    // socket_id, channel_name parameters are automatically set in the POST body of the request
    // eg.socket_id=1232.12&channel_name=presence-my-channel
    Map<String, String> data = splitQuery(query);
    String socketId = data.get("socket_id");
    String channelId = data.get("channel_name");
    // Presence channels (presence-*) require user identification for authentication
    Map<String, String> userInfo = new HashMap<>();
    userInfo.put("displayName", displayName);
    // Inject custom authentication code for your application here to allow/deny current request
    String auth = pusher.authenticate(socketId, channelId, new PresenceUser(currentUserId, userInfo));
    // if successful, returns authorization in the format
    // {
    // "auth":"49e26cb8e9dde3dfc009:a8cf1d3deefbb1bdc6a9d1547640d49d94b4b512320e2597c257a740edd1788f",
    // "channel_data":"{\"user_id\":\"23423435252\",\"user_info\":{\"displayName\":\"John Doe\"}}"
    // }
    response.getWriter().append(auth);
}
Also used : User(com.google.appengine.api.users.User) PresenceUser(com.pusher.rest.data.PresenceUser) HashMap(java.util.HashMap) PresenceUser(com.pusher.rest.data.PresenceUser) Pusher(com.pusher.rest.Pusher)

Aggregations

User (com.google.appengine.api.users.User)2 Pusher (com.pusher.rest.Pusher)2 PresenceUser (com.pusher.rest.data.PresenceUser)2 HashMap (java.util.HashMap)2