org.omnifaces.cdi.push.SocketPushContext Maven / Gradle / Ivy
Show all versions of omnifaces Show documentation
/*
* Copyright OmniFaces
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.omnifaces.cdi.push;
import static java.util.Collections.singleton;
import static org.omnifaces.cdi.push.SocketChannelManager.EMPTY_SCOPE;
import static org.omnifaces.cdi.push.SocketChannelManager.getChannelId;
import static org.omnifaces.util.Beans.isActive;
import static org.omnifaces.util.Faces.hasContext;
import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Future;
import jakarta.enterprise.context.SessionScoped;
import org.omnifaces.cdi.Push;
import org.omnifaces.cdi.PushContext;
import org.omnifaces.util.Json;
/**
*
* This is a concrete implementation of {@link PushContext} interface which is to be injected by
* @
{@link Push}.
*
* @author Bauke Scholtz
* @see Push
* @since 2.3
*/
public class SocketPushContext implements PushContext {
// Constants ------------------------------------------------------------------------------------------------------
private static final long serialVersionUID = 1L;
// Variables ------------------------------------------------------------------------------------------------------
private String channel;
private Map sessionScopedChannels;
private Map viewScopedChannels;
private SocketSessionManager socketSessions;
private SocketUserManager socketUsers;
// Constructors ---------------------------------------------------------------------------------------------------
/**
* Creates a socket push context whereby the mutable map of session and view scope channel identifiers is
* referenced, so it's still available when another thread invokes {@link #send(Object)} during which the session
* and view scope is not necessarily active anymore.
*/
SocketPushContext(String channel, SocketChannelManager socketChannels, SocketSessionManager socketSessions, SocketUserManager socketUsers) {
this.channel = channel;
boolean hasSession = isActive(SessionScoped.class);
sessionScopedChannels = hasSession ? socketChannels.getSessionScopedChannels() : EMPTY_SCOPE;
viewScopedChannels = hasSession && hasContext() ? socketChannels.getViewScopedChannels(true) : EMPTY_SCOPE;
this.socketSessions = socketSessions;
this.socketUsers = socketUsers;
}
// Actions --------------------------------------------------------------------------------------------------------
@Override
public Set> send(Object message) {
return socketSessions.send(getChannelId(channel, sessionScopedChannels, viewScopedChannels), Json.encode(message));
}
@Override
public Set> send(Object message, S user) {
return send(message, singleton(user)).get(user);
}
@Override
public Map>> send(Object message, Collection users) {
Map>> resultsByUser = new HashMap<>(users.size());
String json = Json.encode(message);
for (S user : users) {
Set channelIds = socketUsers.getChannelIds(user, channel);
Set> results = new HashSet<>(channelIds.size());
for (String channelId : channelIds) {
results.addAll(socketSessions.send(channelId, json));
}
resultsByUser.put(user, results);
}
return resultsByUser;
}
}