use of com.pusher.rest.Pusher 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);
}
use of com.pusher.rest.Pusher 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);
}
use of com.pusher.rest.Pusher in project java-docs-samples by GoogleCloudPlatform.
the class PusherService method getDefaultInstance.
static Pusher getDefaultInstance() {
if (instance != null) {
return instance;
}
// Instantiate a pusher
Pusher pusher = new Pusher(APP_ID, APP_KEY, APP_SECRET);
// required, if not default mt1 (us-east-1)
pusher.setCluster(CLUSTER);
// optional, ensure subscriber also matches these settings
pusher.setEncrypted(true);
instance = pusher;
return pusher;
}
use of com.pusher.rest.Pusher in project community by GoogleCloudPlatform.
the class PusherService method getDefaultInstance.
static Pusher getDefaultInstance() {
if (instance != null) {
return instance;
}
// Instantiate a pusher
Pusher pusher = new Pusher(APP_ID, APP_KEY, APP_SECRET);
// required, if not default mt1 (us-east-1)
pusher.setCluster(CLUSTER);
// optional, ensure subscriber also matches these settings
pusher.setEncrypted(true);
instance = pusher;
return pusher;
}
Aggregations